... |
... |
@@ -795,31 +795,33 @@ stripSpace = fromMaybe <*> mapM strip' |
795
|
795
|
-- | Parses examples. Examples are a paragraph level entity (separated by an empty line).
|
796
|
796
|
-- Consecutive examples are accepted.
|
797
|
797
|
examples :: Parser (DocH mod a)
|
798
|
|
-examples = DocExamples <$> (many (try (skipHorizontalSpace *> "\n")) *> go)
|
|
798
|
+examples = DocExamples <$> (many (try (skipHorizontalSpace *> "\n")) *> go Nothing)
|
799
|
799
|
where
|
800
|
|
- go :: Parser [Example]
|
801
|
|
- go = do
|
|
800
|
+ go :: Maybe Text -> Parser [Example]
|
|
801
|
+ go mbInitialIndent = do
|
802
|
802
|
prefix <- takeHorizontalSpace <* ">>>"
|
|
803
|
+ initialIndent <- maybe takeHorizontalSpace pure mbInitialIndent
|
803
|
804
|
expr <- takeLine
|
804
|
|
- (rs, es) <- resultAndMoreExamples
|
805
|
|
- return (makeExample prefix expr rs : es)
|
|
805
|
+ (rs, es) <- resultAndMoreExamples (Just initialIndent)
|
|
806
|
+ return (makeExample prefix initialIndent expr rs : es)
|
|
807
|
+
|
|
808
|
+ resultAndMoreExamples :: Maybe Text -> Parser ([Text], [Example])
|
|
809
|
+ resultAndMoreExamples mbInitialIndent = choice' [moreExamples, result, pure ([], [])]
|
806
|
810
|
where
|
807
|
|
- resultAndMoreExamples :: Parser ([Text], [Example])
|
808
|
|
- resultAndMoreExamples = choice' [moreExamples, result, pure ([], [])]
|
809
|
|
- where
|
810
|
|
- moreExamples :: Parser ([Text], [Example])
|
811
|
|
- moreExamples = (,) [] <$> go
|
|
811
|
+ moreExamples :: Parser ([Text], [Example])
|
|
812
|
+ moreExamples = (,) [] <$> go mbInitialIndent
|
812
|
813
|
|
813
|
|
- result :: Parser ([Text], [Example])
|
814
|
|
- result = first . (:) <$> nonEmptyLine <*> resultAndMoreExamples
|
|
814
|
+ result :: Parser ([Text], [Example])
|
|
815
|
+ result = first . (:) <$> nonEmptyLine <*> resultAndMoreExamples Nothing
|
815
|
816
|
|
816
|
|
- makeExample :: Text -> Text -> [Text] -> Example
|
817
|
|
- makeExample prefix expression res =
|
818
|
|
- Example (T.unpack (T.strip expression)) result
|
|
817
|
+ makeExample :: Text -> Text -> Text -> [Text] -> Example
|
|
818
|
+ makeExample prefix indent expression res =
|
|
819
|
+ Example (T.unpack (tryStripIndent (T.stripEnd expression))) result
|
819
|
820
|
where
|
820
|
821
|
result = map (T.unpack . substituteBlankLine . tryStripPrefix) res
|
821
|
822
|
|
822
|
823
|
tryStripPrefix xs = fromMaybe xs (T.stripPrefix prefix xs)
|
|
824
|
+ tryStripIndent = liftA2 fromMaybe T.stripStart (T.stripPrefix indent)
|
823
|
825
|
|
824
|
826
|
substituteBlankLine "<BLANKLINE>" = ""
|
825
|
827
|
substituteBlankLine xs = xs
|