git.iko.soy/typeable/pandoc
on master help sign in
HTML reader/writer: Better handling of <q> with cite attribute (#5837)

* HTML reader: Handle cite attribute for quotes. If a `<q>` tag has a `cite` attribute, we interpret it as a Quoted element with an inner Span. Closes #5798 * Refactor url canonicalization into a helper function * Modify HTML writer to handle quote with cite. [0]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/q

commit45479114e88a351fc63bb8eff142bb10cfd2c661 authorOle Martin Ruud <barskern@outlook.com> dateFri 25 Oct 2019, 07:27 (7 years ago) parent91c325c
5 files changed+69−25
src/Text/Pandoc/Readers/HTML.hs+34−23 src/Text/Pandoc/Writers/HTML.hs+10−2 test/Tests/Writers/HTML.hs+17 test/html-reader.html+4 test/html-reader.native+4
src/Text/Pandoc/Readers/HTML.hs+34−23
@@ -684,19 +684,27 @@ pSelfClosing f g = do
684684 return open
685685
686686pQ :: PandocMonad m => TagParser m Inlines
687−pQ = do
688− context <- asks quoteContext
689− let quoteType = case context of
690− InDoubleQuote -> SingleQuote
691− _ -> DoubleQuote
692− let innerQuoteContext = if quoteType == SingleQuote
693− then InSingleQuote
694− else InDoubleQuote
695− let constructor = case quoteType of
696− SingleQuote -> B.singleQuoted
697− DoubleQuote -> B.doubleQuoted
698− withQuoteContext innerQuoteContext $
699− pInlinesInTags "q" constructor
687+pQ = choice $ map try [citedQuote, normalQuote]
688+ where citedQuote = do
689+ tag <- pSatisfy $ tagOpenLit "q" (any ((=="cite") . fst))
690+
691+ url <- canonicalizeUrl $ T.unpack $ fromAttrib "cite" tag
692+ let uid = fromMaybe (T.unpack $ fromAttrib "name" tag) $
693+ maybeFromAttrib "id" tag
694+ let cls = words $ T.unpack $ fromAttrib "class" tag
695+
696+ makeQuote $ B.spanWith (uid, cls, [("cite", escapeURI url)])
697+ normalQuote = do
698+ pSatisfy $ tagOpenLit "q" (const True)
699+ makeQuote id
700+ makeQuote wrapper = do
701+ ctx <- asks quoteContext
702+ let (constructor, innerContext) = case ctx of
703+ InDoubleQuote -> (B.singleQuoted, InSingleQuote)
704+ _ -> (B.doubleQuoted, InDoubleQuote)
705+
706+ content <- withQuoteContext innerContext (mconcat <$> manyTill inline (pCloses "q"))
707+ return $ extractSpaces (constructor . wrapper) content
700708
701709pEmph :: PandocMonad m => TagParser m Inlines
702710pEmph = pInlinesInTags "em" B.emph <|> pInlinesInTags "i" B.emph
@@ -757,21 +765,13 @@ pLink = try $ do
757765 Nothing ->
758766 return $ extractSpaces (B.spanWith (uid, cls, [])) lab
759767 Just url' -> do
760− mbBaseHref <- baseHref <$> getState
761− let url = case (parseURIReference url', mbBaseHref) of
762− (Just rel, Just bs) ->
763− show (rel `nonStrictRelativeTo` bs)
764− _ -> url'
768+ url <- canonicalizeUrl url'
765769 return $ extractSpaces (B.linkWith (uid, cls, []) (escapeURI url) title) lab
766770
767771pImage :: PandocMonad m => TagParser m Inlines
768772pImage = do
769773 tag <- pSelfClosing (=="img") (isJust . lookup "src")
770− mbBaseHref <- baseHref <$> getState
771− let url' = T.unpack $ fromAttrib "src" tag
772− let url = case (parseURIReference url', mbBaseHref) of
773− (Just rel, Just bs) -> show (rel `nonStrictRelativeTo` bs)
774− _ -> url'
774+ url <- canonicalizeUrl $ T.unpack $ fromAttrib "src" tag
775775 let title = T.unpack $ fromAttrib "title" tag
776776 let alt = T.unpack $ fromAttrib "alt" tag
777777 let uid = T.unpack $ fromAttrib "id" tag
@@ -1292,6 +1292,17 @@ isSpace '\n' = True
12921292isSpace '\r' = True
12931293isSpace _ = False
12941294
1295+-- Utilities
1296+
1297+-- | Adjusts a url according to the document's base URL.
1298+canonicalizeUrl :: PandocMonad m => String -> TagParser m String
1299+canonicalizeUrl url = do
1300+ mbBaseHref <- baseHref <$> getState
1301+ return $ case (parseURIReference url, mbBaseHref) of
1302+ (Just rel, Just bs) -> show (rel `nonStrictRelativeTo` bs)
1303+ _ -> url
1304+
1305+
12951306-- Instances
12961307
12971308instance HasMacros HTMLState where
src/Text/Pandoc/Writers/HTML.hs+10−2
@@ -1,4 +1,5 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE LambdaCase #-}
23{-# LANGUAGE MultiWayIf #-}
34{-# LANGUAGE NoImplicitPrelude #-}
45{-# LANGUAGE OverloadedStrings #-}
@@ -1047,10 +1048,17 @@ inlineToHtml opts inline = do
10471048 strToHtml "’")
10481049 DoubleQuote -> (strToHtml "“",
10491050 strToHtml "”")
1050− in if writerHtmlQTags opts
1051+
1052+ in if writerHtmlQTags opts
10511053 then do
10521054 modify $ \st -> st{ stQuotes = True }
1053− H.q `fmap` inlineListToHtml opts lst
1055+ let (maybeAttr, lst') = case lst of
1056+ [Span attr@(_, _, kvs) cs]
1057+ | any ((=="cite") . fst) kvs
1058+ -> (Just attr, cs)
1059+ cs -> (Nothing, cs)
1060+ H.q `fmap` inlineListToHtml opts lst'
1061+ >>= maybe return (addAttrs opts) maybeAttr
10541062 else (\x -> leftQuote >> x >> rightQuote)
10551063 `fmap` inlineListToHtml opts lst
10561064 (Math t str) -> do
test/Tests/Writers/HTML.hs+17
@@ -13,6 +13,11 @@ import Text.Pandoc.Builder
1313html :: (ToPandoc a) => a -> String
1414html = unpack . purely (writeHtml4String def{ writerWrapText = WrapNone }) . toPandoc
1515
16+htmlQTags :: (ToPandoc a) => a -> String
17+htmlQTags = unpack
18+ . purely (writeHtml4String def{ writerWrapText = WrapNone, writerHtmlQTags = True })
19+ . toPandoc
20+
1621{-
1722 "my test" =: X =?> Y
1823
@@ -48,4 +53,16 @@ tests = [ testGroup "inline code"
4853 definitionList [(mempty, [para $ text "foo bar"])]
4954 =?> "<dl><dt></dt><dd><p>foo bar</p></dd></dl>"
5055 ]
56+ , testGroup "quotes"
57+ [ "quote with cite attribute (without q-tags)" =:
58+ doubleQuoted (spanWith ("", [], [("cite", "http://example.org")]) (str "examples"))
59+ =?> "“<span cite=\"http://example.org\">examples</span>”"
60+ , tQ "quote with cite attribute (with q-tags)" $
61+ doubleQuoted (spanWith ("", [], [("cite", "http://example.org")]) (str "examples"))
62+ =?> "<q cite=\"http://example.org\">examples</q>"
63+ ]
5164 ]
65+ where
66+ tQ :: (ToString a, ToPandoc a)
67+ => String -> (a, String) -> TestTree
68+ tQ = test htmlQTags
test/html-reader.html+4
@@ -81,6 +81,10 @@ span.pandocNoteMarker { }
8181</blockquote>
8282<p>And a following paragraph.</p>
8383<hr />
84+<h1>Inline quotes</h1>
85+<p>Normal text but then a <q cite="https://www.imdb.com/title/tt0062622/quotes/qt0396921">inline quote</q>.</p>
86+<p><q>Missing a cite attribute means its just normal text</q></p>
87+<hr />
8488<h1>Code Blocks</h1>
8589<p>Code:</p>
8690<pre><code>---- (should be four hyphens)
test/html-reader.native+4
@@ -51,6 +51,10 @@ Pandoc (Meta {unMeta = fromList [("generator",MetaInlines [Str "pandoc"]),("titl
5151 [Para [Str "Don't",Space,Str "quote",Space,Str "me."]]]
5252,Para [Str "And",Space,Str "a",Space,Str "following",Space,Str "paragraph."]
5353,HorizontalRule
54+,Header 1 ("inline-quotes",[],[]) [Str "Inline",Space,Str "quotes"]
55+,Para [Str "Normal",Space,Str "text",Space,Str "but",Space,Str "then",Space,Str "a",Space,Quoted DoubleQuote [Span ("",[],[("cite","https://www.imdb.com/title/tt0062622/quotes/qt0396921")]) [Str "inline",Space,Str "quote"]],Str "."]
56+,Para [Quoted DoubleQuote [Str "Missing",Space,Str "a",Space,Str "cite",Space,Str "attribute",Space,Str "means",Space,Str "its",Space,Str "just",Space,Str "normal",Space,Str "text"]]
57+,HorizontalRule
5458,Header 1 ("code-blocks",[],[]) [Str "Code",Space,Str "Blocks"]
5559,Para [Str "Code:"]
5660,CodeBlock ("",[],[]) "---- (should be four hyphens)\n\nsub status {\n print \"working\";\n}\n\nthis code block is indented by one tab"