示例#1
0
def build_html_doc(template_fname, verbosity, archetype = None):
    """
    Archetype required only when building archetype docs.

    """
    # archetypes all use the same template.. but we don't want to 
    # put them in the same doc file.
    if archetype is not None:
        doc_fname = archetype.get_id()
    else:
        doc_fname = template_fname

    # base name .. no extension
    doc_base_fname, _ = splitext(basename(doc_fname))
    xml_fname = join(build_dir, "%s.xml" % doc_base_fname)
    html_fname = join(build_dir, "%s.html" % doc_base_fname)

    # parse an xml document
    doc = Doc(xml_fname)        
    if not doc.parse():
        print("Problem parsing the xml.")
        exit(0)

    if not doc.validate() and fail_fast:
        print("Fatal: xml errors are fatal!")
        print("Run with the -s cmd line option to ignore xml errors.")
        exit(0)
    
    
    # build the html document by converting the xml into tex
    with codecs.open(html_fname, "w", "utf-8") as f:
        html_formatter = HtmlFormatter(f)
        errors = doc.format(html_formatter)
        if len(errors) > 0:
            print("Errors:")
            for error in errors:
                print("\t%s\n\n\n" % error)
                
            if fail_fast:
                sys.exit()
    return
示例#2
0
def build_pdf_doc(template_fname, doc_fname, verbosity,
                  db,                  
                  archetype=None,
                  patron=None):
    # base name .. no extension
    doc_base_fname, _ = splitext(basename(doc_fname))
    xml_fname = join(build_dir, "%s.xml" % doc_base_fname)
    pdf_fname = join(build_dir, "%s.pdf" % doc_base_fname)
    tex_fname = join(build_dir, "%s.tex" % doc_base_fname) 
    
    # makeindex won't write to files outside of the cwd.
    # We don't want a path here.  Just a filename
    idx_fname = "%s.idx" % doc_base_fname

    print("===============================================")
    print("Building %s " % doc_fname)
    print("===============================================")

    # the very first thing we do is run the xml through a template engine 
    # (Doing it like this allows us to include files relative to the doc 
    # dir using Jinjas include directive).
    template = jinja_env.get_template(template_fname)
    xml = template.render(db=db,
                          monster_groups=db.monster_groups,                          
                          ability_groups=db.ability_groups,
                          npc_gangs=db.npc_gangs,
                          archetype=archetype,
                          patron=patron,
                          config=config,
                          add_index_to_core = config.add_index_to_core,
                          doc_name = doc_base_fname)
    # write the post-processed xml to the build dir 
    # (has all the included files in it).
    with codecs.open(xml_fname, "w", "utf-8") as f:
       f.write(xml)

    # drop out early?
    if template_only:
        print("Template only! ...")
        print(xml.encode('ascii', 'xmlcharrefreplace'))
        return

    # parse an xml document
    doc = Doc(xml_fname)
    print xml_fname
    if not doc.parse():
        print("Problem parsing the xml.")
        exit(0)

    if not doc.validate() and fail_fast:
        print("Fatal: xml errors are fatal!")
        print("Run with the -s cmd line option to ignore xml errors.")
        exit(0)

    # drop out early?
    if parse_only:
        print("Parse only!")
        return            

    # check we have a book_node to format
    if not doc.has_book_node():
        if verbosity >= 1:
            print("No book node to format in document: %s IGNORING!" % doc_fname)
        return

    # build the latex document by converting the xml into tex
    with codecs.open(tex_fname, "w", "utf-8") as f:           
        latex_formatter = LatexFormatter(f, db)
        errors = doc.format(latex_formatter)
        if len(errors) > 0:
            print("Errors:")
            for error in errors:
                print("\t%s\n\n\n" % error)
                
            if fail_fast:
                sys.exit()

    # exit early if we don't build pdfs
    if latex_only:
        return

    # converts the latex to pdf
    xelatex(tex_fname, verbosity=verbosity)

    # run makeindex to, ah, make the index
    # (makeindex won't let you build an index outside of the cwd!)
    return_code = call([makeindex, 
                        # "-q", 
                        idx_fname], cwd = build_dir)
    if return_code != 0:
        sys.exit("Failed to run makeindex on %s" % idx_fname)

    # Copy the pdf from the build dir to the pdfs dir
    copy(pdf_fname, pdfs_dir)
    

    print("===============================================")
    print(("   Finished building %s " % doc_fname))
    print("===============================================\n")
    return