XPressME Integration Kit

Trac

Initial Version から Version 1 における更新: WikiMacros

差分発生行の前後
Ignore:
Timestamp:
Oct 30, 2008, 4:53:49 PM (16 years 前)
Author:
trac (IP: 127.0.0.1)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WikiMacros

    v1 v1  
     1= Trac マクロ = #TracMacros 
     2 
     3[[PageOutline]] 
     4 
     5Trac マクロとは、 Python で書かれた 'カスタム関数' によって Trac の Wiki エンジンを拡張するプラグインです。 WikiFormatting エンジンが利用可能なあらゆるコンテキストにおいて、マクロを使用することによって、動的な HTML データが挿入されます。 
     6 
     7もう 1 種類のマクロは WikiProcessors です。これは通常、 Wiki 以外のマークアップ形式と表示を取り扱うために使用し、多くは、 (ソースコードハイライトのような) より大きいブロックに使用します。 
     8 
     9== マクロの利用 == #UsingMacros 
     10マクロ呼び出しは、二つの ''角括弧 (square brackets) '' で括られた箇所です。 Python 関数のように、マクロは引数を取ることができ、括弧 (parenthesis) の中に、カンマで区切ったリストで表記します。 
     11 
     12Trac マクロは、 TracPlugins としても作成することができます。 TracPlugins として Trac マクロを作成することで「直接 HTTP リクエストにアクセスする。」といった通常の Trac マクロでは実現できない機能を実装することができます。 
     13 
     14=== 利用例 === #Example 
     15 
     16'Trac' で始まる Wiki ページの最近の変更履歴 3 件分を表示するマクロです: 
     17 
     18{{{ 
     19 [[RecentChanges(Trac,3)]] 
     20}}} 
     21 
     22は、以下のように表示されます: 
     23 [[RecentChanges(Trac,3)]] 
     24 
     25== マクロ一覧 == #AvailableMacros 
     26 
     27''Note: 以下に示すリストはマクロドキュメントを含むものだけです。 `-OO` による最適化や、 [wiki:TracModPython mod_python] での `PythonOptimize` オプションが設定されていると表示されません。'' 
     28 
     29[[MacroList]] 
     30 
     31== 世界のマクロを共有 == #Macrosfromaroundtheworld 
     32 
     33[http://trac-hacks.org/ Trac Hacks] というサイトは、コミュニティに寄稿されたマクロと [TracPlugins プラグイン] を収集し提供しています。新しいマクロを探している、共有したいマクロを作成した、などの場合は遠慮なく Trac Hacks のサイトを訪問してください。 
     34 
     35== カスタムマクロを開発する == #DevelopingCustomMacros 
     36マクロは、 Trac 自身と同じように [http://www.python.org/ Python] で書かれています。 
     37 
     38マクロの開発についての詳しい情報は [http://trac.edgewall.org/wiki/TracDev リソースの開発] を参照してください。 
     39 
     40 
     41== マクロの実装 == #Implementation 
     42 
     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/README README] を参照してください。 
     44 
     45=== 引数なしのマクロ === #Macrowithoutarguments 
     46Trac は マクロ名としてモジュール名を使用するので以下は `TimeStamp.py` という名前で保存しなければなりません。 
     47{{{ 
     48#!python 
     49from datetime import datetime 
     50# Note: since Trac 0.11, datetime objects are used internally 
     51 
     52from genshi.builder import tag 
     53 
     54from trac.util.datefmt import format_datetime, utc 
     55from trac.wiki.macros import WikiMacroBase 
     56 
     57class TimestampMacro(WikiMacroBase): 
     58    """Inserts the current time (in seconds) into the wiki page.""" 
     59 
     60    revision = "$Rev$" 
     61    url = "$URL$" 
     62 
     63    def expand_macro(self, formatter, name, args): 
     64        t = datetime.now(utc) 
     65        return tag.b(format_datetime(t, '%c')) 
     66}}} 
     67 
     68=== 引数付きのマクロ === #Macrowitharguments 
     69Trac は マクロ名としてモジュール名を使用するので以下は `HelloWorld.py` という名前で (plugins/ ディレクトリ内に ) 保存しなければなりません。 
     70{{{ 
     71#!python 
     72from trac.wiki.macros import WikiMacroBase 
     73 
     74class HelloWorldMacro(WikiMacroBase): 
     75    """Simple HelloWorld macro. 
     76 
     77    Note that the name of the class is meaningful: 
     78     - it must end with "Macro" 
     79     - what comes before "Macro" ends up being the macro name 
     80 
     81    The documentation of the class (i.e. what you're reading) 
     82    will become the documentation of the macro, as shown by 
     83    the !MacroList macro (usually used in the WikiMacros page). 
     84    """ 
     85 
     86    revision = "$Rev$" 
     87    url = "$URL$" 
     88 
     89    def expand_macro(self, formatter, name, args): 
     90        """Return some output that will be displayed in the Wiki content. 
     91 
     92        `name` is the actual name of the macro (no surprise, here it'll be 
     93        `'HelloWorld'`), 
     94        `args` is the text enclosed in parenthesis at the call of the macro. 
     95          Note that if there are ''no'' parenthesis (like in, e.g. 
     96          [[HelloWorld]]), then `args` is `None`. 
     97        """ 
     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. 
     102}}} 
     103 
     104{{{ 
     105#!div class=important 
     106訳注 '''重要''': 
     107 
     108Wiki マクロが引数を持つ場合、引数は必ずサニタイズしてください。 
     109expand_macro の戻り値は `<script>` タグやイベントハンドラなどもそのまま出力するので、入力をチェックせずに、そのまま戻り値に使用するのは極めて危険です。 
     110Genshi の tag オブジェクトにラップすれば、エスケープ機構が働きますので、通常はこれを使うとよいでしょう。 
     111`HelloWorld.py` は、以下の通り書き直すことができます: 
     112{{{ 
     113#!python 
     114from trac.wiki.macros import WikiMacroBase 
     115from genshi.builder import tag 
     116class HelloWorldMacro(WikiMacroBase): 
     117    def expand_macro(self, formatter, name, args): 
     118        return tag.div(u'Hello World, args = ', unicode(args)) 
     119}}} 
     120}}} 
     121 
     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 してください。 
     124 
     125マクロで HTML ではなく Wiki マークアップを使用したい場合、以下のように HTML に変換します: 
     126 
     127{{{ 
     128#!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()) 
     134}}}