close
Warning:
Can't synchronize with repository "(default)" (/var/svn/mioproject does not appear to be a Subversion repository.). Look in the Trac log for more information.
- Timestamp:
-
Jul 30, 2013, 8:39:06 PM (12 years ago)
- Author:
-
trac
- Comment:
-
--
Legend:
- Unmodified
- Added
- Removed
- Modified
-
v1
|
v2
|
|
1 | | = Trac マクロ = #TracMacros |
| 1 | = Trac Macros = |
2 | 2 | |
3 | 3 | [[PageOutline]] |
4 | 4 | |
5 | | Trac マクロとは、 Python で書かれた 'カスタム関数' によって Trac の Wiki エンジンを拡張するプラグインです。 WikiFormatting エンジンが利用可能なあらゆるコンテキストにおいて、マクロを使用することによって、動的な HTML データが挿入されます。 |
| 5 | Trac macros are plugins to extend the Trac engine with custom 'functions' written in Python. A macro inserts dynamic HTML data in any context supporting WikiFormatting. Its syntax is `[[macro-name(optional-arguments)]]`. |
6 | 6 | |
7 | | もう 1 種類のマクロは WikiProcessors です。これは通常、 Wiki 以外のマークアップ形式と表示を取り扱うために使用し、多くは、 (ソースコードハイライトのような) より大きいブロックに使用します。 |
| 7 | The WikiProcessors are another kind of macros. They typically deal with alternate markup formats and transformation of larger "blocks" of information (like source code highlighting). They are used for processing the multiline `{{{#!wiki-processor-name ... }}}` blocks. |
8 | 8 | |
9 | | == マクロの利用 == #UsingMacros |
10 | | マクロ呼び出しは、二つの ''角括弧 (square brackets) '' で括られた箇所です。 Python 関数のように、マクロは引数を取ることができ、括弧 (parenthesis) の中に、カンマで区切ったリストで表記します。 |
| 9 | == Using Macros == |
11 | 10 | |
12 | | Trac マクロは、 TracPlugins としても作成することができます。 TracPlugins として Trac マクロを作成することで「直接 HTTP リクエストにアクセスする。」といった通常の Trac マクロでは実現できない機能を実装することができます。 |
| 11 | Macro calls are enclosed in two ''square brackets''. Like Python functions, macros can also have arguments, a comma separated list within parentheses. |
13 | 12 | |
14 | | === 利用例 === #Example |
| 13 | === Getting Detailed Help === |
| 14 | The list of available macros and the full help can be obtained using the !MacroList macro, as seen [#AvailableMacros below]. |
15 | 15 | |
16 | | 'Trac' で始まる Wiki ページの最近の変更履歴 3 件分を表示するマクロです: |
| 16 | A brief list can be obtained via `[[MacroList(*)]]` or `[[?]]`. |
17 | 17 | |
18 | | {{{ |
19 | | [[RecentChanges(Trac,3)]] |
| 18 | Detailed help on a specific macro can be obtained by passing it as an argument to !MacroList, e.g. `[[MacroList(MacroList)]]`, or, more conveniently, by appending a question mark (`?`) to the macro's name, like in `[[MacroList?]]`. |
| 19 | |
| 20 | |
| 21 | |
| 22 | === Example === |
| 23 | |
| 24 | A list of 3 most recently changed wiki pages starting with 'Trac': |
| 25 | |
| 26 | ||= Wiki Markup =||= Display =|| |
| 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>Embed an image in wiki-formatted text. |
| 54 | |
| 55 | The first argument is the file … |
| 56 | <h3><code>[[InterTrac]]</code></h3>Provide a list of known <a class="wiki" href="/wiki/InterTrac">InterTrac</a> prefixes. |
| 57 | <h3><code>[[InterWiki]]</code></h3>Provide a description list for the known <a class="wiki" href="/wiki/InterWiki">InterWiki</a> prefixes. |
| 58 | <h3><code>[[KnownMimeTypes]]</code></h3>List all known mime-types which can be used as <a class="wiki" href="/wiki/WikiProcessors">WikiProcessors</a>. |
| 59 | Can be …</div> |
| 60 | }}} |
| 61 | etc. |
20 | 62 | }}} |
21 | 63 | |
22 | | は、以下のように表示されます: |
23 | | [[RecentChanges(Trac,3)]] |
| 64 | == Available Macros == |
24 | 65 | |
25 | | == マクロ一覧 == #AvailableMacros |
26 | | |
27 | | ''Note: 以下に示すリストはマクロドキュメントを含むものだけです。 `-OO` による最適化や、 [wiki:TracModPython mod_python] での `PythonOptimize` オプションが設定されていると表示されません。'' |
| 66 | ''Note that the following list will only contain the macro documentation if you've not enabled `-OO` optimizations, or not set the `PythonOptimize` option for [wiki:TracModPython mod_python].'' |
28 | 67 | |
29 | 68 | [[MacroList]] |
30 | 69 | |
31 | | == 世界のマクロを共有 == #Macrosfromaroundtheworld |
| 70 | == Macros from around the world == |
32 | 71 | |
33 | | [http://trac-hacks.org/ Trac Hacks] というサイトは、コミュニティに寄稿されたマクロと [TracPlugins プラグイン] を収集し提供しています。新しいマクロを探している、共有したいマクロを作成した、などの場合は遠慮なく Trac Hacks のサイトを訪問してください。 |
| 72 | The [http://trac-hacks.org/ Trac Hacks] site provides a wide collection of macros and other Trac [TracPlugins plugins] contributed by the Trac community. If you're looking for new macros, or have written one that you'd like to share with the world, please don't hesitate to visit that site. |
34 | 73 | |
35 | | == カスタムマクロを開発する == #DevelopingCustomMacros |
36 | | マクロは、 Trac 自身と同じように [http://www.python.org/ Python] で書かれています。 |
| 74 | == Developing Custom Macros == |
| 75 | Macros, like Trac itself, are written in the [http://python.org/ Python programming language] and are developed as part of TracPlugins. |
37 | 76 | |
38 | | マクロの開発についての詳しい情報は [http://trac.edgewall.org/wiki/TracDev リソースの開発] を参照してください。 |
| 77 | For more information about developing macros, see the [trac:TracDev development resources] on the main project site. |
39 | 78 | |
40 | 79 | |
41 | | == マクロの実装 == #Implementation |
| 80 | Here are 2 simple examples showing how to create a Macro with Trac 0.11. |
42 | 81 | |
43 | | Trac 0.11 でマクロを作成する簡単な例を 2 つ紹介します。 |
| 82 | Also, have a look at [trac:source:tags/trac-0.11/sample-plugins/Timestamp.py Timestamp.py] for an example that shows the difference between old style and new style macros and at the [trac:source:tags/trac-0.11/wiki-macros/README macros/README] which provides a little more insight about the transition. |
44 | 83 | |
45 | | 古いマクロと新しいマクロの違いを示す例は [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] を参照してください。 |
46 | | |
47 | | === 引数なしのマクロ === #Macrowithoutarguments |
48 | | Trac は マクロ名としてモジュール名を使用するので以下は `TimeStamp.py` という名前で保存しなければなりません。 |
| 84 | === Macro without arguments === |
| 85 | To test the following code, you should saved it in a `timestamp_sample.py` file located in the TracEnvironment's `plugins/` directory. |
49 | 86 | {{{ |
50 | 87 | #!python |
… |
… |
|
63 | 100 | url = "$URL$" |
64 | 101 | |
65 | | def expand_macro(self, formatter, name, args): |
| 102 | def expand_macro(self, formatter, name, text): |
66 | 103 | t = datetime.now(utc) |
67 | 104 | return tag.b(format_datetime(t, '%c')) |
68 | 105 | }}} |
69 | 106 | |
70 | | === 引数付きのマクロ === #Macrowitharguments |
71 | | Trac は マクロ名としてモジュール名を使用するので以下は `HelloWorld.py` という名前で (plugins/ ディレクトリ内に ) 保存しなければなりません。 |
| 107 | === Macro with arguments === |
| 108 | To test the following code, you should saved it in a `helloworld_sample.py` file located in the TracEnvironment's `plugins/` directory. |
72 | 109 | {{{ |
73 | 110 | #!python |
| 111 | from genshi.core import Markup |
| 112 | |
74 | 113 | from trac.wiki.macros import WikiMacroBase |
75 | 114 | |
… |
… |
|
89 | 128 | url = "$URL$" |
90 | 129 | |
91 | | def expand_macro(self, formatter, name, args): |
| 130 | def expand_macro(self, formatter, name, text, args): |
92 | 131 | """Return some output that will be displayed in the Wiki content. |
93 | 132 | |
94 | 133 | `name` is the actual name of the macro (no surprise, here it'll be |
95 | 134 | `'HelloWorld'`), |
96 | | `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. |
97 | 136 | Note that if there are ''no'' parenthesis (like in, e.g. |
98 | | [[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. |
99 | 140 | """ |
100 | | return 'Hello World, args = ' + unicode(args) |
101 | | |
102 | | # Note that there's no need to HTML escape the returned data, |
103 | | # 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 | |
104 | 144 | }}} |
105 | 145 | |
| 146 | Note that `expand_macro` optionally takes a 4^th^ parameter ''`args`''. When the macro is called as a [WikiProcessors WikiProcessor], it's also possible to pass `key=value` [WikiProcessors#UsingProcessors processor parameters]. If given, those are stored in a dictionary and passed in this extra `args` parameter. On the contrary, when called as a macro, `args` is `None`. (''since 0.12''). |
| 147 | |
| 148 | For example, when writing: |
106 | 149 | {{{ |
107 | | #!div class=important |
108 | | 訳注 '''重要''': |
109 | | |
110 | | Wiki マクロが引数を持つ場合、引数は必ずサニタイズしてください。 |
111 | | expand_macro の戻り値は `<script>` タグやイベントハンドラなどもそのまま出力するので、入力をチェックせずに、そのまま戻り値に使用するのは極めて危険です。 |
112 | | Genshi の tag オブジェクトにラップすれば、エスケープ機構が働きますので、通常はこれを使うとよいでしょう。 |
113 | | `HelloWorld.py` は、以下の通り書き直すことができます: |
114 | | {{{ |
115 | | #!python |
116 | | from trac.wiki.macros import WikiMacroBase |
117 | | from genshi.builder import tag |
118 | | class HelloWorldMacro(WikiMacroBase): |
119 | | def expand_macro(self, formatter, name, args): |
120 | | return tag.div(u'Hello World, args = ', unicode(args)) |
121 | | }}} |
| 150 | {{{#!HelloWorld style="polite" -silent verbose |
| 151 | <Hello World!> |
122 | 152 | }}} |
123 | 153 | |
124 | | === {{{expand_macro}}} について === #expand_macrodetails |
125 | | {{{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 | }}} |
126 | 157 | |
127 | | マクロで HTML ではなく Wiki マークアップを使用したい場合、以下のように HTML に変換します: |
| 158 | [[HelloWorld(<Hello World!>)]] |
| 159 | }}} |
| 160 | One should get: |
| 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 that the return value of `expand_macro` is '''not''' HTML escaped. Depending on the expected result, you should escape it by yourself (using `return Markup.escape(result)`) or, if this is indeed HTML, wrap it in a Markup object (`return Markup(result)`) with `Markup` coming from Genshi, (`from genshi.core import Markup`). |
| 168 | |
| 169 | You can also recursively use a wiki Formatter (`from trac.wiki import Formatter`) to process the `text` as wiki markup, for example by doing: |
128 | 170 | |
129 | 171 | {{{ |
130 | 172 | #!python |
131 | | text = "whatever wiki markup you want, even containing other macros" |
132 | | # Convert Wiki markup to HTML, new style |
133 | | out = StringIO() |
134 | | Formatter(self.env, formatter.context).format(text, out) |
135 | | 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()) |
136 | 185 | }}} |