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 html
    30  from translate.storage import po
    31
    32
    33 -class po2html:
    34      """po2html can take a po file and generate html. best to give it a
    35      template file otherwise will just concat msgstrs"""
    36
    37 -    def lookup(self, string):
    38          unit = self.inputstore.sourceindex.get(string, None)
    39          if unit is None:
    40              return string
    41          unit = unit[0]
    42          if self.includefuzzy or not unit.isfuzzy():
    43              return unit.target
    44          else:
    45              return None
    46
    47 -    def mergestore(self, inputstore, templatetext, includefuzzy):
    48          """converts a file to .po format"""
    49          self.inputstore = inputstore
    50          self.inputstore.makeindex()
    51          self.includefuzzy = includefuzzy
    52          output_store = html.htmlfile(inputfile=templatetext, callback=self.lookup)
    53          return output_store.filesrc
    54
    55
    56 -def converthtml(inputfile, outputfile, templatefile, includefuzzy=False):
    57      """reads in stdin using fromfileclass, converts using convertorclass,
    58      writes to stdout"""
    59      inputstore = po.pofile(inputfile)
    60      convertor = po2html()
    61      if templatefile is None:
    62          raise ValueError("must have template file for HTML files")
    63      else:
    64          outputstring = convertor.mergestore(inputstore, templatefile,
    65                                              includefuzzy)
    66      outputfilepos = outputfile.tell()
    67      outputfile.write(outputstring.encode('utf-8'))
    68      return 1
    69
    70
    71 -def main(argv=None):
    72      from translate.convert import convert
    73      from translate.misc import stdiotell
    74      import sys
    75      sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
    76      formats = {("po", "htm"): ("htm", converthtml),
    77                 ("po", "html"): ("html", converthtml),
    78                 ("po", "xhtml"): ("xhtml", converthtml),
    79                 ("po"): ("html", converthtml),
    80                }
    81      parser = convert.ConvertOptionParser(formats, usetemplates=True,
    82                                           description=__doc__)
    83      parser.add_fuzzy_option()
    84      parser.run(argv)
    85
    86
    87  if __name__ == '__main__':
    88      main()
    89