Advertisement
Guest User

Roberto Alsina

a guest
Nov 14th, 2008
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.48 KB | None | 0 0
  1. class PdfBuilder(Builder):
  2.     name = 'pdf'
  3.     out_suffix = '.pdf'
  4.  
  5.     def init(self):
  6.         self.docnames = []
  7.         self.document_data = []
  8.  
  9.     def init_document_data(self):
  10.         preliminary_document_data = map(list, self.config.pdf_documents)
  11.         if not preliminary_document_data:
  12.             self.warn('No "latex_documents" config value found; no documents '
  13.                       'will be written.')
  14.             return
  15.         # assign subdirs to titles
  16.         self.titles = []
  17.         for entry in preliminary_document_data:
  18.             docname = entry[0]
  19.             if docname not in self.env.all_docs:
  20.                 self.warn('"latex_documents" config value references unknown '
  21.                           'document %s' % docname)
  22.                 continue
  23.             self.document_data.append(entry)
  24.             if docname.endswith(SEP+'index'):
  25.                 docname = docname[:-5]
  26.             self.titles.append((docname, entry[2]))
  27.  
  28.     def write(self, *ignored):
  29.         print "WRITE"
  30.         # first, assemble the "appendix" docs that are in every PDF
  31.         appendices = []
  32.         for fname in self.config.latex_appendices:
  33.             appendices.append(self.env.get_doctree(fname))
  34.  
  35.         docwriter = PdfWriter(self)
  36.         docsettings = OptionParser(
  37.             defaults=self.env.settings,
  38.             components=(docwriter,)).get_default_values()
  39.  
  40.         self.init_document_data()
  41.  
  42.         for entry in self.document_data:
  43.             docname, targetname, title, author, docclass = entry[:5]
  44.             toctree_only = False
  45.             if len(entry) > 5:
  46.                 toctree_only = entry[5]
  47.             destination = FileOutput(
  48.                 destination_path=path.join(self.outdir, targetname),
  49.                 encoding='utf-8')
  50.             self.info("processing " + targetname + "... ", nonl=1)
  51.             doctree = self.assemble_doctree(docname, toctree_only,
  52.                 appendices=(docclass == 'manual') and appendices or [])
  53.             self.post_process_images(doctree)
  54.             self.info("writing... ", nonl=1)
  55.             doctree.settings = docsettings
  56.             doctree.settings.author = author
  57.             doctree.settings.title = title
  58.             doctree.settings.docname = docname
  59.             doctree.settings.docclass = docclass
  60.             docwriter.write(doctree, destination)
  61.             self.info("done")
  62.  
  63.  
  64.     def assemble_doctree(self, indexfile, toctree_only, appendices):
  65.         self.docnames = set([indexfile] + appendices)
  66.         self.info(darkgreen(indexfile) + " ", nonl=1)
  67.         def process_tree(docname, tree):
  68.             tree = tree.deepcopy()
  69.             for toctreenode in tree.traverse(addnodes.toctree):
  70.                 newnodes = []
  71.                 includefiles = map(str, toctreenode['includefiles'])
  72.                 for includefile in includefiles:
  73.                     try:
  74.                         self.info(darkgreen(includefile) + " ", nonl=1)
  75.                         subtree = process_tree(includefile,
  76.                                                self.env.get_doctree(includefile))
  77.                         self.docnames.add(includefile)
  78.                     except Exception:
  79.                         self.warn('%s: toctree contains ref to nonexisting file %r' %
  80.                                   (docname, includefile))
  81.                     else:
  82.                         newnodes.append(addnodes.start_of_file())
  83.                         newnodes.extend(subtree.children)
  84.                 toctreenode.parent.replace(toctreenode, newnodes)
  85.             return tree
  86.         tree = self.env.get_doctree(indexfile)
  87.         if toctree_only:
  88.             # extract toctree nodes from the tree and put them in a fresh document
  89.             new_tree = new_document('<latex output>')
  90.             new_sect = nodes.section()
  91.             new_sect += nodes.title('<temp>', '<temp>')
  92.             new_tree += new_sect
  93.             for node in tree.traverse(addnodes.toctree):
  94.                 new_sect += node
  95.             tree = new_tree
  96.         largetree = process_tree(indexfile, tree)
  97.         largetree.extend(appendices)
  98.         self.info()
  99.         self.info("resolving references...")
  100.         self.env.resolve_references(largetree, indexfile, self)
  101.         # resolve :ref:s to distant tex files -- we can't add a cross-reference,
  102.         # but append the document name
  103.         for pendingnode in largetree.traverse(addnodes.pending_xref):
  104.             docname = pendingnode['refdocname']
  105.             sectname = pendingnode['refsectname']
  106.             newnodes = [nodes.emphasis(sectname, sectname)]
  107.             for subdir, title in self.titles:
  108.                 if docname.startswith(subdir):
  109.                     newnodes.append(nodes.Text(' (in ', ' (in '))
  110.                     newnodes.append(nodes.emphasis(title, title))
  111.                     newnodes.append(nodes.Text(')', ')'))
  112.                     break
  113.             else:
  114.                 pass
  115.             pendingnode.replace_self(newnodes)
  116.         return largetree
  117.  
  118.  
  119.     def get_outdated_docs(self):
  120.         for docname in self.env.found_docs:
  121.             if docname not in self.env.all_docs:
  122.                 yield docname
  123.                 continue
  124.             targetname = self.env.doc2path(docname, self.outdir, self.out_suffix)
  125.             try:
  126.                 targetmtime = path.getmtime(targetname)
  127.             except Exception:
  128.                 targetmtime = 0
  129.             try:
  130.                 srcmtime = path.getmtime(self.env.doc2path(docname))
  131.                 if srcmtime > targetmtime:
  132.                     yield docname
  133.             except EnvironmentError:
  134.                 # source doesn't exist anymore
  135.                 pass
  136.  
  137.     def get_target_uri(self, docname, typ=None):
  138.         return ''
  139.  
  140.     def prepare_writing(self, docnames):
  141.         self.writer = PdfWriter(self)
  142.  
  143.     def write_doc(self, docname, doctree):
  144.         print "WRITEDOC"
  145.         destination = StringOutput(encoding='utf-8')
  146.         self.writer.write(doctree, destination)
  147.         outfilename = path.join(self.outdir, os_path(docname) + self.out_suffix)
  148.         ensuredir(path.dirname(outfilename)) # normally different from self.outdir
  149.         try:
  150.             f = codecs.open(outfilename, 'w', 'utf-8')
  151.             try:
  152.                 f.write(self.writer.output)
  153.             finally:
  154.                 f.close()
  155.         except (IOError, OSError), err:
  156.             self.warn("Error writing file %s: %s" % (outfilename, err))
  157.  
  158.     def finish(self):
  159.         pass
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement