From a44e598d21703adf8b90d68f2befe905c9d410b4 Mon Sep 17 00:00:00 2001 From: Alex Mingoia Date: Wed, 28 Dec 2016 16:25:06 -0800 Subject: [PATCH 1/2] Recursively parse images and links. Images and links are parsed recursively, so that link labels can contain images and vice versa. --- src/Text/Markdown/SlamDown/Parser/Inline.purs | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Text/Markdown/SlamDown/Parser/Inline.purs b/src/Text/Markdown/SlamDown/Parser/Inline.purs index ecf8a11..3684048 100644 --- a/src/Text/Markdown/SlamDown/Parser/Inline.purs +++ b/src/Text/Markdown/SlamDown/Parser/Inline.purs @@ -124,7 +124,7 @@ inlines ∷ ∀ a . (SD.Value a) ⇒ P.Parser String (L.List (SD.Inline a)) -inlines = L.many inline2 <* PS.eof +inlines = L.many inline1 <* PS.eof where inline0 ∷ P.Parser String (SD.Inline a) inline0 = @@ -134,21 +134,17 @@ inlines = L.many inline2 <* PS.eof <|> strongEmph p <|> strong p <|> emph p + <|> link p + <|> image p <|> code <|> autolink <|> entity inline1 ∷ P.Parser String (SD.Inline a) - inline1 = - PC.try inline0 - <|> PC.try link - - inline2 ∷ P.Parser String (SD.Inline a) - inline2 = do + inline1 = do res ← PC.try formField - <|> PC.try (Right <$> inline1) - <|> PC.try (Right <$> image) + <|> PC.try (Right <$> inline0) <|> (Right <$> other) case res of Right v → pure v @@ -202,11 +198,11 @@ inlines = L.many inline2 <* PS.eof pure <<< SD.Code eval <<< S.trim $ contents - link ∷ P.Parser String (SD.Inline a) - link = SD.Link <$> linkLabel <*> linkTarget + link ∷ P.Parser String (SD.Inline a) → P.Parser String (SD.Inline a) + link p = SD.Link <$> linkLabel <*> linkTarget where linkLabel ∷ P.Parser String (L.List (SD.Inline a)) - linkLabel = PS.string "[" *> PC.manyTill (inline0 <|> other) (PS.string "]") + linkLabel = PS.string "[" *> PC.manyTill p (PS.string "]") linkTarget ∷ P.Parser String SD.LinkTarget linkTarget = inlineLink <|> referenceLink @@ -217,11 +213,11 @@ inlines = L.many inline2 <* PS.eof referenceLink ∷ P.Parser String SD.LinkTarget referenceLink = SD.ReferenceLink <$> PC.optionMaybe ((S.fromCharArray <<< A.fromFoldable) <$> (PS.string "[" *> PC.manyTill PS.anyChar (PS.string "]"))) - image ∷ P.Parser String (SD.Inline a) - image = SD.Image <$> imageLabel <*> imageUrl + image ∷ P.Parser String (SD.Inline a) → P.Parser String (SD.Inline a) + image p = SD.Image <$> imageLabel <*> imageUrl where imageLabel ∷ P.Parser String (L.List (SD.Inline a)) - imageLabel = PS.string "![" *> PC.manyTill (inline1 <|> other) (PS.string "]") + imageLabel = PS.string "![" *> PC.manyTill p (PS.string "]") imageUrl ∷ P.Parser String String imageUrl = S.fromCharArray <<< A.fromFoldable <$> (PS.string "(" *> PC.manyTill PS.anyChar (PS.string ")")) From ded8ed66e7654bbccae203eed6ef2f63b4cc8fb3 Mon Sep 17 00:00:00 2001 From: Alex Mingoia Date: Wed, 28 Dec 2016 23:56:12 -0800 Subject: [PATCH 2/2] Recurse one level deep. --- src/Text/Markdown/SlamDown/Parser/Inline.purs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Text/Markdown/SlamDown/Parser/Inline.purs b/src/Text/Markdown/SlamDown/Parser/Inline.purs index 3684048..1bbba59 100644 --- a/src/Text/Markdown/SlamDown/Parser/Inline.purs +++ b/src/Text/Markdown/SlamDown/Parser/Inline.purs @@ -124,7 +124,7 @@ inlines ∷ ∀ a . (SD.Value a) ⇒ P.Parser String (L.List (SD.Inline a)) -inlines = L.many inline1 <* PS.eof +inlines = L.many inline2 <* PS.eof where inline0 ∷ P.Parser String (SD.Inline a) inline0 = @@ -134,17 +134,23 @@ inlines = L.many inline1 <* PS.eof <|> strongEmph p <|> strong p <|> emph p - <|> link p - <|> image p <|> code <|> autolink <|> entity inline1 ∷ P.Parser String (SD.Inline a) - inline1 = do + inline1 = + PC.try inline0 + <|> PC.try (image (inline0 <|> other)) + <|> PC.try (link (inline0 <|> other)) + + inline2 ∷ P.Parser String (SD.Inline a) + inline2 = do res ← PC.try formField <|> PC.try (Right <$> inline0) + <|> PC.try (Right <$> image (inline1 <|> other)) + <|> PC.try (Right <$> link (inline1 <|> other)) <|> (Right <$> other) case res of Right v → pure v