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"""
44
46 """rewraps text as required"""
47 if self.wrap is None:
48 return message
49 return "\n".join([textwrap.fill(line, self.wrap, replace_whitespace=False) for line in message.split("\n")])
50
52 """converts a file to .po format"""
53 htmlresult = ""
54 for inputunit in inputstore.units:
55 if inputunit.isheader():
56 continue
57 if includefuzzy or not inputunit.isfuzzy():
58 htmlresult += self.wrapmessage(inputunit.target) + "\n" + "\n"
59 else:
60 htmlresult += self.wrapmessage(inputunit.source) + "\n" + "\n"
61 return htmlresult.encode('utf-8')
62
63 - def mergestore(self, inputstore, templatetext, includefuzzy):
64 """converts a file to .po format"""
65 htmlresult = templatetext.replace("\n", " ")
66 if isinstance(htmlresult, str):
67
68 htmlresult = htmlresult.decode('utf-8')
69
70
71 for inputunit in inputstore.units:
72 if inputunit.isheader():
73 continue
74 msgid = inputunit.source
75 msgstr = None
76 if includefuzzy or not inputunit.isfuzzy():
77 msgstr = self.wrapmessage(inputunit.target)
78 else:
79 msgstr = self.wrapmessage(inputunit.source)
80 if msgstr.strip():
81
82
83
84 htmlresult = htmlresult.replace(msgid, msgstr, 1)
85 htmlresult = htmlresult.encode('utf-8')
86 if tidy:
87 htmlresult = str(tidy.parseString(htmlresult))
88 return htmlresult
89
90 -def converthtml(inputfile, outputfile, templatefile, wrap=None, includefuzzy=False):
91 """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
92 inputstore = po.pofile(inputfile)
93 convertor = po2html(wrap=wrap)
94 if templatefile is None:
95 outputstring = convertor.convertstore(inputstore, includefuzzy)
96 else:
97 templatestring = templatefile.read()
98 outputstring = convertor.mergestore(inputstore, templatestring, includefuzzy)
99 outputfilepos = outputfile.tell()
100 outputfile.write(outputstring)
101 return 1
102
103 -def main(argv=None):
104 from translate.convert import convert
105 from translate.misc import stdiotell
106 import sys
107 sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
108 formats = {("po", "htm"):("htm", converthtml), ("po", "html"):("html", converthtml), ("po", "xhtml"):("xhtml", converthtml), ("po"):("html", converthtml)}
109 parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__)
110 if textwrap is not None:
111 parser.add_option("-w", "--wrap", dest="wrap", default=None, type="int",
112 help="set number of columns to wrap html at", metavar="WRAP")
113 parser.passthrough.append("wrap")
114 parser.add_fuzzy_option()
115 parser.run(argv)
116
117
118 if __name__ == '__main__':
119 main()
120