summaryrefslogtreecommitdiff
path: root/misc/genFAQ.py
blob: 5306cef7aa695ce926b5007e474c7a89e6902a17 (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
#!/usr/bin/env python
# Quick and dirty script to generate googlecode wiki pages

import codecs
import xml.dom.minidom as dom
import os.path

faqpath = "/Users/arne/oss/ics-openvpn.wiki"

header="""
<wiki:comment>
This page is autogenerated. Do not edit
</wiki:comment>

= Frequently aksed questions =
"""

def getString(strid,lang):
    ostr=""
    if strid in strres[lang]:
        ostr=strres[lang][strid]
    else:
        ostr=strres["default"][strid]

    ostr = ostr.replace("&lt;","<")
    ostr = ostr.replace("&gt;",">")
    ostr = ostr.replace("\\\"","\"")
    ostr = ostr.replace("\\'","'")
    ostr = ostr.replace("\\n","<p>")
    return ostr

def genPage(faqdom,lang):
    out =""
    
    out+="#summary %s\n" % getString("faq_summary",lang)
    out+= header

    for xmld in faqdom.firstChild.childNodes:
        for xmle in xmld.childNodes:
            if xmle.nodeName == "TextView":
                style =  xmle.getAttribute("style")

                textstyle = None
                if style == "@style/faqhead":
                    textstyle = "== %s ==\n"
                elif style == "@style/faqitem":
                    textstyle = "%s\n"

                atext = xmle.getAttribute("android:text")
                aid = xmle.getAttribute("android:id")
                if atext:
                    atextid = atext.replace("@string/","")
                else:
                    atextid = aid.replace("@+id/","")

                out += textstyle % getString(atextid,lang)

    return out
            
	
strres={}

def loadstrres(filename,lang):
    xmlstr = dom.parse(filename)
    strres[lang]={}
    for xmld in xmlstr.childNodes:
        for xmle in xmld.childNodes:
            if xmle.nodeName == "string":
                strname= xmle.getAttribute("name")
                strdata = xmle.firstChild.data
                strres[lang][strname]=strdata
    

def main():
    
    loadstrres("res/values/strings.xml","default")
    
    faqdom = dom.parse("res/layout/faq.xml")
    faq= genPage(faqdom,"default")

    open(faqpath + "/FAQ.wiki","w").write(faq)

    for directory in os.listdir("res"):
        if directory.startswith("values-") and directory.find("-sw")==-1:
            lang = directory.split("-",1)[1]
            print lang
            loadstrres("res/values-%s/strings.xml" % lang,lang)

            langdir= "%s/%s" %(faqpath,lang)
            if lang=="zh-rCN":
                langdir= "%s/%s" %(faqpath,"zh-Hans")
            elif lang=="zh-rTW":
                langdir= "%s/%s" %(faqpath,"zh-Hant")

                
            if not os.path.exists(langdir):
                os.mkdir(langdir)

            faq= genPage(faqdom,lang)
            open("%s/FAQ.wiki" % langdir,"w").write(faq.encode("utf-8"))


if __name__=="__main__":
    main()