Macros in Firedrop2Using and Creating MacrosIntroductionThis page explains the macro system used in Firedrop2. It assumes you are using the realmacros.py macro set [1]. In order to use them you will have to put realmacros.py somewhere on your Python search path [2]. This can either be in your firedrop2 directory, or in your site-packages folder. You should put the modules directory in the same place (or elsewhere in the Python search path). If you want to use the smiley macro, you'll need to put the path from realmacros.py to the smilies directory. The smilies page has more details. About MacrosMacros allow you to use a shorthand for often-used text or HTML. As a simple example, Let's say that I use a certain link a lot. I don’t want to type it over and over again. I can define a macro foo to include this link every time I type {foo} : foo = '<a href="somepage.html>My link</a>' In this case, the “macro definition” is simply a string that contains the HTML we want to substitute. We can use more sophisticated macros, though. Let's assume that I use acronyms a lot, and I’m getting tired of having to type that tag again and again. It would be nice if I could type something like {acronym;IMO;In My Opinion} This becomes IMO. (Move your mouse over the “IMO” to see the effect. Not all browsers may support this.) To achieve this, we define a simple Python function: def acronym(acronym, meaning): return '<acronym title="%s">%s</acronym>' % ( meaning, acronym). {acronym;IMO;In My Opinion} is equivalent to the function call acronym(“IMO”, “In My Opinion”). These macros can also be used to do more complex things, like insert pictures, include whole files, and so on. Where Are Macros Defined?In the options you can specify a path to a macros module. This can have any name - but it should be a module that Python can import. Basically that means the filename has to end in '.py' [3]. In this case, you should put in the path to realmacros.py. If you wanted to start from scratch, here's what you need to do :
Things You Should Know
The OrderMacros are resolved after the pages has been processed by docutils [4]. This means your macros should return html for direct inclusion in the final page. They are resolved before the page is passed to the template engine. This means that dynamic values like <% path_to_root %> output by macros will be resolved. FilepathsBefore the macros are run, the current directory is changed to be the same as the macros file. This means all file paths supplied to macros should be relative to this file. ModulesThe modules directory inculded in some distributions includes various Python modules used by realmacros.py. If you aren't using realmacros.py then you don't need this directory. Credits for all the modules here are in the Credits section below. This directory is put in the Python search path. If you move them elsewhere you
should make sure they are on the sys.path. If you don't know what I'm on
about then leave them where they are Advanced MacrosAs well as the normal macros discussed above, there is a more advanced system of macros as well. This allows you to apply a macro to a whole chunk of text. Unlike the simple macros, these macros can be nested to apply several effects to passages. The advanced macros work by enclosing a passage of text between a {+macro} and a {-macro}. The macro is applied to all the text between the + and the -. In this case it would be applied to and a. You can also nest them So you can do things like :
{+second}{+first} The text {-second}{-first}
{+whole} Some text {+part} a bit in the middle {-part}
more text{-whole}
These macros aren't the same as normal macros though. Here's the idea. While a regular macro {f;x;y} translates to a function call f("x", "y"), the + and - versions use a class definition. A simple example : class rot13(textmacros.BaseMacro): def open(self, text): return text.encode("rot13") Once this is defined, in your macros file (or just imported into it), you can use it as follows :
The solution is {+rot13}not for your eyes{-rot13}.
Upon execution, this will convert the text between the rot13 tags. This becomes : The solution is abg sbe lbhe rlrf. In your class the +tag corresponds to the class's open(text) method, the -tag to the close() method. Note that if you are using docutils to process your text then it will be processed by docutils before it is processed by the macro. If you want to bypass this, and pass the text to your macro only, then you need to use the .. raw:: html directive. e.g. :
.. raw:: html
{+somemacro}
this text will not be processed as reST
{-somemacro}
The Example MacrosThe realmacros.py module has several useful macros. Feel free to experiment with this file. You can modify the macros in it, or use them as examples to create your own. curlyl and curlyrThese two macros perform simple substitution. They are a way of including curly left hand brackets and curly right hand brackets in your pages. They are also have the shorter forms cl and cr. For example, to include {example} in your page - without it being interpreted as a macro - you can write {curlyl}example{curlyr} or {cl}example{cr}. This came in very handy when creating this page smileyThis is one of the nicest macros. It uses a modified version of smiley.py by Mark Andrews to put smilies onto your site. You will need to modify macros.py with the right path to the smilies directory (on disk) and the right path to the smiley images (on your site). You can use sm as a shorter alias for smiley. Examples :
It will read standard smiley packages like the ones used by phpbb. Download more from the stylesdb site. You can see a full list of all of the smilies from the example set in the Smilies Page. acronymWe`ve seen this in the first example. {acronym;acronym;meaning} produces acronym. You can also use {acro;acronym;meaning}. As an added bonus there are a few standard acronyms that can be called without the acronym definition. These are (currently) : # a dictionary of standard acronyms # keys should be lowercase acronyms = { 'wysiwyg': 'What You See Is What You Get', 'html': 'HyperText Markup Language', 'xml': 'eXtensible Markup Language', 'xhtml': 'eXtensible HyperText Markup Language', 'rest': 'ReStructuredText', 'css': 'Cascading Style Sheets', 'ie': 'Internet Exploder', 'ide': 'Integrated Development Environment', 'afaik': 'As Far as I Know', 'ianal': 'I am not a Lawyer', 'ssi': 'Server Side Includes', 'cgi': 'Common Gateway Interface', 'lol': 'Laughing Out Loud', 'rotfl': 'Roll On the Floor Laughing', 'http': 'HyperText Transfer Protocol', 'ascii': 'American Standard Code for Information Interchange', 'gui': 'Graphical User Interface', 'cli': 'Command Line Interface', 'pda': 'Personal Digital Assistant', 'rtfm': 'Read the Manual', 'ftp': 'File Transfer Protocol', 'nntp': 'Network News Transfer Protocol', 'uk': 'United Kingdom', 'pc': 'Personal Computer', 'url': 'Uniform Resource Locator', 'uri': 'Uniform Resource Identifier', 'tcp/ip': 'Transport Control Protocol/Internet Protocol', 'udp': 'User Data Paragram', } So you can do {acro;WYSIWYG}, which becomes WYSIWYG
emoticonThis is another shorthand way of including images in your pages. It's useful for putting emoticons inline with text - hence the name. Unlike the smiley macro it doesn't need to read anything of disk. It assumes the files you are including are all 'gif's and located in the '<% path_to_root %>images/' directory of your website. These are hardwired into the emoticon function - so you can edit them there if you want to. Warning Including images without specifying a size may slow down the browser rendering of your pages. You could make all your images the same size and hardwire the sizes into the 'img' tag that this macro creates. Alternatively you could do something clever with the PIL by Fredrik Lundh - and have it work out the sizes and insert them for you. Examples :
You can use emo as a shorter alias for emoticon. includeThis macro is very simple. Give it a filepath (relative to the macro module)
and it will include the contents of that file into this page. The optional
'escape' parameter allows you to escape whitespace. This will insert files as
they are - without having to use the '<pre>' tag, which breaks my layout - have
I mentioned that before ? For example <tt>{include;somefile.txt;True}</tt> escapes the somefile.txt file and inserts it into the page : some text here the contents of somefile.txt You can use inc as a shorter alias for include. To include HTML files (without escaping), use {include;some_file.html}. colorizeThis macro takes a file and applies Python syntax highlighting to it. You need the right rules in your CSS file for the coloring to be visible. See the rules that start py in test.css. {colorize;example_function.txt} becomes : def colorize(filename): """ format a python script as html Using the appropriate css it will be nicely colored. Needs the colorize.py module. """ fullname = os.path.join(filename) f = open(fullname, 'r') data = f.read() f.close() p = color.Parser(data) p.format(None, None) src = p.getvalue() return src.replace('\n', '<br />\n').replace(' ', ' ') # to avoid having to use <pre>.. You can use col as a shorter alias for colorize. To use the colorize macro, you need the right definitions in your CSS. Something like :
.pysrc {
border: #c0c0ff 2px dotted; padding:10px;
font-weight: normal; background: #e0e0ff; margin: 20px;
padding:10px;
}
.pykeyword {
font-weight: bold;
color: orange;
}
.pystring {
color: green
}
.pycomment {
color: red
}
.pynumber {
color:purple;
}
.pyoperator {
color:purple;
}
.pytext {
color:black;
}
.pyerror {
font-wieght: bold;
color: red;
}
Change the color definitions to alter the appearance. +/- coloringThis is the only example of an advaned macro included. It does the same job as the colorize macro, but instead of passing it a filename - it works on the text it encloses. This :
.. raw:: html
{+coloring}
class coloring:
"""A Macro for coloring chunks of text."""
def open(self, data):
p = color.Parser(data)
p.format(None, None)
src = p.getvalue()
src = src.replace('\n', '<br />\n')
return src.replace(' ', ' ')
def close(self, *args):
pass
{-coloring}
Becomes : class coloring: """A Macro for coloring chunks of text.""" def open(self, data): p = color.Parser(data) p.format(None, None) src = p.getvalue() return src.replace('\n', '<br />\n').replace(' ', ' ') def close(self, *args): pass CreditsThe realmacros.py file uses various Python modules. These are included in the modules directory that comes with the distribution.They come from the following people and places :
Footnotes
Return to Top |
|||||||||||