translate.convert.po2html

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, usetidy=None):
     43          self.wrap = wrap
     44          self.tidy = tidy and usetidy
     45
     46 -    def wrapmessage(self, message):
     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
     52 -    def convertstore(self, inputstore, includefuzzy):
     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              #TODO: get the correct encoding
     69              htmlresult = htmlresult.decode('utf-8')
     70          # TODO: use the algorithm from html2po to get blocks and translate them individually
     71          # rather than using replace
     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                  # TODO: "msgid" is already html-encoded ("&" -> "&amp;"), while
     83                  #   "msgstr" is not encoded -> thus the replace fails
     84                  #   see test_po2html.py in line 67
     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