Changes between Version 2 and Version 3 of WikiMacros


Ignore:
Timestamp:
2009/12/25 16:22:02 (14 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WikiMacros

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