git.iko.soy/typeable/pandoc
on master help sign in
Added preliminary support for syntax highlighting to HTML writer.
b370902fiddlosopher
Text/Pandoc/Writers/HTML.hs+14−4
@@ -36,10 +36,11 @@ import Text.Pandoc.Readers.TeXMath
36import Text.Regex ( mkRegex, matchRegex )
36import Text.Regex ( mkRegex, matchRegex )
37import Numeric ( showHex )
37import Numeric ( showHex )
38import Data.Char ( ord, toLower, isAlpha )
38import Data.Char ( ord, toLower, isAlpha )
39−import Data.List ( isPrefixOf, intersperse )
39+import Data.List ( isPrefixOf, intersperse, find )
40import qualified Data.Set as S
40import qualified Data.Set as S
41import Control.Monad.State
41import Control.Monad.State
42import Text.XHtml.Transitional
42import Text.XHtml.Transitional
43+import Text.Highlighting.Kate
43
44
44data WriterState = WriterState
45data WriterState = WriterState
45 { stNotes :: [Html] -- ^ List of notes
46 { stNotes :: [Html] -- ^ List of notes
@@ -263,9 +264,18 @@ blockToHtml opts (Plain lst) = inlineListToHtml opts lst
263blockToHtml opts (Para lst) = inlineListToHtml opts lst >>= (return . paragraph)
264blockToHtml opts (Para lst) = inlineListToHtml opts lst >>= (return . paragraph)
264blockToHtml opts (RawHtml str) = return $ primHtml str
265blockToHtml opts (RawHtml str) = return $ primHtml str
265blockToHtml opts (HorizontalRule) = return $ hr
266blockToHtml opts (HorizontalRule) = return $ hr
266−blockToHtml opts (CodeBlock (_,classes,_) str) = return $
267+blockToHtml opts (CodeBlock (_,classes,_) rawCode) = do
268+ let fmtOpts =
269+ case find (`elem` ["number","numberLines","number-lines"]) classes of
270+ Nothing -> []
271+ Just _ -> [OptNumberLines]
267− pre ! (if null classes then [] else [theclass $ unwords classes]) $
272+ let toPre str = pre ! (if null classes then [] else [theclass $ unwords classes]) $ thecode << str
268− thecode << (str ++ "\n") -- the final \n for consistency with Markdown.pl
273+ let lcLanguages = map (map toLower) languages
274+ return $ case find (\c -> (map toLower c) `elem` lcLanguages) classes of
275+ Nothing -> toPre (rawCode ++ "\n")
276+ Just lang -> case highlightAs lang rawCode of
277+ Left _ -> toPre (rawCode ++ "\n")
278+ Right hl -> formatAsXHtml fmtOpts lang hl
269blockToHtml opts (BlockQuote blocks) =
279blockToHtml opts (BlockQuote blocks) =
270 -- in S5, treat list in blockquote specially
280 -- in S5, treat list in blockquote specially
271 -- if default is incremental, make it nonincremental;
281 -- if default is incremental, make it nonincremental;