git.iko.soy/typeable/pandoc
on master help sign in
Updated highlighting for highlighting-kate 0.4.
45958deJohn MacFarlane
src/Text/Pandoc/Highlighting.hs+24−40
@@ -29,57 +29,41 @@ Exports functions for syntax highlighting.
29-}
29-}
30
30
31module Text.Pandoc.Highlighting ( languages
31module Text.Pandoc.Highlighting ( languages
32− , highlightHtml
33− , highlightLaTeX
34− , defaultHighlightingCss
35− , defaultLaTeXMacros
36 , languagesByExtension
32 , languagesByExtension
33+ , highlight
34+ , formatLaTeXInline
35+ , formatLaTeXBlock
36+ , styleToLaTeX
37+ , formatHtmlInline
38+ , formatHtmlBlock
39+ , styleToHtml
40+ , pygments
37 ) where
41 ) where
38−import Text.Blaze
39import Text.Pandoc.Definition
42import Text.Pandoc.Definition
40−import Text.Highlighting.Kate ( SourceLine, languages, highlightAs, formatAsHtml,
43+import Text.Highlighting.Kate
41− TokenType(..), formatAsLaTeX, FormatOption (..), defaultHighlightingCss,
42− defaultLaTeXMacros, languagesByExtension )
43import Data.List (find)
44import Data.List (find)
44import Data.Maybe (fromMaybe)
45import Data.Maybe (fromMaybe)
45import Data.Char (toLower)
46import Data.Char (toLower)
46−import qualified Text.Blaze.Html5.Attributes as A
47
47
48+lcLanguages :: [String]
49+lcLanguages = map (map toLower) languages
50+
48−highlight :: ([FormatOption] -> String -> [SourceLine] -> a) -- ^ Formatter
51+highlight :: (FormatOptions -> [SourceLine] -> a) -- ^ Formatter
49− -> Bool -- ^ True if inline
50− -> Attr -- ^ Attributes of the Code or CodeBlock
52+ -> Attr -- ^ Attributes of the CodeBlock
51− -> String -- ^ Raw contents of the Code or CodeBlock
53+ -> String -- ^ Raw contents of the CodeBlock
52− -> Maybe a -- ^ Maybe the formatted result
54+ -> Maybe a -- ^ Maybe the formatted result
53−highlight formatter inline (_, classes, keyvals) rawCode =
55+highlight formatter (_, classes, keyvals) rawCode =
54 let firstNum = case reads (fromMaybe "1" $ lookup "startFrom" keyvals) of
56 let firstNum = case reads (fromMaybe "1" $ lookup "startFrom" keyvals) of
55 ((n,_):_) -> n
57 ((n,_):_) -> n
56 [] -> 1
58 [] -> 1
57− fmtOpts = [OptNumberFrom firstNum] ++
59+ fmtOpts = defaultFormatOpts{
58− [OptInline | inline] ++
60+ startNumber = firstNum,
61+ numberLines = any (`elem`
59− case find (`elem` ["number","numberLines","number-lines"]) classes of
62+ ["number","numberLines", "number-lines"]) classes }
60− Nothing -> []
61− Just _ -> [OptNumberLines]
62− addBirdTracks = "literate" `elem` classes
63− lcLanguages = map (map toLower) languages
63+ lcclasses = map (map toLower) classes
64− in case find (\c -> (map toLower c) `elem` lcLanguages) classes of
64+ in case find (`elem` lcLanguages) lcclasses of
65 Nothing -> Nothing
65 Nothing -> Nothing
66 Just language -> Just
66 Just language -> Just
67− $ formatter fmtOpts language .
67+ $ formatter fmtOpts
68− (if addBirdTracks
69− then map ((OtherTok,"> "):)
70− else id)
71 $ highlightAs language rawCode
68 $ highlightAs language rawCode
72
69
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)
35import Text.Pandoc.Readers.TeXMath
35import Text.Pandoc.Readers.TeXMath
36import Data.List ( isPrefixOf, intercalate, isSuffixOf )
36import Data.List ( isPrefixOf, intercalate, isSuffixOf )
37import Data.Char ( toLower )
37import Data.Char ( toLower )
38−import Text.Pandoc.Highlighting (languages, languagesByExtension)
38+import Text.Pandoc.Highlighting ( languages, languagesByExtension )
39import Text.Pandoc.Pretty
39import Text.Pandoc.Pretty
40
40
41-- | Convert list of authors to a docbook <author> section
41-- | 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 )
35import Text.Pandoc.Shared
35import Text.Pandoc.Shared
36import Text.Pandoc.Templates
36import Text.Pandoc.Templates
37import Text.Pandoc.Readers.TeXMath
37import Text.Pandoc.Readers.TeXMath
38−import Text.Pandoc.Highlighting ( highlightHtml, defaultHighlightingCss )
38+import Text.Pandoc.Highlighting ( highlight, pygments, styleToHtml,
39+ formatHtmlInline, formatHtmlBlock )
39import Text.Pandoc.XML (stripTags, escapeStringForXML)
40import Text.Pandoc.XML (stripTags, escapeStringForXML)
40import Network.HTTP ( urlEncode )
41import Network.HTTP ( urlEncode )
41import Numeric ( showHex )
42import Numeric ( showHex )
@@ -153,7 +154,7 @@ pandocToHtml opts (Pandoc (Meta title' authors' date') blocks) = do
153 ("/*<![CDATA[*/\n" ++ s ++ "/*]]>*/\n")
154 ("/*<![CDATA[*/\n" ++ s ++ "/*]]>*/\n")
154 Nothing -> mempty
155 Nothing -> mempty
155 else mempty
156 else mempty
156− let newvars = [("highlighting-css", defaultHighlightingCss) |
157+ let newvars = [("highlighting-css", renderHtml $ styleToHtml pygments) |
157 stHighlighting st] ++
158 stHighlighting st] ++
158 [("math", renderHtml math) | stMath st]
159 [("math", renderHtml math) | stMath st]
159 return (tit, auths, date, toc, thebody, newvars)
160 return (tit, auths, date, toc, thebody, newvars)
@@ -361,16 +362,21 @@ blockToHtml _ (RawBlock "html" str) = return $ preEscapedString str
361blockToHtml _ (RawBlock _ _) = return mempty
362blockToHtml _ (RawBlock _ _) = return mempty
362blockToHtml _ (HorizontalRule) = return H.hr
363blockToHtml _ (HorizontalRule) = return H.hr
363blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
364blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
364− let classes' = if writerLiterateHaskell opts
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"
365− then classes
371+ else c) classes
366 else filter (/= "literate") classes
372 else filter (/= "literate") classes
373+ adjCode = if tolhs
374+ then unlines . map ("> " ++) . lines $ rawCode
375+ else rawCode
367− case highlightHtml False (id',classes',keyvals) rawCode of
376+ case highlight formatHtmlBlock (id',classes,keyvals) adjCode of
368 Nothing -> let attrs = attrsToHtml opts (id', classes', keyvals)
377 Nothing -> let attrs = attrsToHtml opts (id', classes', keyvals)
369− addBird = if "literate" `elem` classes'
370− then unlines . map ("> " ++) . lines
371− else unlines . lines
372 in return $ foldl (!) H.pre attrs $ H.code
378 in return $ foldl (!) H.pre attrs $ H.code
373− $ toHtml $ addBird rawCode
379+ $ toHtml adjCode
374 Just h -> modify (\st -> st{ stHighlighting = True }) >>
380 Just h -> modify (\st -> st{ stHighlighting = True }) >>
375 return h
381 return h
376blockToHtml opts (BlockQuote blocks) =
382blockToHtml opts (BlockQuote blocks) =
@@ -540,7 +546,7 @@ inlineToHtml opts inline =
540 (Apostrophe) -> return $ strToHtml "’"
546 (Apostrophe) -> return $ strToHtml "’"
541 (Emph lst) -> inlineListToHtml opts lst >>= return . H.em
547 (Emph lst) -> inlineListToHtml opts lst >>= return . H.em
542 (Strong lst) -> inlineListToHtml opts lst >>= return . H.strong
548 (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
544 Nothing -> return
550 Nothing -> return
545 $ foldl (!) H.code (attrsToHtml opts attr)
551 $ foldl (!) H.code (attrsToHtml opts attr)
546 $ strToHtml str
552 $ strToHtml str
src/Text/Pandoc/Writers/LaTeX.hs+7−5
@@ -41,7 +41,8 @@ import Data.Char ( toLower, isPunctuation )
41import Control.Monad.State
41import Control.Monad.State
42import Text.Pandoc.Pretty
42import Text.Pandoc.Pretty
43import System.FilePath (dropExtension)
43import System.FilePath (dropExtension)
44−import Text.Pandoc.Highlighting (highlightLaTeX, defaultLaTeXMacros)
44+import Text.Pandoc.Highlighting (highlight, pygments, styleToLaTeX,
45+ formatLaTeXInline, formatLaTeXBlock)
45
46
46data WriterState =
47data WriterState =
47 WriterState { stInNote :: Bool -- true if we're in a note
48 WriterState { stInNote :: Bool -- true if we're in a note
@@ -60,7 +61,7 @@ data WriterState =
60 , stLHS :: Bool -- true if document has literate haskell code
61 , stLHS :: Bool -- true if document has literate haskell code
61 , stBook :: Bool -- true if document uses book or memoir class
62 , stBook :: Bool -- true if document uses book or memoir class
62 , stCsquotes :: Bool -- true if document uses csquotes
63 , 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
64 }
65 }
65
66
66-- | Convert Pandoc to LaTeX.
67-- | Convert Pandoc to LaTeX.
@@ -131,7 +132,7 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
131 [ ("graphics", "yes") | stGraphics st ] ++
132 [ ("graphics", "yes") | stGraphics st ] ++
132 [ ("book-class", "yes") | stBook st] ++
133 [ ("book-class", "yes") | stBook st] ++
133 [ ("listings", "yes") | writerListings options || stLHS st ] ++
134 [ ("listings", "yes") | writerListings options || stLHS st ] ++
134− [ ("highlighting-macros", defaultLaTeXMacros) | stHighlighting st ] ++
135+ [ ("highlighting-macros", styleToLaTeX pygments) | stHighlighting st ] ++
135 citecontext
136 citecontext
136 return $ if writerStandalone options
137 return $ if writerStandalone options
137 then renderTemplate context template
138 then renderTemplate context template
@@ -160,6 +161,7 @@ stringToLaTeX isUrl = escapeStringUsing latexEscapes
160 , ('\x2019', "'")
161 , ('\x2019', "'")
161 , ('\x201C', "``")
162 , ('\x201C', "``")
162 , ('\x201D', "''")
163 , ('\x201D', "''")
164+ , ('\x2026', "\\ldots{}")
163 ]
165 ]
164
166
165-- | Puts contents into LaTeX command.
167-- | Puts contents into LaTeX command.
@@ -231,7 +233,7 @@ blockToLaTeX (CodeBlock (_,classes,keyvalAttr) str) = do
231 return $ flush ("\\begin{lstlisting}" <> printParams $$ text str $$
233 return $ flush ("\\begin{lstlisting}" <> printParams $$ text str $$
232 "\\end{lstlisting}") $$ cr
234 "\\end{lstlisting}") $$ cr
233 highlightedCodeBlock =
235 highlightedCodeBlock =
234− case highlightLaTeX False ("",classes,keyvalAttr) str of
236+ case highlight formatLaTeXBlock ("",classes,keyvalAttr) str of
235 Nothing -> rawCodeBlock
237 Nothing -> rawCodeBlock
236 Just h -> modify (\st -> st{ stHighlighting = True }) >>
238 Just h -> modify (\st -> st{ stHighlighting = True }) >>
237 return (text h)
239 return (text h)
@@ -407,7 +409,7 @@ inlineToLaTeX (Code (_,classes,_) str) = do
407 let chr = ((enumFromTo '!' '~') \\ str) !! 0
409 let chr = ((enumFromTo '!' '~') \\ str) !! 0
408 return $ text $ "\\lstinline" ++ [chr] ++ str ++ [chr]
410 return $ text $ "\\lstinline" ++ [chr] ++ str ++ [chr]
409 highlightCode = do
411 highlightCode = do
410− case highlightLaTeX True ("",classes,[]) str of
412+ case highlight formatLaTeXInline ("",classes,[]) str of
411 Nothing -> rawCode
413 Nothing -> rawCode
412 Just h -> modify (\st -> st{ stHighlighting = True }) >>
414 Just h -> modify (\st -> st{ stHighlighting = True }) >>
413 return (text h)
415 return (text h)