File: render-dctrl

package info (click to toggle)
python-debian 0.1.35
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,580 kB
  • sloc: python: 6,528; makefile: 230; sh: 14
file content (182 lines) | stat: -rwxr-xr-x 5,468 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python

# render-dctrl
# Copyright (C) 2009 Stefano Zacchiroli <zack@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# Requirements (Debian packages): python-debian python-markdown

from __future__ import print_function

usage = """Usage: render-dctrl [OPTION ...] [FILE ...]

Render a 822-like listing of Debian packages (AKA "Packages" file) to
XHTML, rendering (long) descriptions as Markdown text.  Render text
coming from FILEs, if given, or from standard input otherwise. Typical
usage is within a dctrl-tools pipeline, example:

  grep-available -s Package,Depends,Description ocaml | render-dctrl > foo.html

Warning: beware of #525525 and thus avoid using "-s Description" alone."""

import re
import string
import sys
from debian import deb822
from markdown import markdown
from optparse import OptionParser

options = None		# global, for cmdline options

css = """
body { font-family: sans-serif; }
dt {
  font-weight: bold;
}
dd {
  margin-bottom: 5pt;
}
div.package {
  border: solid 1pt;
  margin-top: 10pt;
  padding-left: 2pt;
  padding-right: 2pt;
}
.raw {
  font-family: monospace;
  background: #ddd;
  padding-left: 2pt;
  padding-right: 2pt;
}
.shortdesc {
  text-decoration: underline;
  margin-bottom: 5pt;
  display: block;
}
.longdesc {
  background: #eee;
}
span.package {
  font-family: monospace;
  font-size: 110%;
}
.uid {
  float: right;
  font-size: x-small;
  padding-right: 10pt;
}
"""
html_header = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">%s</style>
  </head>
  <body>
""" % css
html_trailer = """  </body>
</html>
"""

mdwn_list_line = re.compile(r'^(\s*)[\*\+\-]')	# Markdown list item line
# mdwn_head_line = re.compile(r'^(\s*)#')	# Markdown header
padding = re.compile(r'^(\s*)')

def get_indent(s):
    m = padding.match(s)
    if m:
        return len(m.group(1))
    else:
        return 0

def render_longdesc(lines):
    print('<div class="longdesc">')
    lines = map(lambda s: s[1:], lines)	# strip 822 heading space
    curpara, paragraphs = [], []
    inlist, listindent = False, 0
    store_para = lambda: paragraphs.append(string.join(curpara, '\n') + '\n')
    add_indent = lambda n, s: string.expandtabs('\t', n) + s

    for l in lines:	# recognize Markdown paragraphs
        if l.rstrip() == '.':	# RULE 1: split paragraphs at Debian's "."
            store_para()            
            curpara, inlist, listindent = [], False, 0
        else:
            if inlist:	# currently in a list
                if get_indent(l) <= listindent:	# RULE 3: leave list on underflow
                    store_para()
                    curpara, inlinst, linstindent = [l], False, 0
                else:	# the list goes on ...
                    curpara.append(l)
            else:	# currently not in a list
                if mdwn_list_line.match(l):	# new list start
                    if curpara:	# RULE 2: handle list item *not* at para start
                        store_para()
                    curpara, inlist, listindent = [l], True, get_indent(l)
                elif get_indent(l) >= 1:	# RULE 4: hande non-list verbatim
                    if curpara and get_indent(curpara[-1]) < 4:
                        store_para()
                        curpara = []
                    curpara.append(add_indent(3, l))
                else:
                    curpara.append(l)
    if curpara:
        store_para()

    for p in paragraphs:	# render paragraphs
        print(markdown(p))
    print('</div>')

def render_field(field, val):
    field = field.lower()
    print('<dt>%s</dt>' % field)
    print('<dd class="%s">' % field)
    if field == 'description':
        lines = val.split('\n')
        print('<span class="shortdesc">%s</span>' % lines[0])
        render_longdesc(lines[1:])
    elif field == 'package':
        print('<a href="#%s" class="uid">id</a>' % val)
        print('<span id="%s" class="package">%s</span>' % (val, val))
    elif field in []:	# fields not to be typeset as "raw"
        print('<span class="%s">%s</span>' % (field, val))
    else:
        print('<span class="raw">%s</span>' % val)
    print('</dd>')

def render_file(f):
    global options, html_header, html_trailer

    if options.print_header:
        print(html_header)
    for pkg in deb822.Packages.iter_paragraphs(f):
        print('<div class="package">')
        print('<dl class="fields">')
        for (field, val) in pkg.items():
            render_field(field, val)
        print('</dl>')
        print('</div>\n')
    if options.print_header:
        print(html_trailer)

def main():
    global options, usage

    parser = OptionParser(usage=usage)
    parser.add_option("-n", "--no-headers",
                      action="store_false", dest="print_header", default=True,
                      help="suppress printing of HTML header/trailer")
    (options, args) = parser.parse_args()
    if len(args):
        for fname in args:
            render_file(open(fname))
    else:
        render_file(sys.stdin)

if __name__ == '__main__':
    main()