Skip to content
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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ then
--to=docx \
--filter pandoc-fignos \
--filter pandoc-tablenos \
--filter pandoc-img-glob \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Collaborator Author

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...

--filter build/pandoc-svg.py \
--bibliography=$BIBLIOGRAPHY_PATH \
--metadata link-citations=true \
--csl=$CSL_PATH \
Expand Down
3 changes: 3 additions & 0 deletions build/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ dependencies:
- git+https://github.com/greenelab/manubot@b0da17625e1f12091159f15a329391b85c205565
- pandoc-eqnos==0.16
- pandoc-fignos==0.20
- pandoc-img-glob==0.1.3
- pandocfilters==1.4.1
- panflute==1.10.5
- pandoc-tablenos==0.16
- pybase62==0.3.2
- pysha3==1.0.2
Expand Down
65 changes: 65 additions & 0 deletions build/pandoc-svg.py
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is pandocfilters getting installed? Should it be added to environment.yml?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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]
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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)
6 changes: 3 additions & 3 deletions content/00.front-matter.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ on {{date}}.
{% for author in authors %}
+ **{{author.name}}**<br>
{%- if author.orcid is defined %}
![ORCID icon](images/orcid.svg){height="13px"}
![ORCID icon](images/orcid.svg){height="13px" width="13px"}
[{{author.orcid}}](https://orcid.org/{{author.orcid}})
{%- endif %}
{%- if author.github is defined %}
· ![GitHub icon](images/github.svg){height="13px"}
· ![GitHub icon](images/github.svg){height="13px" width="13px"}
[{{author.github}}](https://github.com/{{author.github}})
{%- endif %}
{%- if author.twitter is defined %}
· ![Twitter icon](images/twitter.svg){height="13px"}
· ![Twitter icon](images/twitter.svg){height="13px" width="13px"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vsmalladi would specifying width="13px" for these SVGs be valuable independent of the rest of the changes in this PR? These SVGs export to DOCX currently, just they are misshapen?

If these changes are valuable alone, you should open a quick PR with just those changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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>
Expand Down
Binary file modified references/pandoc-reference.docx
Binary file not shown.