From 856587ff63b1e89c71b73a367f5ba1730050e6dd Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 2 Nov 2017 16:02:04 -0700 Subject: [PATCH] Use latest skylighting; ensure no duplicate ids on code lines. The line identifiers are built using the code block's identifier as a prefix. If the code block has null identifier, we use "cb1", "cb2", etc. Closes #4031. --- pandoc.cabal | 2 +- src/Text/Pandoc/Highlighting.hs | 7 +++++-- src/Text/Pandoc/Writers/HTML.hs | 14 +++++++++++--- stack.yaml | 2 +- test/lhs-test.html | 6 +++--- test/lhs-test.html+lhs | 6 +++--- 6 files changed, 24 insertions(+), 13 deletions(-) diff --git a/pandoc.cabal b/pandoc.cabal index a52f89492..322a6bcaa 100644 --- a/pandoc.cabal +++ b/pandoc.cabal @@ -320,7 +320,7 @@ library tagsoup >= 0.13.7 && < 0.15, base64-bytestring >= 0.1 && < 1.1, zlib >= 0.5 && < 0.7, - skylighting >= 0.4.2 && <0.5, + skylighting >= 0.4.3 && <0.5, data-default >= 0.4 && < 0.8, temporary >= 1.1 && < 1.3, blaze-html >= 0.5 && < 0.10, diff --git a/src/Text/Pandoc/Highlighting.hs b/src/Text/Pandoc/Highlighting.hs index cc72967e4..cd8c5fd4b 100644 --- a/src/Text/Pandoc/Highlighting.hs +++ b/src/Text/Pandoc/Highlighting.hs @@ -81,12 +81,15 @@ highlight :: SyntaxMap -> Attr -- ^ Attributes of the CodeBlock -> String -- ^ Raw contents of the CodeBlock -> Either String a -highlight syntaxmap formatter (_, classes, keyvals) rawCode = +highlight syntaxmap formatter (ident, classes, keyvals) rawCode = let firstNum = fromMaybe 1 (safeRead (fromMaybe "1" $ lookup "startFrom" keyvals)) fmtOpts = defaultFormatOpts{ startNumber = firstNum, numberLines = any (`elem` - ["number","numberLines", "number-lines"]) classes } + ["number","numberLines", "number-lines"]) classes, + lineIdPrefix = if null ident + then mempty + else T.pack (ident ++ "-") } tokenizeOpts = TokenizerConfig{ syntaxMap = syntaxmap , traceOutput = False } classes' = map T.pack classes diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index 89cec38a2..1999bdbcf 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -101,6 +101,7 @@ data WriterState = WriterState , stHtml5 :: Bool -- ^ Use HTML5 , stEPUBVersion :: Maybe EPUBVersion -- ^ EPUB version if for epub , stSlideVariant :: HTMLSlideVariant + , stCodeBlockNum :: Int -- ^ Number of code block } defaultWriterState :: WriterState @@ -108,7 +109,8 @@ defaultWriterState = WriterState {stNotes= [], stMath = False, stQuotes = False, stHighlighting = False, stSecNum = [], stElement = False, stHtml5 = False, stEPUBVersion = Nothing, - stSlideVariant = NoSlides} + stSlideVariant = NoSlides, + stCodeBlockNum = 0} -- Helpers to render HTML with the appropriate function. @@ -703,6 +705,12 @@ blockToHtml _ HorizontalRule = do html5 <- gets stHtml5 return $ if html5 then H5.hr else H.hr blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do + id'' <- if null id' + then do + modify $ \st -> st{ stCodeBlockNum = stCodeBlockNum st + 1 } + codeblocknum <- gets stCodeBlockNum + return ("cb" ++ show codeblocknum) + else return id' let tolhs = isEnabled Ext_literate_haskell opts && any (\c -> map toLower c == "haskell") classes && any (\c -> map toLower c == "literate") classes @@ -716,7 +724,7 @@ blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do else rawCode hlCode = if isJust (writerHighlightStyle opts) then highlight (writerSyntaxMap opts) formatHtmlBlock - (id',classes',keyvals) adjCode + (id'',classes',keyvals) adjCode else Left "" case hlCode of Left msg -> do @@ -725,7 +733,7 @@ blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do addAttrs opts (id',classes,keyvals) $ H.pre $ H.code $ toHtml adjCode Right h -> modify (\st -> st{ stHighlighting = True }) >> - addAttrs opts (id',[],keyvals) h + addAttrs opts (id'',[],keyvals) h blockToHtml opts (BlockQuote blocks) = do -- in S5, treat list in blockquote specially -- if default is incremental, make it nonincremental; diff --git a/stack.yaml b/stack.yaml index 5e25529f5..da5869ff3 100644 --- a/stack.yaml +++ b/stack.yaml @@ -9,7 +9,7 @@ packages: extra-deps: - pandoc-types-1.17.2 - hslua-0.9.2 -- skylighting-0.4.2 +- skylighting-0.4.3 - cmark-gfm-0.1.1 - QuickCheck-2.10.0.1 - tasty-quickcheck-0.9.1 diff --git a/test/lhs-test.html b/test/lhs-test.html index 28cfdf7a4..a921c6a56 100644 --- a/test/lhs-test.html +++ b/test/lhs-test.html @@ -72,9 +72,9 @@ code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Infor

lhs test

unsplit is an arrow that takes a pair of values and combines them to return a single value:

-
unsplit :: (Arrow a) => (b -> c -> d) -> a (b, c) d
-
unsplit = arr . uncurry
-
-- arr (\op (x,y) -> x `op` y)
+
unsplit :: (Arrow a) => (b -> c -> d) -> a (b, c) d
+
unsplit = arr . uncurry
+
-- arr (\op (x,y) -> x `op` y)

(***) combines two arrows into a new arrow by running the two arrows on a pair of values (one arrow on the first item of the pair and one arrow on the second item of the pair).

diff --git a/test/lhs-test.html+lhs b/test/lhs-test.html+lhs index f7a8302a4..8d27e6a58 100644 --- a/test/lhs-test.html+lhs +++ b/test/lhs-test.html+lhs @@ -72,9 +72,9 @@ code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Infor

lhs test

unsplit is an arrow that takes a pair of values and combines them to return a single value:

-
> unsplit :: (Arrow a) => (b -> c -> d) -> a (b, c) d
-
> unsplit = arr . uncurry
-
> -- arr (\op (x,y) -> x `op` y)
+
> unsplit :: (Arrow a) => (b -> c -> d) -> a (b, c) d
+
> unsplit = arr . uncurry
+
> -- arr (\op (x,y) -> x `op` y)

(***) combines two arrows into a new arrow by running the two arrows on a pair of values (one arrow on the first item of the pair and one arrow on the second item of the pair).