0

Ok, I've looked all over, and I think I'm doing this right, but I'm not getting any results. Is there anyone out there who's written Trac macros that can guide me through the first steps? Here's what I've written:

from trac.wiki.macros import WikiMacroBase
from genshi.builder import tag

class MyMacro(WikiMacroBase):
    """Proof of concept"""

    revision = "$Rev$"
    url = "$URL$"

    def expand_macro(self, formatter, name, args):
        return tag.b("Hello world.")

I've saved it as a .py file and put it in my Trac project's /plugins directory. Do I need to restart apache? Am I correct in expecting [[MyMacro]] to output a Hello world. on the page?

end-user
  • 2,845
  • 6
  • 30
  • 56

1 Answers1

2

When creating macros using that format, Trac expects your class to be named "<name>Macro". For example, if you wanted a macro named JustASample, you would name the class JustASampleMacro. Since you named your class MyMacro, Trac thinks that you want your macro to be named My. Try using [[My]] on a wiki page and see if you get the output you're expecting.

After you copy the file into the plugins directory, you will indeed want to restart the web server. Before doing so, delete any .pyc files that were created for your plugin. Also, ensure that the file is readable by the account under which the web server runs.

bta
  • 43,959
  • 6
  • 69
  • 99
  • Oooh. That wasn't clear at all in anything I read. This really helps, thanks. – end-user Nov 06 '12 at 13:27
  • 1
    @end-user- I agree, Trac's documentation is indeed missing some key details like that. I mostly had to learn through trial and error and by examining the source for plugins from [trac-hacks](http://trac-hacks.org). I recommend finding some simple plugins from trac-hacks and using them as a reference. By taking several simple plugins and looking at the things they have in common, you can start to get an idea of what's necessary and how things work under the hood. – bta Nov 06 '12 at 16:10
  • 1
    Note that this clarification about class and macro names is not really a feature of Trac and its macro API - it is a feature of the `WikiMacroBase` abstract helper class. This is very much an optional class, and you can name your file, class (component) and macro anything you like by implementing the `trac.wiki.api.IWikiMacroProvider` interface directly instead of relying on an a helper with strict rules about naming. – osimons Jan 04 '13 at 19:06