Show More
@@ -1,6 +1,6 b'' | |||||
1 | # minirst.py - minimal reStructuredText parser |
|
1 | # minirst.py - minimal reStructuredText parser | |
2 | # |
|
2 | # | |
3 | # Copyright 2009 Matt Mackall <mpm@selenic.com> and others |
|
3 | # Copyright 2009, 2010 Matt Mackall <mpm@selenic.com> and others | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
@@ -186,6 +186,42 b' def updatefieldlists(blocks):' | |||||
186 | return blocks |
|
186 | return blocks | |
187 |
|
187 | |||
188 |
|
188 | |||
|
189 | def prunecontainers(blocks, keep): | |||
|
190 | """Prune unwanted containers. | |||
|
191 | ||||
|
192 | The blocks must have a 'type' field, i.e., they should have been | |||
|
193 | run through findliteralblocks first. | |||
|
194 | """ | |||
|
195 | i = 0 | |||
|
196 | while i + 1 < len(blocks): | |||
|
197 | # Searching for a block that looks like this: | |||
|
198 | # | |||
|
199 | # +-------+---------------------------+ | |||
|
200 | # | ".. container ::" type | | |||
|
201 | # +---+ | | |||
|
202 | # | blocks | | |||
|
203 | # +-------------------------------+ | |||
|
204 | if (blocks[i]['type'] == 'paragraph' and | |||
|
205 | blocks[i]['lines'][0].startswith('.. container::')): | |||
|
206 | indent = blocks[i]['indent'] | |||
|
207 | adjustment = blocks[i + 1]['indent'] - indent | |||
|
208 | containertype = blocks[i]['lines'][0][15:] | |||
|
209 | prune = containertype not in keep | |||
|
210 | ||||
|
211 | # Always delete "..container:: type" block | |||
|
212 | del blocks[i] | |||
|
213 | j = i | |||
|
214 | while j < len(blocks) and blocks[j]['indent'] > indent: | |||
|
215 | if prune: | |||
|
216 | del blocks[j] | |||
|
217 | i -= 1 # adjust outer index | |||
|
218 | else: | |||
|
219 | blocks[j]['indent'] -= adjustment | |||
|
220 | j += 1 | |||
|
221 | i += 1 | |||
|
222 | return blocks | |||
|
223 | ||||
|
224 | ||||
189 | def findsections(blocks): |
|
225 | def findsections(blocks): | |
190 | """Finds sections. |
|
226 | """Finds sections. | |
191 |
|
227 | |||
@@ -281,12 +317,13 b' def formatblock(block, width):' | |||||
281 | subsequent_indent=subindent) |
|
317 | subsequent_indent=subindent) | |
282 |
|
318 | |||
283 |
|
319 | |||
284 | def format(text, width, indent=0): |
|
320 | def format(text, width, indent=0, keep=[]): | |
285 | """Parse and format the text according to width.""" |
|
321 | """Parse and format the text according to width.""" | |
286 | blocks = findblocks(text) |
|
322 | blocks = findblocks(text) | |
287 | for b in blocks: |
|
323 | for b in blocks: | |
288 | b['indent'] += indent |
|
324 | b['indent'] += indent | |
289 | blocks = findliteralblocks(blocks) |
|
325 | blocks = findliteralblocks(blocks) | |
|
326 | blocks = prunecontainers(blocks, keep) | |||
290 | blocks = inlineliterals(blocks) |
|
327 | blocks = inlineliterals(blocks) | |
291 | blocks = splitparagraphs(blocks) |
|
328 | blocks = splitparagraphs(blocks) | |
292 | blocks = updatefieldlists(blocks) |
|
329 | blocks = updatefieldlists(blocks) | |
@@ -298,8 +335,8 b' def format(text, width, indent=0):' | |||||
298 | if __name__ == "__main__": |
|
335 | if __name__ == "__main__": | |
299 | from pprint import pprint |
|
336 | from pprint import pprint | |
300 |
|
337 | |||
301 |
def debug(func, |
|
338 | def debug(func, *args): | |
302 |
blocks = func( |
|
339 | blocks = func(*args) | |
303 | print "*** after %s:" % func.__name__ |
|
340 | print "*** after %s:" % func.__name__ | |
304 | pprint(blocks) |
|
341 | pprint(blocks) | |
305 |
|
342 | |||
@@ -308,6 +345,7 b' if __name__ == "__main__":' | |||||
308 | text = open(sys.argv[1]).read() |
|
345 | text = open(sys.argv[1]).read() | |
309 | blocks = debug(findblocks, text) |
|
346 | blocks = debug(findblocks, text) | |
310 | blocks = debug(findliteralblocks, blocks) |
|
347 | blocks = debug(findliteralblocks, blocks) | |
|
348 | blocks = debug(prunecontainers, blocks, sys.argv[2:]) | |||
311 | blocks = debug(inlineliterals, blocks) |
|
349 | blocks = debug(inlineliterals, blocks) | |
312 | blocks = debug(splitparagraphs, blocks) |
|
350 | blocks = debug(splitparagraphs, blocks) | |
313 | blocks = debug(updatefieldlists, blocks) |
|
351 | blocks = debug(updatefieldlists, blocks) |
@@ -2,10 +2,10 b'' | |||||
2 |
|
2 | |||
3 | from mercurial import minirst |
|
3 | from mercurial import minirst | |
4 |
|
4 | |||
5 | def debugformat(title, text, width): |
|
5 | def debugformat(title, text, width, **kwargs): | |
6 | print "%s formatted to fit within %d characters:" % (title, width) |
|
6 | print "%s formatted to fit within %d characters:" % (title, width) | |
7 | print "-" * 70 |
|
7 | print "-" * 70 | |
8 | print minirst.format(text, width) |
|
8 | print minirst.format(text, width, **kwargs) | |
9 | print "-" * 70 |
|
9 | print "-" * 70 | |
10 |
|
10 | |||
11 |
|
11 | |||
@@ -143,3 +143,25 b' Next list:' | |||||
143 |
|
143 | |||
144 | debugformat('fields', fields, 60) |
|
144 | debugformat('fields', fields, 60) | |
145 | debugformat('fields', fields, 30) |
|
145 | debugformat('fields', fields, 30) | |
|
146 | ||||
|
147 | containers = """ | |||
|
148 | Normal output. | |||
|
149 | ||||
|
150 | .. container:: debug | |||
|
151 | ||||
|
152 | Initial debug output. | |||
|
153 | ||||
|
154 | .. container:: verbose | |||
|
155 | ||||
|
156 | Verbose output. | |||
|
157 | ||||
|
158 | .. container:: debug | |||
|
159 | ||||
|
160 | Debug output. | |||
|
161 | """ | |||
|
162 | ||||
|
163 | debugformat('containers (normal)', containers, 60) | |||
|
164 | debugformat('containers (verbose)', containers, 60, keep=['verbose']) | |||
|
165 | debugformat('containers (debug)', containers, 60, keep=['debug']) | |||
|
166 | debugformat('containers (verbose debug)', containers, 60, | |||
|
167 | keep=['verbose', 'debug']) |
@@ -246,3 +246,33 b' much too large' | |||||
246 | own line. |
|
246 | own line. | |
247 | ---------------------------------------------------------------------- |
|
247 | ---------------------------------------------------------------------- | |
248 |
|
248 | |||
|
249 | containers (normal) formatted to fit within 60 characters: | |||
|
250 | ---------------------------------------------------------------------- | |||
|
251 | Normal output. | |||
|
252 | ---------------------------------------------------------------------- | |||
|
253 | ||||
|
254 | containers (verbose) formatted to fit within 60 characters: | |||
|
255 | ---------------------------------------------------------------------- | |||
|
256 | Normal output. | |||
|
257 | ||||
|
258 | Verbose output. | |||
|
259 | ---------------------------------------------------------------------- | |||
|
260 | ||||
|
261 | containers (debug) formatted to fit within 60 characters: | |||
|
262 | ---------------------------------------------------------------------- | |||
|
263 | Normal output. | |||
|
264 | ||||
|
265 | Initial debug output. | |||
|
266 | ---------------------------------------------------------------------- | |||
|
267 | ||||
|
268 | containers (verbose debug) formatted to fit within 60 characters: | |||
|
269 | ---------------------------------------------------------------------- | |||
|
270 | Normal output. | |||
|
271 | ||||
|
272 | Initial debug output. | |||
|
273 | ||||
|
274 | Verbose output. | |||
|
275 | ||||
|
276 | Debug output. | |||
|
277 | ---------------------------------------------------------------------- | |||
|
278 |
General Comments 0
You need to be logged in to leave comments.
Login now