git.iko.soy/typeable/pandoc
on master help sign in
Added highlightLaTeX to Text.Pandoc.Highlighting.
commita936e5d34f8a5557d5d1ad212d0236dc0597ee51 authorJohn MacFarlane <fiddlosopher@gmail.com> dateMon 19 Dec 2011, 11:51 (15 years ago) parent5e38d6a
1 file changed+29−11
src/Text/Pandoc/Highlighting.hs+29−11
src/Text/Pandoc/Highlighting.hs+29−11
@@ -28,21 +28,28 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2828Exports functions for syntax highlighting.
2929-}
3030
31−module Text.Pandoc.Highlighting ( languages, highlightHtml, defaultHighlightingCss, languagesByExtension ) where
31+module Text.Pandoc.Highlighting ( languages
32+ , highlightHtml
33+ , highlightLaTeX
34+ , defaultHighlightingCss
35+ , languagesByExtension
36+ ) where
3237import Text.Blaze
3338import Text.Pandoc.Definition
3439#ifdef _HIGHLIGHTING
35−import Text.Highlighting.Kate ( languages, highlightAs, formatAsHtml, FormatOption (..), defaultHighlightingCss, languagesByExtension )
40+import Text.Highlighting.Kate ( SourceLine, languages, highlightAs, formatAsHtml,
41+ TokenType(..), formatAsLaTeX, FormatOption (..), defaultHighlightingCss, languagesByExtension )
3642import Data.List (find)
3743import Data.Maybe (fromMaybe)
3844import Data.Char (toLower)
3945import qualified Text.Blaze.Html5.Attributes as A
4046
41−highlightHtml :: Bool -- ^ True if inline HTML
42− -> Attr -- ^ Attributes of the Code or CodeBlock
43− -> String -- ^ Raw contents of the Code or CodeBlock
44− -> Either String Html -- ^ An error or the formatted Html
45−highlightHtml inline (id', classes, keyvals) rawCode =
47+highlight :: ([FormatOption] -> String -> [SourceLine] -> a) -- ^ Formatter
48+ -> Bool -- ^ True if inline
49+ -> Attr -- ^ Attributes of the Code or CodeBlock
50+ -> String -- ^ Raw contents of the Code or CodeBlock
51+ -> Either String a -- ^ An error or the formatted result
52+highlight formatter inline (_, classes, keyvals) rawCode =
4653 let firstNum = case reads (fromMaybe "1" $ lookup "startFrom" keyvals) of
4754 ((n,_):_) -> n
4855 [] -> 1
@@ -53,17 +60,28 @@ highlightHtml inline (id', classes, keyvals) rawCode =
5360 Just _ -> [OptNumberLines]
5461 addBirdTracks = "literate" `elem` classes
5562 lcLanguages = map (map toLower) languages
56− addId = if null id' then id else (! A.id (toValue id'))
5763 in case find (\c -> (map toLower c) `elem` lcLanguages) classes of
5864 Nothing -> Left "Unknown or unsupported language"
5965 Just language -> case highlightAs language rawCode of
6066 Left err -> Left err
61− Right hl -> Right $ addId
62− $ formatAsHtml fmtOpts language
67+ Right hl -> Right $ formatter fmtOpts language
6368 $ if addBirdTracks
64− then map (("ot","> "):) hl
69+ then map ((OtherTok,"> "):) hl
6570 else hl
6671
72+highlightHtml :: Bool -- ^ True if inline HTML
73+ -> Attr -- ^ Attributes of the Code or CodeBlock
74+ -> String -- ^ Raw contents of the Code or CodeBlock
75+ -> Either String Html -- ^ An error or the formatted Html
76+highlightHtml inline attr@(id',_,_) = fmap addId . highlight formatAsHtml inline attr
77+ where addId = if null id' then id else (! A.id (toValue id'))
78+
79+highlightLaTeX :: Bool -- ^ True if inline
80+ -> Attr -- ^ Attributes of the Code or CodeBlock
81+ -> String -- ^ Raw contents of the Code or CodeBlock
82+ -> Either String String -- ^ An error or the formatted LaTeX string
83+highlightLaTeX = highlight formatAsLaTeX
84+
6785#else
6886defaultHighlightingCss :: String
6987defaultHighlightingCss = ""