MojoMojo: Formatter
記事を整形処理するクラス
Formatter とは
MojoMojo には MojoMojo::Formatter::* というクラスがあり、それらのクラスがwikiの記事を整形処理してHTMLを生成しています。
Markdown や Textile も MojoMojo::Formatter::Markdown や MojoMojo::Formatter::Textile として実装されていて、何か記事を整形する処理を追加したいときは、Formatter を追加すれば良い仕組みになっています。
デフォルト Formatter
MojoMojo-1.00 でデフォルトでインストールされる Formatter は次のようなものです。
- MojoMojo::Formatter::Amazon
Include Amazon objects on your page. - MojoMojo::Formatter::CPANHyperlink
automatically hyperlink CPAN modules when using the syntax { {cpan Some::Module} } - MojoMojo::Formatter::Comment
Include comments on your page. - MojoMojo::Formatter::Defang
Scrub user HTML and XSS - MojoMojo::Formatter::Dir
format local directory as XHTML - MojoMojo::Formatter::DocBook
format part of content as DocBook - MojoMojo::Formatter::DocBook::Colorize
- MojoMojo::Formatter::Emote
MSN Smileys in your text - MojoMojo::Formatter::File
format file as XHTML - MojoMojo::Formatter::File::DocBook
format Docbook in xhtml - MojoMojo::Formatter::File::Image
Image formatter - MojoMojo::Formatter::File::Pod
format Pod File in xhtml - MojoMojo::Formatter::File::Text
- MojoMojo::Formatter::GoogleSearch
Linked Google Search Engine by writing { {google:<search kind> <keyword>} } - MojoMojo::Formatter::IRCLog
format part of content as an IRC log - MojoMojo::Formatter::Include
Include files in your content. - MojoMojo::Formatter::Main
MojoMojo's main formatter, dispatching between Textile and MultiMarkdown - MojoMojo::Formatter::Markdown
MultiMarkdown formatting for your content. MultiMarkdown is an extension of Markdown, adding support for tables, footnotes, bibliography, automatic cross-references, glossaries, appendices, definition - MojoMojo::Formatter::Pod
format part of content as POD - MojoMojo::Formatter::RSS
Include RSS feeds on your page. - MojoMojo::Formatter::Redirect
Handles { {redirect /path} } - MojoMojo::Formatter::SyntaxHighlight
syntax highlighting for code blocks - MojoMojo::Formatter::TOC
generate table of contents - MojoMojo::Formatter::Text
format plain text as xhtml - MojoMojo::Formatter::Textile
Texile+SmartyPants formatting for your content - MojoMojo::Formatter::Wiki
Handle interpage linking. - MojoMojo::Formatter::WikipediaLink
Linked Wikipedia by writing { {wikipedia:<lang> <word>} } - MojoMojo::Formatter::YouTube
Embed YouTube player
Formatter の書き方
上に挙げたいくつもの実装を参考にすれば独自の Formatter を書くのも簡単です。
ポイントは2点あります。
format_content_order
MojoMojo の Formatter は Module::Pluggable::Ordered によって処理順がコントロールされていて、それぞれの Formatter のなかで、format_content_order
メソッドを書き、処理順を明示しなければいけません。
デフォルトの処理順は、Formatter のベースクラスとなる MojoMojo::Formatter の POD に掲載されています。gen_re
Formatter は記事の中で {{foo}} という中括弧で囲まれた部分を整形処理します。
Formatter の中で、$class->gen_re(regex)
とすることで、処理するべき記述を取得することができます。
もしも記事全体を整形対象にするなら、format_content
の第2引数 $content (my ( $class, $content, $c ) = @_;
)を書き換えます。
Formatter のテスト
Formatter のテストは、t/ 直下に formatter_*.t というファイル名で置かれます。
デフォルトでインストールされるどの Formatter にもテストがあるので、参考にして書けば難しいところは何もありません。