This is a short follow up post to fix one minor thing. In the previous post if you created hyper links in your markdown file (say pointing to another markdown document, which for my purposes was multiple chapters in some teaching notes) then all the formats (pdf, html and docx) would all point to the same format.
Here's a slight modification of the
pandocipy_given_file.py script (from the previous post) which will use sed to replace the .md to the relevant format in all links.
#!/usr/bin/env python
from sys import argv
from os import system
e = argv[1][:-3]
print e
system("sed 's/.md/.html/g' %s > tmp" % (e + ".md"))
system("pandoc -s tmp -N -o " + e + ".html --mathjax")
system("sed 's/.md/.docx/g' %s > tmp" % (e + ".md"))
system("pandoc tmp -o " + e + ".docx")
system("sed 's/.md/.pdf/g' %s > tmp" % (e + ".md"))
system("pandoc tmp -N -o " + e + ".pdf --latex-engine=xelatex")
system("rm tmp")
tmp- this is actually important to make sure that the makefile doesn't think that you're recurrently changing the md files) that sed makes sure has the correct format for each link and pandoc can use to get the required files. For example the sed 's/.md/.html/g' command will replace all instances of ".md" with ".html". If you're not familiar with sed it's a *nix program that allows you to do find and replace in files "easily" (it's still a bit of voodoo to me). The script can be used by simply typing:
python pandocipy_given_file.py file.md(Which assumes that
file.md has all links sending to other md files.)I use this script in the makefile as I did in the previous post:
md = $(wildcard *.md)
htmls = $(md:%.md=%.html)
all: $(htmls)
%.html: %.md
    ./pandocipy_given_file.py $<
    ../Scripts/generate_website.py
make to update the website file.If any of this is of interest the github repo is here.
Note that in this particular case the links being "format loyal" is mainly useful for the `html` files to the general audience. The loyal links in pdf and docx format are mainly useful to me as all the paths are relative to my setup. I'm posting this again so that I don't forget it but also in case it's useful to others (you can obviously use sed to replace other things).
 
No comments:
Post a Comment