Changes between Version 5 and Version 6 of WikiMacros
- Timestamp:
- Jan 28, 2010, 4:25:37 AM (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
WikiMacros
v5 v6 1 = Wiki マクロ = 2 Wiki マクロとは、 Python で書かれた 'カスタム関数' によって Trac の Wiki エンジンを拡張するプラグインです。 WikiFormatting エンジンが利用可能なあらゆるコンテキストにおいて、マクロを使用することによって、動的な HTML データが挿入されます。 1 = Trac Macros = 3 2 4 もう 1 種類のマクロは WikiProcessors です。これは通常、Wiki以外のマークアップ形式と表示を取り扱うために使用し、多くは、 (ソースコードハイライトのような) より大きいブロックに使用します。 3 [[PageOutline]] 5 4 6 == マクロの利用 == 7 マクロ呼び出しは、二つの ''角括弧 (square brackets) '' で括られた箇所です。 Python 関数のように、マクロは引数を取ることができ、括弧 (parenthesis) の中に、カンマで区切ったリストで表記します。 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. 8 6 9 === 利用例 === 7 Another kind of macros are WikiProcessors. They typically deal with alternate markup formats and representation of larger blocks of information (like source code highlighting). 8 9 == Using Macros == 10 Macro calls are enclosed in two ''square brackets''. Like Python functions, macros can also have arguments, a comma separated list within parentheses. 11 12 Trac macros can also be written as TracPlugins. This gives them some capabilities that macros do not have, such as being able to directly access the HTTP request. 13 14 === Example === 15 16 A list of 3 most recently changed wiki pages starting with 'Trac': 10 17 11 18 {{{ 12 [[ Timestamp]]19 [[RecentChanges(Trac,3)]] 13 20 }}} 14 は、以下のように表示されます:15 [[Timestamp]]16 21 17 {{{ 18 [[HelloWorld(Testing)]] 19 }}} 20 は、以下のように表示されます: 21 [[HelloWorld(Testing)]] 22 Display: 23 [[RecentChanges(Trac,3)]] 22 24 23 == マクロ一覧==25 == Available Macros == 24 26 25 ''Note : 以下に示すリストはマクロドキュメントを含むものだけです。 `-OO` による最適化や、 [wiki:TracModPython mod_python] での `PythonOptimize` オプションが設定されていると表示されません。''27 ''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].'' 26 28 27 29 [[MacroList]] … … 31 33 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. 32 34 33 == カスタムマクロを開発する==34 マクロは、 Trac 自身と同じように [http://www.python.org/ Python programming language] で書かれています。とてもシンプルなモジュールで、たった一つの `execute()` 関数だけを持ちます。マクロの識別はファイル名で行います。 Trac は、呼び出されたマクロが返却したデータをマクロが呼び出された Wiki ページの HTML に挿入して表示を行います。 35 == Developing Custom Macros == 36 Macros, like Trac itself, are written in the [http://python.org/ Python programming language]. 35 37 36 最も簡単なマクロの例です: 38 For more information about developing macros, see the [wiki:TracDev development resources] on the main project site. 39 40 41 == Implementation == 42 43 Here are 2 simple examples on how to create a Macro with [wiki:0.11 Trac 0.11] have a look at source:trunk/sample-plugins/Timestamp.py for an example that shows the difference between old style and new style macros and also source:trunk/wiki-macros/README which provides a little more insight about the transition. 44 45 === Macro without arguments === 46 It should be saved as `TimeStamp.py` as Trac will use the module name as the Macro name 37 47 {{{ 38 48 #!python 39 # MyMacro.py -- The world's simplest macro 49 from datetime import datetime 50 # Note: since Trac 0.11, datetime objects are used internally 40 51 41 def execute(hdf, args, env): 42 return "Hello World called with args: %s" % args 52 from genshi.builder import tag 53 54 from trac.util.datefmt import format_datetime, utc 55 from trac.wiki.macros import WikiMacroBase 56 57 class 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')) 43 66 }}} 44 67 45 Environment (`env`) オブジェクトを使用することも出来ます。この例では、コンフィグレーションとデータベースにアクセスしています: 68 === Macro with arguments === 69 It should be saved as `HelloWorld.py` (in the plugins/ directory) as Trac will use the module name as the Macro name 46 70 {{{ 47 71 #!python 48 def execute(hdf, txt, env): 49 return env.config.get('trac', 'repository_dir') 72 from trac.wiki.macros import WikiMacroBase 73 74 class 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. 50 102 }}} 51 103 52 Note: バージョン 0.9 以降、 Wiki マクロは TracPlugins でも書くことが出来るようになりました。これによって、 HTTP request へのダイレクトアクセスなど、 "古い" マクロでは実現できなかったことが出来るようになりました。53 104 54 マクロ開発についての詳しい情報は、プロジェクトメインサイトの [http://trac.edgewall.org/wiki/TracDev 開発リソース] を参照してください。 105 === {{{expand_macro}}} details === 106 {{{expand_macro}}} should return either a simple Python string which will be interpreted as HTML, or preferably a Markup object (use {{{from trac.util.html import Markup}}}). {{{Markup(string)}}} just annotates the string so the renderer will render the HTML string as-is with no escaping. You will also need to import Formatter using {{{from trac.wiki import Formatter}}}. 55 107 56 ---- 57 See also: WikiProcessors, WikiFormatting, TracGuide 108 If your macro creates wiki markup instead of HTML, you can convert it to HTML like this: 109 110 {{{ 111 #!python 112 text = "whatever wiki markup you want, even containing other macros" 113 # Convert Wiki markup to HTML, new style 114 out = StringIO() 115 Formatter(self.env, formatter.context).format(text, out) 116 return Markup(out.getvalue()) 117 }}}