aboutsummaryrefslogtreecommitdiffstats
path: root/readme_generate.py
blob: 126d62e485ab8bb6cf64f009a0cd8bd1e7dd006a (plain) (blame)
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
#! /usr/bin/env python

import sys;
import os;

class Reader():
    def __init__(self, suffix, stp):
        self.suffix = suffix;
        self._stop  = stp;
    #
    def checkOk(self, pathname):
        for suffix in self.suffix:
            if pathname.endswith(suffix):
                return True;
        return False;
    def read(self, pathname):
        f = open(pathname, 'r');
        input_string = f.read();
        f.close();
        return self.getOutputString(input_string);
    def getOutputString(self, input_string):
        return ''
    def stop(self):
        return self._stop;
#
class AsciidocReader(Reader):
    def __init__(self):
        Reader.__init__(self, ['.asciidoc',
                               '.adoc',
                               '.ascii',
                              ],
                       True);
    def getOutputString(self, input_string):
        return input_string;
#
class InReader(Reader):
    def __init__(self, suffix, start_string, end_string):
        Reader.__init__(self, suffix, False)
        self.start_string = start_string
        self.  end_string =   end_string
    def getOutputString(self, input_string):
        start_index = 0
        ret = ''
        while True:
            start = input_string.find(self.start_string, start_index)
            if start == -1:
                break
            end = input_string.find(self.end_string,
                                    start + len(self.start_string))
            if end == -1:
                break
            start_index = end + len(self.end_string)
            ret += input_string[start + len(self.start_string) : end]
        index = 0
        while index < len(ret):
            if index == 0 or ret[index - 1] == "\n":
                if ret[index] == ' ' or ret == "\t":
                    ret = ret[:index] + ret[index + 1:]
                else:
                    index += 1
            else:
                index += 1
        return ret
#
class InLineReader(Reader):
    def __init__(self, suffix, prefix):
        Reader.__init__(self, suffix, False)
        self.prefix = prefix
    def getOutputString(self, input_string):
        ret = ''
        line_begin = 0
        while line_begin < len(input_string):
            line_end = line_begin
            for line_end in range(line_begin, len(input_string) + 1):
                if input_string[line_end] == "\n":
                    break
            ok = False
            for i in range(line_begin, line_end - len(self.prefix) + 1):
                if input_string[i : i + len(self.prefix)] == self.prefix:
                    ok = True
                    break
            if ok:
                start = i + len(self.prefix)
                while start < line_end:
                    if input_string[start]!=' ' and input_string[start]!="\t":
                        break
                    start += 1
                ret += input_string[start: line_end].replace('\\', " +\n")+"\n"
            line_begin = line_end + 1
        return ret;
#
class CppReader(InReader):
    def __init__(self):
        InReader.__init__(self,
                          ['.c', '.cpp', '.h', '.hpp'],
                          '@asciidoc',
                          '@asciidoc-')
class CppLineReader(InLineReader):
    def __init__(self):
        InLineReader.__init__(self, ['.c', '.cpp', '.h', '.hpp'], '//#');
#
readers = [AsciidocReader(),
           CppReader(),
           CppLineReader(),
          ];

if len(sys.argv) <= 1: readme = 'README.asciidoc';
else                 : readme = sys.argv[1];

readme_f = open(readme, 'w');
footer_n = 'footer';

footer_lst = [];

for (root, sub_folders, files) in os.walk('./'):
    files.sort();
    sub_folders.sort();
    for reader in readers:
        deleted = []
        tmp1 = []
        tmp2 = []
        for filename in files:
            if filename.find(footer_n) == -1:
                tmp1.append(filename)
            else:
                if not os.path.join(root, filename) in footer_lst:
                    footer_lst.append(os.path.join(root, filename))
        files = tmp1
        for filename in files:
            path = os.path.join(root, filename)
            if path == './' + readme:
                continue
            if reader.checkOk(filename):
                s = reader.read(path)
                if len(s) > 0:
                    print 'Get asciidoc from ' + path
                    dir_name = ''
                    dirs_ct = 0
                    for i in range(0, len(root)):
                        if root[i] == '/':
                            dirs_ct += 1
                            dir_name = root[i + 1:] + '/'
                    if dir_name != '/':
                        readme_f.write('\n');
                        readme_f.write('=' * (dirs_ct + 2) + ' ' + dir_name);
                        readme_f.write('\n');
                    readme_f.write(s)
                if reader.stop():
                    deleted.append(filename)
        for filename in deleted:
            files.remove(filename)
for reader in readers:
    deleted = []
    for path in footer_lst:
        if path == './' + readme:
            continue
        if reader.checkOk(path):
            s = reader.read(path)
            if len(s) > 0:
                print 'Get asciidoc from ' + path
            readme_f.write(s)
            if reader.stop():
                deleted.append(path)
    for filename in deleted:
        footer_lst.remove(filename)
readme_f.close()