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                  htmlresult = htmlresult.replace(msgid, msgstr, 1) 
 82          htmlresult = htmlresult.encode('utf-8') 
 83          if tidy: 
 84              htmlresult = str(tidy.parseString(htmlresult)) 
 85          return htmlresult 
   86   
 87 -def converthtml(inputfile, outputfile, templatefile, wrap=None, includefuzzy=False): 
  88      """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout""" 
 89      inputstore = po.pofile(inputfile) 
 90      convertor = po2html(wrap=wrap) 
 91      if templatefile is None: 
 92          outputstring = convertor.convertstore(inputstore, includefuzzy) 
 93      else: 
 94          templatestring = templatefile.read() 
 95          outputstring = convertor.mergestore(inputstore, templatestring, includefuzzy) 
 96      outputfilepos = outputfile.tell() 
 97      outputfile.write(outputstring) 
 98      return 1 
  99   
100 -def main(argv=None): 
 101      from translate.convert import convert 
102      from translate.misc import stdiotell 
103      import sys 
104      sys.stdout = stdiotell.StdIOWrapper(sys.stdout) 
105      formats = {("po", "htm"):("htm",converthtml), ("po", "html"):("html",converthtml), ("po", "xhtml"):("xhtml",converthtml), ("po"):("html",converthtml)} 
106      parser = convert.ConvertOptionParser(formats, usetemplates=True, description=__doc__) 
107      if textwrap is not None: 
108          parser.add_option("-w", "--wrap", dest="wrap", default=None, type="int", 
109                  help="set number of columns to wrap html at", metavar="WRAP") 
110          parser.passthrough.append("wrap") 
111      parser.add_fuzzy_option() 
112      parser.run(argv) 
 113   
114   
115  if __name__ == '__main__': 
116      main() 
117