Package translate :: Package convert :: Module po2html
[hide private]
[frames] | no frames]

Source Code for Module translate.convert.po2html

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  # Copyright 2004-2006 Zuza Software Foundation 
  5  #  
  6  # This file is part of translate. 
  7  # 
  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. 
 12  #  
 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. 
 17  # 
 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 
 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   
40 -class po2html:
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):
43 self.wrap = wrap
44
45 - def wrapmessage(self, message):
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
51 - def convertstore(self, inputstore, includefuzzy):
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 #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(): 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 # 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') 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