From 7fdc01ac0d91a037b48847939b762654174b4125 Mon Sep 17 00:00:00 2001 From: Elliot Bobrow <77182873+ebobrow@users.noreply.github.com> Date: Wed, 6 Jul 2022 13:10:24 -0700 Subject: [PATCH] Use `formatCode` from #7525 in HTML and MediaWiki (#8162) Move formatting from inside inline code elements to the outside in order to retain formatting. --- src/Text/Pandoc/Readers/HTML.hs | 14 ++++++++------ src/Text/Pandoc/Readers/MediaWiki.hs | 12 ++---------- test/command/7525.md | 14 ++++++++++++++ test/epub/features.native | 14 ++++++++++++-- test/epub/formatting.native | 7 ++++++- 5 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/Text/Pandoc/Readers/HTML.hs b/src/Text/Pandoc/Readers/HTML.hs index 47b6af193..dd0e54c27 100644 --- a/src/Text/Pandoc/Readers/HTML.hs +++ b/src/Text/Pandoc/Readers/HTML.hs @@ -63,7 +63,7 @@ import Text.Pandoc.Options ( import Text.Pandoc.Parsing hiding ((<|>)) import Text.Pandoc.Shared ( addMetaField, blocksToInlines', escapeURI, extractSpaces, - htmlSpanLikeElements, renderTags', safeRead, tshow) + htmlSpanLikeElements, renderTags', safeRead, tshow, formatCode) import Text.Pandoc.Walk import Text.Parsec.Error import Text.TeXMath (readMathML, writeTeX) @@ -786,18 +786,20 @@ pSvg = do pCodeWithClass :: PandocMonad m => Text -> Text -> TagParser m Inlines pCodeWithClass name class' = try $ do TagOpen open attr' <- pSatisfy $ tagOpen (== name) (const True) - result <- manyTill pAny (pCloses open) let (ids,cs,kvs) = toAttr attr' cs' = class' : cs - return . B.codeWith (ids,cs',kvs) . - T.unwords . T.lines . innerText $ result + code open (ids,cs',kvs) pCode :: PandocMonad m => TagParser m Inlines pCode = try $ do (TagOpen open attr') <- pSatisfy $ tagOpen (`elem` ["code","tt"]) (const True) let attr = toAttr attr' - result <- manyTill pAny (pCloses open) - return $ B.codeWith attr $ T.unwords $ T.lines $ innerText result + code open attr + +code :: PandocMonad m => Text -> Attr -> TagParser m Inlines +code open attr = do + result <- mconcat <$> manyTill inline (pCloses open) + return $ formatCode attr result -- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/bdo -- Bidirectional Text Override diff --git a/src/Text/Pandoc/Readers/MediaWiki.hs b/src/Text/Pandoc/Readers/MediaWiki.hs index 7a406ec4b..2dc289f18 100644 --- a/src/Text/Pandoc/Readers/MediaWiki.hs +++ b/src/Text/Pandoc/Readers/MediaWiki.hs @@ -37,8 +37,7 @@ import Text.Pandoc.Options import Text.Pandoc.Parsing hiding (nested, tableCaption) import Text.Pandoc.Readers.HTML (htmlTag, isBlockTag, isCommentTag) import Text.Pandoc.Shared (safeRead, stringify, stripTrailingNewlines, - trim, splitTextBy, tshow) -import Text.Pandoc.Walk (walk) + trim, splitTextBy, tshow, formatCode) import Text.Pandoc.XML (fromEntities) -- | Read mediawiki from an input string and return a Pandoc document. @@ -392,14 +391,7 @@ preformatted = try $ do else return $ B.para $ encode contents encode :: Inlines -> Inlines -encode = B.fromList . normalizeCode . B.toList . walk strToCode - where strToCode (Str s) = Code ("",[],[]) s - strToCode Space = Code ("",[],[]) " " - strToCode x = x - normalizeCode [] = [] - normalizeCode (Code a1 x : Code a2 y : zs) | a1 == a2 = - normalizeCode $ Code a1 (x <> y) : zs - normalizeCode (x:xs) = x : normalizeCode xs +encode = formatCode nullAttr header :: PandocMonad m => MWParser m Blocks header = try $ do diff --git a/test/command/7525.md b/test/command/7525.md index 788c749a6..0283b19a8 100644 --- a/test/command/7525.md +++ b/test/command/7525.md @@ -17,3 +17,17 @@ ] ] ``` + +``` +% pandoc -f html -t native +hi +^D +[ Plain [ Strong [ Code ( "" , [] , [] ) "hi" ] ] ] +``` + +``` +% pandoc -f mediawiki -t native +''hey'' +^D +[ Para [ Emph [ Code ( "" , [] , [] ) "hey" ] ] ] +``` diff --git a/test/epub/features.native b/test/epub/features.native index 61d0f8ba9..659c9692e 100644 --- a/test/epub/features.native +++ b/test/epub/features.native @@ -1505,7 +1505,12 @@ , Space , Str "the" , Space - , Code ( "" , [] , [] ) "epub:switch" + , Link + ( "" , [] , [] ) + [ Code ( "" , [] , [] ) "epub:switch" ] + ( "http://idpf.org/epub/30/spec/epub30-contentdocs.html#sec-xhtml-content-switch" + , "" + ) , Space , Str "element" , Space @@ -1615,7 +1620,12 @@ , Space , Str "an" , Space - , Code ( "" , [] , [] ) "epub:case" + , Link + ( "" , [] , [] ) + [ Code ( "" , [] , [] ) "epub:case" ] + ( "http://idpf.org/epub/30/spec/epub30-contentdocs.html#sec-xhtml-epub-case" + , "" + ) , Space , Str "element." ] diff --git a/test/epub/formatting.native b/test/epub/formatting.native index cab9e40fa..41559d1bd 100644 --- a/test/epub/formatting.native +++ b/test/epub/formatting.native @@ -605,7 +605,12 @@ , Space , Str "the" , Space - , Code ( "" , [] , [] ) "CSS Multi-Column Layout" + , Link + ( "" , [] , [] ) + [ Code ( "" , [] , [] ) "CSS Multi-Column Layout" ] + ( "http://idpf.org/epub/30/spec/epub30-contentdocs.html#sec-css-multi-column" + , "" + ) , Space , Str "properties" , Space