git.iko.soy/typeable/pandoc
on master help sign in
Updated highlighting for highlighting-kate 0.4.

Text.Pandoc.Highlighting now exports just one new function, 'highlight', and reexports all the other functions from highlighting-kate that are used in the writers. This should make it easy to switch highlighting engines if that is ever desired.

commit45958de0e1ae4a1ed18659f33d3f710f64d57f84 authorJohn MacFarlane <fiddlosopher@gmail.com> dateMon 26 Dec 2011, 22:49 (15 years ago) parentcc18291
4 files changed+48−56
src/Text/Pandoc/Highlighting.hs+24−40 src/Text/Pandoc/Writers/Docbook.hs+1−1 src/Text/Pandoc/Writers/HTML.hs+16−10 src/Text/Pandoc/Writers/LaTeX.hs+7−5
src/Text/Pandoc/Highlighting.hs+24−40
@@ -29,57 +29,41 @@ Exports functions for syntax highlighting.
2929-}
3030
3131module Text.Pandoc.Highlighting ( languages
32− , highlightHtml
33− , highlightLaTeX
34− , defaultHighlightingCss
35− , defaultLaTeXMacros
3632 , languagesByExtension
33+ , highlight
34+ , formatLaTeXInline
35+ , formatLaTeXBlock
36+ , styleToLaTeX
37+ , formatHtmlInline
38+ , formatHtmlBlock
39+ , styleToHtml
40+ , pygments
3741 ) where
38−import Text.Blaze
3942import Text.Pandoc.Definition
40−import Text.Highlighting.Kate ( SourceLine, languages, highlightAs, formatAsHtml,
41− TokenType(..), formatAsLaTeX, FormatOption (..), defaultHighlightingCss,
42− defaultLaTeXMacros, languagesByExtension )
43+import Text.Highlighting.Kate
4344import Data.List (find)
4445import Data.Maybe (fromMaybe)
4546import Data.Char (toLower)
46−import qualified Text.Blaze.Html5.Attributes as A
4747
48−highlight :: ([FormatOption] -> String -> [SourceLine] -> a) -- ^ Formatter
49− -> Bool -- ^ True if inline
50− -> Attr -- ^ Attributes of the Code or CodeBlock
51− -> String -- ^ Raw contents of the Code or CodeBlock
52− -> Maybe a -- ^ Maybe the formatted result
53−highlight formatter inline (_, classes, keyvals) rawCode =
48+lcLanguages :: [String]
49+lcLanguages = map (map toLower) languages
50+
51+highlight :: (FormatOptions -> [SourceLine] -> a) -- ^ Formatter
52+ -> Attr -- ^ Attributes of the CodeBlock
53+ -> String -- ^ Raw contents of the CodeBlock
54+ -> Maybe a -- ^ Maybe the formatted result
55+highlight formatter (_, classes, keyvals) rawCode =
5456 let firstNum = case reads (fromMaybe "1" $ lookup "startFrom" keyvals) of
5557 ((n,_):_) -> n
5658 [] -> 1
57− fmtOpts = [OptNumberFrom firstNum] ++
58− [OptInline | inline] ++
59− case find (`elem` ["number","numberLines","number-lines"]) classes of
60− Nothing -> []
61− Just _ -> [OptNumberLines]
62− addBirdTracks = "literate" `elem` classes
63− lcLanguages = map (map toLower) languages
64− in case find (\c -> (map toLower c) `elem` lcLanguages) classes of
59+ fmtOpts = defaultFormatOpts{
60+ startNumber = firstNum,
61+ numberLines = any (`elem`
62+ ["number","numberLines", "number-lines"]) classes }
63+ lcclasses = map (map toLower) classes
64+ in case find (`elem` lcLanguages) lcclasses of
6565 Nothing -> Nothing
6666 Just language -> Just
67− $ formatter fmtOpts language .
68− (if addBirdTracks
69− then map ((OtherTok,"> "):)
70− else id)
67+ $ formatter fmtOpts
7168 $ highlightAs language rawCode
7269
73−highlightHtml :: Bool -- ^ True if inline HTML
74− -> Attr -- ^ Attributes of the Code or CodeBlock
75− -> String -- ^ Raw contents of the Code or CodeBlock
76− -> Maybe Html -- ^ Maybe formatted Html
77−highlightHtml inline attr@(id',_,_) = fmap addId . highlight formatAsHtml inline attr
78− where addId = if null id' then id else (! A.id (toValue id'))
79−
80−highlightLaTeX :: Bool -- ^ True if inline
81− -> Attr -- ^ Attributes of the Code or CodeBlock
82− -> String -- ^ Raw contents of the Code or CodeBlock
83− -> Maybe String -- ^ Maybe formatted LaTeX
84−highlightLaTeX = highlight formatAsLaTeX
85−
src/Text/Pandoc/Writers/Docbook.hs+1−1
@@ -35,7 +35,7 @@ import Text.Pandoc.Templates (renderTemplate)
3535import Text.Pandoc.Readers.TeXMath
3636import Data.List ( isPrefixOf, intercalate, isSuffixOf )
3737import Data.Char ( toLower )
38−import Text.Pandoc.Highlighting (languages, languagesByExtension)
38+import Text.Pandoc.Highlighting ( languages, languagesByExtension )
3939import Text.Pandoc.Pretty
4040
4141-- | Convert list of authors to a docbook <author> section
src/Text/Pandoc/Writers/HTML.hs+16−10
@@ -35,7 +35,8 @@ import Text.Pandoc.CharacterReferences ( decodeCharacterReferences )
3535import Text.Pandoc.Shared
3636import Text.Pandoc.Templates
3737import Text.Pandoc.Readers.TeXMath
38−import Text.Pandoc.Highlighting ( highlightHtml, defaultHighlightingCss )
38+import Text.Pandoc.Highlighting ( highlight, pygments, styleToHtml,
39+ formatHtmlInline, formatHtmlBlock )
3940import Text.Pandoc.XML (stripTags, escapeStringForXML)
4041import Network.HTTP ( urlEncode )
4142import Numeric ( showHex )
@@ -153,7 +154,7 @@ pandocToHtml opts (Pandoc (Meta title' authors' date') blocks) = do
153154 ("/*<![CDATA[*/\n" ++ s ++ "/*]]>*/\n")
154155 Nothing -> mempty
155156 else mempty
156− let newvars = [("highlighting-css", defaultHighlightingCss) |
157+ let newvars = [("highlighting-css", renderHtml $ styleToHtml pygments) |
157158 stHighlighting st] ++
158159 [("math", renderHtml math) | stMath st]
159160 return (tit, auths, date, toc, thebody, newvars)
@@ -361,16 +362,21 @@ blockToHtml _ (RawBlock "html" str) = return $ preEscapedString str
361362blockToHtml _ (RawBlock _ _) = return mempty
362363blockToHtml _ (HorizontalRule) = return H.hr
363364blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
364− let classes' = if writerLiterateHaskell opts
365− then classes
365+ let tolhs = writerLiterateHaskell opts &&
366+ any (\c -> map toLower c == "haskell") classes &&
367+ any (\c -> map toLower c == "literate") classes
368+ classes' = if tolhs
369+ then map (\c -> if map toLower c == "haskell"
370+ then "literatehaskell"
371+ else c) classes
366372 else filter (/= "literate") classes
367− case highlightHtml False (id',classes',keyvals) rawCode of
373+ adjCode = if tolhs
374+ then unlines . map ("> " ++) . lines $ rawCode
375+ else rawCode
376+ case highlight formatHtmlBlock (id',classes,keyvals) adjCode of
368377 Nothing -> let attrs = attrsToHtml opts (id', classes', keyvals)
369− addBird = if "literate" `elem` classes'
370− then unlines . map ("> " ++) . lines
371− else unlines . lines
372378 in return $ foldl (!) H.pre attrs $ H.code
373− $ toHtml $ addBird rawCode
379+ $ toHtml adjCode
374380 Just h -> modify (\st -> st{ stHighlighting = True }) >>
375381 return h
376382blockToHtml opts (BlockQuote blocks) =
@@ -540,7 +546,7 @@ inlineToHtml opts inline =
540546 (Apostrophe) -> return $ strToHtml "’"
541547 (Emph lst) -> inlineListToHtml opts lst >>= return . H.em
542548 (Strong lst) -> inlineListToHtml opts lst >>= return . H.strong
543− (Code attr str) -> case highlightHtml True attr str of
549+ (Code attr str) -> case highlight formatHtmlInline attr str of
544550 Nothing -> return
545551 $ foldl (!) H.code (attrsToHtml opts attr)
546552 $ strToHtml str
src/Text/Pandoc/Writers/LaTeX.hs+7−5
@@ -41,7 +41,8 @@ import Data.Char ( toLower, isPunctuation )
4141import Control.Monad.State
4242import Text.Pandoc.Pretty
4343import System.FilePath (dropExtension)
44−import Text.Pandoc.Highlighting (highlightLaTeX, defaultLaTeXMacros)
44+import Text.Pandoc.Highlighting (highlight, pygments, styleToLaTeX,
45+ formatLaTeXInline, formatLaTeXBlock)
4546
4647data WriterState =
4748 WriterState { stInNote :: Bool -- true if we're in a note
@@ -60,7 +61,7 @@ data WriterState =
6061 , stLHS :: Bool -- true if document has literate haskell code
6162 , stBook :: Bool -- true if document uses book or memoir class
6263 , stCsquotes :: Bool -- true if document uses csquotes
63− , stHighlighting :: Bool -- true if document has highlighted code
64+ , stHighlighting :: Bool -- true if document has highlighted code
6465 }
6566
6667-- | Convert Pandoc to LaTeX.
@@ -131,7 +132,7 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
131132 [ ("graphics", "yes") | stGraphics st ] ++
132133 [ ("book-class", "yes") | stBook st] ++
133134 [ ("listings", "yes") | writerListings options || stLHS st ] ++
134− [ ("highlighting-macros", defaultLaTeXMacros) | stHighlighting st ] ++
135+ [ ("highlighting-macros", styleToLaTeX pygments) | stHighlighting st ] ++
135136 citecontext
136137 return $ if writerStandalone options
137138 then renderTemplate context template
@@ -160,6 +161,7 @@ stringToLaTeX isUrl = escapeStringUsing latexEscapes
160161 , ('\x2019', "'")
161162 , ('\x201C', "``")
162163 , ('\x201D', "''")
164+ , ('\x2026', "\\ldots{}")
163165 ]
164166
165167-- | Puts contents into LaTeX command.
@@ -231,7 +233,7 @@ blockToLaTeX (CodeBlock (_,classes,keyvalAttr) str) = do
231233 return $ flush ("\\begin{lstlisting}" <> printParams $$ text str $$
232234 "\\end{lstlisting}") $$ cr
233235 highlightedCodeBlock =
234− case highlightLaTeX False ("",classes,keyvalAttr) str of
236+ case highlight formatLaTeXBlock ("",classes,keyvalAttr) str of
235237 Nothing -> rawCodeBlock
236238 Just h -> modify (\st -> st{ stHighlighting = True }) >>
237239 return (text h)
@@ -407,7 +409,7 @@ inlineToLaTeX (Code (_,classes,_) str) = do
407409 let chr = ((enumFromTo '!' '~') \\ str) !! 0
408410 return $ text $ "\\lstinline" ++ [chr] ++ str ++ [chr]
409411 highlightCode = do
410− case highlightLaTeX True ("",classes,[]) str of
412+ case highlight formatLaTeXInline ("",classes,[]) str of
411413 Nothing -> rawCode
412414 Just h -> modify (\st -> st{ stHighlighting = True }) >>
413415 return (text h)