summaryrefslogtreecommitdiffstats
path: root/helpcontent2/to-wiki/getalltitles.py
blob: ffa5e815f5026089b5266e305ee2147a8dc65006 (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
#!/usr/bin/env python
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import sys
import os
import xml.parsers.expat

alltitles=[]

def is_present(title):
    for i in alltitles:
        try:
            if i.strip() == title.strip():
                return True
        except:
            return False
    return False

def make_unique(title):
    n=0
    t = title
    while is_present(t):
        n=n+1
        t = title+"_%d"%(n)
    return t

replace_text_list = [
        ["$[officename]","LibreOffice"],
        ["%PRODUCTNAME","LibreOffice"],
        ["$PRODUCTNAME","LibreOffice"],
        ['"+"',"Plus"],
        ['"*"',"Star"],
        ['"-"',"Minus"],
        ['"/"',"Slash"],
        ['"^"',"Cap"],
        ['#',"No"],
        [')','_'],
        ['(','_'],
        [']','_'],
        ['[','_'],
        ['\\','_'],
        ['/','_'],
        ['&',"and"],
        [';','_']
        ]

replace_readable_list = [
        ["$[officename]","{{ProductName}}"],
        ["%PRODUCTNAME","{{ProductName}}"],
        ["$PRODUCTNAME","{{ProductName}}"]
        ]

modules_list = [
        ["sbasic","Basic"],
        ["scalc","Calc"],
        ["schart","Chart"],
        ["sdraw","Draw"],
        ["simpress","Impress"],
        ["smath","Math"],
        ["swriter","Writer"],
        ["shared","Common"]
        ]

def get_module(text):
    for i in modules_list:
        if text.find('/' + i[0] + '/') >=0:
            return i[1]
    return ""

def replace_text(text, replace_list):
    for i in replace_list:
        if text.find(i[0]) >= 0:
            text = text.replace(i[0],i[1])
    return text

def wiki_text(text):
    t = replace_text(text, replace_text_list)
    if t == '':
        t = 'LibreOffice' # hardcoded fallback
    return t.strip()

def readable_text(text):
    return replace_text(text, replace_readable_list)

class TitleParser:
    title = ''
    is_title = False

    def start_element(self, name, attrs):
        if name == 'title':
            self.is_title = True

    def end_element(self, name):
        if name == 'title':
            self.is_title = False

    def char_data(self, data):
        if self.is_title:
            self.title = self.title + data

    def get_title(self):
        return self.title.strip()

def parsexhp(filename):
    module = get_module(filename)
    if module == '':
        return

    parsing = True
    file=open(filename,"r")
    p = xml.parsers.expat.ParserCreate()
    tp = TitleParser()
    p.StartElementHandler = tp.start_element
    p.EndElementHandler = tp.end_element
    p.CharacterDataHandler = tp.char_data
    buf = file.read()
    try:
        p.Parse(buf)
    except:
        sys.stderr.write('Cannot parse %s, skipping it\n'% filename)
        file.close()
        return
    file.close()
    title = tp.get_title()
    if len(title) > 0:
        readable_title = readable_text(title)
        title = module + '/' + wiki_text(title)
        title = title.replace(' ', '_')
        title = title.replace('___', '_')
        title = title.replace('__', '_')
        title = title.strip('_')
        title = make_unique(title)
        alltitles.append(title)
        return((filename, title, readable_title))

# Main Function
def gettitles(path):
    pattern = "xhp"
    alltitles = []
    for root, dirs, files in os.walk(path):
        for i in files:
            if i.find(pattern) >= 0:
                t = parsexhp(root+"/"+i)
                if t is not None:
                    alltitles.append(t)
    return alltitles

# vim:set shiftwidth=4 softtabstop=4 expandtab: