From af85d14f001cf4c2976ee659ec04101d6b054a4d Mon Sep 17 00:00:00 2001 From: Simon Hengel Date: Sun, 2 Nov 2014 13:54:19 +0800 Subject: Add support for markdown images --- .../src/Documentation/Haddock/Parser.hs | 12 +++++++++-- .../test/Documentation/Haddock/ParserSpec.hs | 24 ++++++++++------------ 2 files changed, 21 insertions(+), 15 deletions(-) (limited to 'haddock-library') diff --git a/haddock-library/src/Documentation/Haddock/Parser.hs b/haddock-library/src/Documentation/Haddock/Parser.hs index f1fd5dda..ff03c7bb 100644 --- a/haddock-library/src/Documentation/Haddock/Parser.hs +++ b/haddock-library/src/Documentation/Haddock/Parser.hs @@ -102,7 +102,7 @@ parseStringBS = parse p where p :: Parser (DocH mod Identifier) p = docConcat <$> many (monospace <|> anchor <|> identifier <|> moduleName - <|> picture <|> hyperlink <|> bold + <|> picture <|> markdownImage <|> hyperlink <|> bold <|> emphasis <|> encodedChar <|> string' <|> skipSpecialChar) @@ -209,6 +209,11 @@ picture :: Parser (DocH mod a) picture = DocPic . makeLabeled Picture . decodeUtf8 <$> disallowNewline ("<<" *> takeUntil ">>") +markdownImage :: Parser (DocH mod a) +markdownImage = fromHyperlink <$> ("!" *> linkParser) + where + fromHyperlink (Hyperlink url label) = DocPic (Picture url label) + -- | Paragraph parser, called by 'parseParas'. paragraph :: Parser (DocH mod Identifier) paragraph = examples <|> skipSpace *> ( @@ -467,7 +472,10 @@ hyperlink = DocHyperlink . makeLabeled Hyperlink . decodeUtf8 <|> markdownLink markdownLink :: Parser (DocH mod a) -markdownLink = DocHyperlink <$> (flip Hyperlink <$> label <*> (whitespace *> url)) +markdownLink = DocHyperlink <$> linkParser + +linkParser :: Parser Hyperlink +linkParser = flip Hyperlink <$> label <*> (whitespace *> url) where label :: Parser (Maybe String) label = Just . strip . decode <$> ("[" *> takeUntil "]") diff --git a/haddock-library/test/Documentation/Haddock/ParserSpec.hs b/haddock-library/test/Documentation/Haddock/ParserSpec.hs index 6d152ee2..4373234c 100644 --- a/haddock-library/test/Documentation/Haddock/ParserSpec.hs +++ b/haddock-library/test/Documentation/Haddock/ParserSpec.hs @@ -183,24 +183,22 @@ spec = do "foo https://example.com/example bar" `shouldParseTo` "foo " <> hyperlink "https://example.com/example" Nothing <> " bar" - context "when parsing pictures" $ do - let picture :: String -> Maybe String -> Doc String - picture uri = DocPic . Picture uri + context "when parsing images" $ do + let image :: String -> Maybe String -> Doc String + image uri = DocPic . Picture uri - it "parses a simple picture" $ do - "<>" `shouldParseTo` picture "baz" Nothing + it "accepts markdown syntax for images" $ do + "![label](url)" `shouldParseTo` image "url" "label" - it "parses a picture with a title" $ do - "<>" `shouldParseTo` picture "b" (Just "a z") + it "accepts Unicode" $ do + "![灼眼のシャナ](url)" `shouldParseTo` image "url" "灼眼のシャナ" - it "parses a picture with unicode" $ do - "<<灼眼のシャナ>>" `shouldParseTo` picture "灼眼のシャナ" Nothing + it "supports deprecated picture syntax" $ do + "<>" `shouldParseTo` image "baz" Nothing - it "allows for escaping of the closing tags" $ do - "<>z>>" `shouldParseTo` picture "ba>>z" Nothing + it "supports title for deprecated picture syntax" $ do + "<>" `shouldParseTo` image "b" "a z" - it "doesn't allow for multi-line picture tags" $ do - "<>" `shouldParseTo` "<>" context "when parsing anchors" $ do it "parses a single word anchor" $ do -- cgit v1.2.3