LoginSignup
11
15

More than 5 years have passed since last update.

MarkdownをRedmine用のTextile記法に変換する

Last updated at Posted at 2018-09-14

標準だとTextile記法で書かなくてはならないRedmineですが、管理者権限があればMarkdown記法に変更できるらしい。

でも管理者権限を持っていないので、ドキュメント変換ツール「Pandoc」でmdファイルをtextileファイルに変換しRedmineに反映することにした。

Pandocをインストール

$ brew install pandoc

mdファイルをtextileファイルに変換して別名保存

$ pandoc test.md -o test.textile

これをRedmineに貼る。
しかしRedmineがコードブロックを処理してくれない。

textile記法だとコードブロックは

bc(html). <link href="/css/index.css" rel="stylesheet" type="text/css">

となるが、Redmineでは

<pre><code class="html">
<link href="/css/index.css" rel="stylesheet" type="text/css">
</code></pre>

のようにHTMLで書かなければならないようだ。
そこで、Pandocのフィルター機能でこの部分を変換するようにする。

まず、pandoc-filterをインストール。
これでfilterをnode.jsで記述できるようになります。

$ npm i pandoc-filter -g

次にフィルターとなるファイルを用意する。

forRedmine.js
#!/usr/bin/env node

const pandoc = require('pandoc-filter');
const RawBlock = pandoc.RawBlock;

const action = (key, value, format, meta) => {
  if (key === 'CodeBlock') {
    [ [, lang, ], code ] = value;
    const syntax = (lang.length) ? ` class="${lang}"` : '';
    return RawBlock('html', `<pre><code${syntax}>${code}</code></pre>\n`);
  }
}

pandoc.toJSONFilter(action);

このファイルをフィルターとして呼び出す。

$ pandoc test.md -o test.textile --filter=forRedmine.js

成功。

ちなみにSublime Text 3で行う場合は、Package ControlでPandocプラグインをインストール。
Pandoc.sublime.settingsに以下のようなセッティングを追記。

{
  "user": {
    "pandoc-path": "/usr/local/bin/pandoc",
    "transformations": {
      "Textile": {
        "new-buffer": 1,
        "scope": {
          "text.html.markdown": "markdown"
        },
        "syntax_file": "Packages/Textile/Textile.tmLanguage",
        "pandoc-arguments": [
          "--to=Textile",
          "--filter=/〜/forRedmine.js",
        ]
      },
    },
    "pandoc-format-file": ["docx", "epub", "pdf", "odt", "beamer", "textile"]
  }
}

これでわざわざコマンドを叩かなくても変換したものを新規ファイルとして開いてくれます。

11
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
15