Changes between Version 3 and Version 4 of WikiMacros


Ignore:
Timestamp:
2017/01/19 10:24:30 (7 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • WikiMacros

    v3 v4  
    88 
    99== マクロの利用 == #UsingMacros 
     10 
    1011マクロ呼び出しは、二つの ''角括弧 (square brackets) '' で括られた箇所です。 Python 関数のように、マクロは引数を取ることができ、括弧 (parenthesis) の中に、カンマで区切ったリストで表記します。 
    1112 
    12 Trac マクロは、 TracPlugins としても作成することができます。 TracPlugins として Trac マクロを作成することで「直接 HTTP リクエストにアクセスする。」といった通常の Trac マクロでは実現できない機能を実装することができます。 
     13=== 詳細なヘルプを見るには ===#GettingDetailedHelp  
     14マクロの一覧と完全なヘルプは、 下記の[#AvailableMacros マクロ一覧] にある !MacroList マクロを使用してみることができます。 
     15 
     16簡単なマクロ一覧は ![[MacroList(*)]] や ![[?]] で見ることができます。 
     17 
     18特定のマクロの詳細なヘルプを参照したい場合は、 !MacroList マクロに引数渡すことによって参照することができます。例) ![[MacroList(MacroList)]] 。もしくは、便宜上、 ![[MacroList?]] のようにマクロ名にクエスチョンマーク (?) をつけることでヘルプをみることができます。 
     19 
     20 
    1321 
    1422=== 利用例 === #Example 
     
    1624'Trac' で始まる Wiki ページの最近の変更履歴 3 件分を表示するマクロです: 
    1725 
    18 {{{ 
    19  [[RecentChanges(Trac,3)]] 
     26||= Wiki マークアップ =||= 表示 =|| 
     27{{{#!td 
     28  {{{ 
     29  [[RecentChanges(Trac,3)]] 
     30  }}} 
    2031}}} 
    21  
    22 は、以下のように表示されます: 
    23  [[RecentChanges(Trac,3)]] 
     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[[?]] 
     52}}} 
    2453 
    2554== マクロ一覧 == #AvailableMacros 
     
    3463 
    3564== カスタムマクロを開発する == #DevelopingCustomMacros 
    36 マクロは、 Trac 自身と同じように [http://www.python.org/ Python] で書かれています。 
     65マクロは、 Trac 本体と同様 [http://python.org/ Python] で書かれています。そして TracPlugins の一種として開発します 
    3766 
    3867マクロの開発についての詳しい情報は [http://trac.edgewall.org/wiki/TracDev リソースの開発] を参照してください。 
    3968 
    40  
    41 == マクロの実装 == #Implementation 
    4269 
    4370Trac 0.11 でマクロを作成する簡単な例を 2 つ紹介します。 
     
    4673 
    4774=== 引数なしのマクロ === #Macrowithoutarguments 
    48 Trac は マクロ名としてモジュール名を使用するので以下は `TimeStamp.py` という名前で保存しなければなりません。 
     75下記のソースコードをテストするためには、このソースコードを `timestamp_sample.py` として保存し、 TracEnvironment の `plugins/` に配置しなければなりません。 
    4976{{{ 
    5077#!python 
     
    6390    url = "$URL$" 
    6491 
    65     def expand_macro(self, formatter, name, args): 
     92    def expand_macro(self, formatter, name, text): 
    6693        t = datetime.now(utc) 
    6794        return tag.b(format_datetime(t, '%c')) 
     
    6996 
    7097=== 引数付きのマクロ === #Macrowitharguments 
    71 Trac は マクロ名としてモジュール名を使用するので以下は `HelloWorld.py` という名前で (plugins/ ディレクトリ内に ) 保存しなければなりません。 
     98下記のソースコードをテストするためには、このソースコードを `helloworld_sample.py` として保存し、 TracEnvironment の `plugins/` に配置しなければなりません。 
    7299{{{ 
    73100#!python 
     101from genshi.core import Markup 
     102 
    74103from trac.wiki.macros import WikiMacroBase 
    75104 
     
    89118    url = "$URL$" 
    90119 
    91     def expand_macro(self, formatter, name, args): 
     120    def expand_macro(self, formatter, name, text, args): 
    92121        """Return some output that will be displayed in the Wiki content. 
    93122 
    94123        `name` is the actual name of the macro (no surprise, here it'll be 
    95124        `'HelloWorld'`), 
    96         `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. 
    97126          Note that if there are ''no'' parenthesis (like in, e.g. 
    98           [[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. 
    99130        """ 
    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. 
     131        return 'Hello World, text = %s, args = %s' % \ 
     132            (Markup.escape(text), Markup.escape(repr(args))) 
     133 
    104134}}} 
    105135 
     136Note: `expand_macro` は 第4パラメータに、 ''`args`'' を任意に取ることもできます。 このマクロが [WikiProcessors WikiProcessor] として呼ばれたとき、 `key=value` 形式の [WikiProcessors#UsingProcessors プロセッサパラメータ] を渡すことも可能です。もし、このパラメータを指定したとき、これらの値は、ディクショナリの中に保存され、 追加の `args` パラメータによって渡されます。一方で、マクロとして呼び出されたときは、 `args` パラメータは、 `None` として扱われます (''0.12 以降'') 。 
     137 
     138例として、このように記述した場合: 
    106139{{{ 
    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 }}} 
     140{{{#!HelloWorld style="polite" 
     141<Hello World!> 
    122142}}} 
    123143 
    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 してください。 
     144{{{#!HelloWorld 
     145<Hello World!> 
     146}}} 
    126147 
    127 マクロで HTML ではなく Wiki マークアップを使用したい場合、以下のように HTML に変換します: 
     148[[HelloWorld(<Hello World!>)]] 
     149}}} 
     150結果はこのようになります: 
     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: `expand_macro` が返す値は、 HTML がエスケープされて '''いない''' ことに注意して下さい。期待する戻り値によっては、あなた自身でエスケープする必要があります (`return Markup.escape(result)` を使用できます)。また、戻り値として HTML が返ってくると分かっているならば、結果を (`return Markup(result)`) という風に Genshi が提供している Markup (`from genshi.core import Markup`) オブジェクトでラップすることもできます。 
     158 
     159また、`text` を Wiki としてマークアップする場合、 Wiki Formatter (`from trac.wiki import Formatter`) オブジェクトも再帰的に使用することができます。以下がサンプルです: 
    128160 
    129161{{{ 
    130162#!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()) 
     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()) 
    136168}}}