Esempio n. 1
0
def main():
  try:
    import argparse
  except ImportError:
    print "argparse is needed for commandline"
    return 1

  parser = argparse.ArgumentParser(description='Converts a module directory to an xhtml file and additional images')
  parser.add_argument('directory')
  parser.add_argument('-i', dest='module_id', help='Published Module id')
  parser.add_argument('-c', dest='css_file', help='CSS File to include')# , type=argparse.FileType('r'))
  parser.add_argument('-e', dest='epub_script', help='Name of XSL file that generates an epub from a dbk file (should look like "./xsl/dbk2___.xsl")')
  parser.add_argument('-r', dest='reduce_quality', help='Reduce image quality', action='store_true')
  parser.add_argument('-t', dest='content_type', help='The type of content being converted. One of ["module", "collection"]')
  # parser.add_argument('-t', dest='temp_dir', help='Path to store temporary files to (default is a temp dir that will be removed)', nargs='?')
  parser.add_argument('-o', dest='output', nargs='?') # , type=argparse.FileType('w'), default=sys.stdout)
  args = parser.parse_args()

  epubXsl = args.epub_script
  if not epubXsl:
    epubXsl = DEFAULT_DOCBOOK2XHTML_XSL

  temp_dir = args.directory

  p = util.Progress()

  if args.content_type == 'module':
    cnxml, allFiles = util.loadModule(args.directory)
    dbk, newFiles = module2dbk.convert(args.module_id, cnxml, allFiles, {}, temp_dir, svg2png=True, math2svg=True, reduce_quality=args.reduce_quality)
    dbk = newFiles['index.standalone.dbk']
    allFiles.update(newFiles)
    cover, newFiles = util.dbk2cover(etree.parse(StringIO(dbk)), allFiles, svg2pngFlag=True)
    newFiles['cover.png'] = cover


  elif args.content_type == 'collection':
    p = util.Progress()
    collxml, modulesDict, allFiles = util.loadCollection(args.directory)

    dbk, newFiles = collection2dbk.convert(p, collxml, modulesDict, temp_dir, svg2png=True, math2svg=True, reduce_quality=args.reduce_quality)
    allFiles.update(newFiles)

  else:
    print "Invalid content type. Must be one of ['module', 'collection']"
    return 1

  # Write out all the added files
  for name in newFiles:
    f = open(os.path.join(temp_dir, name), 'w')
    f.write(newFiles[name])
    f.close()

  # Now, run the epub script
  nothing = convert(etree.parse(StringIO(dbk)), temp_dir, args.css_file, epubXsl, args.output)
Esempio n. 2
0
def convert(p, collxml, modulesDict, temp_dir, svg2png=True, math2svg=True, reduce_quality=False):
  """ Convert a collxml file (and dictionary of module info) to a Docbook file and dict of filename:bytes) """

  newFiles = {}

  p.start(len(modulesDict), 'collxml to dbk')

  paramsStr = PARAMS_XPATH(COLLXML_PARAMS(collxml))[0]
  collParamsUnicode = eval(paramsStr) #json.loads(paramsStr)
  collParams = {}
  for key, value in collParamsUnicode.items():
  	collParams[key.encode('utf-8')] = value
  dbk1 = transform(COLLXML2DOCBOOK_XSL, collxml)

  modDbkDict = {}
  # Each module can be converted in parallel
  for module, (cnxml, filesDict) in modulesDict.items():

    p.tick('Converting ' + module)
    module_temp_dir = os.path.join(temp_dir, module)
    if not os.path.exists(module_temp_dir):
      os.makedirs(module_temp_dir)
    modDbk, newFilesMod = module2dbk.convert(module, cnxml, filesDict, collParams, module_temp_dir, svg2png, math2svg, reduce_quality)
    modDbkDict[module] = etree.parse(StringIO(modDbk)).getroot()
    # Add newFiles with the module prefix
    for f, data in newFilesMod.items():
    	newFiles[os.path.join(module, f)] = data

  # Combine into a single large file
  # Replacing Xpath xinclude magic with explicit pyhton code
  for i, module in enumerate(XINCLUDE_XPATH(dbk1)):
    # m9003/index.included.dbk
    id = module.get('href').split('/')[0]
    if id in modDbkDict:
      module.getparent().replace(module, modDbkDict[id])
    else:
      print >> sys.stderr, "ERROR: Didn't find module source!!!!"
  
  # Clean up image paths
  dbk2 = transform(DOCBOOK_NORMALIZE_PATHS_XSL, dbk1)
  
  dbk3 = transform(DOCBOOK_CLEANUP_XSL, dbk2)
  dbk4 = transform(DOCBOOK_NORMALIZE_GLOSSARY_XSL, dbk3)

  # Create cover SVG and convert it to an image
  cover, newFiles2 = util.dbk2cover(dbk4, filesDict, svg2png)

  if svg2png:
    newFiles['cover.png'] = cover

  p.finish()
  return dbk4, newFiles
Esempio n. 3
0
def convert(p, collxml, modulesDict, temp_dir, svg2png=True, math2svg=True, reduce_quality=False):
  """ Convert a collxml file (and dictionary of module info) to a Docbook file and dict of filename:bytes) """

  newFiles = {}

  p.start(len(modulesDict), 'collxml to dbk')

  paramsStr = PARAMS_XPATH(COLLXML_PARAMS(collxml))[0]
  collParamsUnicode = eval(paramsStr) #json.loads(paramsStr)
  collParams = {}
  for key, value in collParamsUnicode.items():
  	collParams[key.encode('utf-8')] = value
  dbk1 = transform(COLLXML2DOCBOOK_XSL, collxml)

  modDbkDict = {}
  # Each module can be converted in parallel
  for module, (cnxml, filesDict) in modulesDict.items():

    p.tick('Converting ' + module)
    module_temp_dir = os.path.join(temp_dir, module)
    if not os.path.exists(module_temp_dir):
      os.makedirs(module_temp_dir)
    modDbk, newFilesMod = module2dbk.convert(module, cnxml, filesDict, collParams, module_temp_dir, svg2png, math2svg, reduce_quality)
    modDbkDict[module] = etree.parse(StringIO(modDbk)).getroot()
    # Add newFiles with the module prefix
    for f, data in newFilesMod.items():
    	newFiles[os.path.join(module, f)] = data

  # Combine into a single large file
  # Replacing Xpath xinclude magic with explicit pyhton code
  for i, module in enumerate(XINCLUDE_XPATH(dbk1)):
    # m9003/index.included.dbk
    id = module.get('href').split('/')[0]
    if id in modDbkDict:
      module.getparent().replace(module, modDbkDict[id])
    else:
      print >> sys.stderr, "ERROR: Didn't find module source!!!!"

  # Clean up image paths
  dbk2 = transform(DOCBOOK_NORMALIZE_PATHS_XSL, dbk1)

  dbk3 = transform(DOCBOOK_CLEANUP_XSL, dbk2)
  dbk4 = transform(DOCBOOK_NORMALIZE_GLOSSARY_XSL, dbk3)

  # Create cover SVG and convert it to an image
  cover, newFiles2 = util.dbk2cover(dbk4, filesDict, svg2png)

  if svg2png:
    newFiles['cover.png'] = cover

  p.finish()
  return dbk4, newFiles
Esempio n. 4
0
def main():
    try:
        import argparse
    except ImportError:
        print "argparse is needed for commandline"
        return 1

    parser = argparse.ArgumentParser(
        description=
        'Converts a module directory to an xhtml file and additional images')
    parser.add_argument('directory')
    parser.add_argument('-i', dest='module_id', help='Published Module id')
    parser.add_argument(
        '-c', dest='css_file',
        help='CSS File to include')  # , type=argparse.FileType('r'))
    parser.add_argument(
        '-e',
        dest='epub_script',
        help=
        'Name of XSL file that generates an epub from a dbk file (should look like "./xsl/dbk2___.xsl")'
    )
    parser.add_argument('-r',
                        dest='reduce_quality',
                        help='Reduce image quality',
                        action='store_true')
    parser.add_argument(
        '-t',
        dest='content_type',
        help=
        'The type of content being converted. One of ["module", "collection"]')
    # parser.add_argument('-t', dest='temp_dir', help='Path to store temporary files to (default is a temp dir that will be removed)', nargs='?')
    parser.add_argument(
        '-o', dest='output',
        nargs='?')  # , type=argparse.FileType('w'), default=sys.stdout)
    args = parser.parse_args()

    epubXsl = args.epub_script
    if not epubXsl:
        epubXsl = DEFAULT_DOCBOOK2XHTML_XSL

    temp_dir = args.directory

    p = util.Progress()

    if args.content_type == 'module':
        cnxml, allFiles = util.loadModule(args.directory)
        dbk, newFiles = module2dbk.convert(args.module_id,
                                           cnxml,
                                           allFiles, {},
                                           temp_dir,
                                           svg2png=True,
                                           math2svg=True,
                                           reduce_quality=args.reduce_quality)
        dbk = newFiles['index.standalone.dbk']
        allFiles.update(newFiles)
        cover, newFiles = util.dbk2cover(etree.parse(StringIO(dbk)),
                                         allFiles,
                                         svg2pngFlag=True)
        newFiles['cover.png'] = cover

    elif args.content_type == 'collection':
        p = util.Progress()
        collxml, modulesDict, allFiles = util.loadCollection(args.directory)

        dbk, newFiles = collection2dbk.convert(
            p,
            collxml,
            modulesDict,
            temp_dir,
            svg2png=True,
            math2svg=True,
            reduce_quality=args.reduce_quality)
        allFiles.update(newFiles)

    else:
        print "Invalid content type. Must be one of ['module', 'collection']"
        return 1

    # Write out all the added files
    for name in newFiles:
        f = open(os.path.join(temp_dir, name), 'w')
        f.write(newFiles[name])
        f.close()

    # Now, run the epub script
    nothing = convert(etree.parse(StringIO(dbk)), temp_dir, args.css_file,
                      epubXsl, args.output)