1010BEGINDEFINITION = re .compile ("^<<(?P<name>.*)>>=\s*" )
1111USEDEFINITION = re .compile ("^(?P<pre>.*)<<(?P<name>.*)>>(?P<post>[^=].*)" )
1212ENDDEFINITION = re .compile ("^@" )
13+ GREMLINS = re .compile ("[\xa0 \xca ]" )
1314
1415DEFAULT_CONFIG = """
15- config = [
16+ filepatterns = [
1617 ("^.*\.cp$", ":unweave-src"),
1718 ("^.*\.h$", ":unweave-include"),
1819]
@@ -38,6 +39,13 @@ def __init__(self, filename, config={}):
3839 self .gencomments = config ["gencomments" ]
3940 else :
4041 self .gencomments = 0
42+ if config .has_key ("filepatterns" ):
43+ self .filepatterns = config ["filepatterns" ]
44+ else :
45+ self .filepatterns = []
46+ self .filepattern_relist = []
47+ for pat , dummy in self .filepatterns :
48+ self .filepattern_relist .append (re .compile (pat ))
4149
4250 def _readline (self ):
4351 """Read a line. Allow for pushback"""
@@ -100,27 +108,42 @@ def read(self):
100108 savedcomment = savedcomment + [(lineno , '//\n ' )] + defline
101109 else :
102110 savedcomment = defline
103- savedcomment = self ._extendlines (savedcomment )
111+ savedcomment = self ._processcomment (savedcomment )
104112 value = savedcomment + value
105113 savedcomment = []
114+ isfilepattern = 0
115+ for rexp in self .filepattern_relist :
116+ if rexp .search (name ):
117+ isfilepattern = 1
118+ break
119+ if 0 and not isfilepattern :
120+ value = self ._addspace (value )
106121 self ._define (name , value )
107122 else :
108123 if self .gencomments :
109124 # It seems initial blank lines are ignored:-(
110125 if savedcomment or line .strip ():
111126 savedcomment .append ((lineno , '// ' + line ))
112127
113- def _extendlines (self , comment ):
128+ def _processcomment (self , comment ):
114129 # This routine mimicks some artefact of Matthias' code.
115130 rv = []
116131 for lineno , line in comment :
117132 line = line [:- 1 ]
133+ line = GREMLINS .subn (' ' , line )[0 ]
118134 if len (line ) < 75 :
119135 line = line + (75 - len (line ))* ' '
120136 line = line + '\n '
121137 rv .append ((lineno , line ))
122138 return rv
123139
140+ def _addspace (self , value , howmany ):
141+ # Yet another routine to mimick yet another artefact
142+ rv = value [0 :1 ]
143+ for lineno , line in value [1 :]:
144+ rv .append ((lineno , (' ' * howmany )+ line ))
145+ return rv
146+
124147 def resolve (self ):
125148 """Resolve all references"""
126149 for name in self .items .keys ():
@@ -141,15 +164,22 @@ def _resolve_one(self, name):
141164 # No rest for the wicked: we have work to do.
142165 self .resolving [name ] = 1
143166 result = []
167+ lastlineincomplete = 0
144168 for lineno , line in self .items [name ]:
145169 mo = USEDEFINITION .search (line )
146170 if mo :
147171 # We replace the complete line. Is this correct?
148172 macro = mo .group ('name' )
149173 replacement = self ._resolve_one (macro )
174+ if lastlineincomplete :
175+ replacement = self ._addspace (replacement , lastlineincomplete )
150176 result = result + replacement
151177 else :
152178 result .append ((lineno , line ))
179+ if line [- 1 ] == '\n ' :
180+ lastlineincomplete = 0
181+ else :
182+ lastlineincomplete = len (line )
153183 self .items [name ] = result
154184 self .resolved [name ] = 1
155185 del self .resolving [name ]
@@ -194,7 +224,7 @@ def process(file, config):
194224 pr = Processor (file , config )
195225 pr .read ()
196226 pr .resolve ()
197- for pattern , folder in config ['config ' ]:
227+ for pattern , folder in config ['filepatterns ' ]:
198228 pr .save (folder , pattern )
199229
200230def readconfig ():
0 commit comments