]>
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
)): 
  30             return struct
[len(struct
)-1].getSubparagraphs() 
  35         run 
= run
.getSubparagraphs()[len(run
.getSubparagraphs())-1] 
  37     #print "parent for level ", level, " was => ", run.getColorizableTexts() 
  38     return run
.getSubparagraphs() 
  42     runs through the structure and prints out 
  43     the paragraphs. If the insertion works 
  44     correctly, display's results should mimic 
  45     the orignal paragraphs. 
  48     if struct
.getColorizableTexts(): 
  49         print join(struct
.getColorizableTexts()),"\n" 
  50     if struct
.getSubparagraphs(): 
  51         for x 
in struct
.getSubparagraphs(): 
  56     runs through the structure and prints out 
  57     the paragraphs. If the insertion works 
  58     correctly, display's results should mimic 
  59     the orignal paragraphs.     
  62     if struct
.getNodeValue(): 
  63         print struct
.getNodeValue(),"\n" 
  64     if struct
.getSubparagraphs(): 
  65         for x 
in struct
.getSubparagraphs(): 
  68 def findlevel(levels
,indent
): 
  70     remove all level information of levels 
  71     with a greater level of indentation. 
  72     Then return which level should insert this 
  78         if levels
[key
] > indent
: 
  85             if levels
[key
] == indent
: 
  93 ##################################################################### 
  95 # Golly, the capitalization of this function always makes me think it's a class 
  96 def StructuredText(paragraphs
, paragraph_delimiter
=re
.compile('\n\s*\n')): 
  98     StructuredText accepts paragraphs, which is a list of  
  99     lines to be parsed. StructuredText creates a structure 
 100     which mimics the structure of the paragraphs. 
 101     Structure => [paragraph,[sub-paragraphs]] 
 107     level             
= 0        # which header are we under 
 108     struct            
= []      # the structure to be returned 
 113         paragraph_delimiter
.split(expandtabs('\n\n'+paragraphs
+'\n\n')) 
 116     if not paragraphs
: return [] 
 118     ind 
= []     # structure based on indention levels 
 119     for paragraph 
in paragraphs
: 
 120         ind
.append([indention(paragraph
), paragraph
]) 
 122     currentindent 
= indention(paragraphs
[0]) 
 123     levels
[0]        = currentindent
 
 125     ############################################################# 
 127     ############################################################# 
 129     for indent
,paragraph 
in ind 
: 
 135             struct
.append(StructuredTextParagraph(paragraph
, indent
=indent
, level
=currentlevel
)) 
 136         elif indent 
> currentindent
: 
 137             currentlevel            
= currentlevel 
+ 1 
 138             currentindent           
= indent
 
 139             levels
[currentlevel
]    = indent
 
 140             run 
= insert(struct
,level
,currentlevel
) 
 141             run
.append(StructuredTextParagraph(paragraph
, indent
=indent
, level
=currentlevel
)) 
 142         elif indent 
< currentindent
: 
 143             result   
= findlevel(levels
,indent
) 
 145                 currentlevel 
= result
 
 146             currentindent  
= indent
 
 148                 struct
.append(StructuredTextParagraph(paragraph
, indent
=indent
, level
=currentlevel
)) 
 150                 run 
= insert(struct
,level
,currentlevel
) 
 151                 run
.append(StructuredTextParagraph(paragraph
, indent
=indent
, level
=currentlevel
)) 
 153             if insert(struct
,level
,currentlevel
): 
 154                 run 
= insert(struct
,level
,currentlevel
) 
 157                 currentindet 
= indent
 
 158             run
.append(StructuredTextParagraph(paragraph
, indent
=indent
, level
=currentlevel
)) 
 160     return StructuredTextDocument(struct
) 
 162 Basic 
= StructuredText
 
 164 class StructuredTextParagraph(STDOM
.Element
): 
 168     def __init__(self
, src
, subs
=None, **kw
): 
 169         if subs 
is None: subs
=[] 
 171         self
._subs
=list(subs
) 
 173         self
._attributes
=kw
.keys() 
 174         for k
, v 
in kw
.items(): setattr(self
, k
, v
) 
 176     def getChildren(self
, type=type, lt
=type([])): 
 178         if type(src
) is not lt
: src
=[src
] 
 179         return src
+self
._subs
 
 181     def getAttribute(self
, name
): 
 182         return getattr(self
, name
, None) 
 184     def getAttributeNode(self
, name
): 
 185         if hasattr(self
, name
): 
 186             return STDOM
.Attr(name
, getattr(self
, name
)) 
 188     def getAttributes(self
): 
 190         for a 
in self
._attributes
: 
 191             d
[a
]=getattr(self
, a
, '') 
 192         return STDOM
.NamedNodeMap(d
) 
 194     def getSubparagraphs(self
): 
 197     def setSubparagraphs(self
, subs
): 
 200     def getColorizableTexts(self
): 
 203     def setColorizableTexts(self
, src
): 
 208         a((' '*(self
.indent 
or 0))+ 
 209           ('%s(' % self
.__class
__.__name
__) 
 210           +str(self
._src
)+', [' 
 212         for p 
in self
._subs
: a(`p`
) 
 213         a((' '*(self
.indent 
or 0))+'])') 
 217     create aliases for all above functions in the pythony way. 
 220     def _get_Children(self
, type=type, lt
=type([])): 
 221         return self
.getChildren(type,lt
) 
 223     def _get_Attribute(self
, name
): 
 224         return self
.getAttribute(name
) 
 226     def _get_AttributeNode(self
, name
): 
 227         return self
.getAttributeNode(name
) 
 229     def _get_Attributes(self
): 
 230         return self
.getAttributes() 
 232     def _get_Subparagraphs(self
): 
 233         return self
.getSubparagraphs() 
 235     def _set_Subparagraphs(self
, subs
): 
 236         return self
.setSubparagraphs(subs
) 
 238     def _get_ColorizableTexts(self
): 
 239         return self
.getColorizableTexts() 
 241     def _set_ColorizableTexts(self
, src
): 
 242         return self
.setColorizableTexts(src
) 
 244 class StructuredTextDocument(StructuredTextParagraph
): 
 246     A StructuredTextDocument holds StructuredTextParagraphs 
 247     as its subparagraphs. 
 251     def __init__(self
, subs
=None, **kw
): 
 252         apply(StructuredTextParagraph
.__init
__, 
 256     def getChildren(self
): 
 259     def getColorizableTexts(self
): 
 262     def setColorizableTexts(self
, src
): 
 267         a('%s([' % self
.__class
__.__name
__) 
 268         for p 
in self
._subs
: a(`p`
+',') 
 273     create aliases for all above functions in the pythony way. 
 276     def _get_Children(self
): 
 277         return self
.getChildren() 
 279     def _get_ColorizableTexts(self
): 
 280         return self
.getColorizableTexts() 
 282     def _set_ColorizableTexts(self
, src
): 
 283         return self
.setColorizableTexts(src
)