Package translate :: Package convert :: Module html2po

[hide private]
[frames] | no frames]

# Source Code for Module translate.convert.html2po

     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 HTML files to Gettext PO localization files
    24
    25  See: http://translate.sourceforge.net/wiki/toolkit/html2po for examples and
    26  usage instructions
    27  """
    28
    29  from translate.storage import po
    30  from translate.storage import html
    31
    32 -class html2po:
    33 -    def convertfile(self, inputfile, filename, includeheader, includeuntagged=False, duplicatestyle="msgctxt", keepcomments=False):
    34          """converts a html file to .po format"""
    35          thetargetfile = po.pofile()
    36          htmlparser = html.htmlfile(includeuntaggeddata=includeuntagged, inputfile=inputfile)
    37          if includeheader:
    38              targetheader = thetargetfile.init_headers(charset="UTF-8", encoding="8bit")
    39          for htmlunit in htmlparser.units:
    40              thepo = thetargetfile.addsourceunit(htmlunit.source)
    41              thepo.addlocations(htmlunit.getlocations())
    42              if keepcomments:
    43                  thepo.addnote(htmlunit.getnotes(), "developer")
    44          thetargetfile.removeduplicates(duplicatestyle)
    45          return thetargetfile
    46
    47 -def converthtml(inputfile, outputfile, templates, includeuntagged=False, pot=False, duplicatestyle="msgctxt", keepcomments=False):
    48      """reads in stdin using fromfileclass, converts using convertorclass, writes to stdout"""
    49      convertor = html2po()
    50      outputfilepos = outputfile.tell()
    51      includeheader = outputfilepos == 0
    52      outputstore = convertor.convertfile(inputfile, getattr(inputfile, "name", "unknown"), includeheader, includeuntagged, duplicatestyle=duplicatestyle, keepcomments=keepcomments)
    53      outputfile.write(str(outputstore))
    54      return 1
    55
    56 -def main(argv=None):
    57      from translate.convert import convert
    58      from translate.misc import stdiotell
    59      import sys
    60      sys.stdout = stdiotell.StdIOWrapper(sys.stdout)
    61      formats = {"html":("po", converthtml), "htm":("po", converthtml), "xhtml":("po", converthtml), None:("po", converthtml)}
    62      parser = convert.ConvertOptionParser(formats, usepots=True, description=__doc__)
    63      parser.add_option("-u", "--untagged", dest="includeuntagged", default=False, action="store_true",
    64              help="include untagged sections")
    65      parser.passthrough.append("includeuntagged")
    66      parser.add_option("--keepcomments", dest="keepcomments", default=False, action="store_true",
    67              help="preserve html comments as translation notes in the output")
    68      parser.passthrough.append("keepcomments")
    69      parser.add_duplicates_option()
    70      parser.passthrough.append("pot")
    71      parser.run(argv)
    72
    73
    74  if __name__ == '__main__':
    75      main()
    76