summaryrefslogtreecommitdiff
blob: fed852d27ff994187d73e2e364524efa3920d5f0 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#! /usr/bin/env python
# guidexml -- guidexml class for python
# Copyright 2009-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

import datetime
import os
import xml.etree.cElementTree

MAXLENGTH = 80

LANGUAGES = ['en', 'de']

LICENSE = ''.join(['<!-- The content of this document is licensed under the CC-BY-SA license -->\n',
'<!-- See http://creativecommons.org/licenses/by-sa/2.5 -->\n<license/>'])

TAGTYPES = ['p', 'c', 'b', 'e', 'i', 'pre', 'path', 'uri', 'note', 'warn', 'impo',
           'comment', 'sub', 'sup', 'keyword', 'ident', 'const', 'stmt', 'var']

#pre only in first body
#<pre caption="Code Sample">

class Document(object):
    title      = str()
    abstract   = str()
    docversion = str()
    encoding   = str()
    doctype    = str()
    date       = str()
    version    = str()
    license    = str()
    link       = str()
    lang       = str()

    chapters   = list()
    authors    = list()


    def __init__(self, title, version, abstract, lang, link, docversion='1.0',
        encoding='UTF-8', doctype='/dtd/guide.dtd', date=None, license=None):

        if type(title) is not str:
            raise TypeError
        if type(abstract) is not str:
            raise TypeError
        if type(docversion) is not str:
            raise TypeError
        if type(encoding) is not str:
            raise TypeError
        if type(doctype) is not str:
            raise TypeError
        if type(version) is not str:
            raise TypeError
        if type(date) is not str and date is not None:
            raise TypeError
        if type(license) is not str and license is not None:
            raise TypeError
        if type(link) is not str:
            raise TypeError
        if type(lang) is not str:
            raise TypeError
        if lang not in LANGUAGES:
            raise ValueError

        self.title = title
        self.abstract = abstract
        self.docversion = docversion
        self.encoding = encoding
        self.doctype = doctype
        self.version = version
        self.link = link
        self.lang = lang

        if date is None:
            self.date = datetime.datetime.now().isoformat()[:10]
        else:
            self.date = date #TODO check YYYY-MM-DD

        if license is None:
            self.license = LICENSE
        else:
            self.license = license


    def addAuthor(self, title, name, email=None):
        self.authors.append(Author(title, name, email))


    def append(self, chapter):
        if type(chapter) is not Chapter:
            raise TypeError
        else:
            self.chapters.append(chapter)


    def create(self, path, filename):
        xmlfile = os.path.join(path, filename) #TODO

        output = list()
        
        if len(self.authors) == 0:
            raise ValueError

        output.append('<?xml version="%s" encoding="%s"?>\n' % (self.docversion, self.encoding))
        output.append('<!DOCTYPE guide SYSTEM "%s">\n<!-- $Header$ -->\n\n' % (self.doctype))
        output.append('<guide>\n<title>%s</title>\n\n' % (self.title))

        for item in self.authors:
            output.append('<author title="%s">\n' % (item.title))
            if item.email is not None:
                output.append('  <mail link="%s">%s</mail>\n</author>' % (item.email, item.name))
            else:
                output.append('  %s\n</author>' % (item.name))

        output.append('\n\n<abstract>\n%s\n</abstract>' % (linebreak(self.abstract)))
        output.append('\n\n%s\n\n<version>%s</version>\n' % (self.license, self.version))
        output.append('<date>%s</date>\n\n' % (self.date))

        if len(self.chapters) > 0:
            for chapter in self.chapters:
                output.append('<chapter>\n')
                if len(chapter.sections) > 0:
                    for section in chapter.sections:
                        output.append('<section>\n')
                        output.append('<body>\n')
                        for tag in section.tags:
                            if tag.tagtype in ['pre', 'p']:
                                output.append('\n%s\n' % repr(tag))
                            else:
                                output.append(repr(tag))

                        output.append('\n</body>\n')
                        output.append('</section>\n')
                output.append('</chapter>\n')

        output.append('</guide>')
        
        print ''.join(output)


#TODO
def linebreak(text):
    if len(text) <= MAXLENGTH:
        return text

    linebreak = str()
    i = 0;
    while i < len(text):
        if i + MAXLENGTH < len(text):
            linebreak += ''.join([text[i:i + MAXLENGTH], '\n'])
        else:
            linebreak += text[i:i + MAXLENGTH]
        i += MAXLENGTH
    return linebreak


class Author(object):
    title = str()
    name  = str()
    email = str()

    def __init__(self, title, name, email=None):
        if type(title) is not str:
            raise TypeError
        if type(name) is not str:
            raise TypeError
        if type(email) is not str and email is not None:
            raise TypeError

        self.title = title
        self.name = name
        self.email = email


class Chapter(object):
    title    = None
    sections = list()

    def __init__(self, title):
        if type(title) is not str:
            raise TypeError

        self.title = title


    def append(self, section):
        if type(section) is not Section:
            raise TypeError
        else:
            self.sections.append(section)


class Section(object):
    title = None
    bodys = list()

    def __init__(self, title):
        if type(title) is not str:
            raise TypeError

        self.title = title


    tags = list()

    def append(self, tag):
        if type(tag) is Tag:
            self.tags.append(tag)
        elif type(tag) is list:
            for item in tag:
                self.append(item)
        elif type(tag) is str:
            self.tags.append(Tag('text', tag))
        else:
            raise TypeError


class Tag(object):
    tagtype = str()
    text    = str()

    def __init__(self, tagtype, text):
        if tagtype not in TAGTYPES:
            raise TypeError
        if type(text) is not str:
            raise TypeError

        self.tagtype = tagtype
        self.text = text

    def __repr__(self):
        if (len(self.tagtype) * 2 + 5 + len(self.text)) > MAXLENGTH:
            return '<%s>\n%s\n</%s>' % (self.tagtype, self.text, self.tagtype)
        else:
            return '<%s>%s</%s>' % (self.tagtype, self.text, self.tagtype)

def preserve(text):
    return Tag('pre', text)


def paragraph(text):
    return Tag('p', text)


def warning(text):
    return Tag('warn', text)


def important(text):
    return Tag('impo', text)


def note(text):
    return Tag('note', text)


def comment(text):
    return Tag('comment', text)


def path(text):
    return Tag('path', text)


def command(text):
    return Tag('c', text)


def userinput(text):
    return Tag('i', text)


def keyword(text):
    return Tag('keyword', text)


def identifier(text):
    return Tag('ident', text)


def constant(text):
    return Tag('const', text)


def statement(text):
    return Tag('stmt', text)


def variable(text):
    return Tag('var', text)


def bold(text):
    return Tag('b', text)


def emphasize(text):
    return Tag('e', text)


def subscript(text):
    return Tag('sub', text)


def superscript(text):
    return Tag('sup', text)


def uri(text):
    return Tag('uri', text)