git.iko.soy/typeable/pandoc
on master help sign in
HTML writer: keep attributes from code nested below pre tag.
1843a87Albert Krewinkel
src/Text/Pandoc/Readers/HTML.hs+12−1
@@ -557,7 +557,18 @@ pFigure = try $ do
557pCodeBlock :: PandocMonad m => TagParser m Blocks
557pCodeBlock :: PandocMonad m => TagParser m Blocks
558pCodeBlock = try $ do
558pCodeBlock = try $ do
559 TagOpen _ attr' <- pSatisfy (matchTagOpen "pre" [])
559 TagOpen _ attr' <- pSatisfy (matchTagOpen "pre" [])
560− let attr = toAttr attr'
560+ -- if the `pre` has no attributes, try if it is followed by a `code`
561+ -- element and use those attributes if possible.
562+ attr <- case attr' of
563+ _:_ -> pure (toAttr attr')
564+ [] -> option nullAttr $ do
565+ TagOpen _ codeAttr <- pSatisfy (matchTagOpen "code" [])
566+ pure $ toAttr
567+ [ (k, v') | (k, v) <- codeAttr
568+ -- strip language from class
569+ , let v' = if k == "class"
570+ then fromMaybe v (T.stripPrefix "language-" v)
571+ else v ]
561 contents <- manyTill pAny (pCloses "pre" <|> eof)
572 contents <- manyTill pAny (pCloses "pre" <|> eof)
562 let rawText = T.concat $ map tagToText contents
573 let rawText = T.concat $ map tagToText contents
563 -- drop leading newline if any
574 -- drop leading newline if any
test/Tests/Readers/HTML.hs+11
@@ -108,6 +108,17 @@ tests = [ testGroup "base tag"
108 "<header id=\"title\">Title</header>" =?>
108 "<header id=\"title\">Title</header>" =?>
109 divWith ("title", mempty, mempty) (plain "Title")
109 divWith ("title", mempty, mempty) (plain "Title")
110 ]
110 ]
111+ , testGroup "code block"
112+ [ test html "attributes in pre > code element" $
113+ "<pre><code id=\"a\" class=\"python\">\nprint('hi')\n</code></pre>"
114+ =?>
115+ codeBlockWith ("a", ["python"], []) "print('hi')"
116+
117+ , test html "attributes in pre take precendence" $
118+ "<pre id=\"c\"><code id=\"d\">\nprint('hi mom!')\n</code></pre>"
119+ =?>
120+ codeBlockWith ("c", [], []) "print('hi mom!')"
121+ ]
111 , askOption $ \(QuickCheckTests numtests) ->
122 , askOption $ \(QuickCheckTests numtests) ->
112 testProperty "Round trip" $
123 testProperty "Round trip" $
113 withMaxSuccess (if QuickCheckTests numtests == defaultValue
124 withMaxSuccess (if QuickCheckTests numtests == defaultValue