LaurentRDC / pandoc-plot

Render and include figures in Pandoc documents using your plotting toolkit of choice
https://laurentrdc.github.io/pandoc-plot/
GNU General Public License v2.0
216 stars 8 forks source link

Image path #24

Closed adamgordonbell closed 3 years ago

adamgordonbell commented 3 years ago

Great project. I am using pandoc to generate HTML from markdown for a Jekyll site and trying to use pandoc-plot with it.

To do this I need the output location of the generated image and the HTML path to the image to vary.

For example, config like this would be helpful:

format: SVG
source: false
output_directory: /site/_site/assets/images/
path_to_images: /blog/assets/images/

where output_directory is where the image is saved to output_directory, but in the actual HTML it refers to the image using path_to_images. Is this possible?

Here is an example to explain. It is possible I am using it wrong, but it seems like other static site generators would need this feature as well to use pandoc-plot.

Here is what the file structure of a Jekyll site using pandoc might look like:

/_posts/example.md <- has pandoc plot in it
/_site/ -> mapped in a webserver to somedomain.com/blog/
/_site/example/index.html <- example.md generated to here by jekyll/pandoc becoming somedomain.com/blog/example/
/_site/assets/images/ <- need images to show up here 

I can get the images in the correct place like this:

directory: /_site/assets/images/

But in the document, the path to the image will be incorrect:

<img src="/_site/assets/images/796308625745987558.svg" />

If there were a second config, path_to_images = /blog/assets/images the image could go to the same place, but the correct (for this use case) path coudl be in the document:

<img src="/blog/assets/images/796308625745987558.svg" />
LaurentRDC commented 3 years ago

Hi,

Right now you can already control where the plots are saved with the directory parameter, like so:

#.pandoc-plot.yml
directory: /_site/assets/images/

To have the plots be saved in this directory, but the output document to refer to a different location, increases the complexity of pandoc-plot too much.

The best solution is to create an additional pandoc filter which changes image paths from /_site/assets/images/xyz.png to /blog/assets/images/xyz.png. You can refer to the Pandoc documentation on filters to learn how to write filters, either using Lua, Python, or Haskell. Then, you can run pandoc-plot first, and your new filter (e.g.reroute.py) second like so:

pandoc --filter pandoc-plot --filter reroute.py ....
LaurentRDC commented 3 years ago

Here's that rerouting filter in python:

# reroute.py
from pandocfilters import toJSONFilter, Image

path_from = "_site/assets/images/"
path_to = "blog/assets/images/"

def reroute(key, value, format, meta):
  if key == 'Image':
    attrs, alttext, (url, title) = value
    if url.startswith(path_from):
        newurl = path_to + url.split(path_from)[-1]
    return Image(attrs, alttext, (newurl, title))

if __name__ == "__main__":
  toJSONFilter(reroute)

Test file:

# Hello

![This is a test](_site/assets/images/test.png)

command:

pandoc --filter reroute.py -i test.md -o test-reroute.md

Output:

# Hello

![This is a test](blog/assets/images/test.png)
adamgordonbell commented 3 years ago

Great, I will give this is a try!

LaurentRDC commented 3 years ago

I'm going to close this for now, but don't hesitate to re-open if the solution above does not work.

adamgordonbell commented 3 years ago

I didn't try it, because #23 solves this for me, but I expect it would work for my usecase.