Example #1
0
def run():
    options = Options()
    try:
        options.parseOptions()
    except usage.UsageError as e:
        print(str(e))
        sys.exit(1)
    filename = options['filename']
    if options.get('stylesheet') is not None:
        stylesheet = styleLink % (options['stylesheet'], )
    else:
        stylesheet = ''

    with open(filename + '.html', 'wb') as output:
        outHeader = (header % {
            'title': filename,
            'generator': 'htmlizer/%s' % (copyright.longversion, ),
            'alternate': alternateLink % {
                'source': filename
            },
            'stylesheet': stylesheet
        })
        output.write(outHeader.encode("utf-8"))
        with open(filename, 'rb') as f:
            htmlizer.filter(f, output, htmlizer.SmallerHTMLWriter)
        output.write(footer.encode("utf-8"))
Example #2
0
def run():
    options = Options()
    try:
        options.parseOptions()
    except usage.UsageError as e:
        print(str(e))
        sys.exit(1)
    filename = options['filename']
    if options.get('stylesheet') is not None:
        stylesheet = styleLink % (options['stylesheet'],)
    else:
        stylesheet = ''

    output = open(filename + '.html', 'w')
    try:
        output.write(header % {
            'title': filename,
            'generator': 'htmlizer/%s' % (copyright.longversion,),
            'alternate': alternateLink % {'source': filename},
            'stylesheet': stylesheet
            })
        htmlizer.filter(open(filename), output,
                        htmlizer.SmallerHTMLWriter)
        output.write(footer)
    finally:
        output.close()
Example #3
0
def fontifyPythonNode(node):
    """
    Syntax color the given node containing Python source code.

    The node must have a parent.

    @return: C{None}
    """
    oldio = cStringIO.StringIO()
    latex.getLatexText(node,
                       oldio.write,
                       entities={
                           'lt': '<',
                           'gt': '>',
                           'amp': '&'
                       })
    oldio = cStringIO.StringIO(oldio.getvalue().strip() + '\n')
    howManyLines = len(oldio.getvalue().splitlines())
    newio = cStringIO.StringIO()
    htmlizer.filter(oldio, newio, writer=htmlizer.SmallerHTMLWriter)
    lineLabels = _makeLineNumbers(howManyLines)
    newel = dom.parseString(newio.getvalue()).documentElement
    newel.setAttribute("class", "python")
    node.parentNode.replaceChild(newel, node)
    newel.insertBefore(lineLabels, newel.firstChild)
Example #4
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 #5
0
def run():
    options = Options()
    try:
        options.parseOptions()
    except usage.UsageError as e:
        print str(e)
        sys.exit(1)
    filename = options['filename']
    if options.get('stylesheet') is not None:
        stylesheet = styleLink % (options['stylesheet'],)
    else:
        stylesheet = ''

    output = open(filename + '.html', 'w')
    try:
        output.write(header % {
            'title': filename,
            'generator': 'htmlizer/%s' % (copyright.longversion,),
            'alternate': alternateLink % {'source': filename},
            'stylesheet': stylesheet
            })
        htmlizer.filter(open(filename), output,
                        htmlizer.SmallerHTMLWriter)
        output.write(footer)
    finally:
        output.close()
Example #6
0
def run():
    options = Options()
    try:
        options.parseOptions()
    except usage.UsageError as e:
        print(str(e))
        sys.exit(1)
    filename = options["filename"]
    if options.get("stylesheet") is not None:
        stylesheet = styleLink % (options["stylesheet"], )
    else:
        stylesheet = ""

    with open(filename + ".html", "wb") as output:
        outHeader = header % {
            "title": filename,
            "generator": "htmlizer/{}".format(copyright.longversion),
            "alternate": alternateLink % {
                "source": filename
            },
            "stylesheet": stylesheet,
        }
        output.write(outHeader.encode("utf-8"))
        with open(filename, "rb") as f:
            htmlizer.filter(f, output, htmlizer.SmallerHTMLWriter)
        output.write(footer.encode("utf-8"))
Example #7
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 #8
0
    def render_htmlizer(self, ctx, path):
        from twisted.python import htmlizer
        from StringIO import StringIO

        output = StringIO()
        htmlizer.filter(open(path), output, writer=htmlizer.SmallerHTMLWriter)
        return tags.xml(output.getvalue())
Example #9
0
def python(name, arguments, options, content, lineno,
           content_offset, block_text, state, state_machine):
    inp = StringIO('\n'.join(content))
    outp = StringIO()
    htmlizer.filter(inp, outp, writer=htmlizer.SmallerHTMLWriter)
    html = outp.getvalue()
    return [nodes.raw('', html, format='html')]
Example #10
0
def _python_colouriser(text):
    out = StringIO()
    try:
        htmlizer.filter(StringIO(text), out)
    except AttributeError:
        out = StringIO("""Starting after Nevow 0.4.1 Twisted
2.0 is a required dependency. Please install it""")
    return out.getvalue()
Example #11
0
def _python_colouriser(text):
    out = StringIO()
    try:
        htmlizer.filter(StringIO(text), out)
    except AttributeError:
        out = StringIO("""Starting after Nevow 0.4.1 Twisted
2.0 is a required dependency. Please install it""")
    return out.getvalue()
Example #12
0
 def test_empty(self):
     """
     If passed an empty input file, L{filter} writes a I{pre} tag containing
     only an end marker to the output file.
     """
     input = StringIO("")
     output = StringIO()
     filter(input, output)
     self.assertEqual(output.getvalue(), '<pre><span class="py-src-endmarker"></span></pre>\n')
Example #13
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 #14
0
 def test_empty(self):
     """
     If passed an empty input file, L{filter} writes a I{pre} tag containing
     only an end marker to the output file.
     """
     input = BytesIO(b"")
     output = BytesIO()
     filter(input, output)
     self.assertEqual(
         output.getvalue(),
         b'<pre><span class="py-src-endmarker"></span></pre>\n')
Example #15
0
def fontifyPythonNode(node):
    oldio = cStringIO.StringIO()
    latex.getLatexText(node, oldio.write,
                       entities={'lt': '<', 'gt': '>', 'amp': '&'})
    oldio = cStringIO.StringIO(oldio.getvalue().strip()+'\n')
    newio = cStringIO.StringIO()
    htmlizer.filter(oldio, newio, writer=htmlizer.SmallerHTMLWriter)
    newio.seek(0)
    newel = microdom.parse(newio).documentElement
    newel.setAttribute("class", "python")
    node.parentNode.replaceChild(newel, node)
Example #16
0
 def test_variable(self):
     """
     If passed an input file containing a variable access, L{filter} writes
     a I{pre} tag containing a I{py-src-variable} span containing the
     variable.
     """
     input = StringIO("foo\n")
     output = StringIO()
     filter(input, output)
     self.assertEqual(
         output.getvalue(),
         '<pre><span class="py-src-variable">foo</span><span class="py-src-newline">\n'
         '</span><span class="py-src-endmarker"></span></pre>\n')
Example #17
0
 def test_variable(self):
     """
     If passed an input file containing a variable access, L{filter} writes
     a I{pre} tag containing a I{py-src-variable} span containing the
     variable.
     """
     input = BytesIO(b"foo\n")
     output = BytesIO()
     filter(input, output)
     self.assertEqual(
         output.getvalue(), b'<pre><span class="py-src-variable">foo</span>'
         b'<span class="py-src-newline">\n'
         b'</span><span class="py-src-endmarker"></span></pre>\n')
Example #18
0
def fontifyPythonNode(node):
    """
    Syntax color the given node containing Python source code.

    @return: C{None}
    """
    oldio = cStringIO.StringIO()
    latex.getLatexText(node, oldio.write, entities={"lt": "<", "gt": ">", "amp": "&"})
    oldio = cStringIO.StringIO(oldio.getvalue().strip() + "\n")
    newio = cStringIO.StringIO()
    htmlizer.filter(oldio, newio, writer=htmlizer.SmallerHTMLWriter)
    newio.seek(0)
    newel = microdom.parse(newio).documentElement
    newel.setAttribute("class", "python")
    node.parentNode.replaceChild(newel, node)
Example #19
0
def fontifyPythonNode(node):
    """
    Syntax color the given node containing Python source code.

    @return: C{None}
    """
    oldio = cStringIO.StringIO()
    latex.getLatexText(node, oldio.write,
                       entities={'lt': '<', 'gt': '>', 'amp': '&'})
    oldio = cStringIO.StringIO(oldio.getvalue().strip()+'\n')
    newio = cStringIO.StringIO()
    htmlizer.filter(oldio, newio, writer=htmlizer.SmallerHTMLWriter)
    newio.seek(0)
    newel = microdom.parse(newio).documentElement
    newel.setAttribute("class", "python")
    node.parentNode.replaceChild(newel, node)
Example #20
0
def fontifyPythonNode(node):
    """
    Syntax color the given node containing Python source code.

    The node must have a parent.

    @return: C{None}
    """
    oldio = cStringIO.StringIO()
    latex.getLatexText(node, oldio.write,
                       entities={'lt': '<', 'gt': '>', 'amp': '&'})
    oldio = cStringIO.StringIO(oldio.getvalue().strip()+'\n')
    howManyLines = len(oldio.getvalue().splitlines())
    newio = cStringIO.StringIO()
    htmlizer.filter(oldio, newio, writer=htmlizer.SmallerHTMLWriter)
    lineLabels = _makeLineNumbers(howManyLines)
    newel = dom.parseString(newio.getvalue()).documentElement
    newel.setAttribute("class", "python")
    node.parentNode.replaceChild(newel, node)
    newel.insertBefore(lineLabels, newel.firstChild)
Example #21
0
def run():
    options = Options()
    try:
        options.parseOptions()
    except usage.UsageError as e:
        print(str(e))
        sys.exit(1)
    filename = options['filename']
    if options.get('stylesheet') is not None:
        stylesheet = styleLink % (options['stylesheet'],)
    else:
        stylesheet = ''

    with open(filename + '.html', 'wb') as output:
        outHeader = (header % {
            'title': filename,
            'generator': 'htmlizer/%s' % (copyright.longversion,),
            'alternate': alternateLink % {'source': filename},
            'stylesheet': stylesheet
            })
        output.write(outHeader.encode("utf-8"))
        with open(filename, 'rb') as f:
            htmlizer.filter(f, output, htmlizer.SmallerHTMLWriter)
        output.write(footer.encode("utf-8"))
Example #22
0
    def parseArgs(self, filename):
        self['filename'] = filename

def run():
    options = Options()
    try:
        options.parseOptions()
    except usage.UsageError, e:
        print str(e)
        sys.exit(1)
    filename = options['filename']
    if options.get('stylesheet') is not None:
        stylesheet = styleLink % (options['stylesheet'],)
    else:
        stylesheet = ''

    output = open(filename + '.html', 'w')
    try:
        output.write(header % {
            'title': filename,
            'generator': 'htmlizer/%s' % (copyright.longversion,),
            'alternate': alternateLink % {'source': filename},
            'stylesheet': stylesheet
            })
        htmlizer.filter(open(filename), output,
                        htmlizer.SmallerHTMLWriter)
        output.write(footer)
    finally:
        output.close()
Example #23
0
    def parseArgs(self, filename):
        self['filename'] = filename

def run():
    options = Options()
    try:
        options.parseOptions()
    except usage.UsageError, e:
        print str(e)
        sys.exit(1)
    filename = options['filename']
    if options.get('stylesheet') is not None:
        stylesheet = styleLink % (options['stylesheet'],)
    else:
        stylesheet = ''

    output = open(filename + '.html', 'w')
    try:
        output.write(header % {
            'title': filename,
            'generator': 'htmlizer/%s' % (copyright.longversion,),
            'alternate': alternateLink % {'source': filename},
            'stylesheet': stylesheet
            })
        htmlizer.filter(open(filename), output,
                        htmlizer.SmallerHTMLWriter)
        output.write(footer)
    finally:
        output.close()