XPressME Integration Kit

Trac

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

差分発生行の前後
Ignore:
Timestamp:
Nov 29, 2010, 3:25:29 PM (13 years 前)
Author:
trac (IP: 127.0.0.1)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WikiMacros

    v1 v2  
    1 = Trac マクロ = #TracMacros 
     1= Trac Macros = 
    22 
    33[[PageOutline]] 
    44 
    5 Trac マクロとは、 Python で書かれた 'カスタム関数' によって Trac の Wiki エンジンを拡張するプラグインです。 WikiFormatting エンジンが利用可能なあらゆるコンテキストにおいて、マクロを使用することによって、動的な HTML データが挿入されます。 
     5Trac 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. 
    66 
    7 もう 1 種類のマクロは WikiProcessors です。これは通常、 Wiki 以外のマークアップ形式と表示を取り扱うために使用し、多くは、 (ソースコードハイライトのような) より大きいブロックに使用します。 
     7Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting). 
    88 
    9 == マクロの利用 == #UsingMacros 
    10 マクロ呼び出しは、二つの ''角括弧 (square brackets) '' で括られた箇所です。 Python 関数のように、マクロは引数を取ることができ、括弧 (parenthesis) の中に、カンマで区切ったリストで表記します。 
     9== Using Macros == 
    1110 
    12 Trac マクロは、 TracPlugins としても作成することができます。 TracPlugins として Trac マクロを作成することで「直接 HTTP リクエストにアクセスする。」といった通常の Trac マクロでは実現できない機能を実装することができます。 
     11Macro calls are enclosed in two ''square brackets''. Like Python functions, macros can also have arguments, a comma separated list within parentheses. 
    1312 
    14 === 利用例 === #Example 
     13=== Getting Detailed Help === 
     14The list of available macros and the full help can be obtained using the !MacroList macro, as seen [#AvailableMacros below]. 
    1515 
    16 'Trac' で始まる Wiki ページの最近の変更履歴 3 件分を表示するマクロです: 
     16A brief list can be obtained via ![[MacroList(*)]] or ![[?]]. 
    1717 
    18 {{{ 
    19  [[RecentChanges(Trac,3)]] 
     18Detailed 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 
     24A 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; font-size: 80%" 
     51[[?]] 
    2052}}} 
    2153 
    22 は、以下のように表示されます: 
    23  [[RecentChanges(Trac,3)]] 
     54== Available Macros == 
    2455 
    25 == マクロ一覧 == #AvailableMacros 
    26  
    27 ''Note: 以下に示すリストはマクロドキュメントを含むものだけです。 `-OO` による最適化や、 [wiki:TracModPython mod_python] での `PythonOptimize` オプションが設定されていると表示されません。'' 
     56''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].'' 
    2857 
    2958[[MacroList]] 
    3059 
    31 == 世界のマクロを共有 == #Macrosfromaroundtheworld 
     60== Macros from around the world == 
    3261 
    33 [http://trac-hacks.org/ Trac Hacks] というサイトは、コミュニティに寄稿されたマクロと [TracPlugins プラグイン] を収集し提供しています。新しいマクロを探している、共有したいマクロを作成した、などの場合は遠慮なく Trac Hacks のサイトを訪問してください。 
     62The [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. 
    3463 
    35 == カスタムマクロを開発する == #DevelopingCustomMacros 
    36 マクロは、 Trac 自身と同じように [http://www.python.org/ Python] で書かれています。 
     64== Developing Custom Macros == 
     65Macros, like Trac itself, are written in the [http://python.org/ Python programming language] and are developed as part of TracPlugins. 
    3766 
    38 マクロの開発についての詳しい情報は [http://trac.edgewall.org/wiki/TracDev リソースの開発] を参照してください。 
     67For more information about developing macros, see the [trac:TracDev development resources] on the main project site. 
    3968 
    4069 
    41 == マクロの実装 == #Implementation 
     70Here are 2 simple examples showing how to create a Macro with Trac 0.11.  
    4271 
    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] を参照してください。 
     72Also, 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. 
    4473 
    45 === 引数なしのマクロ === #Macrowithoutarguments 
    46 Trac は マクロ名としてモジュール名を使用するので以下は `TimeStamp.py` という名前で保存しなければなりません。 
     74=== Macro without arguments === 
     75To test the following code, you should saved it in a `timestamp_sample.py` file located in the TracEnvironment's `plugins/` directory. 
    4776{{{ 
    4877#!python 
     
    5584from trac.wiki.macros import WikiMacroBase 
    5685 
    57 class TimestampMacro(WikiMacroBase): 
     86class TimeStampMacro(WikiMacroBase): 
    5887    """Inserts the current time (in seconds) into the wiki page.""" 
    5988 
     
    6190    url = "$URL$" 
    6291 
    63     def expand_macro(self, formatter, name, args): 
     92    def expand_macro(self, formatter, name, text): 
    6493        t = datetime.now(utc) 
    6594        return tag.b(format_datetime(t, '%c')) 
    6695}}} 
    6796 
    68 === 引数付きのマクロ === #Macrowitharguments 
    69 Trac は マクロ名としてモジュール名を使用するので以下は `HelloWorld.py` という名前で (plugins/ ディレクトリ内に ) 保存しなければなりません。 
     97=== Macro with arguments === 
     98To test the following code, you should saved it in a `helloworld_sample.py` file located in the TracEnvironment's `plugins/` directory. 
    7099{{{ 
    71100#!python 
     101from genshi.core import Markup 
     102 
    72103from trac.wiki.macros import WikiMacroBase 
    73104 
     
    87118    url = "$URL$" 
    88119 
    89     def expand_macro(self, formatter, name, args): 
     120    def expand_macro(self, formatter, name, text, args): 
    90121        """Return some output that will be displayed in the Wiki content. 
    91122 
    92123        `name` is the actual name of the macro (no surprise, here it'll be 
    93124        `'HelloWorld'`), 
    94         `args` is the text enclosed in parenthesis at the call of the macro. 
     125        `text` is the text enclosed in parenthesis at the call of the macro. 
    95126          Note that if there are ''no'' parenthesis (like in, e.g. 
    96           [[HelloWorld]]), then `args` is `None`. 
     127          [[HelloWorld]]), then `text` is `None`. 
     128        `args` are the arguments passed when HelloWorld is called using a 
     129        `#!HelloWorld` code block. 
    97130        """ 
    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. 
     131        return 'Hello World, text = %s, args = %s' % \ 
     132            (Markup.escape(text), Markup.escape(repr(args))) 
     133 
    102134}}} 
    103135 
     136Note 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''). 
     137 
     138For example, when writing: 
    104139{{{ 
    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 }}} 
     140{{{#!HelloWorld style="polite" 
     141<Hello World!> 
    120142}}} 
    121143 
    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 してください。 
     144{{{#!HelloWorld 
     145<Hello World!> 
     146}}} 
    124147 
    125 マクロで HTML ではなく Wiki マークアップを使用したい場合、以下のように HTML に変換します: 
     148[[HelloWorld(<Hello World!>)]] 
     149}}} 
     150One should get: 
     151{{{ 
     152Hello World, text = <Hello World!> , args = {'style': u'polite'} 
     153Hello World, text = <Hello World!> , args = {} 
     154Hello World, text = <Hello World!> , args = None 
     155}}} 
     156 
     157Note 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`).   
     158 
     159You can also recursively use a wiki Formatter (`from trac.wiki import Formatter`) to process the `text` as wiki markup, for example by doing: 
    126160 
    127161{{{ 
    128162#!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()) 
     163    text = "whatever wiki markup you want, even containing other macros" 
     164    # Convert Wiki markup to HTML, new style 
     165    out = StringIO() 
     166    Formatter(self.env, formatter.context).format(text, out) 
     167    return Markup(out.getvalue()) 
    134168}}}