Applying Formatters

MoinMoin developers recommend that you use a Formatter to format output from your Macro, Processor, or Parser - see MoinDev/PluginConcept.

They made Formatters so that you only have to write output code once. That is, you don't have to write output code for HTML, and then XML, and then plain text.

Macro Example

Formatters are very simple to use:

   1 def execute(macro, args):
   2     result = []
   3     result.append(macro.formatter.paragraph(1))
   4     result.append(macro.formatter.text("This text appears inside of a macro."))
   5     result.append(macro.formatter.text("No to escape text like </html> and <p>."))
   6     result.append(macro.formatter.paragraph(0))
   7     return "".join(result)

If you make many macro.formatter calls, you can shorten it:

   1 def execute(macro, args):
   2     result = []
   3     fmtr = macro.formatter # saves a bit of time if used often
   4     for i in range(10000):
   5         result.append(fmtr.paragraph(1))
   6         result.append(fmtr.text("Paragraph %d" % i))
   7         result.append(fmtr.paragraph(0))
   8     return "".join(result)

Keys to Using Formatters

Know where to find your formatter, and know what it can do for you.

output from:

formatter at:

Macro

macro.formatter parameter

Processor

formatter parameter

Parser

self.formatter

To find everything a formatter can do, read the base formatter class:

MoinMoin: ApplyingFormatters (last edited 2008-10-24 16:20:04 by user27-007)