WikiMacros のバージョン 1 とバージョン 2 との変更
- 日時:
- 2012/01/16 5:19:14 (13年前)
凡例:
- 未変更
- 追加
- 削除
- 更新
-
WikiMacros
v1 v2 8 8 9 9 == マクロの利用 == #UsingMacros 10 10 11 マクロ呼び出しは、二つの ''角括弧 (square brackets) '' で括られた箇所です。 Python 関数のように、マクロは引数を取ることができ、括弧 (parenthesis) の中に、カンマで区切ったリストで表記します。 11 12 12 Trac マクロは、 TracPlugins としても作成することができます。 TracPlugins として Trac マクロを作成することで「直接 HTTP リクエストにアクセスする。」といった通常の Trac マクロでは実現できない機能を実装することができます。 13 === 詳細なヘルプを見るには ===#GettingDetailedHelp 14 マクロの一覧と完全なヘルプは、 下記の[#AvailableMacros マクロ一覧] にある !MacroList マクロを使用してみることができます。 15 16 簡単なマクロ一覧は ![[MacroList(*)]] や ![[?]] で見ることができます。 17 18 特定のマクロの詳細なヘルプを参照したい場合は、 !MacroList マクロに引数渡すことによって参照することができます。例) ![[MacroList(MacroList)]] 。もしくは、便宜上、 ![[MacroList?]] のようにマクロ名にクエスチョンマーク (?) をつけることでヘルプをみることができます。 19 20 13 21 14 22 === 利用例 === #Example … … 16 24 'Trac' で始まる Wiki ページの最近の変更履歴 3 件分を表示するマクロです: 17 25 18 {{{ 19 [[RecentChanges(Trac,3)]] 26 ||= Wiki マークアップ =||= 表示 =|| 27 {{{#!td 28 {{{ 29 [[RecentChanges(Trac,3)]] 30 }}} 20 31 }}} 32 {{{#!td style="padding-left: 2em;" 33 [[RecentChanges(Trac,3)]] 34 }}} 35 |----------------------------------- 36 {{{#!td 37 {{{ 38 [[RecentChanges?(Trac,3)]] 39 }}} 40 }}} 41 {{{#!td style="padding-left: 2em;" 42 [[RecentChanges?(Trac,3)]] 43 }}} 44 |----------------------------------- 45 {{{#!td 46 {{{ 47 [[?]] 48 }}} 49 }}} 50 {{{#!td style="padding-left: 2em" 51 {{{#!html 52 <div style="font-size: 80%" class="trac-macrolist"> 53 <h3><code>[[Image]]</code></h3>画像を Wiki 形式のテキストに組み込みます。 21 54 22 は、以下のように表示されます: 23 [[RecentChanges(Trac,3)]] 55 1 番目の引数は、ファイル名を指定します。ファイルの指定は添付ファイルなど … 56 <h3><code>[[InterTrac]]</code></h3>設定済みの <a class="wiki" href="/wiki/InterTrac">InterTrac</a> のプレフィックスを表示します。 57 <h3><code>[[InterWiki]]</code></h3><a class="wiki" href="/wiki/InterWiki">InterWiki</a> のプレフィックスの概要を表示します。 58 <h3><code>[[KnownMimeTypes]]</code></h3><a class="wiki" href="/wiki/WikiProcessors">WikiProcessors</a> で処理できる既知の mime-type を表示します。 59 引数が与えられた場合は、 mime-type …</div> 60 }}} 61 etc. 62 }}} 24 63 25 64 == マクロ一覧 == #AvailableMacros … … 34 73 35 74 == カスタムマクロを開発する == #DevelopingCustomMacros 36 マクロは、 Trac 自身と同じように [http://www.python.org/ Python] で書かれています。75 マクロは、 Trac 本体と同様 [http://python.org/ Python] で書かれています。そして TracPlugins の一種として開発します 37 76 38 77 マクロの開発についての詳しい情報は [http://trac.edgewall.org/wiki/TracDev リソースの開発] を参照してください。 39 78 40 79 41 == マクロの実装 == #Implementation 80 Trac 0.11 でマクロを作成する簡単な例を 2 つ紹介します。 42 81 43 [http://trac.edgewall.org/wiki/0.11 Trac 0.11] でマクロを作成する 2 つの単純な例を紹介します。古いマクロと新しいマクロの違いを示す例は [http://trac.edgewall.org/browser/trunk/sample-plugins/Timestamp.py Timestamp.py] を参照してください。また古いマクロから新しいマクロに移行するための情報は [http://trac.edgewall.org/browser/trunk/wiki-macros/READMEREADME] を参照してください。82 古いマクロと新しいマクロの違いを示す例は [http://trac.edgewall.org/browser/tags/trac-0.11/sample-plugins/Timestamp.py Timestamp.py] を参照してください。また、古いマクロから新しいマクロに移行するための情報は [http://trac.edgewall.org/browser/tags/trac-0.11/wiki-macros/README macros/README] を参照してください。 44 83 45 84 === 引数なしのマクロ === #Macrowithoutarguments 46 Trac は マクロ名としてモジュール名を使用するので以下は `TimeStamp.py` という名前で保存しなければなりません。85 下記のソースコードをテストするためには、このソースコードを `timestamp_sample.py` として保存し、 TracEnvironment の `plugins/` に配置しなければなりません。 47 86 {{{ 48 87 #!python … … 55 94 from trac.wiki.macros import WikiMacroBase 56 95 57 class Time stampMacro(WikiMacroBase):96 class TimeStampMacro(WikiMacroBase): 58 97 """Inserts the current time (in seconds) into the wiki page.""" 59 98 … … 61 100 url = "$URL$" 62 101 63 def expand_macro(self, formatter, name, args):102 def expand_macro(self, formatter, name, text): 64 103 t = datetime.now(utc) 65 104 return tag.b(format_datetime(t, '%c')) … … 67 106 68 107 === 引数付きのマクロ === #Macrowitharguments 69 Trac は マクロ名としてモジュール名を使用するので以下は `HelloWorld.py` という名前で (plugins/ ディレクトリ内に ) 保存しなければなりません。108 下記のソースコードをテストするためには、このソースコードを `helloworld_sample.py` として保存し、 TracEnvironment の `plugins/` に配置しなければなりません。 70 109 {{{ 71 110 #!python 111 from genshi.core import Markup 112 72 113 from trac.wiki.macros import WikiMacroBase 73 114 … … 87 128 url = "$URL$" 88 129 89 def expand_macro(self, formatter, name, args):130 def expand_macro(self, formatter, name, text, args): 90 131 """Return some output that will be displayed in the Wiki content. 91 132 92 133 `name` is the actual name of the macro (no surprise, here it'll be 93 134 `'HelloWorld'`), 94 ` args` is the text enclosed in parenthesis at the call of the macro.135 `text` is the text enclosed in parenthesis at the call of the macro. 95 136 Note that if there are ''no'' parenthesis (like in, e.g. 96 [[HelloWorld]]), then `args` is `None`. 137 [[HelloWorld]]), then `text` is `None`. 138 `args` are the arguments passed when HelloWorld is called using a 139 `#!HelloWorld` code block. 97 140 """ 98 return 'Hello World, args = ' + unicode(args) 99 100 # Note that there's no need to HTML escape the returned data, 101 # as the template engine (Genshi) will do it for us. 141 return 'Hello World, text = %s, args = %s' % \ 142 (Markup.escape(text), Markup.escape(repr(args))) 143 102 144 }}} 103 145 146 Note: `expand_macro` は 第4パラメータに、 ''`args`'' を任意に取ることもできます。 このマクロが [WikiProcessors WikiProcessor] として呼ばれたとき、 `key=value` 形式の [WikiProcessors#UsingProcessors プロセッサパラメータ] を渡すことも可能です。もし、このパラメータを指定したとき、これらの値は、ディクショナリの中に保存され、 追加の `args` パラメータによって渡されます。一方で、マクロとして呼び出されたときは、 `args` パラメータは、 `None` として扱われます (''0.12 以降'') 。 147 148 例として、このように記述した場合: 104 149 {{{ 105 #!div class=important 106 訳注 '''重要''': 107 108 Wiki マクロが引数を持つ場合、引数は必ずサニタイズしてください。 109 expand_macro の戻り値は `<script>` タグやイベントハンドラなどもそのまま出力するので、入力をチェックせずに、そのまま戻り値に使用するのは極めて危険です。 110 Genshi の tag オブジェクトにラップすれば、エスケープ機構が働きますので、通常はこれを使うとよいでしょう。 111 `HelloWorld.py` は、以下の通り書き直すことができます: 112 {{{ 113 #!python 114 from trac.wiki.macros import WikiMacroBase 115 from genshi.builder import tag 116 class HelloWorldMacro(WikiMacroBase): 117 def expand_macro(self, formatter, name, args): 118 return tag.div(u'Hello World, args = ', unicode(args)) 119 }}} 150 {{{#!HelloWorld style="polite" 151 <Hello World!> 120 152 }}} 121 153 122 === {{{expand_macro}}} について === #expand_macrodetails 123 {{{expand_macro}}} は HTML として解釈できる単純な文字列か Markup オブジェクト ({{{from trac.util.html import Markup}}} を使用する ) のどちらかを返さなければなりません。 {{{Markup(string)}}} はそのまま `string` を解釈するので、 HTML 文字列はエスケープされずに、そのとおり表示されます。 Wiki フォーマッタを使用した場合は {{{from trac.wiki import Formatter}}} のように import してください。 154 {{{#!HelloWorld 155 <Hello World!> 156 }}} 124 157 125 マクロで HTML ではなく Wiki マークアップを使用したい場合、以下のように HTML に変換します: 158 [[HelloWorld(<Hello World!>)]] 159 }}} 160 結果はこのようになります: 161 {{{ 162 Hello World, text = <Hello World!> , args = {'style': u'polite'} 163 Hello World, text = <Hello World!> , args = {} 164 Hello World, text = <Hello World!> , args = None 165 }}} 166 167 Note: `expand_macro` が返す値は、 HTML がエスケープされて '''いない''' ことに注意して下さい。期待する戻り値によっては、あなた自身でエスケープする必要があります (`return Markup.escape(result)` を使用できます)。また、戻り値として HTML が返ってくると分かっているならば、結果を (`return Markup(result)`) という風に Genshi が提供している Markup (`from genshi.core import Markup`) オブジェクトでラップすることもできます。 168 169 また、`text` を Wiki としてマークアップする場合、 Wiki Formatter (`from trac.wiki import Formatter`) オブジェクトも再帰的に使用することができます。以下がサンプルです: 126 170 127 171 {{{ 128 172 #!python 129 text = "whatever wiki markup you want, even containing other macros" 130 # Convert Wiki markup to HTML, new style 131 out = StringIO() 132 Formatter(self.env, formatter.context).format(text, out) 133 return Markup(out.getvalue()) 173 from genshi.core import Markup 174 from trac.wiki.macros import WikiMacroBase 175 from trac.wiki import Formatter 176 import StringIO 177 178 class HelloWorldMacro(WikiMacroBase): 179 def expand_macro(self, formatter, name, text, args): 180 text = "whatever '''wiki''' markup you want, even containing other macros" 181 # Convert Wiki markup to HTML, new style 182 out = StringIO.StringIO() 183 Formatter(self.env, formatter.context).format(text, out) 184 return Markup(out.getvalue()) 134 185 }}}