]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/samples/stxview/StructuredText/ST.py
2 from string
import split
, join
, replace
, expandtabs
, strip
, find
4 #####################################################################
6 #####################################################################
8 def indention(str,front
= re
.compile("^\s+").match
):
10 Convert all tabs to the appropriate number of spaces.
11 Find the number of leading spaces. If none, return 0
15 start
,end
= front(str).span()
18 return 0 # no leading spaces
20 def insert(struct
, top
, level
):
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
27 #print "struct", struct, top-1
28 if not top
-1 in range(len(struct
)):
33 run
= run
.getSubparagraphs()[len(run
.getSubparagraphs())-1]
35 #print "parent for level ", level, " was => ", run.getColorizableTexts()
36 return run
.getSubparagraphs()
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.
46 if struct
.getColorizableTexts():
47 print join(struct
.getColorizableTexts()),"\n"
48 if struct
.getSubparagraphs():
49 for x
in struct
.getSubparagraphs():
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.
60 if struct
.getNodeValue():
61 print struct
.getNodeValue(),"\n"
62 if struct
.getSubparagraphs():
63 for x
in struct
.getSubparagraphs():
66 def findlevel(levels
,indent
):
68 remove all level information of levels
69 with a greater level of indentation.
70 Then return which level should insert this
76 if levels
[key
] > indent
:
83 if levels
[key
] == indent
:
91 #####################################################################
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')):
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]]
105 level
= 0 # which header are we under
106 struct
= [] # the structure to be returned
111 paragraph_delimiter
.split(expandtabs('\n\n'+paragraphs
+'\n\n'))
114 if not paragraphs
: return []
116 ind
= [] # structure based on indention levels
117 for paragraph
in paragraphs
:
118 ind
.append([indention(paragraph
), paragraph
])
120 currentindent
= indention(paragraphs
[0])
121 levels
[0] = currentindent
123 #############################################################
125 #############################################################
127 for indent
,paragraph
in ind
:
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
)
143 currentlevel
= result
144 currentindent
= indent
145 run
= insert(struct
,level
,currentlevel
)
146 run
.append(StructuredTextParagraph(paragraph
, indent
=indent
, level
=currentlevel
))
148 if insert(struct
,level
,currentlevel
):
149 run
= insert(struct
,level
,currentlevel
)
152 currentindet
= indent
153 run
.append(StructuredTextParagraph(paragraph
, indent
=indent
, level
=currentlevel
))
155 return StructuredTextDocument(struct
)
157 Basic
= StructuredText
159 class StructuredTextParagraph(STDOM
.Element
):
163 def __init__(self
, src
, subs
=None, **kw
):
164 if subs
is None: subs
=[]
166 self
._subs
=list(subs
)
168 self
._attributes
=kw
.keys()
169 for k
, v
in kw
.items(): setattr(self
, k
, v
)
171 def getChildren(self
, type=type, lt
=type([])):
173 if type(src
) is not lt
: src
=[src
]
174 return src
+self
._subs
176 def getAttribute(self
, name
):
177 return getattr(self
, name
, None)
179 def getAttributeNode(self
, name
):
180 if hasattr(self
, name
):
181 return STDOM
.Attr(name
, getattr(self
, name
))
183 def getAttributes(self
):
185 for a
in self
._attributes
:
186 d
[a
]=getattr(self
, a
, '')
187 return STDOM
.NamedNodeMap(d
)
189 def getSubparagraphs(self
):
192 def setSubparagraphs(self
, subs
):
195 def getColorizableTexts(self
):
198 def setColorizableTexts(self
, src
):
203 a((' '*(self
.indent
or 0))+
204 ('%s(' % self
.__class
__.__name
__)
205 +str(self
._src
)+', ['
207 for p
in self
._subs
: a(`p`
)
208 a((' '*(self
.indent
or 0))+'])')
212 create aliases for all above functions in the pythony way.
215 def _get_Children(self
, type=type, lt
=type([])):
216 return self
.getChildren(type,lt
)
218 def _get_Attribute(self
, name
):
219 return self
.getAttribute(name
)
221 def _get_AttributeNode(self
, name
):
222 return self
.getAttributeNode(name
)
224 def _get_Attributes(self
):
225 return self
.getAttributes()
227 def _get_Subparagraphs(self
):
228 return self
.getSubparagraphs()
230 def _set_Subparagraphs(self
, subs
):
231 return self
.setSubparagraphs(subs
)
233 def _get_ColorizableTexts(self
):
234 return self
.getColorizableTexts()
236 def _set_ColorizableTexts(self
, src
):
237 return self
.setColorizableTexts(src
)
239 class StructuredTextDocument(StructuredTextParagraph
):
241 A StructuredTextDocument holds StructuredTextParagraphs
242 as its subparagraphs.
246 def __init__(self
, subs
=None, **kw
):
247 apply(StructuredTextParagraph
.__init
__,
251 def getChildren(self
):
254 def getColorizableTexts(self
):
257 def setColorizableTexts(self
, src
):
262 a('%s([' % self
.__class
__.__name
__)
263 for p
in self
._subs
: a(`p`
+',')
268 create aliases for all above functions in the pythony way.
271 def _get_Children(self
):
272 return self
.getChildren()
274 def _get_ColorizableTexts(self
):
275 return self
.getColorizableTexts()
277 def _set_ColorizableTexts(self
, src
):
278 return self
.setColorizableTexts(src
)