9 #include "HsVersions.h"
14 import Data.Char (isSpace)
18 | DocAppend (HsDoc id) (HsDoc id)
20 | DocParagraph (HsDoc id)
23 | DocEmphasis (HsDoc id)
24 | DocMonospaced (HsDoc id)
25 | DocUnorderedList [HsDoc id]
26 | DocOrderedList [HsDoc id]
27 | DocDefList [(HsDoc id, HsDoc id)]
28 | DocCodeBlock (HsDoc id)
34 type LHsDoc a = Located (HsDoc a)
36 instance Outputable (HsDoc a) where
37 ppr _ = text "<document comment>"
39 ppr_mbDoc :: Maybe (LHsDoc a) -> SDoc
40 ppr_mbDoc (Just doc) = ppr doc
41 ppr_mbDoc Nothing = empty
43 -- used to make parsing easier; we group the list items later
44 docAppend :: HsDoc id -> HsDoc id -> HsDoc id
45 docAppend (DocUnorderedList ds1) (DocUnorderedList ds2)
46 = DocUnorderedList (ds1++ds2)
47 docAppend (DocUnorderedList ds1) (DocAppend (DocUnorderedList ds2) d)
48 = DocAppend (DocUnorderedList (ds1++ds2)) d
49 docAppend (DocOrderedList ds1) (DocOrderedList ds2)
50 = DocOrderedList (ds1++ds2)
51 docAppend (DocOrderedList ds1) (DocAppend (DocOrderedList ds2) d)
52 = DocAppend (DocOrderedList (ds1++ds2)) d
53 docAppend (DocDefList ds1) (DocDefList ds2)
54 = DocDefList (ds1++ds2)
55 docAppend (DocDefList ds1) (DocAppend (DocDefList ds2) d)
56 = DocAppend (DocDefList (ds1++ds2)) d
57 docAppend DocEmpty d = d
58 docAppend d DocEmpty = d
62 -- again to make parsing easier - we spot a paragraph whose only item
63 -- is a DocMonospaced and make it into a DocCodeBlock
64 docParagraph :: HsDoc id -> HsDoc id
65 docParagraph (DocMonospaced p)
66 = DocCodeBlock (docCodeBlock p)
67 docParagraph (DocAppend (DocString s1) (DocMonospaced p))
69 = DocCodeBlock (docCodeBlock p)
70 docParagraph (DocAppend (DocString s1)
71 (DocAppend (DocMonospaced p) (DocString s2)))
72 | all isSpace s1 && all isSpace s2
73 = DocCodeBlock (docCodeBlock p)
74 docParagraph (DocAppend (DocMonospaced p) (DocString s2))
76 = DocCodeBlock (docCodeBlock p)
81 -- Drop trailing whitespace from @..@ code blocks. Otherwise this:
87 -- turns into (DocCodeBlock "\nfoo\n ") which when rendered in HTML
88 -- gives an extra vertical space after the code block. The single space
89 -- on the final line seems to trigger the extra vertical space.
91 docCodeBlock :: HsDoc id -> HsDoc id
92 docCodeBlock (DocString s)
93 = DocString (reverse $ dropWhile (`elem` " \t") $ reverse s)
94 docCodeBlock (DocAppend l r)
95 = DocAppend l (docCodeBlock r)