@@ -1,6 +1,7 @@
11-- | Wrapper around Markdown library
22module Idris.Docstrings (
3− Docstring, parseDocstring, renderDocstring, emptyDocstring, nullDocstring, noDocs
3+ Docstring, parseDocstring, renderDocstring, emptyDocstring, nullDocstring, noDocs,
4+ overview, containsText
45 ) where
56
67import qualified Cheapskate as C
@@ -14,8 +15,10 @@ import qualified Data.Text as T
1415import qualified Data.Foldable as F
1516import qualified Data.Sequence as S
1617
18+-- | Representation of Idris's inline documentation
1719type Docstring = CT.Doc
1820
21+-- | Construct a docstring from a Text that contains Markdown-formatted docs
1922parseDocstring :: T.Text -> CT.Doc
2023parseDocstring = C.markdown options
2124
@@ -26,9 +29,15 @@ options = CT.Options { CT.sanitize = True
2629 , CT.debug = False
2730 }
2831
32+-- | Convert a docstring to be shown by the pretty-printer
2933renderDocstring :: Docstring -> Doc OutputAnnotation
3034renderDocstring (CT.Doc _ blocks) = renderBlocks blocks
3135
36+-- | Construct a docstring consisting of the first block-level element of the
37+-- argument docstring, for use in summaries.
38+overview :: Docstring -> Docstring
39+overview (CT.Doc opts blocks) = CT.Doc opts (S.take 1 blocks)
40+
3241renderBlocks :: CT.Blocks -> Doc OutputAnnotation
3342renderBlocks blocks | S.length blocks > 1 = F.foldr1 (\b1 b2 -> b1 <> line <> line <> b2) $
3443 fmap renderBlock blocks
@@ -70,11 +79,39 @@ renderInline (CT.Image body url title) = text "<image>" -- TODO
7079renderInline (CT.Entity a) = text $ "<entity " ++ T.unpack a ++ ">" -- TODO
7180renderInline (CT.RawHtml txt) = text "<html content>" --TODO
7281
82+-- | The empty docstring
7383emptyDocstring :: Docstring
7484emptyDocstring = CT.Doc options S.empty
7585
86+-- | Check whether a docstring is emtpy
7687nullDocstring :: Docstring -> Bool
7788nullDocstring (CT.Doc _ blocks) = S.null blocks
7889
90+-- | Empty documentation for a definition
7991noDocs :: (Docstring, [(Name, Docstring)])
8092noDocs = (emptyDocstring, [])
93+
94+-- | Does a string occur in the docstring?
95+containsText :: T.Text -> Docstring -> Bool
96+containsText str (CT.Doc _ blocks) = F.any (blockContains str) blocks
97+ where blockContains :: T.Text -> CT.Block -> Bool
98+ blockContains str (CT.Para inlines) = F.any (inlineContains str) inlines
99+ blockContains str (CT.Header lvl inlines) = F.any (inlineContains str) inlines
100+ blockContains str (CT.Blockquote blocks) = F.any (blockContains str) blocks
101+ blockContains str (CT.List b ty blockss) = F.any (F.any (blockContains str)) blockss
102+ blockContains str (CT.CodeBlock attr src) = T.isInfixOf str src
103+ blockContains str (CT.HtmlBlock txt) = False -- TODO
104+ blockContains str CT.HRule = False
105+
106+ inlineContains :: T.Text -> CT.Inline -> Bool
107+ inlineContains str (CT.Str s) = T.isInfixOf str s
108+ inlineContains str CT.Space = False
109+ inlineContains str CT.SoftBreak = False
110+ inlineContains str CT.LineBreak = False
111+ inlineContains str (CT.Emph txt) = F.any (inlineContains str) txt
112+ inlineContains str (CT.Strong txt) = F.any (inlineContains str) txt
113+ inlineContains str (CT.Code txt) = T.isInfixOf str txt
114+ inlineContains str (CT.Link body url title) = F.any (inlineContains str) body
115+ inlineContains str (CT.Image body url title) = False
116+ inlineContains str (CT.Entity a) = False
117+ inlineContains str (CT.RawHtml txt) = T.isInfixOf str txt