0

I am trying to compile one single markdown file into different outputs (HTML, EPUB and LaTeX for printing via PDF). The document requires some formats that are not native to Markdown, which means the original is not portable. However, the features I need are possible in all three output formats.

The solution I am considering is to have some custom markup in the original file and process it using sed (or any other preprocessor you advise me) to create different outuputs to feed Pandoc.

For example:

<sig>Placename, 2016</sig>

would become

<div class="signature">Placename, 2016</div>

in both HTML and EPUB, but

\signature{Placename, 2016}

in LaTeX.

and

<quote>To be or not to be</quote><qaut>Shakespeare</qaut>

would be this in HTML and EPUB

<div class="epigraph"><quote>To be or not to be</quote>Shakespeare</div>

and simply

\epigraph{To be or not to be}{Shakespeare}

in LaTeX.

Though the replacements don't seem terribly difficult, I would like some advice on how to deal with multiple instances of these replacements before invoking Pandoc (from within a Makefile, of course).


I would prefer to pipe the edited file to Pandoc using the RAM disk, but saving a tempfile on /tmp would be the same for me, since I have my /tmp on RAM.

Joe Avg.
  • 1
  • 1
  • Possible duplicate of http://stackoverflow.com/questions/19590980/multiple-replacements-with-one-sed-command – tripleee Oct 12 '16 at 08:01
  • have a look at https://randomdeterminism.wordpress.com/2012/06/01/how-i-stopped-worring-and-started-using-markdown-like-tex/ – mb21 Oct 13 '16 at 13:18

1 Answers1

0

You may use pattern substitution with sed:

sed -i -e 's/pattern1\(pattern_to_keep\)pattern2/text1\1text2/g' filename

this command will change in filename the line pattern1pattern_to_keeppattern2 and change it to text1pattern_to_keeptext2. sed command (or other tools with regexp and substitution) put each \(...\) part to numbered buffers like \1, \2

So to convert sig and quote to LaTex format, your makefile will look like:

file.latex: file
        sed -e 's/<sig>\(.*\)<\/sig>/\\signature{\1}/g' \
            -e 's/<quote>\(.*\)<\/quote>.*<qaut>\(.*\)<\/qaut>/\\epigraph{\1}{\2}/g' $< > $@

Also you may put all sed rules to separate script. In this case the makefile will looks like

file.latex: file script
        sed -f script $< > $@


script:
        @echo 's/<sig>\(.*\)<\/sig>/\\signature{\1}/g' > $@
        @echo 's/<quote>\(.*\)<\/quote>.*<qaut>\(.*\)<\/qaut>/\\epigraph{\1}{\2}/g' >> $@

(note, here we create script file just from Makefile. For big scripts it make sense to create/store script separately to not overload Makefile)

Peter K.
  • 134
  • 7
  • Using `cp` followed by `sed -i` is wasteful, and extremely inadvisable in a Makefile. You want simply `sed -e things -e more $< >$@` – tripleee Oct 12 '16 at 07:35
  • in general yes, but we need to apply number of different substitutions for 1 file. For sure we may use sed script file to apply number of rules at a time. For big set of changes it would be really better. Will update comment with script usage. – Peter K. Oct 12 '16 at 07:45
  • 1
    You are making this needlessly complex. `sed -e 's%\(.*\)%\\signature{\1}%g' -e 's%\(.*\).*\(.*\)%\\epigraph{\1}{\2}%g' $< >$@` does this in a one-liner. – tripleee Oct 12 '16 at 07:49
  • thanks for clarification. The fact of listing number of `-e '...' -e '...'` is new for me. – Peter K. Oct 12 '16 at 07:56