##// END OF EJS Templates
minirst: add basic HTML formatting support
Matt Mackall -
r15261:e2df5b86 default
parent child Browse files
Show More
@@ -497,6 +497,93 b' def formatblock(block, width):'
497 497 initindent=indent,
498 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 587 def parse(text, indent=0, keep=None):
501 588 """Parse text into a list of blocks"""
502 589 pruned = []
@@ -3,8 +3,9 b' from mercurial import minirst'
3 3
4 4 def debugformat(title, text, width, **kwargs):
5 5 print "%s formatted to fit within %d characters:" % (title, width)
6 formatted = minirst.format(text, width, **kwargs)
7 html = minirst.formathtml(minirst.parse(text, **kwargs)[0])
6 8 print "-" * 70
7 formatted = minirst.format(text, width, **kwargs)
8 9 if type(formatted) == tuple:
9 10 print formatted[0]
10 11 print "-" * 70
@@ -12,6 +13,8 b' def debugformat(title, text, width, **kw'
12 13 else:
13 14 print formatted
14 15 print "-" * 70
16 print html
17 print "-" * 70
15 18 print
16 19
17 20 paragraphs = """
@@ -8,6 +8,19 b' This is some text in the first paragraph'
8 8 The third and final paragraph.
9 9
10 10 ----------------------------------------------------------------------
11 <p>
12 This is some text in the first paragraph.
13 </p>
14 <p>
15 A small indented paragraph.
16 It is followed by some lines
17 containing random whitespace.
18 </p>
19 <p>
20 The third and final paragraph.
21 </p>
22
23 ----------------------------------------------------------------------
11 24
12 25 paragraphs formatted to fit within 30 characters:
13 26 ----------------------------------------------------------------------
@@ -22,6 +35,19 b' paragraph.'
22 35 The third and final paragraph.
23 36
24 37 ----------------------------------------------------------------------
38 <p>
39 This is some text in the first paragraph.
40 </p>
41 <p>
42 A small indented paragraph.
43 It is followed by some lines
44 containing random whitespace.
45 </p>
46 <p>
47 The third and final paragraph.
48 </p>
49
50 ----------------------------------------------------------------------
25 51
26 52 definitions formatted to fit within 60 characters:
27 53 ----------------------------------------------------------------------
@@ -37,6 +63,16 b' Another Term'
37 63 Definition.
38 64
39 65 ----------------------------------------------------------------------
66 <dl>
67 <dt>A Term
68 <dd>Definition. The indented lines make up the definition.
69 <dt>Another Term
70 <dd>Another definition. The final line in the definition determines the indentation, so this will be indented with four spaces.
71 <dt>A Nested/Indented Term
72 <dd>Definition.
73 </dl>
74
75 ----------------------------------------------------------------------
40 76
41 77 definitions formatted to fit within 30 characters:
42 78 ----------------------------------------------------------------------
@@ -57,6 +93,16 b' Another Term'
57 93 Definition.
58 94
59 95 ----------------------------------------------------------------------
96 <dl>
97 <dt>A Term
98 <dd>Definition. The indented lines make up the definition.
99 <dt>Another Term
100 <dd>Another definition. The final line in the definition determines the indentation, so this will be indented with four spaces.
101 <dt>A Nested/Indented Term
102 <dd>Definition.
103 </dl>
104
105 ----------------------------------------------------------------------
60 106
61 107 literals formatted to fit within 60 characters:
62 108 ----------------------------------------------------------------------
@@ -78,6 +124,31 b' space-double-colon.'
78 124 with '::' disappears in the final output.
79 125
80 126 ----------------------------------------------------------------------
127 <p>
128 The fully minimized form is the most
129 convenient form:
130 </p>
131 <pre>
132 Hello
133 literal
134 world
135 </pre>
136 <p>
137 In the partially minimized form a paragraph
138 simply ends with space-double-colon.
139 </p>
140 <pre>
141 ////////////////////////////////////////
142 long un-wrapped line in a literal block
143 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
144 </pre>
145 <pre>
146 This literal block is started with '::',
147 the so-called expanded form. The paragraph
148 with '::' disappears in the final output.
149 </pre>
150
151 ----------------------------------------------------------------------
81 152
82 153 literals formatted to fit within 30 characters:
83 154 ----------------------------------------------------------------------
@@ -101,6 +172,31 b' with space-double-colon.'
101 172 with '::' disappears in the final output.
102 173
103 174 ----------------------------------------------------------------------
175 <p>
176 The fully minimized form is the most
177 convenient form:
178 </p>
179 <pre>
180 Hello
181 literal
182 world
183 </pre>
184 <p>
185 In the partially minimized form a paragraph
186 simply ends with space-double-colon.
187 </p>
188 <pre>
189 ////////////////////////////////////////
190 long un-wrapped line in a literal block
191 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
192 </pre>
193 <pre>
194 This literal block is started with '::',
195 the so-called expanded form. The paragraph
196 with '::' disappears in the final output.
197 </pre>
198
199 ----------------------------------------------------------------------
104 200
105 201 lists formatted to fit within 60 characters:
106 202 ----------------------------------------------------------------------
@@ -137,6 +233,49 b' This is the first line. The line continu'
137 233 This is the second line.
138 234
139 235 ----------------------------------------------------------------------
236 <ul>
237 <li> This is the first list item.
238 <p>
239 Second paragraph in the first list item.
240 </p>
241 <li> List items need not be separated by a blank line.
242 <li> And will be rendered without one in any case.
243 </ul>
244 <p>
245 We can have indented lists:
246 </p>
247 <ul>
248 <li> This is an indented list item
249 <li> Another indented list item:
250 <pre>
251 - A literal block in the middle
252 of an indented list.
253 </pre>
254 <pre>
255 (The above is not a list item since we are in the literal block.)
256 </pre>
257 </ul>
258 <pre>
259 Literal block with no indentation (apart from
260 the two spaces added to all literal blocks).
261 </pre>
262 <ol>
263 <li> This is an enumerated list (first item).
264 <li> Continuing with the second item.
265 <li> foo
266 <li> bar
267 <li> Another
268 <li> List
269 </ol>
270 <p>
271 Line blocks are also a form of list:
272 </p>
273 <ol>
274 <li> This is the first line. The line continues here.
275 <li> This is the second line.
276 </ol>
277
278 ----------------------------------------------------------------------
140 279
141 280 lists formatted to fit within 30 characters:
142 281 ----------------------------------------------------------------------
@@ -182,6 +321,49 b' line continues here.'
182 321 This is the second line.
183 322
184 323 ----------------------------------------------------------------------
324 <ul>
325 <li> This is the first list item.
326 <p>
327 Second paragraph in the first list item.
328 </p>
329 <li> List items need not be separated by a blank line.
330 <li> And will be rendered without one in any case.
331 </ul>
332 <p>
333 We can have indented lists:
334 </p>
335 <ul>
336 <li> This is an indented list item
337 <li> Another indented list item:
338 <pre>
339 - A literal block in the middle
340 of an indented list.
341 </pre>
342 <pre>
343 (The above is not a list item since we are in the literal block.)
344 </pre>
345 </ul>
346 <pre>
347 Literal block with no indentation (apart from
348 the two spaces added to all literal blocks).
349 </pre>
350 <ol>
351 <li> This is an enumerated list (first item).
352 <li> Continuing with the second item.
353 <li> foo
354 <li> bar
355 <li> Another
356 <li> List
357 </ol>
358 <p>
359 Line blocks are also a form of list:
360 </p>
361 <ol>
362 <li> This is the first line. The line continues here.
363 <li> This is the second line.
364 </ol>
365
366 ----------------------------------------------------------------------
185 367
186 368 options formatted to fit within 60 characters:
187 369 ----------------------------------------------------------------------
@@ -210,6 +392,37 b' paragraph:'
210 392 --foo bar baz
211 393
212 394 ----------------------------------------------------------------------
395 <p>
396 There is support for simple option lists,
397 but only with long options:
398 </p>
399 <dl>
400 <dt>-X --exclude filter
401 <dd>an option with a short and long option with an argument
402 <dt>-I --include
403 <dd>an option with both a short option and a long option
404 <dt> --all
405 <dd>Output all.
406 <dt> --both
407 <dd>Output both (this description is quite long).
408 <dt> --long
409 <dd>Output all day long.
410 <dt> --par
411 <dd>This option has two paragraphs in its description. This is the first.
412 <p>
413 This is the second. Blank lines may be omitted between
414 options (as above) or left in (as here).
415 </p>
416 </dl>
417 <p>
418 The next paragraph looks like an option list, but lacks the two-space
419 marker after the option. It is treated as a normal paragraph:
420 </p>
421 <p>
422 --foo bar baz
423 </p>
424
425 ----------------------------------------------------------------------
213 426
214 427 options formatted to fit within 30 characters:
215 428 ----------------------------------------------------------------------
@@ -283,6 +496,37 b' normal paragraph:'
283 496 --foo bar baz
284 497
285 498 ----------------------------------------------------------------------
499 <p>
500 There is support for simple option lists,
501 but only with long options:
502 </p>
503 <dl>
504 <dt>-X --exclude filter
505 <dd>an option with a short and long option with an argument
506 <dt>-I --include
507 <dd>an option with both a short option and a long option
508 <dt> --all
509 <dd>Output all.
510 <dt> --both
511 <dd>Output both (this description is quite long).
512 <dt> --long
513 <dd>Output all day long.
514 <dt> --par
515 <dd>This option has two paragraphs in its description. This is the first.
516 <p>
517 This is the second. Blank lines may be omitted between
518 options (as above) or left in (as here).
519 </p>
520 </dl>
521 <p>
522 The next paragraph looks like an option list, but lacks the two-space
523 marker after the option. It is treated as a normal paragraph:
524 </p>
525 <p>
526 --foo bar baz
527 </p>
528
529 ----------------------------------------------------------------------
286 530
287 531 fields formatted to fit within 60 characters:
288 532 ----------------------------------------------------------------------
@@ -298,6 +542,23 b' much too large'
298 542 This key is big enough to get its own line.
299 543
300 544 ----------------------------------------------------------------------
545 <dl>
546 <dt>a
547 <dd>First item.
548 <dt>ab
549 <dd>Second item. Indentation and wrapping is handled automatically.
550 </dl>
551 <p>
552 Next list:
553 </p>
554 <dl>
555 <dt>small
556 <dd>The larger key below triggers full indentation here.
557 <dt>much too large
558 <dd>This key is big enough to get its own line.
559 </dl>
560
561 ----------------------------------------------------------------------
301 562
302 563 fields formatted to fit within 30 characters:
303 564 ----------------------------------------------------------------------
@@ -318,12 +579,34 b' much too large'
318 579 own line.
319 580
320 581 ----------------------------------------------------------------------
582 <dl>
583 <dt>a
584 <dd>First item.
585 <dt>ab
586 <dd>Second item. Indentation and wrapping is handled automatically.
587 </dl>
588 <p>
589 Next list:
590 </p>
591 <dl>
592 <dt>small
593 <dd>The larger key below triggers full indentation here.
594 <dt>much too large
595 <dd>This key is big enough to get its own line.
596 </dl>
597
598 ----------------------------------------------------------------------
321 599
322 600 containers (normal) formatted to fit within 60 characters:
323 601 ----------------------------------------------------------------------
324 602 Normal output.
325 603
326 604 ----------------------------------------------------------------------
605 <p>
606 Normal output.
607 </p>
608
609 ----------------------------------------------------------------------
327 610
328 611 containers (verbose) formatted to fit within 60 characters:
329 612 ----------------------------------------------------------------------
@@ -334,6 +617,14 b' Verbose output.'
334 617 ----------------------------------------------------------------------
335 618 ['debug', 'debug']
336 619 ----------------------------------------------------------------------
620 <p>
621 Normal output.
622 </p>
623 <p>
624 Verbose output.
625 </p>
626
627 ----------------------------------------------------------------------
337 628
338 629 containers (debug) formatted to fit within 60 characters:
339 630 ----------------------------------------------------------------------
@@ -344,6 +635,14 b' Initial debug output.'
344 635 ----------------------------------------------------------------------
345 636 ['verbose']
346 637 ----------------------------------------------------------------------
638 <p>
639 Normal output.
640 </p>
641 <p>
642 Initial debug output.
643 </p>
644
645 ----------------------------------------------------------------------
347 646
348 647 containers (verbose debug) formatted to fit within 60 characters:
349 648 ----------------------------------------------------------------------
@@ -358,12 +657,31 b' Debug output.'
358 657 ----------------------------------------------------------------------
359 658 []
360 659 ----------------------------------------------------------------------
660 <p>
661 Normal output.
662 </p>
663 <p>
664 Initial debug output.
665 </p>
666 <p>
667 Verbose output.
668 </p>
669 <p>
670 Debug output.
671 </p>
672
673 ----------------------------------------------------------------------
361 674
362 675 roles formatted to fit within 60 characters:
363 676 ----------------------------------------------------------------------
364 677 Please see "hg add".
365 678
366 679 ----------------------------------------------------------------------
680 <p>
681 Please see "hg add".
682 </p>
683
684 ----------------------------------------------------------------------
367 685
368 686 sections formatted to fit within 20 characters:
369 687 ----------------------------------------------------------------------
@@ -380,6 +698,12 b' Markup: "foo" and "hg help"'
380 698 ---------------------------
381 699
382 700 ----------------------------------------------------------------------
701 <h1>Title</h1>
702 <h2>Section</h2>
703 <h3>Subsection</h3>
704 <h2>Markup: "foo" and "hg help"</h2>
705
706 ----------------------------------------------------------------------
383 707
384 708 admonitions formatted to fit within 30 characters:
385 709 ----------------------------------------------------------------------
@@ -397,6 +721,21 b' Note:'
397 721 This is danger
398 722
399 723 ----------------------------------------------------------------------
724 <p>
725 <b>Note:</b> This is a note
726 </p>
727 <ul>
728 <li> Bullet 1
729 <li> Bullet 2
730 </ul>
731 <p>
732 <b>Warning!</b> This is a warning Second input line of warning
733 </p>
734 <p>
735 <b>!Danger!</b> This is danger
736 </p>
737
738 ----------------------------------------------------------------------
400 739
401 740 comments formatted to fit within 30 characters:
402 741 ----------------------------------------------------------------------
@@ -407,6 +746,17 b' Some text.'
407 746 Empty comment above
408 747
409 748 ----------------------------------------------------------------------
749 <p>
750 Some text.
751 </p>
752 <p>
753 Some indented text.
754 </p>
755 <p>
756 Empty comment above
757 </p>
758
759 ----------------------------------------------------------------------
410 760
411 761 === === ========================================
412 762 a b c
@@ -425,4 +775,11 b' table formatted to fit within 30 charact'
425 775 man
426 776
427 777 ----------------------------------------------------------------------
778 <table>
779 <tr><th>a</th><th>b</th><th>c</th></tr>
780 <tr><td>1</td><td>2</td><td>3</td></tr>
781 <tr><td>foo</td><td>bar</td><td>baz this list is very very very long man</td></tr>
782 </table>
428 783
784 ----------------------------------------------------------------------
785
General Comments 0
You need to be logged in to leave comments. Login now