forked from micbou/vim-tools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhtml2vimdoc.py
executable file
·1463 lines (1269 loc) · 50.6 KB
/
html2vimdoc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# vim: fileencoding=utf-8 ft=python ts=4 sw=4 et :
# Convert HTML (and Markdown) documents to Vim help files
#
# Author: Peter Odding <[email protected]>
# Last Change: May 21, 2015
# URL: http://peterodding.com/code/vim/tools/
#
# Missing features:
# TODO Improved/full support for <table> elements.
#
# Finding the right abstractions:
# FIXME Quirky mix of classes and functions?
# FIXME The OutputDelimiter stuff is a bit crazy, but I kind of need it? Complexity :-(
"""
html2vimdoc [OPTIONS] [LOCATION]
Convert HTML (and Markdown) documents to Vim help files. When LOCATION is given
it is assumed to be the filename or URL of the input, if --url is given that
URL will be used, otherwise the script reads from standard input. The generated
Vim help file is written to standard output.
Valid options:
-f, --file=NAME name of generated help file (embedded
in Vim help file as first defined tag)
-t, --title=STR title of generated help file
-u, --url=ADDR URL of document (to detect relative links)
-x, --ext=NAME enable the named Markdown extension (only
relevant when input is Markdown; the extension
'fenced_code' is enabled by default)
-p, --preview preview generated Vim help file in Vim
-v, --verbose make more noise (a lot of noise)
-h, --help show this message and exit
This program tries to produce reasonable output given only an HTML or Markdown
document as input, but you can change most defaults with the command line
options listed above.
There are several dependencies that need to be installed to run this program.
The easiest way to install them is in a Python virtual environment:
# Create the virtual environment.
virtualenv html2vimdoc
# Install the dependencies (Markdown is optional).
html2vimdoc/bin/pip install beautifulsoup coloredlogs markdown
# Run the program.
html2vimdoc/bin/python ./html2vimdoc.py --help
"""
# Standard library modules.
import collections.abc
import getopt
import logging
import os
import re
import sys
import textwrap
import types
# Python 2 compatibility.
try:
from urlparse import urljoin, urlparse, unquote
from urllib import urlopen
except ImportError:
from urllib.parse import urljoin, urlparse, unquote
from urllib.request import urlopen
try:
unicode = unicode
except NameError:
unicode = str
if sys.version_info[ 0 ] == 2:
def read_file(path):
with open(path) as f:
return unicode(f.read(), 'UTF-8')
else:
def read_file(path):
with open(path, encoding='utf8') as f:
return f.read()
# External dependency, install with:
# sudo apt-get install python-beautifulsoup
# pip install beautifulsoup
from bs4 import BeautifulSoup, NavigableString, Comment, UnicodeDammit
# External dependency, install with:
# pip install coloredlogs
import coloredlogs
# External dependency, bundled because it's not on PyPi.
import libs.soupselect as soupselect
# Sensible defaults (you probably shouldn't change these).
TEXT_WIDTH = 79
SHIFT_WIDTH = 2
# Initialize the logging subsystem.
logger = logging.getLogger('html2vimdoc')
coloredlogs.install(level='DEBUG')
logger.setLevel(logging.INFO)
# Mapping of HTML element names to custom Node types.
name_to_type_mapping = {}
def main():
"""
Command line interface for html2vimdoc.
"""
filename, title, url, arguments, preview, markdown_extensions = parse_args(sys.argv[1:])
filename, url, text = get_input(filename, url, arguments, markdown_extensions)
vimdoc = html2vimdoc(text, title=title, filename=filename, url=url)
logger.info("Done!")
if preview:
os.popen("gvim -c 'set nomod' -", 'w').write(vimdoc)
else:
print(vimdoc)
def parse_args(argv):
"""
Parse the command line arguments given to html2vimdoc.
"""
preview = False
markdown_extensions = ['pymdownx.superfences', 'tables']
filename = ''
title = ''
url = ''
try:
options, arguments = getopt.getopt(argv, 'f:t:u:x:pvh', ['file=',
'title=', 'url=', 'ext=', 'preview', 'verbose', 'help'])
except getopt.GetoptError as err:
print(str(err))
print(__doc__.strip())
sys.exit(1)
for option, value in options:
if option in ('-f', '--file'):
filename = value
elif option in ('-t', '--title'):
title = value
elif option in ('-u', '--url'):
url = value
elif option in ('-x', '--ext'):
markdown_extensions.append(value)
elif option in ('-p', '--preview'):
preview = True
elif option in ('-v', '--verbose'):
logger.setLevel(logging.DEBUG)
elif option in ('-h', '--help'):
print(__doc__.strip())
sys.exit(0)
else:
assert False, "Unknown option"
return filename, title, url, arguments, preview, markdown_extensions
def get_input(filename, url, args, markdown_extensions):
"""
Get text to be converted from standard input, path name or URL.
"""
source = ''
if not url and not args:
logger.info("Reading HTML from standard input ..")
text = sys.stdin.read()
else:
source = args[0] if args else url
logger.info("Reading input from %s ..", source)
# NOTE: this breaks URL support but we don't need it.
text = read_file(source)
if '://' in source and not url:
# Positional argument was used with same meaning as --url.
url = source
if not filename:
# Generate embedded filename from base name of input document.
filename = os.path.basename(source)
filename = os.path.splitext(filename)[0] + '.txt'
if source.lower().endswith(('.md', '.mkd', '.mkdn', '.mdown', '.markdown')):
text = markdown_to_html(text, markdown_extensions)
return filename, url, text
def markdown_to_html(text, markdown_extensions):
"""
When the input is Markdown, convert it to HTML so we can parse that.
"""
logger.info("Converting Markdown to HTML using extensions: %s.", ", ".join(sorted(markdown_extensions)))
# We import the markdown module here so that the markdown module is not
# required to use html2vimdoc when the input is HTML.
from markdown import markdown
# The Python Markdown module only accepts Unicode and ASCII strings, but we
# don't know what the encoding of the Markdown text is. BeautifulSoup comes
# to the rescue with the aptly named UnicodeDammit class :-).
# NOTE: UnicodeDammit is not needed for our use case.
return markdown(text, extensions=markdown_extensions)
def html2vimdoc(html, title='', filename='', url='', content_selector='#content', selectors_to_ignore=[], modeline='vim: ft=help'):
"""
Convert HTML documents to the Vim help file format.
"""
logger.info("Parsing HTML ..")
html = remove_hexadecimal_character_references(html)
tree = BeautifulSoup(html, features="html.parser")
logger.info("Transforming contents ..")
title = select_title(tree, title)
ignore_comments(tree)
ignore_given_selectors(tree, selectors_to_ignore)
root = find_root_node(tree, content_selector)
simple_tree = simplify_node(root)
shift_headings(simple_tree)
find_references(simple_tree, url)
# Add an "Introduction" heading to separate the table of contents from the
# start of the document text.
simple_tree.contents.insert(0, Heading(level=1, contents=[Text(text="Introduction")]))
logger.info("Tagging document headings ..")
tagged_headings = tag_headings(simple_tree, filename)
logger.info("Marking internal references (pass 1, before TOC) ..")
simple_tree = mark_tags(simple_tree, tagged_headings)
logger.info("Generating table of contents ..")
generate_table_of_contents(simple_tree)
logger.info("Marking internal references (pass 2, after TOC) ..")
simple_tree = mark_tags(simple_tree, tagged_headings)
make_parents_explicit(simple_tree)
prune_empty_blocks(simple_tree)
logger.info("Rendering output ..")
vimdoc = simple_tree.render(indent=0)
output = list(flatten(vimdoc))
logger.debug("Output strings before deduplication: %s", list(unicode(v) for v in output))
deduplicate_delimiters(output)
logger.debug("Output strings after deduplication: %s", list(unicode(v) for v in output))
# Render the final text.
vimdoc = u"".join(unicode(v) for v in output)
# Add the first line with the file tag and/or document title?
if title or filename:
firstline = []
if filename:
firstline.append("*%s*" % filename)
if title:
firstline.append(title)
vimdoc = "%s\n\n%s" % (" ".join(firstline), vimdoc)
# Add a mode line at the end of the document.
if modeline and not modeline.isspace():
vimdoc += "\n\n" + modeline
return vimdoc
def select_title(tree, title):
"""
If the caller didn't specify a help file title, we'll try to extract it
from the HTML <title> or the first <h1> element. Regardless, we'll remove
the first <h1> element because it's usually the page title (other headings
are nested below it so the table of contents becomes a bit awkward :-).
"""
# Improvise a document title?
if not title:
elements = tree.findAll(('title', 'h1'))
if elements:
title = ''.join(elements[0].findAll(text=True))
# Remove the first top level heading from the tree.
headings = tree.findAll('h1')
if headings:
headings[0].extract()
return title
def deduplicate_delimiters(output):
"""
Deduplicate redundant block delimiters from the rendered Vim help text.
"""
i = 0
while i < len(output) - 1:
if isinstance(output[i], OutputDelimiter) and isinstance(output[i + 1], OutputDelimiter):
if output[i].string.isspace() and not output[i + 1].string.isspace():
output.pop(i)
continue
elif output[i + 1].string.isspace() and not output[i].string.isspace():
output.pop(i + 1)
continue
elif len(output[i].string) < len(output[i + 1].string):
output.pop(i)
continue
elif len(output[i].string) > len(output[i + 1].string):
output.pop(i + 1)
continue
elif output[i].string.isspace():
output.pop(i)
continue
i += 1
# Strip leading block delimiters.
while output and isinstance(output[0], OutputDelimiter) and output[0].string.isspace():
output.pop(0)
# Strip trailing block delimiters.
while output and isinstance(output[-1], OutputDelimiter) and output[-1].string.isspace():
output.pop(-1)
def remove_hexadecimal_character_references(html):
"""
BeautifulSoup doesn't support hexadecimal character references but it does
support decimal character references, so by converting one to the other we
can get it to convert everything for us... (kind of silly that every caller
of BeautifulSoup is forced to do this ಠ_ಠ).
"""
def hex_to_dec_entity(match):
code_point = int(match.group(1), 16)
return '&#%d;' % code_point
return re.sub(r'&#x([0-9A-Fa-f]+);', hex_to_dec_entity, html)
def find_root_node(tree, selector):
"""
Given a document tree generated by BeautifulSoup, find the most
specific document node that doesn't "lose any information" (i.e.
everything that we want to be included in the Vim help file) while
ignoring as much fluff as possible (e.g. headers, footers and
navigation menus included in the original HTML document).
"""
# Try to find the root node using a CSS selector provided by the caller.
matches = soupselect.select(tree, selector)
if matches:
return matches[0]
# Otherwise we'll fall back to the <body> element.
try:
return tree.html.body
except:
# Don't break when html.body doesn't exist.
return tree
def ignore_comments(tree):
"""
Remove HTML comments from the parse tree generated by BeautifulSoup.
"""
for html_node in tree.findAll(text = lambda n: isinstance(n, Comment)):
html_node.extract()
def ignore_given_selectors(tree, selectors_to_ignore):
"""
Remove all HTML elements matching any of the CSS selectors provided by
the caller from the parse tree generated by BeautifulSoup.
"""
for selector in selectors_to_ignore:
for element in soupselect.select(tree, selector):
element.extract()
def simplify_node(html_node):
"""
Recursive function to simplify parse trees generated by BeautifulSoup into
something we can more easily convert into HTML.
"""
# First we'll get text nodes out of the way since they're very common.
if isinstance(html_node, NavigableString):
internal_node = Text.parse(html_node)
logger.debug("Mapping text %r -> %r", html_node, internal_node)
return internal_node
# Now we deal with all of the known & supported HTML elements.
name = getattr(html_node, 'name', None)
if name in name_to_type_mapping:
mapped_type = name_to_type_mapping[name]
internal_node = mapped_type.parse(html_node)
logger.debug("Mapping HTML element <%s> -> %r", name, internal_node)
return internal_node
# Finally we improvise, trying not to lose information.
internal_node = simplify_children(html_node)
logger.debug("Not a supported element! Improvising to preserve content.")
return internal_node
def simplify_children(node):
"""
Simplify the child nodes of the given node taken from a parse tree
generated by BeautifulSoup.
"""
contents = []
for child in getattr(node, 'contents', []):
contents.append(simplify_node(child))
if is_block_level(contents):
logger.debug("Sequence contains some block level elements")
return BlockLevelSequence(contents=contents)
else:
logger.debug("Sequence contains only inline elements")
return InlineSequence(contents=contents)
def shift_headings(root):
"""
Perform an intermediate pass over the simplified parse tree to shift
headings in such a way that top level headings have level 1.
"""
# Find the largest headings (lowest level).
min_level = None
logger.debug("Finding largest headings ..")
for node in walk_tree(root, Heading):
if min_level is None:
min_level = node.level
elif node.level < min_level:
min_level = node.level
if min_level is None:
logger.debug("HTML document doesn't contain any headings?")
return
else:
logger.debug("Largest headings have level %i.", min_level)
# Shift the headings if necessary.
if min_level > 1:
to_subtract = min_level - 1
logger.debug("Shifting headings by %i levels.", to_subtract)
for node in walk_tree(root, Heading):
node.level -= to_subtract
def tag_headings(root, filename):
"""
Generate Vim help file tags for headings.
"""
tagged_headings = {}
# Use base name of filename of help file as prefix (scope) for tags.
prefix = re.sub(r'\.txt$', '', filename)
logger.debug("Vim help file name without file extension: %r", prefix)
# If the base name ends in a version number, we'll strip it.
prefix = re.sub(r'-\d+(\.\d+)*$', '', prefix)
logger.debug("Tagging headings using prefix %r ..", prefix)
for node in walk_tree(root, Heading):
logger.debug("Selecting tag for heading: %s", node)
tag = node.tag_heading(tagged_headings, prefix)
if tag:
logger.debug("Found suitable tag: %s", tag)
tagged_headings[tag] = node
return tagged_headings
def mark_tags(root, tags):
"""
Mark references to tags defined in the document.
"""
def recurse(node, parent):
if isinstance(node, CodeFragment) and node.text in tags:
return TagReference(node.text, [Text(text=node.text)], parent=node.parent)
if isinstance(node, SequenceNode):
new_contents = []
for child in node:
new_contents.append(recurse(child, node))
node.contents = new_contents
return node
return recurse(root, None)
def find_references(root, url):
"""
Scan the document tree for hyper links. Each hyper link is given a unique
number so that it can be referenced inside the Vim help file. A new section
is appended to the tree which lists an overview of all references to hyper
links extracted from the HTML document.
"""
# Mapping of hyper link targets to "Reference" objects.
by_target = {}
# Ordered list of "Reference" objects.
by_reference = []
logger.debug("Scanning parse tree for hyper links and other references ..")
for node in walk_tree(root, (HyperLink, Image)):
if isinstance(node, Image):
target = node.src
else:
target = node.target
if not target:
continue
if target == 'http://www.vim.org/':
# Don't add a reference to the Vim homepage in Vim help files.
continue
if target.startswith('http://vimdoc.sourceforge.net/htmldoc/'):
# Don't add a reference to the online Vim documentation.
continue
# Try to convert relative URLs into absolute URLs.
if url and not re.match(r'^\w+:', target):
target = urljoin(url, target)
# Now try to convert absolute URLs into relative URLs... This does
# actually make sense, but it sure sounds stupid :-p. All it really
# does is normalize URLs to a common format.
relative_target = target
if url:
relative_target = os.path.relpath(target, url)
if relative_target.startswith('#'):
# Skip links to page anchors on the same page.
continue
# Exclude literal URLs from list of references.
if target.replace('mailto:', '') == node.render(indent=0):
continue
# Make sure we don't duplicate references.
if target in by_target:
r = by_target[target]
else:
number = len(by_reference) + 1
logger.debug("Extracting reference #%i to %s ..", number, target)
r = Reference(number=number, target=target)
by_reference.append(r)
by_target[target] = r
node.reference = r
logger.debug("Found %i references.", len(by_reference))
if by_reference:
logger.debug("Generating 'References' section ..")
root.contents.append(Heading(level=1, contents=[Text(text="References")]))
root.contents.extend(by_reference)
def generate_table_of_contents(root):
"""
Generate a table of contents for the Vim help file based on the headings
defined in the Markdown or HTML document provided by the user.
"""
entries = []
counters = []
for heading in walk_tree(root, Heading):
logger.debug("Stack of counters before reset: %s", counters)
# Forget no longer relevant counters.
counters = counters[:heading.level]
logger.debug("Stack of counters after reset: %s", counters)
# Make the stack of counters big enough.
while len(counters) < heading.level:
counters.append(1)
logger.debug("Stack of counters after padding: %s", counters)
entries.append(TableOfContentsEntry(
indent=heading.level,
number=counters[heading.level - 1],
contents=copy(heading.contents),
tag=getattr(heading, 'tag', None)))
counters[heading.level - 1] += 1
for i, entry in enumerate(entries, start=1):
logger.debug("Table of contents entry %i: %s", i, entry)
root.contents.insert(0, Heading(level=1, contents=[Text(text="Contents")]))
root.contents.insert(1, BlockLevelSequence(contents=entries))
def copy(node):
"""
Copy a subtree, breaking references to the old position in the tree.
"""
if isinstance(node, list):
return map(copy, node)
elif isinstance(node, Node):
attributes = {}
for name in dir(node):
value = getattr(node, name)
# Ignore private attributes.
if name.startswith('_'):
logger.debug("Ignoring private attribute: %s", name)
continue
# Ignore methods reported by 'dir'.
if isinstance(value, types.MethodType):
logger.debug("Ignoring non-attribute: %s", name)
continue
# Copy only child nodes, never parent nodes.
if 'parent' in name:
logger.debug("Ignoring parent node attribute: %s", name)
continue
# Copy attribute value.
logger.debug("Copying attribute %s ..", name)
attributes[name] = copy(getattr(node, name))
# Instantiate the new object.
return node.__class__(**attributes)
else:
return node
def prune_empty_blocks(root):
"""
Prune empty block level nodes from the tree.
"""
def recurse(node):
if isinstance(node, SequenceNode):
filtered_children = []
for child in node:
recurse(child)
if child:
filtered_children.append(child)
node.contents = filtered_children
recurse(root)
def make_parents_explicit(root):
"""
Add links from child nodes to parent nodes.
"""
def recurse(node, parent):
if isinstance(node, Node):
node.parent = parent
for child in getattr(node, 'contents', []):
recurse(child, node)
recurse(root, None)
def walk_tree(root, *node_types):
"""
Return a list of nodes (optionally filtered by type) ordered by the
original document order (i.e. the left to right, top to bottom reading
order of English text).
"""
ordered_nodes = []
def recurse(node):
if not (node_types and not isinstance(node, node_types)):
ordered_nodes.append(node)
for child in getattr(node, 'contents', []):
recurse(child)
recurse(root)
return ordered_nodes
# Objects to encapsulate output text with a bit of state.
# All classes defining __bool__ must also define __nonzero__ as __bool__ is only
# Python 3. Same for __str__ and __unicode__.
class OutputDelimiter(object):
"""
Trivial class to encapsulate output text (delimiters between the rendered
text of block level nodes) with a bit of state (to distinguish rendered
text from delimiters).
Note that most of the actual logic for handling of output delimiters is
currently contained in the function ``deduplicate_delimiters()``.
"""
def __init__(self, string):
self.string = string
def __str__(self):
return self.string
__unicode__ = __str__
def __repr__(self):
return "OutputDelimiter(string=%r)" % self.string
# Decorator for abstract syntax tree nodes.
def html_element(*element_names):
"""
Decorator to associate AST nodes and HTML nodes at the point where the AST
node is defined.
"""
def wrap(c):
for name in element_names:
name_to_type_mapping[name] = c
return c
return wrap
# Abstract parse tree nodes.
class Node(object):
"""
Abstract superclass for all parse tree nodes.
"""
def __init__(self, **kw):
"""
Short term hack for prototyping :-).
"""
if 'parent' not in kw:
kw['parent'] = None
self.__dict__ = kw
def __repr__(self):
"""
Dumb but useful representation of parse tree for debugging purposes.
"""
nodes = [repr(n) for n in self]
if not nodes:
contents = ""
elif len(nodes) == 1:
contents = nodes[0]
else:
contents = "\n" + ",\n".join(nodes)
return "%s(%s)" % (self.__class__.__name__, contents)
@property
def parents(self):
"""
Generator that yields all parents of a node.
"""
node = self
while node:
node = node.parent
yield node
class BlockLevelNode(Node):
"""
Abstract superclass for all block level parse tree nodes. Block level nodes
are the nodes which take care of indentation and line wrapping by
themselves.
"""
start_delimiter = OutputDelimiter('\n\n')
end_delimiter = OutputDelimiter('\n\n')
class InlineNode(Node):
"""
Abstract superclass for all inline parse tree nodes. Inline nodes are the
nodes which are subject to indenting and line wrapping by the block level
nodes that contain them.
"""
pass
class SequenceNode(Node):
"""
Abstract superclass for nodes that contain a sequence of zero or more other
nodes.
"""
@classmethod
def parse(cls, html_node):
"""
Default parse behavior: Just simplify any child nodes.
"""
return cls(contents=simplify_children(html_node))
def __bool__(self):
"""
Make it possible to determine whether a subtree contains
only whitespace.
"""
return any(bool(c) for c in self)
__nonzero__ = __bool__
def __len__(self):
"""
Needed by HyperLink.render().
"""
return len(self.contents)
def __iter__(self):
"""
Make it very simple to walk through the tree.
"""
return iter(self.contents)
# Concrete parse tree nodes.
class BlockLevelSequence(BlockLevelNode, SequenceNode):
"""
A sequence of one or more block level nodes.
"""
def render(self, **kw):
text = join_blocks(self.contents, **kw)
return [self.start_delimiter, text, self.end_delimiter]
@html_element('h1', 'h2', 'h3', 'h4', 'h5', 'h6')
class Heading(BlockLevelNode, SequenceNode):
"""
Block level node to represent headings. Maps to the HTML elements ``<h1>``
to ``<h6>``, however Vim help files have only two levels of headings so
during conversion some information about the structure of the original
document is lost.
"""
@staticmethod
def parse(html_node):
return Heading(level=int(html_node.name[1]),
contents=simplify_children(html_node))
def should_look_for_code(self):
nodes = walk_tree(self, Text)
return nodes[0].text == 'The '
def tag_heading(self, existing_tags, prefix):
if self.should_look_for_code():
# Look for a <code> element (indicating a source code
# entity) whose text has not yet been used as a tag.
matches = walk_tree(self, CodeFragment)
logger.debug("Found %i code fragments inside heading: %s", len(matches), matches)
for node in matches:
tag = create_tag(node.text, prefix=prefix, is_code=True)
logger.debug("Checking if %r (from %r) can be used as a tag ..", tag, node.text)
if tag not in existing_tags:
# Found a usable tag.
self.tag = tag
return tag
# Fall back to a tag generated from the heading's text.
text = join_inline(self.contents, indent=0)
tag = create_tag(text, prefix=prefix, is_code=False)
logger.debug("Checking if %r (from %r) can be used as a tag ..", tag, text)
if tag not in existing_tags:
self.tag = tag
return tag
def render(self, **kw):
logger.debug("Rendering heading: %s", self)
# We start with a line containing the marker symbol for headings,
# repeated on the full line. The symbol depends on the level.
lines = [('=' if self.level == 1 else '-') * TEXT_WIDTH]
# Render the heading's text.
text = join_inline(self.contents, **kw)
suffix = ' ~'
# Add a section tag?
if hasattr(self, 'tag'):
tag = "*%s*" % self.tag
if self.tag in text:
# If the heading references the tag literally, we'll just use
# that (instead of having to add a redundant tag).
text = text.replace(self.tag, tag)
# References to tags are actually invalid inside proper
# headings, but since we also added the marker line at the top
# of the heading, we can leave off the "~" symbol and forgo the
# additional highlighting.
suffix = ''
else:
# If we can't reference the tag literally, we'll add the
# section tag on the second line, aligned to the right.
prefix = ' ' * (TEXT_WIDTH - len(tag))
lines.append(prefix + tag)
# Prepare the prefix & suffix for each line, hard wrap the
# heading text and apply the prefix & suffix to each line.
prefix = ' ' * kw['indent']
width = TEXT_WIDTH - len(prefix) - len(suffix)
lines.extend(prefix + l + suffix for l in textwrap.wrap(text, width=width))
return [self.start_delimiter, "\n".join(lines), self.end_delimiter]
@html_element('p')
class Paragraph(BlockLevelNode, SequenceNode):
"""
Block level node to represent paragraphs of text.
Maps to the HTML element ``<p>``.
"""
def render(self, **kw):
# If the paragraph contains only an image (possible wrapped in another
# element) the paragraph is indented by a minimum of two spaces.
if len(self.contents) == 1 and len(walk_tree(self, Image)) == 1:
kw['indent'] = max(2, kw['indent'])
# Support code block inside paragraph.
rendered_node = [self.start_delimiter]
nodes = []
for node in self.contents:
if isinstance(node,
BlockLevelSequence) or isinstance(node,
PreformattedText):
rendered_node.append(join_inline(nodes, **kw))
nodes = []
rendered_node.extend(node.render(**kw))
continue
nodes.append(node)
if nodes:
rendered_node.append(join_inline(nodes, **kw))
if not isinstance(rendered_node[-1], OutputDelimiter):
rendered_node.append(self.end_delimiter)
return rendered_node
@html_element('pre')
class PreformattedText(BlockLevelNode):
"""
Block level node to represent preformatted text.
Maps to the HTML element ``<pre>``.
"""
# Vim help file markers for preformatted text.
start_delimiter = OutputDelimiter('\n>\n')
end_delimiter = OutputDelimiter('\n<\n')
@staticmethod
def parse(html_node):
# This is the easiest way to get all of the text in the preformatted
# block while ignoring HTML elements (what would we do with them?).
text = ''.join(html_node.findAll(text=True))
# Remove common indentation from the original text.
text = textwrap.dedent(text)
# Remove leading/trailing empty lines.
lines = text.splitlines()
while lines and not lines[0].strip():
lines.pop(0)
while lines and not lines[-1].strip():
lines.pop(-1)
return PreformattedText(text="\n".join(lines))
def __repr__(self):
return "PreformattedText(text=%r)" % self.text
def __bool__(self):
return self.text and not self.text.isspace()
__nonzero__ = __bool__
def render(self, **kw):
prefix = ' ' * max(kw['indent'], 2)
lines = self.text.splitlines()
text = "\n".join(prefix + line for line in lines)
return [self.start_delimiter, text, self.end_delimiter]
@html_element('ul', 'ol')
class List(BlockLevelNode, SequenceNode):
"""
Block level node to represent ordered and unordered lists.
Maps to the HTML elements ``<ol>`` and ``<ul>``.
"""
@staticmethod
def parse(html_node):
return List(ordered=(html_node.name=='ol'),
contents=simplify_children(html_node))
def render(self, **kw):
# First pass: Render the child nodes and pick the right delimiter.
items = []
delimiter = OutputDelimiter('\n')
num_lines = 0
for node in self.contents:
if isinstance(node, ListItem):
text = node.render(number=len(items) + 1, **kw)
items.append(text)
for x in text:
if isinstance(x, (str, unicode)):
num_lines += x.count('\n')
num_lines += 1
logger.debug("num_lines=%i, #items=%i, ratio=%.2f", num_lines, len(items), num_lines / float(len(items)))
if (num_lines / float(len(items))) > 1.5:
delimiter = OutputDelimiter('\n\n')
# Second pass: Combine the delimiters & rendered child nodes.
output = [self.start_delimiter]
for i, item in enumerate(items):
if i > 0:
output.append(delimiter)
output.extend(item)
output.append(self.end_delimiter)
return output
@html_element('li')
class ListItem(BlockLevelNode, SequenceNode):
"""
Block level node to represent list items.
Maps to the HTML element ``<li>``.
"""
def render(self, number, **kw):
# Get the original prefix (indent).
prefix = ' ' * kw['indent']
# Append the list item bullet.
if self.parent.ordered:
prefix += '%i. ' % number
else:
prefix += '- '
# Update indent for child nodes.
kw['indent'] = len(prefix)
# Render the child node(s).
text = join_smart(self.contents, **kw)
# Make sure we're dealing with a list of output delimiters and text.
if not isinstance(text, list):
text = [text]
# Ignore (remove) any leading output delimiters from the
# text (only when the delimiter itself is whitespace).
while text and isinstance(text[0], OutputDelimiter) and text[0].string.isspace():
text.pop(0)
# Remove leading indent from first text node.
if text and isinstance(text[0], (str, unicode)):
for i in range(len(prefix)):
if text[0] and text[0][0].isspace():
text[0] = text[0][1:]
# Prefix the list item bullet and return the result.
# XXX We explicitly *don't* add any output delimiters here, because
# they will be chosen *after* all of the list items have been rendered.
return [prefix] + text
@html_element('table')
class Table(BlockLevelSequence):
"""
Block level node to represent tabular data.
Maps to the HTML element ``<table>``.
Note: only supports trivial tables, such as those generated from markdown
and all rows are flattened to a single line.
Ultimately, the output is going to be wrong for any other type of table, or
there's going to be a python exception.
"""
def render(self, **kw):
widths={}
text = []
for node in walk_tree(self, TableRow):
node.calculate_widths(widths, **kw)
for i,node in enumerate(walk_tree(self, RowSequenceNode)):
text += node.render(i, widths, **kw)
return text
class RowSequenceNode(SequenceNode):
"""
Base node for collections of table rows
"""
pass
@html_element('thead')
class TableHeading(BlockLevelNode,RowSequenceNode):
"""
Block level node to represent tabular header data.
Maps to the HTML element ``<thead>``.
Renders all child rows using '=' for the line separator
"""
def render(self, section_index, widths, **kw):
text = []
for i,node in enumerate(walk_tree(self, TableRow)):
text += node.render(section_index + i, widths, '=', **kw)
return text