Debian Codegraph @

examples/deb822/render-dctrl

raw
1 #!/usr/bin/python3
2
3 # render-dctrl
4 # Copyright (C) 2009 Stefano Zacchiroli <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # Requirements (Debian packages): python-debian python-markdown
12
13 usage = """Usage: render-dctrl [OPTION ...] [FILE ...]
14
15 Render a 822-like listing of Debian packages (AKA "Packages" file) to
16 XHTML, rendering (long) descriptions as Markdown text. Render text
17 coming from FILEs, if given, or from standard input otherwise. Typical
18 usage is within a dctrl-tools pipeline, example:
19
20 grep-available -s Package,Depends,Description ocaml | render-dctrl > foo.html
21
22 Warning: beware of #525525 and thus avoid using "-s Description" alone."""
23
24 import re
25 import string
26 import sys
27 from debian import deb822
28 from markdown import markdown
29 from optparse import OptionParser
30
31 options = None # global, for cmdline options
32
33 css = """
34 body { font-family: sans-serif; }
35 dt {
36 font-weight: bold;
37 }
38 dd {
39 margin-bottom: 5pt;
40 }
41 div.package {
42 border: solid 1pt;
43 margin-top: 10pt;
44 padding-left: 2pt;
45 padding-right: 2pt;
46 }
47 .raw {
48 font-family: monospace;
49 background: #ddd;
50 padding-left: 2pt;
51 padding-right: 2pt;
52 }
53 .shortdesc {
54 text-decoration: underline;
55 margin-bottom: 5pt;
56 display: block;
57 }
58 .longdesc {
59 background: #eee;
60 }
61 span.package {
62 font-family: monospace;
63 font-size: 110%;
64 }
65 .uid {
66 float: right;
67 font-size: x-small;
68 padding-right: 10pt;
69 }
70 """
71 html_header = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
72 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
73 <html xmlns="http://www.w3.org/1999/xhtml">
74 <head>
75 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
76 <style type="text/css">%s</style>
77 </head>
78 <body>
79 """ % css
80 html_trailer = """ </body>
81 </html>
82 """
83
84 mdwn_list_line = re.compile(r'^(\s*)[\*\+\-]') # Markdown list item line
85 # mdwn_head_line = re.compile(r'^(\s*)#') # Markdown header
86 padding = re.compile(r'^(\s*)')
87
88 def get_indent(s):
89 m = padding.match(s)
90 if m:
91 return len(m.group(1))
92 else:
93 return 0
94
95 def render_longdesc(lines):
96 print('<div class="longdesc">')
97 lines = map(lambda s: s[1:], lines) # strip 822 heading space
98 curpara, paragraphs = [], []
99 inlist, listindent = False, 0
100 store_para = lambda: paragraphs.append(string.join(curpara, '\n') + '\n')
101 add_indent = lambda n, s: string.expandtabs('\t', n) + s
102
103 for l in lines: # recognize Markdown paragraphs
104 if l.rstrip() == '.': # RULE 1: split paragraphs at Debian's "."
105 store_para()
106 curpara, inlist, listindent = [], False, 0
107 else:
108 if inlist: # currently in a list
109 if get_indent(l) <= listindent: # RULE 3: leave list on underflow
110 store_para()
111 curpara, inlinst, linstindent = [l], False, 0
112 else: # the list goes on ...
113 curpara.append(l)
114 else: # currently not in a list
115 if mdwn_list_line.match(l): # new list start
116 if curpara: # RULE 2: handle list item *not* at para start
117 store_para()
118 curpara, inlist, listindent = [l], True, get_indent(l)
119 elif get_indent(l) >= 1: # RULE 4: hande non-list verbatim
120 if curpara and get_indent(curpara[-1]) < 4:
121 store_para()
122 curpara = []
123 curpara.append(add_indent(3, l))
124 else:
125 curpara.append(l)
126 if curpara:
127 store_para()
128
129 for p in paragraphs: # render paragraphs
130 print(markdown(p))
131 print('</div>')
132
133 def render_field(field, val):
134 field = field.lower()
135 print('<dt>%s</dt>' % field)
136 print('<dd class="%s">' % field)
137 if field == 'description':
138 lines = val.split('\n')
139 print('<span class="shortdesc">%s</span>' % lines[0])
140 render_longdesc(lines[1:])
141 elif field == 'package':
142 print('<a href="#%s" class="uid">id</a>' % val)
143 print('<span id="%s" class="package">%s</span>' % (val, val))
144 elif field in []: # fields not to be typeset as "raw"
145 print('<span class="%s">%s</span>' % (field, val))
146 else:
147 print('<span class="raw">%s</span>' % val)
148 print('</dd>')
149
150 def render_file(f):
151 global options, html_header, html_trailer
152
153 if options.print_header:
154 print(html_header)
155 for pkg in deb822.Packages.iter_paragraphs(f):
156 print('<div class="package">')
157 print('<dl class="fields">')
158 for (field, val) in pkg.items():
159 render_field(field, val)
160 print('</dl>')
161 print('</div>\n')
162 if options.print_header:
163 print(html_trailer)
164
165 def main():
166 global options, usage
167
168 parser = OptionParser(usage=usage)
169 parser.add_option("-n", "--no-headers",
170 action="store_false", dest="print_header", default=True,
171 help="suppress printing of HTML header/trailer")
172 (options, args) = parser.parse_args()
173 if len(args):
174 for fname in args:
175 render_file(open(fname))
176 else:
177 render_file(sys.stdin)
178
179 if __name__ == '__main__':
180 main()