From f4efe8e99b2d52d7796c4a01c06deb5187729e94 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Tue, 3 Jun 2014 21:39:02 -0400 Subject: [PATCH] Codelet.serialize() update (for #46). --- bitshift/codelet.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/bitshift/codelet.py b/bitshift/codelet.py index 3021ffe..2defbf8 100644 --- a/bitshift/codelet.py +++ b/bitshift/codelet.py @@ -1,3 +1,9 @@ +from operator import concat + +from pygments import highlight +from pygments.lexers import get_lexer_by_name +from pygments.formatters.html import HtmlFormatter + from .languages import LANGS __all__ = ["Codelet"] @@ -75,10 +81,15 @@ class Codelet(object): :return: The codelet as a dictionary. :rtype: str """ + lang = LANGS[self.language] + lines = reduce(concat, [[loc[0] for loc in sym[1] + sym[2]] for sym in + reduce(concat, self.symbols.values())]) + formatter = HtmlFormatter(linenos=True, hl_lines=lines) + code = highlight(self.code, get_lexer_by_name(lang.lower()), formatter) + return { - "name": self.name, "code": self.code, "lang": LANGS[self.language], + "name": self.name, "code": code, "lang": lang, "authors": self.authors, "url": self.url, "created": self.date_created.isoformat(), - "modified": self.date_modified.isoformat(), - "symbols": self.symbols, "origin": self.origin + "modified": self.date_modified.isoformat(), "origin": self.origin }