]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/stxview/StructuredText/MML.py
515bd3272c2d742096f70d77fc233b48b9d6bc7f
[wxWidgets.git] / wxPython / samples / stxview / StructuredText / MML.py
1 ##############################################################################
2 #
3 # Zope Public License (ZPL) Version 1.0
4 # -------------------------------------
5 #
6 # Copyright (c) Digital Creations. All rights reserved.
7 #
8 # This license has been certified as Open Source(tm).
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions are
12 # met:
13 #
14 # 1. Redistributions in source code must retain the above copyright
15 # notice, this list of conditions, and the following disclaimer.
16 #
17 # 2. Redistributions in binary form must reproduce the above copyright
18 # notice, this list of conditions, and the following disclaimer in
19 # the documentation and/or other materials provided with the
20 # distribution.
21 #
22 # 3. Digital Creations requests that attribution be given to Zope
23 # in any manner possible. Zope includes a "Powered by Zope"
24 # button that is installed by default. While it is not a license
25 # violation to remove this button, it is requested that the
26 # attribution remain. A significant investment has been put
27 # into Zope, and this effort will continue if the Zope community
28 # continues to grow. This is one way to assure that growth.
29 #
30 # 4. All advertising materials and documentation mentioning
31 # features derived from or use of this software must display
32 # the following acknowledgement:
33 #
34 # "This product includes software developed by Digital Creations
35 # for use in the Z Object Publishing Environment
36 # (http://www.zope.org/)."
37 #
38 # In the event that the product being advertised includes an
39 # intact Zope distribution (with copyright and license included)
40 # then this clause is waived.
41 #
42 # 5. Names associated with Zope or Digital Creations must not be used to
43 # endorse or promote products derived from this software without
44 # prior written permission from Digital Creations.
45 #
46 # 6. Modified redistributions of any form whatsoever must retain
47 # the following acknowledgment:
48 #
49 # "This product includes software developed by Digital Creations
50 # for use in the Z Object Publishing Environment
51 # (http://www.zope.org/)."
52 #
53 # Intact (re-)distributions of any official Zope release do not
54 # require an external acknowledgement.
55 #
56 # 7. Modifications are encouraged but must be packaged separately as
57 # patches to official Zope releases. Distributions that do not
58 # clearly separate the patches from the original work must be clearly
59 # labeled as unofficial distributions. Modifications which do not
60 # carry the name Zope may be packaged in any form, as long as they
61 # conform to all of the clauses above.
62 #
63 #
64 # Disclaimer
65 #
66 # THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
67 # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
69 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
70 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
71 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
72 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
73 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
74 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
75 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
76 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 # SUCH DAMAGE.
78 #
79 #
80 # This software consists of contributions made by Digital Creations and
81 # many individuals on behalf of Digital Creations. Specific
82 # attributions are listed in the accompanying credits file.
83 #
84 ##############################################################################
85 '''
86 $Id$'''
87
88 from StructuredText import * # :-)
89
90 def ctag(s):
91 # Blech, wish we could use character tags
92 if s is None: s=''
93 s=gsub(strong,'\\1<bold>\\2<plain>\\3',s)
94 s=gsub(code, '\\1<family Courier>\\2<family Times>\\3',s)
95 s=gsub(em, '\\1<italic>\\2<plain>\\3',s)
96 return join(map(strip,split(s,'\n')),'\n')
97
98 class MML(StructuredText):
99
100 '''\
101 An MML structured text formatter.
102 '''\
103
104 def __str__(self,
105 ):
106 '''\
107 Return an HTML string representation of the structured text data.
108
109 '''
110 s=self._str(self.structure,self.level)
111 return s
112
113 def ul(self, before, p, after):
114 return ("%s\n\n<Bulleted>\n%s%s"
115 % (before, ctag(p), after))
116
117 def ol(self, before, p, after):
118 return ("%s\n\n<Numbered>\n%s%s"
119 % (before, ctag(p), after))
120
121 def dl(self, before, t, d, after):
122 return ("%s\n\n<Term>\n%s\n\n<Definition>\n%s%s"
123 % (before,ctag(t),ctag(d),after))
124
125 def head(self, before, t, level, d):
126 return ("%s\n\n<Heading%d>\n%s%s"
127 % (before,level,ctag(t),d))
128
129 def normal(self,before,p,after):
130 return "%s\n\n<Body>\n%s%s" % (before, ctag(p), after)
131
132 def pre(self,structure,r=None):
133 if r is None: r=['']
134 for s in structure:
135 for line in split(s[0],'\n'):
136 r.append('\n<PRE>')
137 r.append(line)
138 self.pre(s[1],r)
139 return join(r,'\n')
140
141 def _str(self,structure,level):
142 r=''
143 for s in structure:
144 # print s[0],'\n', len(s[1]), '\n\n'
145 if bullet.match(s[0]) >= 0:
146 p=bullet.group(1)
147 r=self.ul(r,p,self._str(s[1],level))
148 elif ol.match(s[0]) >= 0:
149 p=ol.group(3)
150 r=self.ol(r,p,self._str(s[1],level))
151 elif olp.match(s[0]) >= 0:
152 p=olp.group(1)
153 r=self.ol(r,p,self._str(s[1],level))
154 elif dl.match(s[0]) >= 0:
155 t,d=dl.group(1,2)
156 r=self.dl(r,t,d,self._str(s[1],level))
157 elif example.search(s[0]) >= 0 and s[1]:
158 # Introduce an example, using pre tags:
159 r=self.normal(r,s[0],self.pre(s[1]))
160 elif s[0][-2:]=='::' and s[1]:
161 # Introduce an example, using pre tags:
162 r=self.normal(r,s[0][:-1],self.pre(s[1]))
163 elif nl.search(s[0]) < 0 and s[1] and s[0][-1:] != ':':
164 # Treat as a heading
165 t=s[0]
166 r=self.head(r,t,level,
167 self._str(s[1],level and level+1))
168 else:
169 r=self.normal(r,s[0],self._str(s[1],level))
170 return r