どうも、シローです。
今回は、文書管理の仕組みを作ります。
記事の属性について
ブログの記事は大体、
- 記事のタイトル
- 記事の本文
- キーワード
- 記事の状態(本番、下書きなど)
- 記事の種類(固定ページか、日記かなど)
- タイムスタンプ(投稿時間や更新時間)
- 記事の作成者
を持つとします。
記事ファイルのフォーマットについて
スマートフォンやタブレットでも記述しやすい、また、Gitでプレビューを見れるという観点から
HTMLではなく、マークダウンを使います。
マークダウンで編集したファイルをPandocというツールでHTMLに変換して表示するという仕組みにします。
Pandocを使って、マークダウンをHTMLに変換してみよう
以下のようなマークダウンを用意します。
1 2 3 4 5 6 7 8 9 |
# これはタイトル ## これはサブタイトル * リストA * リストB * リストC pandocについて: https://pandoc.org/getting-started.html |
これをPandocでHTMLに変換します。
1 2 3 4 5 6 7 8 9 |
shiro@shiro-mmm:~$ pandoc -f markdown_github hoge.md <h1>これはタイトル</h1> <h2>これはサブタイトル</h2> <ul> <li>リストA</li> <li>リストB</li> <li>リストC</li> </ul> <p>pandocについて: <a href="https://pandoc.org/getting-started.html" class="uri">https://pandoc.org/getting-started.html</a></p> |
メタデータを付与する
次のようにキーワードや作成日などのメタデータを持つマークダウンを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
--- Keywords: test, pandoc Created: 2020-01-18 Author: shiro --- # これはタイトル ## これはサブタイトル * リストA * リストB * リストC pandocについて: https://pandoc.org/getting-started.html |
これを先ほどのコマンドで変換してみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
shiro@shiro-mmm:~$ pandoc -f markdown_github hoge.md <hr /> <p>Keywords: test, pandoc<br /> Created: 2020-01-18<br /> Author: shiro<br /> ---</p> <h1>これはタイトル</h1> <h2>これはサブタイトル</h2> <ul> <li>リストA</li> <li>リストB</li> <li>リストC</li> </ul> <p>pandocについて: <a href="https://pandoc.org/getting-started.html" class="uri">https://pandoc.org/getting-started.html</a></p> |
本文以外にも、メタデータがついてしまうので、次のようにメタデータは無視する設定(yaml_metadata_block)を付与してコマンドを実行してみます。
1 2 3 4 5 6 7 8 9 |
shiro@shiro-mmm:~$ pandoc -f markdown_github+yaml_metadata_block hoge.md <h1>これはタイトル</h1> <h2>これはサブタイトル</h2> <ul> <li>リストA</li> <li>リストB</li> <li>リストC</li> </ul> <p>pandocについて: <a href="https://pandoc.org/getting-started.html" class="uri">https://pandoc.org/getting-started.html</a></p> |
無事メタデータは無視されて出力されました。
Gitで記事リポジトリを管理する
ホームディレクトリにリポジトリ用のディレクトリを作成します。
1 |
shiro@shiro-mmm:~$ mkdir shiro-mmm_contents |
ディレクトリに入ると記事用のディレクトリ「posts」と固定ページ用のディレクトリ「pages」を作成します。
1 2 |
shiro@shiro-mmm:~$ cd shiro-mmm_contents/ shiro@shiro-mmm:~/shiro-mmm_contents$ mkdir pages posts |
記事のテンプレート用のファイルをおくディレクトリ「template」を作成します。
1 2 3 |
shiro@shiro-mmm:~/shiro-mmm_contents$ mkdir template shiro@shiro-mmm:~/shiro-mmm_contents$ ls pages posts template |
templateに入り、テンプレートファイル「main.md」を作成します。
1 2 |
shiro@shiro-mmm:~/shiro-mmm_contents$ cd template/ shiro@shiro-mmm:~/shiro-mmm_contents/template$ vim main.md |
1 2 3 4 5 6 7 8 |
--- KeyWords: CopyRight: (C) 2020 Shiro --- # title Write contents here. |
記事リポジトリをGitの管理下におく
記事リポジトリをGitで管理できるようにします。
1 2 3 4 5 6 |
shiro@shiro-mmm:~/shiro-mmm_contents$ git init shiro@shiro-mmm:~/shiro-mmm_contents$ git add -A shiro@shiro-mmm:~/shiro-mmm_contents$ git commit -m 'first commit' [master (root-commit) 778ab1f] first commit 1 file changed, 8 insertions(+) create mode 100644 template/main.md |
Github管理画面でリポジトリを作成し、
最初のコミットをプッシュします。
1 2 |
shiro@shiro-mmm:~/shiro-mmm_contents$ git remote add origin https://github.com/smithshiro/shiro-mmm_contents.git shiro@shiro-mmm:~/shiro-mmm_contents$ git push -u origin master |
今回はここまでにします。
次回は記事を公開する本体のプログラムを作成していきます。