~j5-dev/+junk/Creoleparser-0.6.1

« back to all changes in this revision

Viewing changes to creoleparser/dialects.py

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2008-06-18 23:28:55 UTC
  • Revision ID: james.westby@ubuntu.com-20080618232855-npggebivmf0bu9w3
Tags: upstream-0.5.0
ImportĀ upstreamĀ versionĀ 0.5.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# dialects.py
 
2
#
 
3
# Copyright (c) 2007 Stephen Day
 
4
#
 
5
# This module is part of Creoleparser and is released under
 
6
# the MIT License: http://www.opensource.org/licenses/mit-license.php
 
7
#
 
8
 
 
9
from elements import *
 
10
 
 
11
class Creole10(object):
 
12
 
 
13
    """This class contains most of the logic and specification of the markup."""
 
14
 
 
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.
 
22
 
 
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
 
25
        classes for details.
 
26
 
 
27
        :parameters:
 
28
          wiki_links_base_url
 
29
            self explanitory
 
30
          wiki_links_space_char
 
31
            When wiki_links have spaces, this character replaces those spaces in
 
32
            the url. 
 
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.
 
39
          interwiki_links_funcs
 
40
            Dictionary of functions that will be called for interwiki link
 
41
            names. Works like wiki_links_path_func
 
42
          no_wiki_monospace
 
43
            If ``True``, inline no_wiki will be rendered as <tt> not <span>
 
44
          use_additions
 
45
            If ``True``, markup beyond the Creole 1.0 spec will be allowed.
 
46
            Including monospace (##).
 
47
          wiki_links_class_func
 
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).
 
53
          wiki_links_path_func
 
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.
 
59
          macro_func
 
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.
 
70
            Examples:
 
71
 
 
72
            These are regular macros::
 
73
            
 
74
              <<macro-name arg_string>>the body<</macro-name>>
 
75
              <<macro-name2 I have no body, just this argument string>>
 
76
 
 
77
            These are "block" macros. The won't be enclosed automatically in
 
78
            paragraphs like those above::
 
79
 
 
80
              <<note-blank-lines-before-and-after>>
 
81
 
 
82
 
 
83
              <<macro-name-alone>>
 
84
              nor does this onu
 
85
              <</macro-name-alone>>
 
86
                       
 
87
         """
 
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:
 
108
            no_wiki_tag = 'tt'
 
109
        else:
 
110
            no_wiki_tag = 'span'
 
111
        self.no_wiki = NoWikiElement(no_wiki_tag,['{{{','}}}'],[])
 
112
        
 
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]
 
118
 
 
119
        if use_additions:
 
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])
 
131
        
 
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
 
135
 
 
136
            
 
137
        self.hr = LoneElement('hr','----',[])
 
138
        #self.lone_br = LoneElement('br',r'\\',[])
 
139
        self.blank_line = BlankLine()
 
140
        self.lone_place_holder = LonePlaceHolder('',['<<<','>>>'],[])
 
141
 
 
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)
 
148
 
 
149
        headings = [self.h1,self.h2,self.h3,self.h4,self.h5,self.h6]
 
150
 
 
151
        self.td = TableCell('td','|',table_cell_children)
 
152
        self.th = TableCell('th','|=',table_cell_children)
 
153
        if use_additions:
 
154
            self.tr = TableRow('tr','|',[(self.no_wiki,self.bodiedmacro,self.macro),self.img,self.link,self.th,self.td])
 
155
        else:
 
156
            self.tr = TableRow('tr','|',[self.no_wiki,self.img,self.link,self.th,self.td])
 
157
        self.table = Table('table','|',[self.tr])
 
158
 
 
159
        self.p = Paragraph('p',inline_elements)
 
160
 
 
161
        if use_additions:
 
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='*#')
 
165
     
 
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
 
174
        if use_additions:
 
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]
 
177
 
 
178
        else:
 
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.
 
185
        """