@@ -1,6 +1,7 @@
1-- | Wrapper around Markdown library
1-- | Wrapper around Markdown library
2module Idris.Docstrings (
2module Idris.Docstrings (
3− Docstring, parseDocstring, renderDocstring, emptyDocstring, nullDocstring, noDocs
3+ Docstring, parseDocstring, renderDocstring, emptyDocstring, nullDocstring, noDocs,
4+ overview, containsText
6import qualified Cheapskate as C
7import qualified Cheapskate as C
@@ -14,8 +15,10 @@ import qualified Data.Text as T
14import qualified Data.Foldable as F
15import qualified Data.Foldable as F
15import qualified Data.Sequence as S
16import qualified Data.Sequence as S
18+-- | Representation of Idris's inline documentation
17type Docstring = CT.Doc
19type Docstring = CT.Doc
21+-- | Construct a docstring from a Text that contains Markdown-formatted docs
19parseDocstring :: T.Text -> CT.Doc
22parseDocstring :: T.Text -> CT.Doc
20parseDocstring = C.markdown options
23parseDocstring = C.markdown options
@@ -26,9 +29,15 @@ options = CT.Options { CT.sanitize = True
26 , CT.debug = False
29 , CT.debug = False
32+-- | Convert a docstring to be shown by the pretty-printer
29renderDocstring :: Docstring -> Doc OutputAnnotation
33renderDocstring :: Docstring -> Doc OutputAnnotation
30renderDocstring (CT.Doc _ blocks) = renderBlocks blocks
34renderDocstring (CT.Doc _ blocks) = renderBlocks blocks
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)
32renderBlocks :: CT.Blocks -> Doc OutputAnnotation
41renderBlocks :: CT.Blocks -> Doc OutputAnnotation
33renderBlocks blocks | S.length blocks > 1 = F.foldr1 (\b1 b2 -> b1 <> line <> line <> b2) $
42renderBlocks blocks | S.length blocks > 1 = F.foldr1 (\b1 b2 -> b1 <> line <> line <> b2) $
34 fmap renderBlock blocks
43 fmap renderBlock blocks
@@ -70,11 +79,39 @@ renderInline (CT.Image body url title) = text "<image>" -- TODO
70renderInline (CT.Entity a) = text $ "<entity " ++ T.unpack a ++ ">" -- TODO
79renderInline (CT.Entity a) = text $ "<entity " ++ T.unpack a ++ ">" -- TODO
71renderInline (CT.RawHtml txt) = text "<html content>" --TODO
80renderInline (CT.RawHtml txt) = text "<html content>" --TODO
82+-- | The empty docstring
73emptyDocstring :: Docstring
83emptyDocstring :: Docstring
74emptyDocstring = CT.Doc options S.empty
84emptyDocstring = CT.Doc options S.empty
86+-- | Check whether a docstring is emtpy
76nullDocstring :: Docstring -> Bool
87nullDocstring :: Docstring -> Bool
77nullDocstring (CT.Doc _ blocks) = S.null blocks
88nullDocstring (CT.Doc _ blocks) = S.null blocks
90+-- | Empty documentation for a definition
79noDocs :: (Docstring, [(Name, Docstring)])
91noDocs :: (Docstring, [(Name, Docstring)])
80noDocs = (emptyDocstring, [])
92noDocs = (emptyDocstring, [])
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
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