hpygments-0.1.2: Highlight source code using Pygments

Portabilityunknown
Stabilityexperimental
Maintainerlazar6@illinois.edu
Safe HaskellNone

Text.Highlighting.Pygments

Contents

Description

This library uses the pygmentize script that comes with Pygments to highlight code in many languages. For documentation on the various lexers, formatters, and options, see the Pygments documentation http://pygments.org/docs/.

Synopsis

Documentation

highlight :: Lexer -> Formatter -> Options -> String -> IO StringSource

Highlight code robustly. This function is more robust than the lower-level pygmentize function since this library forbids the construction of invalid Lexer and Formatter values. Invalid Options may still cause this function to raise an exception.

pygmentize :: LexerAlias -> FormatterAlias -> Options -> String -> IO StringSource

Highlight code (less robustly) using the pygmentize script that comes with Pygments. Invalid values for LexerAlias, FormatterAlias, or Options will cause this function to raise an exception.

Options

type Option = (String, String)Source

The lexer/formatter option (key, value) is passed to the pygmentize script via the command-line flag -P key=value.

Examples:

 [("hl_lines", "16,23,42"), ("encoding", "utf-8"), ("anchorlines", "True")]

Examples

Highlight a proposition:

>>> Just coqLexer <- getLexerByName "coq"
>>> highlight coqLexer terminalFormatter [("encoding", "utf-8")] "∀ x y : Z, x * y = 0 -> x = 0 \\/ y = 0" >>= putStr
∀ x y : Z, x * y = 0 -> x = 0 \/ y = 0

Output a complete HTML document:

>>> highlight haskellLexer htmlFormatter [("full", "True"), ("linenos", "table"), ("style", "emacs")] "fix f = let x = f x in x" >>= writeFile "fix.html"

Self-highlighting quine:

 quine = pygmentize "hs" "terminal" [] (s ++ show s) >>= putStr
   where s = "quine = pygmentize \"hs\" \"terminal\" [] (s ++ show s) >>= putStr\n  where s = "

Highlight the code "answer = 42" using every language Pygments knows about:

>>> lexers <- getAllLexers
>>> forM_ lexers $ \l -> highlight l terminalFormatter [] "answer = 42" >>= printf "(%s) %s" (lexerName l)
...
(Prolog) answer = 42
(CSS+Django/Jinja) answer = 42
(Smalltalk) answer = 42
...