Linuxのコマンドとして簡単に使えるC製のMarkdownパーサー「DISCOUNT」

  • Markdownを簡単にHTMLに変換したい。

  • Pandocもいいけど、自由度は下げてもよいのでとにかく簡単にしたい。

  • DISCOUNTというC製のMarkdownパーサーがDavid Parsons氏によりBSDライセンスで提供しており、良さげ。

本論

DISCOUNTについて

Discount – a C implementation of the Markdown markup language

インストール方法について

基本的には付属のREADMEを読めば大体わかる。 個人作成のソフトなのでyumやらaptやらのレポジトリがあるわけでもなく、ソースをダウンロードしてきてコンパイルしてやる必要がある

1.まずは作者のサイトからソース一式(discount-2.2.2.tar.bz2 )ダウンロードして、適当なディレクトリに置く

2.tarを解凍する

[mk55@localhost ~]$ tar xfvj discount-2.2.2.tar.bz2 
discount-2.2.2/
discount-2.2.2/COPYRIGHT
discount-2.2.2/CREDITS
discount-2.2.2/Csio.c
discount-2.2.2/INSTALL
discount-2.2.2/Makefile.in
discount-2.2.2/Plan9/
discount-2.2.2/Plan9/README.md
discount-2.2.2/Plan9/markdown.1
discount-2.2.2/Plan9/markdown.2
discount-2.2.2/Plan9/markdown.6
discount-2.2.2/Plan9/mkfile
discount-2.2.2/README
discount-2.2.2/VERSION
discount-2.2.2/amalloc.c
discount-2.2.2/amalloc.h
discount-2.2.2/basename.c
discount-2.2.2/configure.inc
discount-2.2.2/configure.sh
discount-2.2.2/css.c
discount-2.2.2/cstring.h
discount-2.2.2/docheader.c
discount-2.2.2/dumptree.c
discount-2.2.2/emmatch.c
discount-2.2.2/flags.c
discount-2.2.2/generate.c
discount-2.2.2/github_flavoured.c
discount-2.2.2/html5.c
discount-2.2.2/libmarkdown.pc.in
discount-2.2.2/main.c
discount-2.2.2/makepage.1
discount-2.2.2/makepage.c
discount-2.2.2/markdown.1
discount-2.2.2/markdown.3
discount-2.2.2/markdown.7
discount-2.2.2/markdown.c
discount-2.2.2/markdown.h
discount-2.2.2/mkd-callbacks.3
discount-2.2.2/mkd-extensions.7
discount-2.2.2/mkd-functions.3
discount-2.2.2/mkd-line.3
discount-2.2.2/mkd2html.1
discount-2.2.2/mkd2html.c
discount-2.2.2/mkdio.c
discount-2.2.2/mkdio.h.in
discount-2.2.2/mktags.c
discount-2.2.2/pgm_options.c
discount-2.2.2/pgm_options.h
discount-2.2.2/resource.c
discount-2.2.2/setup.c
discount-2.2.2/tags.c
discount-2.2.2/tags.h
discount-2.2.2/tests/
discount-2.2.2/tests/autolink.t
discount-2.2.2/tests/automatic.t
discount-2.2.2/tests/backslash.t
discount-2.2.2/tests/callbacks.t
discount-2.2.2/tests/chrome.text
discount-2.2.2/tests/code.t
discount-2.2.2/tests/compat.t
discount-2.2.2/tests/crash.t
discount-2.2.2/tests/defects.t
discount-2.2.2/tests/div.t
discount-2.2.2/tests/dl.t
discount-2.2.2/tests/embedlinks.text
discount-2.2.2/tests/emphasis.t
discount-2.2.2/tests/extrafootnotes.t
discount-2.2.2/tests/flow.t
discount-2.2.2/tests/footnotes.t
discount-2.2.2/tests/functions.sh
discount-2.2.2/tests/githubtags.t
discount-2.2.2/tests/header.t
discount-2.2.2/tests/html.t
discount-2.2.2/tests/html5.t
discount-2.2.2/tests/html_comment.t
discount-2.2.2/tests/latex.t
discount-2.2.2/tests/links.text
discount-2.2.2/tests/linkylinky.t
discount-2.2.2/tests/linkypix.t
discount-2.2.2/tests/list.t
discount-2.2.2/tests/list3deep.t
discount-2.2.2/tests/misc.t
discount-2.2.2/tests/muñoz.t
discount-2.2.2/tests/pandoc.t
discount-2.2.2/tests/para.t
discount-2.2.2/tests/paranoia.t
discount-2.2.2/tests/peculiarities.t
discount-2.2.2/tests/pseudo.t
discount-2.2.2/tests/reddit.t
discount-2.2.2/tests/reparse.t
discount-2.2.2/tests/schiraldi.t
discount-2.2.2/tests/smarty.t
discount-2.2.2/tests/snakepit.t
discount-2.2.2/tests/strikethrough.t
discount-2.2.2/tests/style.t
discount-2.2.2/tests/superscript.t
discount-2.2.2/tests/syntax.text
discount-2.2.2/tests/tables.t
discount-2.2.2/tests/tabstop.t
discount-2.2.2/tests/toc.t
discount-2.2.2/tests/xml.t
discount-2.2.2/theme.1
discount-2.2.2/theme.c
discount-2.2.2/toc.c
discount-2.2.2/tools/
discount-2.2.2/tools/checkbits.sh
discount-2.2.2/tools/cols.c
discount-2.2.2/tools/echo.c
discount-2.2.2/version.c.in
discount-2.2.2/xml.c
discount-2.2.2/xmlpage.c

3.解凍して作られるdiscountのディレクトリへ移動して、configure.shを実行する。これで、関連するファイルを確認して、Makefileや一部のヘッダを自動的に生成してくれる。

[mk55@localhost ~]$ cd discount-2.2.2/
[mk55@localhost discount-2.2.2]$ ./configure.sh 
Configuring for [markdown]
checking the C compiler (cc) oh ick, it looks like gcc
Looking for cpp (/lib/cpp) ok
looking for install (/usr/bin/install)
Checking __attribute__((__noreturn__)) (yes)
looking for ar (/usr/bin/ar)
looking for ranlib (/usr/bin/ranlib)
looking for pkg-config (/usr/bin/pkg-config)
checking for "volatile" keyword (found)
checking for "const" keyword (found)
Checking for "inline" keyword (found)
defining WORD & DWORD scalar types
looking for a reentrant basename (found)
looking for header libgen.h (found)
looking for header stdlib.h (found)
looking for the alloca function (found in alloca.h)
looking for header pwd.h (found)
looking for the getpwuid function (found)
looking for the srandom function (found)
looking for the bzero function (found)
looking for the random function (found)
looking for the strcasecmp function (found)
looking for the strncasecmp function (found)
looking for the fchdir function (found)
looking for header malloc.h (found)
looking for find (/usr/bin/find)
looking for "ln -s" (/usr/bin/ln)
looking for ar (/usr/bin/ar)
looking for ranlib (/usr/bin/ranlib)
looking for sed (/usr/bin/sed)
generating Makefile
generating version.c
generating mkdio.h
generating libmarkdown.pc

4.makeコンパイルする

[mk55@localhost discount-2.2.2]$ make
cc -Wno-return-type -Wno-implicit-int -I. -g -I. -c main.c
cc -Wno-return-type -Wno-implicit-int -I. -g -I. -c pgm_options.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o mkdio.o mkdio.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o markdown.o markdown.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o dumptree.o dumptree.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o generate.o generate.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o resource.o resource.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o docheader.o docheader.c
cc -Wno-return-type -Wno-implicit-int -I. -g -DVERSION=\"`cat VERSION`\" -c version.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o toc.o toc.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o css.o css.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o xml.o xml.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o Csio.o Csio.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o xmlpage.o xmlpage.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o basename.o basename.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o emmatch.o emmatch.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o github_flavoured.o github_flavoured.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o setup.o setup.c
cc -Wno-return-type -Wno-implicit-int -I. -g    mktags.c   -o mktags
./mktags > blocktags
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o tags.o tags.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o html5.o html5.c
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o flags.o flags.c
./librarian.sh make libmarkdown VERSION mkdio.o markdown.o dumptree.o generate.o resource.o docheader.o version.o toc.o css.o xml.o Csio.o xmlpage.o basename.o emmatch.o github_flavoured.o setup.o tags.o html5.o flags.o 
a - mkdio.o
a - markdown.o
a - dumptree.o
a - generate.o
a - resource.o
a - docheader.o
a - version.o
a - toc.o
a - css.o
a - xml.o
a - Csio.o
a - xmlpage.o
a - basename.o
a - emmatch.o
a - github_flavoured.o
a - setup.o
a - tags.o
a - html5.o
a - flags.o
cc -Wno-return-type -Wno-implicit-int -I. -g -L. -o markdown main.o pgm_options.o -lmarkdown 
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o mkd2html.o mkd2html.c
cc -Wno-return-type -Wno-implicit-int -I. -g -L. -o mkd2html mkd2html.o -lmarkdown 
cc -Wno-return-type -Wno-implicit-int -I. -g -L. -o makepage makepage.c pgm_options.o -lmarkdown 
cc -Wno-return-type -Wno-implicit-int -I. -g   -c -o theme.o theme.c
cc -Wno-return-type -Wno-implicit-int -I. -g -L. -o theme theme.o pgm_options.o -lmarkdown 
cc -Wno-return-type -Wno-implicit-int -I. -o echo tools/echo.c
cc -Wno-return-type -Wno-implicit-int -I. -o cols tools/cols.c

5.make installする。今回は個別にインストールしたが、make install.everythingで一括インストールもできる

[root@localhost mk55]# cd discount-2.2.2/
[root@localhost discount-2.2.2]# make install
./config.md /usr/local/lib/pkgconfig
/usr/bin/install -s -m 755 markdown /usr/local/bin
./librarian.sh install libmarkdown VERSION /usr/local/lib
/usr/bin/install -m 444 mkdio.h /usr/local/include
[root@localhost discount-2.2.2]# which markdown
/usr/local/bin/markdown
[root@localhost discount-2.2.2]# make install.man
./config.md /usr/local/man/man3
/usr/bin/install -m 444 mkd-callbacks.3 mkd-functions.3 markdown.3 mkd-line.3 /usr/local/man/man3
for x in mkd_line mkd_generateline; do \
    ( echo '.\"' ; echo ".so man3/mkd-line.3" ) > /usr/local/man/man3/$x.3;\
done
for x in mkd_in mkd_string; do \
    ( echo '.\"' ; echo ".so man3/markdown.3" ) > /usr/local/man/man3/$x.3;\
done
for x in mkd_compile mkd_css mkd_generatecss mkd_generatehtml mkd_cleanup mkd_doc_title mkd_doc_author mkd_doc_date; do \
    ( echo '.\"' ; echo ".so man3/mkd-functions.3" ) > /usr/local/man/man3/$x.3; \
done
./config.md /usr/local/man/man7
/usr/bin/install -m 444 markdown.7 mkd-extensions.7 /usr/local/man/man7
./config.md /usr/local/man/man1
/usr/bin/install -m 444 markdown.1 /usr/local/man/man1
[root@localhost discount-2.2.2]# make install.samples
/usr/bin/install -s -m 755 markdown /usr/local/bin
./librarian.sh install libmarkdown VERSION /usr/local/lib
/usr/bin/install -m 444 mkdio.h /usr/local/include
./config.md /usr/local/man/man1
for x in mkd2html makepage theme; do \
    /usr/bin/install -s -m 755 $x /usr/local/bin/$x; \
    /usr/bin/install -m 444 $x.1 /usr/local/man/man1/$x.1; \
done

使い方

manを読めば大体わかる。 一番簡単な使い方をすると、以下のように.mdファイルを読み込んで標準出力に変換後のHTMLを出力できる。

[mk55@localhost tmp]$ cat test.md 
#h1
###h3

test

test2


| TH | TH |
| -- | -- |
| TD | TD |
| TD | TD |
[mk55@localhost tmp]$ 
[mk55@localhost tmp]$ markdown test.md 
<h1>h1</h1>

<h3>h3</h3>

<p>test</p>

<p>test2</p>

<table>
<thead>
<tr>
<th> TH </th>
<th> TH </th>
</tr>
</thead>
<tbody>
<tr>
<td> TD </td>
<td> TD </td>
</tr>
<tr>
<td> TD </td>
<td> TD </td>
</tr>
</tbody>
</table>

インデントはしてくれないのでxmllintなりtidyなりで整形しましょう*1。以下はtidyの例。

[mk55@localhost tmp]$ markdown test.md | tidy -i -utf8  2> /dev/null
<!DOCTYPE html>
<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for HTML5 for Linux version 5.4.0">
  <title></title>
</head>
<body>
  <h1>h1</h1>
  <h3>h3</h3>
  <p>test</p>
  <p>test2</p>
  <table>
    <thead>
      <tr>
        <th>TH</th>
        <th>TH</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>TD</td>
        <td>TD</td>
      </tr>
      <tr>
        <td>TD</td>
        <td>TD</td>
      </tr>
    </tbody>
  </table>
</body>
</html>

tidyのインストールについては以下に書いた

mk-55.hatenablog.com