##// END OF EJS Templates
minirst: Support for admonitions...
Erik Zielke -
r12388:75f044d4 default
parent child Browse files
Show More
@@ -24,6 +24,8 b' It only supports a small subset of reStr'
24 24
25 25 - definition lists
26 26
27 - specific admonitions
28
27 29 - bullet lists (items must start with '-')
28 30
29 31 - enumerated lists (no autonumbering)
@@ -37,6 +39,8 b' It only supports a small subset of reStr'
37 39
38 40 import re, sys
39 41 import util, encoding
42 from i18n import _
43
40 44
41 45 def replace(text, substs):
42 46 utext = text.decode(encoding.encoding)
@@ -292,12 +296,58 b' def addmargins(blocks):'
292 296 i += 2
293 297 return blocks
294 298
299 def findadmonitions(blocks):
300 """
301 Makes the type of the block an admonition block if
302 the first line is an admonition directive
303 """
304
305 i = 0
306
307 pattern = (r"\.\. (admonition|attention|caution|danger|error|hint|"
308 r"important|note|tip|warning)::")
309
310 prog = re.compile(pattern, flags=re.IGNORECASE)
311 while i < len(blocks):
312 m = prog.match(blocks[i]['lines'][0])
313 if m:
314 blocks[i]['type'] = 'admonition'
315 admonitiontitle = blocks[i]['lines'][0][3:m.end() - 2].lower()
316
317 firstline = blocks[i]['lines'][0][m.end() + 1:]
318 if firstline != '':
319 blocks[i]['lines'].insert(1, ' ' + firstline + '')
320
321
322 blocks[i]['admonitiontitle'] = admonitiontitle
323 del blocks[i]['lines'][0]
324 i = i + 1
325 return blocks
295 326
296 327 def formatblock(block, width):
297 328 """Format a block according to width."""
298 329 if width <= 0:
299 330 width = 78
300 331 indent = ' ' * block['indent']
332 if block['type'] == 'admonition':
333 titles = {'attention': _('Attention:'),
334 'caution': _('Caution:'),
335 'danger': _('!Danger!') ,
336 'error': _('Error:'),
337 'hint': _('Hint:'),
338 'important': _('Important:'),
339 'note': _('Note:'),
340 'tip': _('Tip:'),
341 'warning': _('Warning!')}
342
343 admonition = titles[block['admonitiontitle']]
344 hang = len(block['lines'][-1]) - len(block['lines'][-1].lstrip())
345
346 defindent = indent + hang * ' '
347 text = ' '.join(map(str.strip, block['lines']))
348 return '%s\n%s' % (indent + admonition, util.wrap(text, width=width,
349 initindent=defindent,
350 hangindent=defindent))
301 351 if block['type'] == 'margin':
302 352 return ''
303 353 if block['type'] == 'literal':
@@ -363,6 +413,7 b' def format(text, width, indent=0, keep=N'
363 413 blocks = splitparagraphs(blocks)
364 414 blocks = updatefieldlists(blocks)
365 415 blocks = addmargins(blocks)
416 blocks = findadmonitions(blocks)
366 417 text = '\n'.join(formatblock(b, width) for b in blocks)
367 418 if keep is None:
368 419 return text
@@ -389,4 +440,5 b' if __name__ == "__main__":'
389 440 blocks = debug(updatefieldlists, blocks)
390 441 blocks = debug(findsections, blocks)
391 442 blocks = debug(addmargins, blocks)
443 blocks = debug(findadmonitions, blocks)
392 444 print '\n'.join(formatblock(b, 30) for b in blocks)
@@ -197,3 +197,20 b' Markup: ``foo`` and :hg:`help`'
197 197 ------------------------------
198 198 """
199 199 debugformat('sections', sections, 20)
200
201
202 admonitions = """
203 .. note::
204 This is a note
205
206 - Bullet 1
207 - Bullet 2
208
209 .. warning:: This is a warning Second
210 input line of warning
211
212 .. danger::
213 This is danger
214 """
215
216 debugformat('admonitions', admonitions, 30)
@@ -318,3 +318,19 b' Markup: "foo" and "hg help"'
318 318 ---------------------------
319 319 ----------------------------------------------------------------------
320 320
321 admonitions formatted to fit within 30 characters:
322 ----------------------------------------------------------------------
323 Note:
324 This is a note
325
326 - Bullet 1
327 - Bullet 2
328
329 Warning!
330 This is a warning Second
331 input line of warning
332
333 !Danger!
334 This is danger
335 ----------------------------------------------------------------------
336
General Comments 0
You need to be logged in to leave comments. Login now