Show More
@@ -113,7 +113,7 b' def findliteralblocks(blocks):' | |||||
113 |
|
113 | |||
114 | _bulletre = re.compile(r'(-|[0-9A-Za-z]+\.|\(?[0-9A-Za-z]+\)) ') |
|
114 | _bulletre = re.compile(r'(-|[0-9A-Za-z]+\.|\(?[0-9A-Za-z]+\)) ') | |
115 | _optionre = re.compile(r'^(--[a-z-]+)((?:[ =][a-zA-Z][\w-]*)? +)(.*)$') |
|
115 | _optionre = re.compile(r'^(--[a-z-]+)((?:[ =][a-zA-Z][\w-]*)? +)(.*)$') | |
116 |
_fieldre = re.compile(r':(?![: ])([^:]*)(?<! ): |
|
116 | _fieldre = re.compile(r':(?![: ])([^:]*)(?<! ):[ ]+(.*)') | |
117 | _definitionre = re.compile(r'[^ ]') |
|
117 | _definitionre = re.compile(r'[^ ]') | |
118 |
|
118 | |||
119 | def splitparagraphs(blocks): |
|
119 | def splitparagraphs(blocks): | |
@@ -159,6 +159,33 b' def splitparagraphs(blocks):' | |||||
159 | return blocks |
|
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 | def findsections(blocks): |
|
189 | def findsections(blocks): | |
163 | """Finds sections. |
|
190 | """Finds sections. | |
164 |
|
191 | |||
@@ -228,11 +255,21 b' def formatblock(block, width):' | |||||
228 | m = _bulletre.match(block['lines'][0]) |
|
255 | m = _bulletre.match(block['lines'][0]) | |
229 | subindent = indent + m.end() * ' ' |
|
256 | subindent = indent + m.end() * ' ' | |
230 | elif block['type'] == 'field': |
|
257 | elif block['type'] == 'field': | |
231 | m = _fieldre.match(block['lines'][0]) |
|
258 | keywidth = block['keywidth'] | |
232 | key, spaces, rest = m.groups() |
|
259 | key = block['key'] | |
233 | # Turn ":foo: bar" into "foo bar". |
|
260 | ||
234 | block['lines'][0] = '%s %s%s' % (key, spaces, rest) |
|
261 | subindent = indent + _fieldwidth * ' ' | |
235 | subindent = indent + (2 + len(key) + len(spaces)) * ' ' |
|
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 | elif block['type'] == 'option': |
|
273 | elif block['type'] == 'option': | |
237 | m = _optionre.match(block['lines'][0]) |
|
274 | m = _optionre.match(block['lines'][0]) | |
238 | option, arg, rest = m.groups() |
|
275 | option, arg, rest = m.groups() | |
@@ -252,6 +289,7 b' def format(text, width, indent=0):' | |||||
252 | blocks = findliteralblocks(blocks) |
|
289 | blocks = findliteralblocks(blocks) | |
253 | blocks = inlineliterals(blocks) |
|
290 | blocks = inlineliterals(blocks) | |
254 | blocks = splitparagraphs(blocks) |
|
291 | blocks = splitparagraphs(blocks) | |
|
292 | blocks = updatefieldlists(blocks) | |||
255 | blocks = findsections(blocks) |
|
293 | blocks = findsections(blocks) | |
256 | blocks = addmargins(blocks) |
|
294 | blocks = addmargins(blocks) | |
257 | return '\n'.join(formatblock(b, width) for b in blocks) |
|
295 | return '\n'.join(formatblock(b, width) for b in blocks) | |
@@ -272,6 +310,7 b' if __name__ == "__main__":' | |||||
272 | blocks = debug(findliteralblocks, blocks) |
|
310 | blocks = debug(findliteralblocks, blocks) | |
273 | blocks = debug(inlineliterals, blocks) |
|
311 | blocks = debug(inlineliterals, blocks) | |
274 | blocks = debug(splitparagraphs, blocks) |
|
312 | blocks = debug(splitparagraphs, blocks) | |
|
313 | blocks = debug(updatefieldlists, blocks) | |||
275 | blocks = debug(findsections, blocks) |
|
314 | blocks = debug(findsections, blocks) | |
276 | blocks = debug(addmargins, blocks) |
|
315 | blocks = debug(addmargins, blocks) | |
277 | print '\n'.join(formatblock(b, 30) for b in blocks) |
|
316 | print '\n'.join(formatblock(b, 30) for b in blocks) |
@@ -13,9 +13,9 b' output the current or given revision of ' | |||||
13 | a format string. The formatting rules are the same as for the export |
|
13 | a format string. The formatting rules are the same as for the export | |
14 | command, with the following additions: |
|
14 | command, with the following additions: | |
15 |
|
15 | |||
16 |
"%s" |
|
16 | "%s" basename of file being printed | |
17 |
"%d" |
|
17 | "%d" dirname of file being printed, or '.' if in repository root | |
18 |
"%p" |
|
18 | "%p" root-relative path name of file being printed | |
19 |
|
19 | |||
20 | options: |
|
20 | options: | |
21 |
|
21 |
@@ -134,13 +134,14 b" debugformat('options', options, 30)" | |||||
134 |
|
134 | |||
135 |
|
135 | |||
136 | fields = """ |
|
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 |
|
141 | Next list: | |
140 | significant for the wrapping of this text. |
|
142 | ||
141 | :another key: More text. |
|
143 | :small: The larger key below triggers full indentation here. | |
142 | The indentation on the following |
|
144 | :much too large: This key is big enough to get its own line. | |
143 | lines is not significant. |
|
|||
144 | """ |
|
145 | """ | |
145 |
|
146 | |||
146 | debugformat('fields', fields, 60) |
|
147 | debugformat('fields', fields, 60) |
@@ -215,29 +215,34 b' normal paragraph:' | |||||
215 |
|
215 | |||
216 | fields formatted to fit within 60 characters: |
|
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 |
|
222 | Next list: | |
221 | significant for the wrapping of this text. |
|
223 | ||
222 | another key More text. The indentation on the following |
|
224 | small The larger key below triggers full indentation | |
223 | lines is not significant. |
|
225 | here. | |
|
226 | much too large | |||
|
227 | This key is big enough to get its own line. | |||
224 | ---------------------------------------------------------------------- |
|
228 | ---------------------------------------------------------------------- | |
225 |
|
229 | |||
226 | fields formatted to fit within 30 characters: |
|
230 | fields formatted to fit within 30 characters: | |
227 | ---------------------------------------------------------------------- |
|
231 | ---------------------------------------------------------------------- | |
228 | Field lists give a simple two- |
|
232 | a First item. | |
229 | column layout: |
|
233 | ab Second item. Indentation | |
|
234 | and wrapping is handled | |||
|
235 | automatically. | |||
|
236 | ||||
|
237 | Next list: | |||
230 |
|
238 | |||
231 | key The whitespace |
|
239 | small The larger key | |
232 |
|
|
240 | below triggers | |
233 | key is |
|
241 | full indentation | |
234 | significant for |
|
242 | here. | |
235 | the wrapping of |
|
243 | much too large | |
236 |
|
|
244 | This key is big | |
237 | another key More text. The |
|
245 | enough to get its | |
238 |
|
|
246 | own line. | |
239 | the following |
|
|||
240 | lines is not |
|
|||
241 | significant. |
|
|||
242 | ---------------------------------------------------------------------- |
|
247 | ---------------------------------------------------------------------- | |
243 |
|
248 |
General Comments 0
You need to be logged in to leave comments.
Login now