2 # -*- coding: utf-8 -*-
4 # Copyright 2004-2006 Zuza Software Foundation
6 # This file is part of translate.
8 # translate is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # translate is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with translate; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 """convert Gettext PO localization files to HTML files
25 see: http://translate.sourceforge.net/wiki/toolkit/po2html for examples and
29 from translate
.storage
import po
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):
45 def wrapmessage(self
, message
):
46 """rewraps text as required"""
49 return "\n".join([textwrap
.fill(line
, self
.wrap
, replace_whitespace
=False) for line
in message
.split("\n")])
51 def convertstore(self
, inputstore
, includefuzzy
):
52 """converts a file to .po format"""
54 for inputunit
in inputstore
.units
:
55 if inputunit
.isheader():
57 if includefuzzy
or not inputunit
.isfuzzy():
58 htmlresult
+= self
.wrapmessage(inputunit
.target
) + "\n" + "\n"
60 htmlresult
+= self
.wrapmessage(inputunit
.source
) + "\n" + "\n"
61 return htmlresult
.encode('utf-8')
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 #TODO: get the correct encoding
68 htmlresult
= htmlresult
.decode('utf-8')
69 # TODO: use the algorithm from html2po to get blocks and translate them individually
70 # rather than using replace
71 for inputunit
in inputstore
.units
:
72 if inputunit
.isheader():
74 msgid
= inputunit
.source
76 if includefuzzy
or not inputunit
.isfuzzy():
77 msgstr
= self
.wrapmessage(inputunit
.target
)
79 msgstr
= self
.wrapmessage(inputunit
.source
)
81 # TODO: "msgid" is already html-encoded ("&" -> "&"), while
82 # "msgstr" is not encoded -> thus the replace fails
83 # see test_po2html.py in line 67
84 htmlresult
= htmlresult
.replace(msgid
, msgstr
, 1)
85 htmlresult
= htmlresult
.encode('utf-8')
87 htmlresult
= str(tidy
.parseString(htmlresult
))
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
)
97 templatestring
= templatefile
.read()
98 outputstring
= convertor
.mergestore(inputstore
, templatestring
, includefuzzy
)
99 outputfilepos
= outputfile
.tell()
100 outputfile
.write(outputstring
)
104 from translate
.convert
import convert
105 from translate
.misc
import stdiotell
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()
118 if __name__
== '__main__':