Skip to content

Commit 7ac8e65

Browse files
Hong Minheedahlia
authored andcommitted
Render module-level docs
1 parent 85aedff commit 7ac8e65

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ To be released.
126126
127127
- Docs now have a sidebar which contains table of contents. [[#257]]
128128
129+
- Fixed a bug that a module-level docs hadn't been rendered.
130+
Now it's shown in the right below the module name. [[#259]]
131+
129132
### Python target
130133
131134
- Generated Python packages became to have two [entry points] (a feature
@@ -178,6 +181,7 @@ To be released.
178181
[#254]: https://github.com/spoqa/nirum/pull/254
179182
[#255]: https://github.com/spoqa/nirum/pull/255
180183
[#257]: https://github.com/spoqa/nirum/pull/257
184+
[#259]: https://github.com/spoqa/nirum/pull/259
181185
[entry points]: https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points
182186
[python2-numbers-integral]: https://docs.python.org/2/library/numbers.html#numbers.Integral
183187
[python2-int]: https://docs.python.org/2/library/functions.html#int

examples/builtins.nrm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Introducing built-in types
22
# ==========================
3+
#
4+
# This example module introduces the built-in types Nirum provides.
35

46
record number-types (
57
bigint a,

src/Nirum/Docs.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module Nirum.Docs ( Block ( BlockQuote
3737
, headingLevelFromInt
3838
, headingLevelInt
3939
, parse
40+
, trimTitle
4041
) where
4142

4243
import Data.String (IsString (fromString))
@@ -112,6 +113,13 @@ data Inline
112113
| Image { imageUrl :: Url, imageTitle :: Title }
113114
deriving (Eq, Ord, Show)
114115

116+
-- | Trim the top-level first heading from the block, if it exists.
117+
trimTitle :: Block -> Block
118+
trimTitle block =
119+
case block of
120+
Document (Heading {} : rest) -> Document rest
121+
b -> b
122+
115123
parse :: T.Text -> Block
116124
parse =
117125
transBlock . M.commonmarkToNode [M.optNormalize, M.optSmart]

src/Nirum/Targets/Docs.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import qualified Nirum.Constructs.TypeDeclaration as TD
3939
import qualified Nirum.Constructs.TypeExpression as TE
4040
import Nirum.Docs ( Block (Heading)
4141
, filterReferences
42+
, trimTitle
4243
)
4344
import Nirum.Docs.Html (render, renderInlines)
4445
import Nirum.Package
@@ -161,6 +162,9 @@ $maybe tit <- headingTitle
161162
&#32;&mdash; #{tit}
162163
$nothing
163164
<h1><code>#{path}</code>
165+
$maybe m <- mod'
166+
$maybe d <- docsBlock m
167+
#{blockToHtml (trimTitle d)}
164168
$forall (ident, decl) <- types'
165169
<div class="#{showKind decl}" id="#{toNormalizedText ident}">
166170
#{typeDecl docsModule ident decl}

test/Nirum/DocsSpec.hs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,7 @@ import Data.Text (Text)
55
import Test.Hspec.Meta
66
import Text.InterpolatedString.Perl6 (q)
77

8-
import Nirum.Docs ( Block (..)
9-
, HeadingLevel (..)
10-
, Inline (..)
11-
, ItemList (..)
12-
, ListDelimiter (..)
13-
, ListType (..)
14-
, filterReferences
15-
, headingLevelFromInt
16-
, parse
17-
)
8+
import Nirum.Docs
189

1910
sampleSource :: Text
2011
sampleSource = [q|
@@ -38,11 +29,18 @@ A [complex *link*][1].
3829

3930
|]
4031

32+
sampleHeading :: Block
33+
sampleHeading =
34+
Heading H1 ["Hello"]
35+
4136
sampleDocument :: Block
4237
sampleDocument =
43-
Document
44-
[ Heading H1 ["Hello"]
45-
, Paragraph ["Tight list:"]
38+
sampleDocument' (sampleHeading :)
39+
40+
sampleDocument' :: ([Block] -> [Block]) -> Block
41+
sampleDocument' adjust =
42+
(Document . adjust)
43+
[ Paragraph ["Tight list:"]
4644
, List BulletList $ TightItemList [ ["List test"]
4745
, ["test2"]
4846
]
@@ -91,3 +89,8 @@ spec = do
9189
, "image"
9290
, "."
9391
]
92+
specify "trimTitle" $ do
93+
-- Remove the top-level heading if it exists:
94+
trimTitle sampleDocument `shouldBe` sampleDocument' id
95+
-- No-op if there is no top-level heading:
96+
trimTitle (sampleDocument' id) `shouldBe` sampleDocument' id

0 commit comments

Comments
 (0)