Show More
@@ -113,7 +113,7 b' def findliteralblocks(blocks):' | |||
|
113 | 113 | |
|
114 | 114 | _bulletre = re.compile(r'(-|[0-9A-Za-z]+\.|\(?[0-9A-Za-z]+\)) ') |
|
115 | 115 | _optionre = re.compile(r'^(--[a-z-]+)((?:[ =][a-zA-Z][\w-]*)? +)(.*)$') |
|
116 |
_fieldre = re.compile(r':(?![: ])([^:]*)(?<! ): |
|
|
116 | _fieldre = re.compile(r':(?![: ])([^:]*)(?<! ):[ ]+(.*)') | |
|
117 | 117 | _definitionre = re.compile(r'[^ ]') |
|
118 | 118 | |
|
119 | 119 | def splitparagraphs(blocks): |
@@ -159,6 +159,33 b' def splitparagraphs(blocks):' | |||
|
159 | 159 | return blocks |
|
160 | 160 | |
|
161 | 161 | |
|
162 | _fieldwidth = 12 | |
|
163 | ||
|
164 | def updatefieldlists(blocks): | |
|
165 | """Find key and maximum key width for field lists.""" | |
|
166 | i = 0 | |
|
167 | while i < len(blocks): | |
|
168 | if blocks[i]['type'] != 'field': | |
|
169 | i += 1 | |
|
170 | continue | |
|
171 | ||
|
172 | keywidth = 0 | |
|
173 | j = i | |
|
174 | while j < len(blocks) and blocks[j]['type'] == 'field': | |
|
175 | m = _fieldre.match(blocks[j]['lines'][0]) | |
|
176 | key, rest = m.groups() | |
|
177 | blocks[j]['lines'][0] = rest | |
|
178 | blocks[j]['key'] = key | |
|
179 | keywidth = max(keywidth, len(key)) | |
|
180 | j += 1 | |
|
181 | ||
|
182 | for block in blocks[i:j]: | |
|
183 | block['keywidth'] = keywidth | |
|
184 | i = j + 1 | |
|
185 | ||
|
186 | return blocks | |
|
187 | ||
|
188 | ||
|
162 | 189 | def findsections(blocks): |
|
163 | 190 | """Finds sections. |
|
164 | 191 | |
@@ -228,11 +255,21 b' def formatblock(block, width):' | |||
|
228 | 255 | m = _bulletre.match(block['lines'][0]) |
|
229 | 256 | subindent = indent + m.end() * ' ' |
|
230 | 257 | elif block['type'] == 'field': |
|
231 | m = _fieldre.match(block['lines'][0]) | |
|
232 | key, spaces, rest = m.groups() | |
|
233 | # Turn ":foo: bar" into "foo bar". | |
|
234 | block['lines'][0] = '%s %s%s' % (key, spaces, rest) | |
|
235 | subindent = indent + (2 + len(key) + len(spaces)) * ' ' | |
|
258 | keywidth = block['keywidth'] | |
|
259 | key = block['key'] | |
|
260 | ||
|
261 | subindent = indent + _fieldwidth * ' ' | |
|
262 | if len(key) + 2 > _fieldwidth: | |
|
263 | # key too large, use full line width | |
|
264 | key = key.ljust(width) | |
|
265 | elif keywidth + 2 < _fieldwidth: | |
|
266 | # all keys are small, add only two spaces | |
|
267 | key = key.ljust(keywidth + 2) | |
|
268 | subindent = indent + (keywidth + 2) * ' ' | |
|
269 | else: | |
|
270 | # mixed sizes, use fieldwidth for this one | |
|
271 | key = key.ljust(_fieldwidth) | |
|
272 | block['lines'][0] = key + block['lines'][0] | |
|
236 | 273 | elif block['type'] == 'option': |
|
237 | 274 | m = _optionre.match(block['lines'][0]) |
|
238 | 275 | option, arg, rest = m.groups() |
@@ -252,6 +289,7 b' def format(text, width, indent=0):' | |||
|
252 | 289 | blocks = findliteralblocks(blocks) |
|
253 | 290 | blocks = inlineliterals(blocks) |
|
254 | 291 | blocks = splitparagraphs(blocks) |
|
292 | blocks = updatefieldlists(blocks) | |
|
255 | 293 | blocks = findsections(blocks) |
|
256 | 294 | blocks = addmargins(blocks) |
|
257 | 295 | return '\n'.join(formatblock(b, width) for b in blocks) |
@@ -272,6 +310,7 b' if __name__ == "__main__":' | |||
|
272 | 310 | blocks = debug(findliteralblocks, blocks) |
|
273 | 311 | blocks = debug(inlineliterals, blocks) |
|
274 | 312 | blocks = debug(splitparagraphs, blocks) |
|
313 | blocks = debug(updatefieldlists, blocks) | |
|
275 | 314 | blocks = debug(findsections, blocks) |
|
276 | 315 | blocks = debug(addmargins, blocks) |
|
277 | 316 | print '\n'.join(formatblock(b, 30) for b in blocks) |
@@ -134,13 +134,14 b" debugformat('options', options, 30)" | |||
|
134 | 134 | |
|
135 | 135 | |
|
136 | 136 | fields = """ |
|
137 | Field lists give a simple two-column layout: | |
|
137 | :a: First item. | |
|
138 | :ab: Second item. Indentation and wrapping | |
|
139 | is handled automatically. | |
|
138 | 140 | |
|
139 | :key: The whitespace following the key is | |
|
140 | significant for the wrapping of this text. | |
|
141 | :another key: More text. | |
|
142 | The indentation on the following | |
|
143 | lines is not significant. | |
|
141 | Next list: | |
|
142 | ||
|
143 | :small: The larger key below triggers full indentation here. | |
|
144 | :much too large: This key is big enough to get its own line. | |
|
144 | 145 | """ |
|
145 | 146 | |
|
146 | 147 | debugformat('fields', fields, 60) |
@@ -215,29 +215,34 b' normal paragraph:' | |||
|
215 | 215 | |
|
216 | 216 | fields formatted to fit within 60 characters: |
|
217 | 217 | ---------------------------------------------------------------------- |
|
218 | Field lists give a simple two-column layout: | |
|
218 | a First item. | |
|
219 | ab Second item. Indentation and wrapping is handled | |
|
220 | automatically. | |
|
219 | 221 | |
|
220 | key The whitespace following the key is | |
|
221 | significant for the wrapping of this text. | |
|
222 | another key More text. The indentation on the following | |
|
223 | lines is not significant. | |
|
222 | Next list: | |
|
223 | ||
|
224 | small The larger key below triggers full indentation | |
|
225 | here. | |
|
226 | much too large | |
|
227 | This key is big enough to get its own line. | |
|
224 | 228 | ---------------------------------------------------------------------- |
|
225 | 229 | |
|
226 | 230 | fields formatted to fit within 30 characters: |
|
227 | 231 | ---------------------------------------------------------------------- |
|
228 | Field lists give a simple two- | |
|
229 | column layout: | |
|
232 | a First item. | |
|
233 | ab Second item. Indentation | |
|
234 | and wrapping is handled | |
|
235 | automatically. | |
|
236 | ||
|
237 | Next list: | |
|
230 | 238 | |
|
231 | key The whitespace | |
|
232 |
|
|
|
233 | key is | |
|
234 | significant for | |
|
235 | the wrapping of | |
|
236 |
|
|
|
237 | another key More text. The | |
|
238 |
|
|
|
239 | the following | |
|
240 | lines is not | |
|
241 | significant. | |
|
239 | small The larger key | |
|
240 | below triggers | |
|
241 | full indentation | |
|
242 | here. | |
|
243 | much too large | |
|
244 | This key is big | |
|
245 | enough to get its | |
|
246 | own line. | |
|
242 | 247 | ---------------------------------------------------------------------- |
|
243 | 248 |
General Comments 0
You need to be logged in to leave comments.
Login now