1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 """convert Gettext PO localization files to HTML files
24
25 see: http://translate.sourceforge.net/wiki/toolkit/po2html for examples and
26 usage instructions
27 """
28
29 from translate.storage import po
30 try:
31 import textwrap
32 except:
33 textwrap = None
34
35 try:
36 import tidy
37 except:
38 tidy = None
39
41 """po2html can take a po file and generate html. best to give it a template file otherwise will just concat msgstrs"""
42 - def __init__(self, wrap=None, usetidy=None):
45
47 """rewraps text as required"""
48 if self.wrap is None:
49 return message
50 return "\n".join([textwrap.fill(line, self.wrap, replace_whitespace=False) for line in message.split("\n")])
51
53 """converts a file to .po format"""
54 htmlresult = ""
55 for inputunit in inputstore.units:
56 if inputunit.isheader():
57 continue
58 if includefuzzy or not inputunit.isfuzzy():
59 htmlresult += self.wrapmessage(inputunit.target) + "\n" + "\n"
60 else:
61 htmlresult += self.wrapmessage(inputunit.source) + "\n" + "\n"
62 return htmlresult.encode('utf-8')
63
64 - def mergestore(self, inputstore, templatetext, includefuzzy):
65 """converts a file to .po format"""
66 htmlresult = templatetext.replace("\n", " ")
67 if isinstance(htmlresult, str):
68
69 htmlresult = htmlresult.decode('utf-8')
70
71
72 for inputunit in inputstore.units:
73 if inputunit.isheader():
74 continue
75 msgid = inputunit.source
76 msgstr = None
77 if includefuzzy or not inputunit.isfuzzy():
78 msgstr = self.wrapmessage(inputunit.target)
79 else:
80 msgstr = self.wrapmessage(inputunit.source)
81 if msgstr.strip():
82
83
84
85 htmlresult = htmlresult.replace(msgid, msgstr, 1)
86 htmlresult = htmlresult.encode('utf-8')
87 if self.tidy:
88 htmlresult = str(tidy.parseString(htmlresult))
89 return htmlresult
90
91 -def converthtml(inputfile, outputfile, templatefile, wrap=None, includefuzzy=False, usetidy=True):
92 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
93 inputstore = po.pofile(inputfile)
94 convertor = po2html(wrap=wrap, usetidy=usetidy)
95 if templatefile is None:
96 outputstring = convertor.convertstore(inputstore, includefuzzy)
97 else:
98 templatestring = templatefile.read()
99 outputstring = convertor.mergestore(inputstore, templatestring, includefuzzy)
100 outputfilepos = outputfile.tell()
101 outputfile.write(outputstring)
102 return 1
103
104 -def main(argv=None):
105 from translate.convert import convert
106 from translate.misc import stdiotell
107 import sys
108 sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
109 formats = {("po", "htm"):("htm", converthtml), ("po", "html"):("html", converthtml), ("po", "xhtml"):("xhtml", converthtml), ("po"):("html", converthtml)}
110 parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
111 if textwrap is not None:
112 parser.add_option("-w", "--wrap", dest="wrap", default=None, type="int",
113 help="set number of columns to wrap html at", metavar="WRAP")
114 parser.passthrough.append("wrap")
115 if tidy is not None:
116 parser.add_option("", "--notidy", dest="usetidy", default=True,
117 help="disables the use of HTML tidy", action="store_false")
118 parser.passthrough.append("usetidy")
119 parser.add_fuzzy_option()
120 parser.run(argv)
121
122
123 if __name__ == '__main__':
124 main()
125