-
Notifications
You must be signed in to change notification settings - Fork 185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Reference] fix image embedding in DOCX output #40
Changes from all commits
0510be0
bb05d9b
3e2eb19
c37ed48
4ec532b
9865451
2df7ea4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#! /usr/bin/env python | ||
""" | ||
Pandoc filter to convert svg files to pdf as suggested at: | ||
https://github.com/jgm/pandoc/issues/265#issuecomment-27317316 | ||
""" | ||
|
||
__author__ = "Jerome Robert" | ||
|
||
import mimetypes | ||
import subprocess | ||
import os | ||
import sys | ||
import re | ||
import urllib | ||
from pandocfilters import toJSONFilter, Para, Image | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be. I just forgot it. Will add it in. |
||
|
||
fmt_to_option = { | ||
"docx": ("--export-pdf", "pdf") | ||
} | ||
|
||
|
||
build_dir = os.getcwd() | ||
img_dir = "content/images/" | ||
|
||
|
||
def svg_to_any(key, value, fmt, meta): | ||
if key == 'Image': | ||
if len(value) == 2: | ||
# before pandoc 1.16 | ||
alt, [src, title] = value | ||
attrs = None | ||
else: | ||
attrs, alt, [src, title] = value | ||
mimet, _ = mimetypes.guess_type(src) | ||
option = fmt_to_option.get(fmt) | ||
if mimet == 'image/svg+xml' and option: | ||
web_image = re.compile('^http[s]?://') | ||
if web_image.match(src): | ||
base_name = os.path.basename(src) | ||
base_name, _ = os.path.splitext(base_name) | ||
eps_name = os.path.join(build_dir, img_dir, base_name + "." + option[1]) | ||
file_name = src | ||
else: | ||
base_name, _ = os.path.splitext(src) | ||
eps_name = os.path.join(build_dir, base_name + "." + option[1]) | ||
file_name = os.path.join(build_dir, src) | ||
try: | ||
mtime = os.path.getmtime(eps_name) | ||
except OSError: | ||
mtime = -1 | ||
try: | ||
src_mtime = os.path.getmtime(src) | ||
except FileNotFoundError: | ||
src_mtime = -1 | ||
if mtime < src_mtime or web_image.match(src): | ||
cmd_line = ['inkscape', option[0], eps_name, file_name] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this method depend on inkscape? That's a pretty heavy dependency? Can inkscape be installed via conda? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dhimmel Yest this method does depend on inkscape. There seems to have been a working conda install, but that seems broken now inkscape-feedstock. |
||
sys.stderr.write("Running %s\n" % " ".join(cmd_line)) | ||
subprocess.call(cmd_line, stdout=sys.stderr.fileno()) | ||
if attrs: | ||
return Image(attrs, alt, [eps_name, title]) | ||
else: | ||
return Image(alt, [eps_name, title]) | ||
|
||
if __name__ == "__main__": | ||
toJSONFilter(svg_to_any) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,15 @@ on {{date}}. | |
{% for author in authors %} | ||
+ **{{author.name}}**<br> | ||
{%- if author.orcid is defined %} | ||
{height="13px"} | ||
{height="13px" width="13px"} | ||
[{{author.orcid}}](https://orcid.org/{{author.orcid}}) | ||
{%- endif %} | ||
{%- if author.github is defined %} | ||
· {height="13px"} | ||
· {height="13px" width="13px"} | ||
[{{author.github}}](https://github.com/{{author.github}}) | ||
{%- endif %} | ||
{%- if author.twitter is defined %} | ||
· {height="13px"} | ||
· {height="13px" width="13px"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vsmalladi would specifying If these changes are valuable alone, you should open a quick PR with just those changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dhimmel I will test them out based on the updates and see if works. |
||
[{{author.twitter}}](https://twitter.com/{{author.twitter}}) | ||
{%- endif %}<br> | ||
<small> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--filter pandoc-img-glob
@dhimmel is restructuring the relative path to the image for docx conversion to absolute paths relative to markdown. Without it I get these errors:
[pandoc warning] Could not find image "images/image2.png", skipping...