From 79cc56726c7e876314c7c21f5bb5f65084e7d8b7 Mon Sep 17 00:00:00 2001
From: Alexander Krotov <ilabdsf@gmail.com>
Date: Wed, 28 Jun 2017 15:32:53 +0300
Subject: [PATCH] Muse reader: parse indented blockquotes (#3769)

---
 src/Text/Pandoc/Readers/Muse.hs | 23 ++++++++++++++++++++++-
 test/Tests/Readers/Muse.hs      |  7 ++++++-
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/src/Text/Pandoc/Readers/Muse.hs b/src/Text/Pandoc/Readers/Muse.hs
index eb0769e0b..a51306347 100644
--- a/src/Text/Pandoc/Readers/Muse.hs
+++ b/src/Text/Pandoc/Readers/Muse.hs
@@ -187,6 +187,7 @@ blockElements = choice [ comment
                        , orderedList
                        , table
                        , commentTag
+                       , indentedBlock
                        , noteBlock
                        ]
 
@@ -209,7 +210,8 @@ separator = try $ do
 header :: PandocMonad m => MuseParser m (F Blocks)
 header = try $ do
   st <- stateParserContext <$> getState
-  getPosition >>= \pos -> guard (st == NullState && sourceColumn pos == 1)
+  q <- stateQuoteContext <$> getState
+  getPosition >>= \pos -> guard (st == NullState && q == NoQuote && sourceColumn pos == 1)
   level <- liftM length $ many1 $ char '*'
   guard $ level <= 5
   skipSpaces
@@ -248,6 +250,25 @@ quoteTag = blockTag B.blockQuote "quote"
 commentTag :: PandocMonad m => MuseParser m (F Blocks)
 commentTag = parseHtmlContent "comment" block >> return mempty
 
+-- Indented block is either center, right or quote
+indentedLine :: PandocMonad m => MuseParser m (Int, String)
+indentedLine = try $ do
+  indent <- length <$> many1 spaceChar
+  line <- anyLine
+  return (indent, line)
+
+rawIndentedBlock :: PandocMonad m => MuseParser m (Int, String)
+rawIndentedBlock = try $ do
+  lns <- many1 indentedLine
+  let indent = minimum $ map fst lns
+  return (indent, unlines $ map snd lns)
+
+indentedBlock :: PandocMonad m => MuseParser m (F Blocks)
+indentedBlock = try $ do
+  (indent, raw) <- rawIndentedBlock
+  contents <- withQuoteContext InDoubleQuote $ parseFromString parseBlocks raw
+  return $ (if indent >= 2 && indent < 6 then B.blockQuote else id) <$> contents
+
 para :: PandocMonad m => MuseParser m (F Blocks)
 para = liftM B.para . trimInlinesF . mconcat <$> many1Till inline endOfParaElement
  where
diff --git a/test/Tests/Readers/Muse.hs b/test/Tests/Readers/Muse.hs
index 3d7baf8f0..fe0a59992 100644
--- a/test/Tests/Readers/Muse.hs
+++ b/test/Tests/Readers/Muse.hs
@@ -102,7 +102,12 @@ tests =
         , "5 dashes is a horizontal rule" =: "-----" =?> horizontalRule
         , "4 dashes with spaces is a horizontal rule" =: "----  " =?> horizontalRule
         ]
-      , "Quote" =: "<quote>Hello, world</quote>" =?> blockQuote (para $ text "Hello, world")
+      , "Quote tag" =: "<quote>Hello, world</quote>" =?> blockQuote (para $ text "Hello, world")
+      , "Quote" =: "  This is a quotation\n" =?> blockQuote (para $ text "This is a quotation")
+      , "Multiline quote" =: T.unlines [ "  This is a quotation"
+                                       , "  with a continuation"
+                                       ]
+        =?> blockQuote (para $ text "This is a quotation with a continuation")
       , "Center" =: "<center>Hello, world</center>" =?> para (text "Hello, world")
       , "Right" =: "<right>Hello, world</right>" =?> para (text "Hello, world")
       , testGroup "Comments"