]> git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/stxview/StructuredText/ST.py
Added some simple sample apps
[wxWidgets.git] / wxPython / samples / stxview / StructuredText / ST.py
1 import re, STDOM
2 from string import split, join, replace, expandtabs, strip, find
3
4 #####################################################################
5 # Updated functions #
6 #####################################################################
7
8 def indention(str,front = re.compile("^\s+").match):
9 """
10 Convert all tabs to the appropriate number of spaces.
11 Find the number of leading spaces. If none, return 0
12 """
13
14 if front(str):
15 start,end = front(str).span()
16 return end-start-1
17 else:
18 return 0 # no leading spaces
19
20 def insert(struct, top, level):
21 """
22 find what will be the parant paragraph of
23 a sentence and return that paragraph's
24 sub-paragraphs. The new paragraph will be
25 appended to those sub-paragraphs
26 """
27 #print "struct", struct, top-1
28 if not top-1 in range(len(struct)):
29 return None
30 run = struct[top-1]
31 i = 0
32 while i+1 < level:
33 run = run.getSubparagraphs()[len(run.getSubparagraphs())-1]
34 i = i + 1
35 #print "parent for level ", level, " was => ", run.getColorizableTexts()
36 return run.getSubparagraphs()
37
38 def display(struct):
39 """
40 runs through the structure and prints out
41 the paragraphs. If the insertion works
42 correctly, display's results should mimic
43 the orignal paragraphs.
44 """
45
46 if struct.getColorizableTexts():
47 print join(struct.getColorizableTexts()),"\n"
48 if struct.getSubparagraphs():
49 for x in struct.getSubparagraphs():
50 display(x)
51
52 def display2(struct):
53 """
54 runs through the structure and prints out
55 the paragraphs. If the insertion works
56 correctly, display's results should mimic
57 the orignal paragraphs.
58 """
59
60 if struct.getNodeValue():
61 print struct.getNodeValue(),"\n"
62 if struct.getSubparagraphs():
63 for x in struct.getSubparagraphs():
64 display(x)
65
66 def findlevel(levels,indent):
67 """
68 remove all level information of levels
69 with a greater level of indentation.
70 Then return which level should insert this
71 paragraph
72 """
73
74 keys = levels.keys()
75 for key in keys:
76 if levels[key] > indent:
77 del(levels[key])
78 keys = levels.keys()
79 if not(keys):
80 return 0
81 else:
82 for key in keys:
83 if levels[key] == indent:
84 return key
85 highest = 0
86 for key in keys:
87 if key > highest:
88 highest = key
89 return highest-1
90
91 #####################################################################
92
93 # Golly, the capitalization of this function always makes me think it's a class
94 def StructuredText(paragraphs, paragraph_delimiter=re.compile('\n\s*\n')):
95 """
96 StructuredText accepts paragraphs, which is a list of
97 lines to be parsed. StructuredText creates a structure
98 which mimics the structure of the paragraphs.
99 Structure => [paragraph,[sub-paragraphs]]
100 """
101
102 currentlevel = 0
103 currentindent = 0
104 levels = {0:0}
105 level = 0 # which header are we under
106 struct = [] # the structure to be returned
107 run = struct
108
109 paragraphs = filter(
110 strip,
111 paragraph_delimiter.split(expandtabs('\n\n'+paragraphs+'\n\n'))
112 )
113
114 if not paragraphs: return []
115
116 ind = [] # structure based on indention levels
117 for paragraph in paragraphs:
118 ind.append([indention(paragraph), paragraph])
119
120 currentindent = indention(paragraphs[0])
121 levels[0] = currentindent
122
123 #############################################################
124 # updated #
125 #############################################################
126
127 for indent,paragraph in ind :
128 if indent == 0:
129 level = level + 1
130 currentlevel = 0
131 currentindent = 0
132 levels = {0:0}
133 struct.append(StructuredTextParagraph(paragraph, indent=indent, level=currentlevel))
134 elif indent > currentindent:
135 currentlevel = currentlevel + 1
136 currentindent = indent
137 levels[currentlevel] = indent
138 run = insert(struct,level,currentlevel)
139 run.append(StructuredTextParagraph(paragraph, indent=indent, level=currentlevel))
140 elif indent < currentindent:
141 result = findlevel(levels,indent)
142 if result > 0:
143 currentlevel = result
144 currentindent = indent
145 run = insert(struct,level,currentlevel)
146 run.append(StructuredTextParagraph(paragraph, indent=indent, level=currentlevel))
147 else:
148 if insert(struct,level,currentlevel):
149 run = insert(struct,level,currentlevel)
150 else:
151 run = struct
152 currentindet = indent
153 run.append(StructuredTextParagraph(paragraph, indent=indent, level=currentlevel))
154
155 return StructuredTextDocument(struct)
156
157 Basic = StructuredText
158
159 class StructuredTextParagraph(STDOM.Element):
160
161 indent=0
162
163 def __init__(self, src, subs=None, **kw):
164 if subs is None: subs=[]
165 self._src=src
166 self._subs=list(subs)
167
168 self._attributes=kw.keys()
169 for k, v in kw.items(): setattr(self, k, v)
170
171 def getChildren(self, type=type, lt=type([])):
172 src=self._src
173 if type(src) is not lt: src=[src]
174 return src+self._subs
175
176 def getAttribute(self, name):
177 return getattr(self, name, None)
178
179 def getAttributeNode(self, name):
180 if hasattr(self, name):
181 return STDOM.Attr(name, getattr(self, name))
182
183 def getAttributes(self):
184 d={}
185 for a in self._attributes:
186 d[a]=getattr(self, a, '')
187 return STDOM.NamedNodeMap(d)
188
189 def getSubparagraphs(self):
190 return self._subs
191
192 def setSubparagraphs(self, subs):
193 self._subs=subs
194
195 def getColorizableTexts(self):
196 return (self._src,)
197
198 def setColorizableTexts(self, src):
199 self._src=src[0]
200
201 def __repr__(self):
202 r=[]; a=r.append
203 a((' '*(self.indent or 0))+
204 ('%s(' % self.__class__.__name__)
205 +str(self._src)+', ['
206 )
207 for p in self._subs: a(`p`)
208 a((' '*(self.indent or 0))+'])')
209 return join(r,'\n')
210
211 """
212 create aliases for all above functions in the pythony way.
213 """
214
215 def _get_Children(self, type=type, lt=type([])):
216 return self.getChildren(type,lt)
217
218 def _get_Attribute(self, name):
219 return self.getAttribute(name)
220
221 def _get_AttributeNode(self, name):
222 return self.getAttributeNode(name)
223
224 def _get_Attributes(self):
225 return self.getAttributes()
226
227 def _get_Subparagraphs(self):
228 return self.getSubparagraphs()
229
230 def _set_Subparagraphs(self, subs):
231 return self.setSubparagraphs(subs)
232
233 def _get_ColorizableTexts(self):
234 return self.getColorizableTexts()
235
236 def _set_ColorizableTexts(self, src):
237 return self.setColorizableTexts(src)
238
239 class StructuredTextDocument(StructuredTextParagraph):
240 """
241 A StructuredTextDocument holds StructuredTextParagraphs
242 as its subparagraphs.
243 """
244 _attributes=()
245
246 def __init__(self, subs=None, **kw):
247 apply(StructuredTextParagraph.__init__,
248 (self, '', subs),
249 kw)
250
251 def getChildren(self):
252 return self._subs
253
254 def getColorizableTexts(self):
255 return ()
256
257 def setColorizableTexts(self, src):
258 pass
259
260 def __repr__(self):
261 r=[]; a=r.append
262 a('%s([' % self.__class__.__name__)
263 for p in self._subs: a(`p`+',')
264 a('])')
265 return join(r,'\n')
266
267 """
268 create aliases for all above functions in the pythony way.
269 """
270
271 def _get_Children(self):
272 return self.getChildren()
273
274 def _get_ColorizableTexts(self):
275 return self.getColorizableTexts()
276
277 def _set_ColorizableTexts(self, src):
278 return self.setColorizableTexts(src)