From 8c243f31782ea3f4053d5a4a4f7a73f413b5d6ca Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Tue, 29 Jun 2010 08:11:53 -0700 Subject: Using the actual pygments module rather than the command line utility --- pocco.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/pocco.py b/pocco.py index 0f12260..7a51c23 100644 --- a/pocco.py +++ b/pocco.py @@ -73,19 +73,10 @@ def parse(source, code): # wherever our markers occur. def highlight(source, sections): language = get_language(source) - pygments = Popen(["pygmentize", "-l", language["name"], "-f", "html"], - stderr=PIPE, - stdin=PIPE, - stdout=PIPE) - - pygments.stdin.write(language["divider_text"].join(section["code_text"] for section in sections)) - output = "" - while pygments.returncode is None: - stdout, stderr = pygments.communicate() - if stderr: - print stderr - if stdout: - output += stdout + + output = pygments.highlight(language["divider_text"].join(section["code_text"] for section in sections), + language["lexer"], + formatters.get_formatter_by_name("html")) output = output.replace(highlight_start, "").replace(highlight_end, "") fragments = re.split(language["divider_html"], output) @@ -115,11 +106,13 @@ def generate_html(source, sections): #### Helpers & Setup # Import our external dependencies. +import pygments import pystache import re import sys from markdown import markdown from os import path +from pygments import lexers, formatters from subprocess import Popen, PIPE # A list of the languages that Pocco supports, mapping the file extension to @@ -145,6 +138,9 @@ for ext, l in languages.items(): # on this to recover the original sections. l["divider_html"] = re.compile(r'\n*' + l["symbol"] + 'DIVIDER\n*') + # Get the Pygments Lexer for this language. + l["lexer"] = lexers.get_lexer_by_name(l["name"]) + # Get the current language we're documenting, based on the extension. def get_language(source): return languages[ source[source.rindex("."):] ] -- cgit v1.2.3