Pandoc + GNU make
Pandocは知らない人のために一応簡単に言うと、
Markdownとかをpdfやhtmlに変換してくれる、控えめに言って最高なツールです。
しかし、コマンドラインで使うときのツラミも大きいです。
そこで、makeコマンドでどうにかするべくMakefileを書きました。
https://github.com/uesseu/makefile_for_pandoc
TLDR;
- 一番下のコードをMakefileとして保存
- 同ディレクトリ内にfilesディレクトリを作る
- filesディレクトリにmarkdownを入れていく
- ファイルに合わせてMakefileの中の一番上のファイルを弄る
- 読ませる順番通りに繋げてくれるはず
- オプションは目的によって弄ること、pandocのマニュアルに色々書いてある
- pandocとGNU makeの存在下でmakeする(デフォルトでPDFをout.pdfとして出す)
- 他の形式が良ければ'make hoge.html'とかな感じにする
- htmlとpdfとdocxは一応オプションに入れてる
- PDF出力するならtexliveとか入れないとダメなはず
- 確認したければデフォルトのはmake openであけられる。
そもそものpandocの辛さ
では、markdownを使ってMarkdownを普通にpdfに変換しましょう。
ワンライナーです。
pandoc -o out.pdf --toc --toc-depth=3 -T Title --indented-code-classes=python,bash -V documentclass=ltjarticle -V geometry:
left=2cm -V geometry:right=2cm -V CJKmainfont=IPAexGothic -V lang=en-US --pdf-engine=lualatex --listings --template hoge.tex -f markd
own+hard_line_breaks fuga.md
▂▅▇█▓▒░(‘ω’)░▒▓█▇▅▂ うわあああああああああ
自動化
僕こんな長過ぎるコマンド書けません。いや、書けるけど書きたくないです。絶対たいぽします。
でも、シェルスクリプトやpython書くのもなんかアレです。
いや、悪くはないんですが本命じゃない気がするし、一寸大げさ過ぎる。
pandoc自体はコンパイラみたいなものだからコンパイラ支援ソフトであるGNUmake使いましょう。
というのが本記事の趣旨です。餅は餅屋なので。
とりあえず、ディレクトリの構造としてはこんな感じにしてみました。
filesの中にMarkdownのファイルを入れていく感じですね。
Doujinshi-folder/
├ Makefile
└ files/
├ hoge.md
├ fuga.md
└ img/
├ piyo.png
└ piyopiyo.jpg
画像はなぜここにある?
filesの中に画像ディレクトリを作ってそこに入れるようにしています。
Markdownの中では![piyo](img/piyo.png)
みたいに書いています。
一方、下記のMakefileでは一々カレントディレクトリ移動してpandoc動かしています。
何故こうしているかと言うと、Githubにうpした時にソースに画像も表示されてほっこりしたいから。
GithubMarkdownは自分自身から見た相対パスで画像を読み込むのです。
このように動くようにした
というわけで、Makefileはこうやってます。
make
でout.pdfという出力が出ます。
また、出力したいものに応じてmakeの時に切り替えられます。
make hoge.html
とか行けます。楽でいいです。
デフォルトの出力は
make open
で開けるようにしています。
pandocが躍動する俺のMakefileを皆さんに見せたいね!
# Names of files.
# In order to keep order of files,
# filenames should be written here.
# If you need not keep order, you can use wild card.
# For example, '*.md'.
files = \
hoge.md\
fuga.md\
# Directory of files.
changed_files = files/*
# OS specific open command.
# If linux, 'xdg-open' and if MacOS, 'open'.
# If WIndows, just a blank.
open_command = xdg-open
# Filetype of output.
default_type = pdf
# Filename without file type.
output_file = out
title = Title
# Reader arguments for pandoc.
reader =
# Writer arguments for pandoc.
writer = --toc\
--toc-depth=3\
-T $(title)\
--indented-code-classes=python,bash\
# PDF specific arguments.
pdf = -V documentclass=ltjarticle\
-V geometry:left=2cm\
-V geometry:right=2cm\
-V CJKmainfont=IPAexGothic\
-V lang=en-US\
--pdf-engine=lualatex\
--listings\
# --template hoge.tex
# HTML specific arguments
html = --metadata pagetitle="$(title)"\
--self-contained\
# Markdown extention should be separated by '+'.
markdown_extention = -f markdown+hard_line_breaks
all: $(changed_files)
cd files;pandoc -o ../$(output_file).$(default_type) $(reader) $(writer) $(pdf) $(markdown_extention) $(files)
%.html: $(changed_files)
cd files;pandoc -o ../$@ $(reader) $(writer) $(html) $(markdown_extention) $(files)
%.pdf: $(changed_files)
cd files;pandoc -o ../$@ $(reader) $(writer) $(pdf) $(markdown_extention) $(files)
%.docx: $(changed_files)
cd files;pandoc -o ../$@ $(reader) $(writer) $(pdf) $(markdown_extention) $(files)
open:
$(open_command) $(output_file).$(default_type)
すごいシンプル