Monday, September 29, 2014

Make word, pdf and html documents with markdown and pandoc

Markdown is a simple text markup language.
Pandoc is a document converter. Pandoc demo and sample command.

Pandoc commands

Convert a markdown file to PDF :
pandoc -o README.pdf
The pandoc man page says: "If  the input or output format is not specified explicitly, pandoc will attempt to guess it from the extensions of the input and output filenames." That's what happens above. However "The input format can be specified using the -r/--read or -f/--from options, the output format using the -w/--write or -t/--to options."


This phsychologist blogs about using a makefile to create beamer presentations.
This researchers providers a make file for pandoc templates.

With this simple make file, I can create Microsoft Word, HTML and PDF documents from the same markdown file:
all: docx pdf html

        pandoc -o file.docx

        pandoc -o file.pdf

        pandoc -o file.html

        rm -f *.html *.pdf *.docx
To create all documents type
To create only a docx type
make docx
To delete all created document type
make clean

Improved makefile with variable

file.pdf :
    pandoc -o file.pdf

    pandoc -o $@ $<

Guide makefiles:
"Here, we have used the percent (%) character to denote that part of the target and dependency that matches whatever the pattern is used for, and the $< is a special variable (imaging it like $(<)) that means "whatever the depencies are". Another useful variable is $@, which means "the target"."

## Makefile to generate documents based on markdown files
## Inspired by this makefile
## I should use vraibles for filenames
## Command line to converts:

## How to make this using variables?
## No space allowed in file names there could be a replacement but I didn't try

## Markdown extension (e.g. md, markdown, mdown).
MEXT = md
## All markdown files in the working directory
SRC = $(wildcard *.$(MEXT))


all: $(PDFS)  $(DOCX)
pdf:    clean $(PDFS)
docx:   clean $(DOCX)
#html:   clean $(HTML)

#scrap :
#    pandoc -o scrap.pdf

# Separator for these lines need to start with a hard tab, not 4 spaces!
    pandoc -o $@ $<

    pandoc -o $@ $<

    rm -f *.html *.pdf *.docx

No comments: