Example #1
0
def addPyListings(document, dir):
    """
    Insert Python source listings into the given document from files in the
    given directory based on C{py-listing} nodes.

    Any node in C{document} with a C{class} attribute set to C{py-listing} will
    have source lines taken from the file named in that node's C{href}
    attribute (searched for in C{dir}) inserted in place of that node.

    If a node has a C{skipLines} attribute, its value will be parsed as an
    integer and that many lines will be skipped at the beginning of the source
    file.

    @type document: A DOM Node or Document
    @param document: The document within which to make listing replacements.

    @type dir: C{str}
    @param dir: The directory in which to find source files containing the
    referenced Python listings.

    @return: C{None}
    """
    for node in domhelpers.findElementsWithAttribute(document, "class", "py-listing"):
        filename = node.getAttribute("href")
        outfile = cStringIO.StringIO()
        lines = map(string.rstrip, open(os.path.join(dir, filename)).readlines())
        data = "\n".join(lines[int(node.getAttribute("skipLines", 0)) :])
        data = cStringIO.StringIO(text.removeLeadingTrailingBlanks(data))
        htmlizer.filter(data, outfile, writer=htmlizer.SmallerHTMLWriter)
        val = outfile.getvalue()
        _replaceWithListing(node, val, filename, "py-listing")
Example #2
0
def addPyListings(document, dir):
    """
    Insert Python source listings into the given document from files in the
    given directory based on C{py-listing} nodes.

    Any node in C{document} with a C{class} attribute set to C{py-listing} will
    have source lines taken from the file named in that node's C{href}
    attribute (searched for in C{dir}) inserted in place of that node.

    If a node has a C{skipLines} attribute, its value will be parsed as an
    integer and that many lines will be skipped at the beginning of the source
    file.

    @type document: A DOM Node or Document
    @param document: The document within which to make listing replacements.

    @type dir: C{str}
    @param dir: The directory in which to find source files containing the
    referenced Python listings.

    @return: C{None}
    """
    for node in domhelpers.findElementsWithAttribute(document, "class",
                                                     "py-listing"):
        filename = node.getAttribute("href")
        outfile = cStringIO.StringIO()
        lines = map(string.rstrip,
                    open(os.path.join(dir, filename)).readlines())
        data = '\n'.join(lines[int(node.getAttribute('skipLines', 0)):])
        data = cStringIO.StringIO(text.removeLeadingTrailingBlanks(data))
        htmlizer.filter(data, outfile, writer=htmlizer.SmallerHTMLWriter)
        val = outfile.getvalue()
        _replaceWithListing(node, val, filename, "py-listing")
Example #3
0
def addPyListings(document, dir):
    for node in domhelpers.findElementsWithAttribute(document, "class",
                                                     "py-listing"):
        filename = node.getAttribute("href")
        outfile = cStringIO.StringIO()
        lines = map(string.rstrip, open(os.path.join(dir, filename)).readlines())
        data = '\n'.join(lines[int(node.getAttribute('skipLines', 0)):])
        data = cStringIO.StringIO(text.removeLeadingTrailingBlanks(data))
        htmlizer.filter(data, outfile, writer=htmlizer.SmallerHTMLWriter)
        val = outfile.getvalue()
        _replaceWithListing(node, val, filename, "py-listing")
Example #4
0
 def visitNode_pre(self, node):
     # TODO: Syntax highlighting
     buf = StringIO()
     getLatexText(node, buf.write, entities=entities)
     data = buf.getvalue()
     data = text.removeLeadingTrailingBlanks(data)
     lines = data.split('\n')
     self.fontStack.append(('typewriter', 4))
     self.writer('%' + self.fontName() + '\n')
     for line in lines:
         self.writer(' ' + line + '\n')
     del self.fontStack[-1]
     self.writer('%' + self.fontName() + '\n')
Example #5
0
 def visitNode_pre(self, node):
     # TODO: Syntax highlighting
     buf = StringIO()
     getLatexText(node, buf.write, entities=entities)
     data = buf.getvalue()
     data = text.removeLeadingTrailingBlanks(data)
     lines = data.split('\n')
     self.fontStack.append(('typewriter', 4))
     self.writer('%' + self.fontName() + '\n')
     for line in lines:
         self.writer(' ' + line + '\n')
     del self.fontStack[-1]
     self.writer('%' + self.fontName() + '\n')
    def visitNode_a_listing(self, node):
        fileName = os.path.join(self.currDir, node.getAttribute('href'))
        self.writer('\\begin{verbatim}\n')
        lines = map(string.rstrip, open(fileName).readlines())
        lines = lines[int(node.getAttribute('skipLines', 0)):]
        self.writer(text.removeLeadingTrailingBlanks('\n'.join(lines)))
        self.writer('\\end{verbatim}')

        # Write a caption for this source listing
        fileName = os.path.basename(fileName)
        caption = domhelpers.getNodeText(node)
        if caption == fileName:
            caption = 'Source listing'
        self.writer('\parbox[b]{\linewidth}{\\begin{center}%s --- '
                    '\\begin{em}%s\\end{em}\\end{center}}'
                    % (latexEscape(caption), latexEscape(fileName)))
Example #7
0
    def visitNode_a_listing(self, node):
        fileName = os.path.join(self.currDir, node.getAttribute('href'))
        self.writer('\\begin{verbatim}\n')
        lines = map(string.rstrip, open(fileName).readlines())
        lines = lines[int(node.getAttribute('skipLines', 0)):]
        self.writer(text.removeLeadingTrailingBlanks('\n'.join(lines)))
        self.writer('\\end{verbatim}')

        # Write a caption for this source listing
        fileName = os.path.basename(fileName)
        caption = domhelpers.getNodeText(node)
        if caption == fileName:
            caption = 'Source listing'
        self.writer('\parbox[b]{\linewidth}{\\begin{center}%s --- '
                    '\\begin{em}%s\\end{em}\\end{center}}' %
                    (latexEscape(caption), latexEscape(fileName)))
 def visitNode_pre(self, node):
     self.writer('\\begin{verbatim}\n')
     buf = StringIO()
     getLatexText(node, buf.write)
     self.writer(text.removeLeadingTrailingBlanks(buf.getvalue()))
     self.writer('\\end{verbatim}\n')
Example #9
0
 def visitNode_pre(self, node):
     self.writer('@verbatim\n')
     buf = StringIO()
     latex.getLatexText(node, buf.write, entities=entities)
     self.writer(text.removeLeadingTrailingBlanks(buf.getvalue()))
     self.writer('@end verbatim\n')
Example #10
0
 def visitNode_pre(self, node):
     self.writer('\\begin{verbatim}\n')
     buf = StringIO()
     getLatexText(node, buf.write)
     self.writer(text.removeLeadingTrailingBlanks(buf.getvalue()))
     self.writer('\\end{verbatim}\n')
Example #11
0
 def visitNode_pre(self, node):
     self.writer('@verbatim\n')
     buf = StringIO()
     latex.getLatexText(node, buf.write, entities=entities)
     self.writer(text.removeLeadingTrailingBlanks(buf.getvalue()))
     self.writer('@end verbatim\n')