Mercurial > hg
comparison mercurial/minirst.py @ 15261:e2df5b866d22
minirst: add basic HTML formatting support
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Sat, 15 Oct 2011 00:39:01 -0500 |
parents | 3834ca04664a |
children | e8076af14498 |
comparison
equal
deleted
inserted
replaced
15260:be2c67a8bf88 | 15261:e2df5b866d22 |
---|---|
495 text = ' '.join(map(str.strip, block['lines'])) | 495 text = ' '.join(map(str.strip, block['lines'])) |
496 return util.wrap(text, width=width, | 496 return util.wrap(text, width=width, |
497 initindent=indent, | 497 initindent=indent, |
498 hangindent=subindent) + '\n' | 498 hangindent=subindent) + '\n' |
499 | 499 |
500 def formathtml(blocks): | |
501 """Format RST blocks as HTML""" | |
502 | |
503 out = [] | |
504 headernest = '' | |
505 listnest = [] | |
506 | |
507 def openlist(start, level): | |
508 if not listnest or listnest[-1][0] != start: | |
509 listnest.append((start, level)) | |
510 out.append('<%s>\n' % start) | |
511 | |
512 blocks = [b for b in blocks if b['type'] != 'margin'] | |
513 | |
514 for pos, b in enumerate(blocks): | |
515 btype = b['type'] | |
516 level = b['indent'] | |
517 lines = b['lines'] | |
518 | |
519 if btype == 'admonition': | |
520 admonition = _admonitiontitles[b['admonitiontitle']] | |
521 text = ' '.join(map(str.strip, lines)) | |
522 out.append('<p>\n<b>%s</b> %s\n</p>\n' % (admonition, text)) | |
523 elif btype == 'paragraph': | |
524 out.append('<p>\n%s\n</p>\n' % '\n'.join(lines)) | |
525 elif btype == 'margin': | |
526 pass | |
527 elif btype == 'literal': | |
528 out.append('<pre>\n%s\n</pre>\n' % '\n'.join(lines)) | |
529 elif btype == 'section': | |
530 i = b['underline'] | |
531 if i not in headernest: | |
532 headernest += i | |
533 level = headernest.index(i) + 1 | |
534 out.append('<h%d>%s</h%d>\n' % (level, lines[0], level)) | |
535 elif btype == 'table': | |
536 table = b['table'] | |
537 t = [] | |
538 for row in table: | |
539 l = [] | |
540 for v in zip(row): | |
541 if not t: | |
542 l.append('<th>%s</th>' % v) | |
543 else: | |
544 l.append('<td>%s</td>' % v) | |
545 t.append(' <tr>%s</tr>\n' % ''.join(l)) | |
546 out.append('<table>\n%s</table>\n' % ''.join(t)) | |
547 elif btype == 'definition': | |
548 openlist('dl', level) | |
549 term = lines[0] | |
550 text = ' '.join(map(str.strip, lines[1:])) | |
551 out.append(' <dt>%s\n <dd>%s\n' % (term, text)) | |
552 elif btype == 'bullet': | |
553 bullet, head = lines[0].split(' ', 1) | |
554 if bullet == '-': | |
555 openlist('ul', level) | |
556 else: | |
557 openlist('ol', level) | |
558 out.append(' <li> %s\n' % ' '.join([head] + lines[1:])) | |
559 elif btype == 'field': | |
560 openlist('dl', level) | |
561 key = b['key'] | |
562 text = ' '.join(map(str.strip, lines)) | |
563 out.append(' <dt>%s\n <dd>%s\n' % (key, text)) | |
564 elif btype == 'option': | |
565 openlist('dl', level) | |
566 opt = b['optstr'] | |
567 desc = ' '.join(map(str.strip, lines)) | |
568 out.append(' <dt>%s\n <dd>%s\n' % (opt, desc)) | |
569 | |
570 # close lists if indent level of next block is lower | |
571 if listnest: | |
572 start, level = listnest[-1] | |
573 if pos == len(blocks) - 1: | |
574 out.append('</%s>\n' % start) | |
575 listnest.pop() | |
576 else: | |
577 nb = blocks[pos + 1] | |
578 ni = nb['indent'] | |
579 if (ni < level or | |
580 (ni == level and | |
581 nb['type'] not in 'definition bullet field option')): | |
582 out.append('</%s>\n' % start) | |
583 listnest.pop() | |
584 | |
585 return ''.join(out) | |
586 | |
500 def parse(text, indent=0, keep=None): | 587 def parse(text, indent=0, keep=None): |
501 """Parse text into a list of blocks""" | 588 """Parse text into a list of blocks""" |
502 pruned = [] | 589 pruned = [] |
503 blocks = findblocks(text) | 590 blocks = findblocks(text) |
504 for b in blocks: | 591 for b in blocks: |