Sha256: d103ca848bcabe2de81d5a75313210a3a36100d61a6d11463a59c4b92def02c8

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

#!/usr/bin/env python

# This script allows you to highlight multiple chunks of code with a single
# invocation. Expected input is on STDIN and takes the form of:
#
#   <lexer>\000<code>\000<lexer>\000<code>...
#
# where <lexer> is the shortname of a Pygments lexer and <code> is the source
# code to be highlighted. Each lexer and code pair is separated with a NULL
# byte and pairs of lexer/code are also separated with NULL bytes.
#
# Output is a list of highlighted code blocks separated by NULL bytes in the
# same order in which they were received.

import sys, os, codecs

sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)

vpath = os.path.realpath(__file__).split("/")
vpath.pop()
vpath.pop()
vpath = "/".join(vpath)

from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter

parts = sys.stdin.read().split("\000")
newparts = []

for i in range(len(parts) / 2):
    lang = parts[i * 2]
    code = parts[i * 2 + 1]
    try:
        lexer = get_lexer_by_name(lang)
    except:
        lexer = get_lexer_by_name('text')
    newparts.append([code, lexer])

def hl(spec):
    code = spec[0]
    lexer = spec[1]
    try:
        return highlight(code, lexer, HtmlFormatter())
    except:
        lexer = get_lexer_by_name('text')
        return highlight(code, lexer, HtmlFormatter())

for spec in newparts:
    sys.stdout.write(hl(spec))
    sys.stdout.write("\000")

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
vanity-1.7.1 vendor/ruby/1.9.1/gems/albino-1.3.3/vendor/multipygmentize
albino-1.3.3 vendor/multipygmentize
albino-1.3.2 vendor/multipygmentize
albino-1.3.0 vendor/multipygmentize