git.iko.soy/typeable/pandoc
on master help sign in
HTML reader: only treat "a" element as link if it has href.
50f0cfcJohn MacFarlane
src/Text/Pandoc/Readers/HTML.hs+19−7
@@ -599,19 +599,31 @@ pLineBreak = do
599 pSelfClosing (=="br") (const True)
599 pSelfClosing (=="br") (const True)
600 return B.linebreak
600 return B.linebreak
601
601
602+-- Unlike fromAttrib from tagsoup, this distinguishes
603+-- between a missing attribute and an attribute with empty content.
604+maybeFromAttrib :: String -> Tag String -> Maybe String
605+maybeFromAttrib name (TagOpen _ attrs) = lookup name attrs
606+maybeFromAttrib _ _ = Nothing
607+
602pLink :: TagParser Inlines
608pLink :: TagParser Inlines
603pLink = try $ do
609pLink = try $ do
604 tag <- pSatisfy $ tagOpenLit "a" (const True)
610 tag <- pSatisfy $ tagOpenLit "a" (const True)
605− mbBaseHref <- baseHref <$> getState
606− let url' = fromAttrib "href" tag
607− let url = case (parseURIReference url', mbBaseHref) of
608− (Just rel, Just bs) -> show (rel `nonStrictRelativeTo` bs)
609− _ -> url'
610 let title = fromAttrib "title" tag
611 let title = fromAttrib "title" tag
612+ -- take id from id attribute if present, otherwise name
611− let uid = fromAttrib "id" tag
613+ let uid = maybe (fromAttrib "name" tag) id $ maybeFromAttrib "id" tag
612 let cls = words $ fromAttrib "class" tag
614 let cls = words $ fromAttrib "class" tag
613 lab <- trimInlines . mconcat <$> manyTill inline (pCloses "a")
615 lab <- trimInlines . mconcat <$> manyTill inline (pCloses "a")
616+ -- check for href; if href, then a link, otherwise a span
617+ case maybeFromAttrib "href" tag of
618+ Nothing ->
619+ return $ B.spanWith (uid, cls, []) lab
620+ Just url' -> do
621+ mbBaseHref <- baseHref <$> getState
622+ let url = case (parseURIReference url', mbBaseHref) of
623+ (Just rel, Just bs) ->
624+ show (rel `nonStrictRelativeTo` bs)
625+ _ -> url'
614− return $ B.linkWith (uid, cls, []) (escapeURI url) title lab
626+ return $ B.linkWith (uid, cls, []) (escapeURI url) title lab
615
627
616pImage :: TagParser Inlines
628pImage :: TagParser Inlines
617pImage = do
629pImage = do
tests/Tests/Readers/HTML.hs+4
@@ -26,4 +26,8 @@ tests = [ testGroup "base tag"
26 "<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"http://example.com/stickman.gif\" alt=\"Stickman\"></head>" =?>
26 "<head><base href=\"http://www.w3schools.com/images/\" ></head><body><img src=\"http://example.com/stickman.gif\" alt=\"Stickman\"></head>" =?>
27 plain (image "http://example.com/stickman.gif" "" (text "Stickman"))
27 plain (image "http://example.com/stickman.gif" "" (text "Stickman"))
28 ]
28 ]
29+ , testGroup "anchors"
30+ [ test html "anchor without href" $ "<a name=\"anchor\"/>" =?>
31+ plain (spanWith ("anchor",[],[]) mempty)
32+ ]
29 ]
33 ]