1

How can I use pandoc-filters to transform relative links like [foo](act1.jpg) to absolute links [foo](pathtoact1/act1.jpg). I want to produce html5 document

Preferred with lua because its minimal footprint. Perhaps we could use this example for extracting document links info.

somenxavier
  • 1,206
  • 3
  • 20
  • 43

1 Answers1

2

Let's try to approach this step-by-step. You already found a very relevant example, meaning you roughly understand how to use lua filters and know, that filter functions act on the elements of the same name. You also know that you'll want to modify links so your filter will look something like this:

function Link (element)
  -- fix link targets
  return element
end

We don't really know yet how a link element looks like, so we check the docs by searching the page for link. After some searching, we find the description for the Link constructor. Somewhere in the docs it's stated that pandoc also uses this to create Link elements, so understanding this will answer most of our questions.

The properties of an element can be accessed using the name of the parameter in the constructor. To get the link target, we just need to write element.target.

function Link (element)
  element.target = fix_path(element.target)
  return element
end

So let's start by thinking about what needs to be done to fix paths. Obviously, we just need to concatenate two strings, viz. the link path and a path prefix.

function fix_path (path)
  return 'path/prefix/' .. path
end

So we are basically done. But this only fixes links. However, there are typically other paths in a document. Most notably paths pointing to images. Quickly searching the docs for image reveals that the property holding an image path is named src. We can use this to also filter on images, so the complete filter looks like this:

function fix_path (path)
  return 'you/path/prefix' .. path
end

function Link (element)
  element.target = fix_path(element.target)
  return element
end

function Image (element)
  element.src = fix_path(element.src)
  return element
end

Voilà, your filter.

tarleb
  • 19,863
  • 4
  • 51
  • 80
  • 1
    How can I determine the `'your/path/prefix'` to the path of the processed document? – somenxavier Feb 05 '18 at 18:35
  • I'm not sure how to answer this. Could you give me an example of what you have, and how you want it to work? – tarleb Feb 06 '18 at 11:57
  • If my document is `dir1/dir2/document.md`, then I want that this filter takes `dir1/dir2` as `your/path/prefix/`. Do you understand me? – somenxavier Feb 12 '18 at 17:05
  • 1
    The simplest way to do this without having to change the filter would be to use an environment variable. E.g. setting `return (os.getenv('PANDOC_PREFIX') or '') .. path` would allow you to set the path while calling pandoc (`PANDOC_PREFIX=dir1/dir2 pandoc dir1/dir2/document.md`). You could use the `dirname` program and write a small script which sets the environment var for you, based on the input file. – tarleb Feb 13 '18 at 20:06
  • How do you run this filter? Save filter in file `foo.filter` and? – somenxavier Mar 18 '18 at 19:39
  • Save the filter as `a.lua`, you can run with `pandoc -s inputfile.md --lua-filter=a.lua -o outputfile.html` – somenxavier Mar 19 '18 at 20:00
  • 1
    This filter does not differentiate between **relative** and **absolute** links: it prepends `your/path/prefix` *always*. Ideally, we should prepend the prefix just with relative links like `[a](something/relative/hoo.html)` – somenxavier Mar 19 '18 at 20:01