git.iko.soy/typeable/pandoc
on master help sign in
Change the implementation of `htmlSpanLikeElements` and implement `<dfn>` (#5882)

* Add HTML Reader support for `<dfn>`, parsing this as a Span with class `dfn`. * Change `htmlSpanLikeElements` implementation to retain classes, attributes and inline content.

commitbf2eb4f288df5d5b5e054cdc4ffa3b3c4dd187fa authorFlorian Beeres <yuuki@protonmail.com> dateMon 11 Nov 2019, 17:55 (7 years ago) parent3bf5362
4 files changed+36−7
src/Text/Pandoc/Readers/HTML.hs+11−4 src/Text/Pandoc/Shared.hs+1−1 src/Text/Pandoc/Writers/HTML.hs+4−2 test/command/5795.md+20
src/Text/Pandoc/Readers/HTML.hs+11−4
@@ -719,10 +719,17 @@ pSubscript :: PandocMonad m => TagParser m Inlines
719719pSubscript = pInlinesInTags "sub" B.subscript
720720
721721pSpanLike :: PandocMonad m => TagParser m Inlines
722−pSpanLike = Set.foldr
723− (\tag acc -> acc <|> pInlinesInTags tag (B.spanWith ("",[T.unpack tag],[])))
724− mzero
725− htmlSpanLikeElements
722+pSpanLike =
723+ Set.foldr
724+ (\tagName acc -> acc <|> parseTag tagName)
725+ mzero
726+ htmlSpanLikeElements
727+ where
728+ parseTag tagName = do
729+ TagOpen _ attrs <- pSatisfy $ tagOpenLit tagName (const True)
730+ let (ids, cs, kvs) = mkAttr . toStringAttr $ attrs
731+ content <- mconcat <$> manyTill inline (pCloses tagName <|> eof)
732+ return $ B.spanWith (ids, T.unpack tagName : cs, kvs) content
726733
727734pSmall :: PandocMonad m => TagParser m Inlines
728735pSmall = pInlinesInTags "small" (B.spanWith ("",["small"],[]))
src/Text/Pandoc/Shared.hs+1−1
@@ -702,7 +702,7 @@ underlineSpan = B.spanWith ("", ["underline"], [])
702702-- | Set of HTML elements that are represented as Span with a class equal as
703703-- the element tag itself.
704704htmlSpanLikeElements :: Set.Set T.Text
705−htmlSpanLikeElements = Set.fromList [T.pack "kbd", T.pack "mark"]
705+htmlSpanLikeElements = Set.fromList [T.pack "kbd", T.pack "mark", T.pack "dfn"]
706706
707707-- | Returns the first sentence in a list of inlines, and the rest.
708708breakSentence :: [Inline] -> ([Inline], [Inline])
src/Text/Pandoc/Writers/HTML.hs+4−2
@@ -998,13 +998,15 @@ inlineToHtml opts inline = do
998998
999999 (Span (id',classes,kvs) ils) ->
10001000 let spanLikeTag = case classes of
1001− [c] -> do
1001+ (c:_) -> do
10021002 let c' = T.pack c
10031003 guard (c' `Set.member` htmlSpanLikeElements)
10041004 pure $ customParent (textTag c')
10051005 _ -> Nothing
10061006 in case spanLikeTag of
1007− Just tag -> tag <$> inlineListToHtml opts ils
1007+ Just tag -> do
1008+ h <- inlineListToHtml opts ils
1009+ addAttrs opts (id',tail classes',kvs') $ tag h
10081010 Nothing -> do
10091011 h <- inlineListToHtml opts ils
10101012 addAttrs opts (id',classes',kvs') (H.span h)
test/command/5795.mdnew file+20
@@ -0,0 +1,20 @@
1+```
2+% pandoc -f html -t html
3+<dfn class="dfn" id="foo" title="bax"><span>foo</span></dfn>
4+^D
5+<dfn id="foo" class="dfn" title="bax"><span>foo</span></dfn>
6+```
7+
8+```
9+% pandoc -f html -t native
10+<dfn class="dfn" id="foo" title="bax"><span>foo</span></dfn>
11+^D
12+[Plain [Span ("foo",["dfn","dfn"],[("title","bax")]) [Span ("",[],[]) [Str "foo"]]]]
13+```
14+
15+```
16+% pandoc -f native -t html
17+[Plain [Span ("foo",["dfn","dfn"],[("title","bax")]) [Span ("",[],[]) [Str "foo"]]]]
18+^D
19+<dfn id="foo" class="dfn" title="bax"><span>foo</span></dfn>
20+```