| | 1 | = Trac マクロ = #TracMacros |
| | 2 | |
| | 3 | [[PageOutline]] |
| | 4 | |
| | 5 | Trac マクロとは、 Python で書かれた 'カスタム関数' によって Trac の Wiki エンジンを拡張するプラグインです。 WikiFormatting エンジンが利用可能なあらゆるコンテキストにおいて、マクロを使用することによって、動的な HTML データが挿入されます。 |
| | 6 | |
| | 7 | もう 1 種類のマクロは WikiProcessors です。これは通常、 Wiki 以外のマークアップ形式と表示を取り扱うために使用し、多くは、 (ソースコードハイライトのような) より大きいブロックに使用します。 |
| | 8 | |
| | 9 | == マクロの利用 == #UsingMacros |
| | 10 | |
| | 11 | マクロ呼び出しは、二つの ''角括弧 (square brackets) '' で括られた箇所です。 Python 関数のように、マクロは引数を取ることができ、括弧 (parenthesis) の中に、カンマで区切ったリストで表記します。 |
| | 12 | |
| | 13 | === 詳細なヘルプを見るには === #GettingDetailedHelp |
| | 14 | マクロの一覧と完全なヘルプは、 下記の[#AvailableMacros マクロ一覧] にある !MacroList マクロを使用してみることができます。 |
| | 15 | |
| | 16 | 簡単なマクロ一覧は `[[MacroList(*)]]` や `[[?]]` で見ることができます。 |
| | 17 | |
| | 18 | 特定のマクロの詳細なヘルプを参照したい場合は、 !MacroList マクロに引数渡すことによって参照することができます。 例) `[[MacroList(MacroList)]]` 。もしくは、便宜上、 `[[MacroList?]]` のようにマクロ名にクエスチョンマーク ('?') をつけることでヘルプをみることができます。 |
| | 19 | |
| | 20 | |
| | 21 | |
| | 22 | === 利用例 === #Example |
| | 23 | |
| | 24 | 'Trac' で始まる Wiki ページの最近の変更履歴 3 件分を表示するマクロです: |
| | 25 | |
| | 26 | ||= Wiki マークアップ =||= 表示 =|| |
| | 27 | {{{#!td |
| | 28 | {{{ |
| | 29 | [[RecentChanges(Trac,3)]] |
| | 30 | }}} |
| | 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 形式のテキストに組み込みます。 |
| | 54 | |
| | 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 | }}} |
| | 63 | |
| | 64 | == マクロ一覧 == #AvailableMacros |
| | 65 | |
| | 66 | ''Note: 以下に示すリストはマクロドキュメントを含むものだけです。 `-OO` による最適化や、 [wiki:TracModPython mod_python] での `PythonOptimize` オプションが設定されていると表示されません。'' |
| | 67 | |
| | 68 | [[MacroList]] |
| | 69 | |
| | 70 | == 世界のマクロを共有 == #Macrosfromaroundtheworld |
| | 71 | |
| | 72 | [http://trac-hacks.org/ Trac Hacks] というサイトは、コミュニティに寄稿されたマクロと [TracPlugins プラグイン] を収集し提供しています。新しいマクロを探している、共有したいマクロを作成した、などの場合は遠慮なく Trac Hacks のサイトを訪問してください。 |
| | 73 | |
| | 74 | == カスタムマクロを開発する == #DevelopingCustomMacros |
| | 75 | マクロは、 Trac 本体と同様 [http://python.org/ Python] で書かれています。そして TracPlugins の一種として開発します。 |
| | 76 | |
| | 77 | マクロの開発についての詳しい情報は [trac:TracDev リソースの開発] を参照してください。 |
| | 78 | |
| | 79 | |
| | 80 | Trac 0.11 でマクロを作成する簡単な例を 2 つ紹介します。 |
| | 81 | |
| | 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] を参照してください。 |
| | 83 | |
| | 84 | === 引数なしのマクロ === #Macrowithoutarguments |
| | 85 | 下記のソースコードをテストするためには、このソースコードを `timestamp_sample.py` として保存し、 TracEnvironment の `plugins/` に配置しなければなりません。 |
| | 86 | {{{ |
| | 87 | #!python |
| | 88 | from datetime import datetime |
| | 89 | # Note: since Trac 0.11, datetime objects are used internally |
| | 90 | |
| | 91 | from genshi.builder import tag |
| | 92 | |
| | 93 | from trac.util.datefmt import format_datetime, utc |
| | 94 | from trac.wiki.macros import WikiMacroBase |
| | 95 | |
| | 96 | class TimeStampMacro(WikiMacroBase): |
| | 97 | """Inserts the current time (in seconds) into the wiki page.""" |
| | 98 | |
| | 99 | revision = "$Rev$" |
| | 100 | url = "$URL$" |
| | 101 | |
| | 102 | def expand_macro(self, formatter, name, text): |
| | 103 | t = datetime.now(utc) |
| | 104 | return tag.b(format_datetime(t, '%c')) |
| | 105 | }}} |
| | 106 | |
| | 107 | === 引数付きのマクロ === #Macrowitharguments |
| | 108 | 下記のソースコードをテストするためには、このソースコードを `helloworld_sample.py` として保存し、 TracEnvironment の `plugins/` に配置しなければなりません。 |
| | 109 | {{{ |
| | 110 | #!python |
| | 111 | from genshi.core import Markup |
| | 112 | |
| | 113 | from trac.wiki.macros import WikiMacroBase |
| | 114 | |
| | 115 | class HelloWorldMacro(WikiMacroBase): |
| | 116 | """Simple HelloWorld macro. |
| | 117 | |
| | 118 | Note that the name of the class is meaningful: |
| | 119 | - it must end with "Macro" |
| | 120 | - what comes before "Macro" ends up being the macro name |
| | 121 | |
| | 122 | The documentation of the class (i.e. what you're reading) |
| | 123 | will become the documentation of the macro, as shown by |
| | 124 | the !MacroList macro (usually used in the WikiMacros page). |
| | 125 | """ |
| | 126 | |
| | 127 | revision = "$Rev$" |
| | 128 | url = "$URL$" |
| | 129 | |
| | 130 | def expand_macro(self, formatter, name, text, args): |
| | 131 | """Return some output that will be displayed in the Wiki content. |
| | 132 | |
| | 133 | `name` is the actual name of the macro (no surprise, here it'll be |
| | 134 | `'HelloWorld'`), |
| | 135 | `text` is the text enclosed in parenthesis at the call of the macro. |
| | 136 | Note that if there are ''no'' parenthesis (like in, e.g. |
| | 137 | [[HelloWorld]]), then `text` is `None`. |
| | 138 | `args` are the arguments passed when HelloWorld is called using a |
| | 139 | `#!HelloWorld` code block. |
| | 140 | """ |
| | 141 | return 'Hello World, text = %s, args = %s' % \ |
| | 142 | (Markup.escape(text), Markup.escape(repr(args))) |
| | 143 | |
| | 144 | }}} |
| | 145 | |
| | 146 | Note: `expand_macro` は 第4パラメータに、 ''`args`'' を任意に取ることもできます。 このマクロが [WikiProcessors WikiProcessor] として呼ばれたとき、 `key=value` 形式の [WikiProcessors#UsingProcessors プロセッサパラメータ] を渡すことも可能です。もし、このパラメータを指定したとき、これらの値は、ディクショナリの中に保存され、 追加の `args` パラメータによって渡されます。一方で、マクロとして呼び出されたときは、 `args` パラメータは、 `None` として扱われます (''0.12 以降'') 。 |
| | 147 | |
| | 148 | 例として、このように記述した場合: |
| | 149 | {{{ |
| | 150 | {{{#!HelloWorld style="polite" -silent verbose |
| | 151 | <Hello World!> |
| | 152 | }}} |
| | 153 | |
| | 154 | {{{#!HelloWorld |
| | 155 | <Hello World!> |
| | 156 | }}} |
| | 157 | |
| | 158 | [[HelloWorld(<Hello World!>)]] |
| | 159 | }}} |
| | 160 | 結果はこのようになります: |
| | 161 | {{{ |
| | 162 | Hello World, text = <Hello World!> , args = {'style': u'polite', 'silent': False, 'verbose': True} |
| | 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`) オブジェクトも再帰的に使用することができます。以下がサンプルです: |
| | 170 | |
| | 171 | {{{ |
| | 172 | #!python |
| | 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()) |
| | 185 | }}} |