#!/usr/bin/env python # -*- coding:utf-8 -*- # author: lintao import pprint from trac.wiki.macros import WikiMacroBase from tracusermanager.profile.api import UserProfileManager from genshi.core import Markup from trac.wiki import Formatter import StringIO class JMNEInstitution(WikiMacroBase): # This is no args MACRO def expand_macro(self, formatter, name, text): env = self.env upm = UserProfileManager(env) profile_fields = upm.get_user_profile_fields() pprint.pprint(profile_fields) result = "" if "institution" in profile_fields: for perinst in profile_fields["institution"]["options"]: result += "* %s\n"%perinst out = StringIO.StringIO() Formatter(self.env, formatter.context).format(result, out) return Markup(out.getvalue())
先贴一段代码。
扩展Trac的Macro不是很复杂,首先,需要把这些脚本放到trac项目的plugins中。Trac会加载这里的脚本。
....................................................................
我们只要实现接口expand_macro即可。另外,我们可以在里面使用wiki。