3
# Copyright (c) 2007 Stephen Day
5
# This module is part of Creoleparser and is released under
6
# the MIT License: http://www.opensource.org/licenses/mit-license.php
11
class Creole10(object):
13
"""This class contains most of the logic and specification of the markup."""
15
def __init__(self,wiki_links_base_url='',wiki_links_space_char='_',
16
interwiki_links_base_urls={},
17
no_wiki_monospace=True, use_additions=False,
18
wiki_links_class_func=None, macro_func=None,
19
wiki_links_path_func=None, interwiki_links_funcs={},
20
interwiki_links_space_chars={},):
21
"""Constructor for Creole10 oblects.
23
Most attributes of new Creole objects are derived from the WikiElement
24
class. Please see the constructor of that class and other specific element
31
When wiki_links have spaces, this character replaces those spaces in
33
interwiki_links_base_urls
34
Dictionary of urls for interwiki links.
35
interwiki_links_space_chars
36
Dictionary of characters that that will be used to replace spaces
37
that occur in interwiki_links. If no key is present for an interwiki
38
name, the wiki_links_space_char will be used.
40
Dictionary of functions that will be called for interwiki link
41
names. Works like wiki_links_path_func
43
If ``True``, inline no_wiki will be rendered as <tt> not <span>
45
If ``True``, markup beyond the Creole 1.0 spec will be allowed.
46
Including monospace (##).
48
If supplied, this fuction will be called when a wiki link is found and
49
the return value (should be a string) will be added as a class attribute
50
of the cooresponding link. The function must accept the page name (any
51
spaces will have been replaced THIS IS NEW IN 0.3.3) as it's only argument.
52
If no class attribute is to be added, return no value (or None).
54
If supplied, this fuction will be called when a wiki link is found and
55
the return value (should be a string) will be joined to the base_url
56
to form the url for href. The function must accept the page name (any
57
spaces will have been replaced) as it's only argument. Returning the
58
unaltered page name is equivalent to not supplying this function at all.
60
If supplied, this fuction will be called when macro markup is found. The
61
function must accept the macro name as its first argument, the argument
62
string (including any delimter) as the second, the macro body as its
63
third (will be None for a macro without a body), and a Boolean as the
64
fourth (True for Block type macros, False for normal macros).
65
The function may return a string (which will be subject to further wiki
66
processing) or a Genshi Stream object. If None is returned, the markup will
67
be rendered unchanged.
68
The macro name must start with a letter and can include letters, numbers,
69
and non-repeating periods and hyphens.
72
These are regular macros::
74
<<macro-name arg_string>>the body<</macro-name>>
75
<<macro-name2 I have no body, just this argument string>>
77
These are "block" macros. The won't be enclosed automatically in
78
paragraphs like those above::
80
<<note-blank-lines-before-and-after>>
88
self.macro = Macro('',('<<','>>'),[],func=macro_func)
89
self.bodiedmacro = BodiedMacro('',('<<','>>'),[],func=macro_func)
90
self.block_macro = BlockMacro('',('<<','>>'),[],func=macro_func)
91
self.bodied_block_macro = BodiedBlockMacro('',('<<','>>'),[],func=macro_func)
92
self.br = LineBreak('br', r'\\')
93
self.raw_link = RawLink('a')
94
self.url_link = URLLink('a','',[],delimiter = '|')
95
self.interwiki_link = InterWikiLink('a','',[],delimiter1=':',delimiter2='|',
96
base_urls=interwiki_links_base_urls,
97
links_funcs=interwiki_links_funcs,
98
default_space_char=wiki_links_space_char,
99
space_chars=interwiki_links_space_chars)
100
self.wiki_link = WikiLink('a','',[],delimiter = '|', base_url=wiki_links_base_url,
101
space_char=wiki_links_space_char,class_func=wiki_links_class_func,
102
path_func=wiki_links_path_func)
103
self.img = Image('img',('{{','}}'),[],delimiter='|')
104
self.link = Link('',('[[',']]'),[self.url_link,self.interwiki_link,self.wiki_link])
105
self.strong = InlineElement('strong', '**',[])
106
self.em = InlineElement('em', '//',[])
107
if no_wiki_monospace:
111
self.no_wiki = NoWikiElement(no_wiki_tag,['{{{','}}}'],[])
113
self.em.child_tags = []
114
self.strong.child_tags = [self.em]
115
link_child_tags = [self.strong, self.em]
116
inline_elements = [self.no_wiki, self.img, self.link, self.br, self.raw_link, self.strong, self.em]
117
table_cell_children = [self.br, self.raw_link, self.strong, self.em]
120
self.sub = InlineElement('sub', ',,',[])
121
self.sup = InlineElement('sup', '^^',[self.sub])
122
self.u = InlineElement('u', '__',[self.sup, self.sub])
123
self.tt = InlineElement('tt', '##',[self.u, self.sup, self.sub])
124
self.em.child_tags.extend([self.tt, self.u, self.sup, self.sub])
125
self.strong.child_tags.extend([self.tt, self.u, self.sup, self.sub])
126
link_child_tags.extend([self.tt, self.u, self.sup, self.sub])
127
inline_elements[0] = (self.no_wiki,self.bodiedmacro,self.macro)
128
#inline_elements.insert(1,self.macro)
129
inline_elements.extend([self.tt, self.u, self.sup, self.sub])
130
table_cell_children.extend([self.tt, self.u, self.sup, self.sub])
132
self.wiki_link.child_tags = link_child_tags
133
self.url_link.child_tags = link_child_tags
134
self.interwiki_link.child_tags = link_child_tags
137
self.hr = LoneElement('hr','----',[])
138
#self.lone_br = LoneElement('br',r'\\',[])
139
self.blank_line = BlankLine()
140
self.lone_place_holder = LonePlaceHolder('',['<<<','>>>'],[])
142
self.h1 = Heading('h1','=',inline_elements)
143
self.h2 = Heading('h2','==',inline_elements)
144
self.h3 = Heading('h3','===',inline_elements)
145
self.h4 = Heading('h4','====',inline_elements)
146
self.h5 = Heading('h5','=====',inline_elements)
147
self.h6 = Heading('h6','======',inline_elements)
149
headings = [self.h1,self.h2,self.h3,self.h4,self.h5,self.h6]
151
self.td = TableCell('td','|',table_cell_children)
152
self.th = TableCell('th','|=',table_cell_children)
154
self.tr = TableRow('tr','|',[(self.no_wiki,self.bodiedmacro,self.macro),self.img,self.link,self.th,self.td])
156
self.tr = TableRow('tr','|',[self.no_wiki,self.img,self.link,self.th,self.td])
157
self.table = Table('table','|',[self.tr])
159
self.p = Paragraph('p',inline_elements)
162
self.dd = DefinitionDef('dd',':',[table_cell_children])
163
self.dt = DefinitionTerm('dt',';',[table_cell_children],stop_token=':')
164
self.dl = List('dl',';',[(self.no_wiki,self.bodiedmacro,self.macro),self.img,self.link,self.dt,self.dd],stop_tokens='*#')
166
self.li = ListItem('li',child_tags=[],list_tokens='*#')
167
self.ol = List('ol','#',[self.li],stop_tokens='*')
168
self.ul = List('ul','*',[self.li],stop_tokens='#')
169
self.nested_ol = NestedList('ol','#',[self.li])
170
self.nested_ul = NestedList('ul','*',[self.li])
171
self.li.child_tags = [(self.nested_ol,self.nested_ul)] + inline_elements
172
self.pre = PreBlock('pre',['{{{','}}}'])
173
self.inline_elements = inline_elements
175
self.block_elements = [(self.bodied_block_macro,self.pre,self.block_macro),self.blank_line,self.table]+ headings\
176
+ [self.hr,self.dl,self.ul,self.ol,self.lone_place_holder,self.p]
179
self.block_elements = [self.pre,self.blank_line,self.table]+ headings\
180
+ [self.hr,self.ul,self.ol,self.lone_place_holder,self.p]
181
"""These are the wiki elements that are searched at the top level of text to be
182
processed. The order matters because elements later in the list need not have any
183
knowledge of those before (as those were parsed out already). This makes the
184
regular expression patterns for later elements very simple.