File: update_inlinedoc.py

package info (click to toggle)
python-pyahocorasick 1.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 748 kB
  • sloc: ansic: 4,554; python: 2,823; sh: 312; makefile: 242
file content (151 lines) | stat: -rw-r--r-- 3,780 bytes parent folder | download | duplicates (2)
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
from pathlib import Path
import sys
import os
import textwrap
import xml.etree.ElementTree as ET


def main():
    dstpath = Path('src/inline_doc.h')
    app = Application(dstpath)
    app.run()


HEADER = """#pragma once
// DO NOT EDIT. File generated by script update_inlinedoc.py.
"""


class Application(object):
    def __init__(self, dstpath):
        self.dstpath = dstpath


    def run(self):
        content = HEADER
        for path, name in self.__get_files():
            content += '\n' + self.__format_file(path, name)

        oldcontent = None
        if self.dstpath.exists():
            oldcontent = self.dstpath.read_text()

        if content != oldcontent:
            print("Creating %s" % self.dstpath)
            self.dstpath.write_text(content)


    def __format_file(self, path, name):
        print("Parsing %s" % path)
        cmd = 'rst2xml %s' % path
        xml = os.popen(cmd).read()
        f = Formatter(xml, name)
        return f.format()


    def __get_files(self):
        rootdir = Path('docs')
        for path in sorted(rootdir.glob("*.rst")):
            if path.name != 'index.rst':
                name = path.stem + '_doc'
                yield (path, name)


WIDTH = 60


class Formatter(object):
    def __init__(self, xml_string, name):
        self.xml  = ET.fromstring(xml_string)
        self.name = name


    def format(self):
        self.lines = []
        for node in next(self.xml.iter('document')):
            if node.tag == 'title':
                self.format_title(node)
            elif node.tag == 'paragraph':
                self.format_paragraph(node)
            elif node.tag == 'bullet_list':
                self.format_bullet_list(node)
            elif node.tag == 'section':
                break # do not add extra sections
            else:
                raise ValueError("tag '%s' not supported" % node.tag)


        return self.format_c_define()


    def format_title(self, node):
        self.lines.append(node.text)


    def format_paragraph(self, node):
        self.lines.append('')
        self.lines.extend(textwrap.wrap(self.normalize(node), width=WIDTH))


    def format_bullet_list(self, node):
        for list_item in node.iter('list_item'):
            for paragraph in list_item.iter('paragraph'):

                text = self.normalize(paragraph)
                lines = textwrap.wrap(text, width=(WIDTH - 2))
                for i, line in enumerate(lines):
                    if i == 0:
                        prefix = '- '
                    else:
                        prefix = '  '

                    self.lines.append(prefix + line)


    def normalize(self, node):
        t = ET.tostring(node, method='text', encoding='unicode')
        t = t.split()
        return ' '.join(t)


    def format_c_define(self):
        lines = []
        prevline = ''

        # 1. do preformatting
        for line in self.lines:
            line = line.rstrip()
            if line == '' and prevline == '':
                continue # compress multiple empty lines

            prevline = line

            line = line.replace(r'\\', r'\\\\')
            line = line.replace('"', r'\"')

            lines.append(line)

        # 2. remove empty lines from the end
        while lines:
            if lines[-1] == '':
                del lines[-1]
            else:
                break


        # 3. add qutations
        n = len(lines)
        indent = '\t'
        result = '#define %s \\\n' % self.name
        for i, line in enumerate(lines):
            result += indent
            if i < n - 1:
                result += '"%s\\n" \\\n' % line
            else:
                result += '"%s"\n' % line

        return result


if __name__ == '__main__':
    main()