File: miaxml2man.py

package info (click to toggle)
mia 2.4.7-17
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,420 kB
  • sloc: cpp: 162,689; python: 1,322; sh: 321; xml: 127; makefile: 42; csh: 24; ansic: 9
file content (173 lines) | stat: -rw-r--r-- 5,356 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3 
#
# Copyright (c) Leipzig, Madrid 1999-2015 Gert Wollny
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# this program is used to translate the XML files obtained by running a mia-* program 
# into a Unix man page. 

import sys
import time
import calendar
import string

python_version = sys.version_info.major
if python_version >= 3:
    from html.entities import name2codepoint
else:
    from htmlentitydefs  import name2codepoint

import re

sys.dont_write_bytecode = True

modules = {'miareadxml' : [0, '', 'none://miareadxml.py' ]
           }


from miareadxml import parse_file

def get_date_string():
    lt = time.localtime(time.time())
    return "{} {} {}".format (lt.tm_mday, calendar.month_name[lt.tm_mon], lt.tm_year)

#taken from http://effbot.org/zone/re-sub.htm#unescape-html
    
def unescape(text):
    def fixup(m):
        text = m.group(0)
        if text[:2] == "&#":
            # character reference
            try:
                if text[:3] == "&#x":
                    return unichr(int(text[3:-1], 16))
                else:
                    return unichr(int(text[2:-1]))
            except ValueError:
                pass
        else:
            # named entity
            try:
                text = unichr(name2codepoint[text[1:-1]])
            except KeyError:
                pass
        return text # leave as is
    return re.sub("&#?\w+;", fixup, text)

def escape_dash(text): 
    return re.sub(r'-', r'\-', text) 

def clean (text):
    text = unescape(text)
    return escape_dash(text)

def print_description(text):
    doi_split = re.split(r'(\[[^\]]*\]\([^)]*\))', text)
    reg = re.compile(r'\[([^\]]*)\]\(([^)]*)\)')
    for d in doi_split:
        if len(d) == 0:
            continue
        if d[0] == "[":
            link = reg.split(d)
            print("")
            print(".RS")
            print(".UR {}".format(link[2]))
            print(link[1])
            print(".UE") 
            print(".RE")
            print("")
        else:
            print(d)

def write_man_file(descr):
    name = escape_dash(descr.name)
    print(".TH {} 1 \"v{}\"  \"USER COMMANDS\"".format(escape_dash(descr.name), descr.version))
    print(".SH NAME")
    print(name,) 
    print("\- {}".format (clean(descr.whatis)))
    print(".SH SYNOPSIS")
    print(".B {}".format (clean(descr.basic_usage)))
    print(".SH DESCRIPTION")
    print(".B {}".format (name))
    print_description(descr.description)
    print(".SH OPTIONS")
    for g in descr.option_groups:
        if len(g.name) > 0:
            print(".SS {}".format(g.name))

        print(".RS")
        for o in g.options:
            o.print_man()
        print(".RE")
    
    handlerkeys = sorted(descr.handlers.keys())
    for k in handlerkeys: 
        h = descr.handlers[k]
        print(".SH PLUGINS: {}".format (h.name))
        for p in h.plugins:
            print(".TP 10")
            print(".B {}".format(p.name))
            if len(p.params) > 0: 
                print("{}, supported parameters are: ".format (p.text))
                print(".P")
                for o in p.params:
                    print(".RS 14")
                    o.print_man()
                    print(".RE")
            else:
                print(p.text)
                print(".P")
                if not p.no_params_info: 
                    print(".RS 14")
                    print("(no parameters)")
                    print(".RE")
                    
            if p.suffixes is not None:
                print(".RS 14")
                print("Recognized file extensions: ", p.suffixes)
                print(".RE")
                print(" ")

            if p.supported_types is not None:
                print(".RS 14")
                print("Supported element types: ")
                print(".RS 2")
                print(p.supported_types)
                print(".RE")
                print(" ")
                print(".RE")


    if descr.Example.text is not None and len(descr.Example.text) > 0:
            print(".SH EXAMPLE")
            print(clean(descr.Example.text))
            for c in descr.Example.code:
                print(".HP")
                print("{} {}".format (name, clean(c.text)))

    print(".SH AUTHOR(s)")
    print(clean(descr.author))
    
    print(".SH COPYRIGHT")
    print("""This software is Copyright (c) 1999\(hy2015 Leipzig, Germany and Madrid, Spain.
It comes  with  ABSOLUTELY  NO WARRANTY  and  you  may redistribute it under the terms of the GNU 
GENERAL PUBLIC LICENSE Version 3 (or later). For more 
information run the program with the option '\-\-copyright'.""")


X=parse_file(sys.argv[1])
write_man_file(X)