Skip to content

Commit

Permalink
generate documentation for local modules
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Aug 31, 2023
1 parent 79323e8 commit 092cf8f
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 101 deletions.
222 changes: 132 additions & 90 deletions src/Juvix/Compiler/Backend/Html/Translation/FromTyped.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,22 @@ createIndexFile ps = do
indexHtml :: Sem r Html
indexHtml = do
tree' <- root tree
return $
Html.div ! Attr.id "content" $
Html.div ! Attr.id "module-list" $
(p ! Attr.class_ "caption" $ "Modules")
<> ( button
! Attr.id "toggle-all-button"
! Attr.class_ "toggle-button opened"
! Attr.onclick "toggle()"
$ Html.span
! Attr.id "toggle-button-text"
! Attr.class_ "toggle-icon"
$ "▼ Hide all modules"
)
<> tree'
return
$ Html.div
! Attr.id "content"
$ Html.div
! Attr.id "module-list"
$ (p ! Attr.class_ "caption" $ "Modules")
<> ( button
! Attr.id "toggle-all-button"
! Attr.class_ "toggle-button opened"
! Attr.onclick "toggle()"
$ Html.span
! Attr.id "toggle-button-text"
! Attr.class_ "toggle-icon"
$ "▼ Hide all modules"
)
<> tree'

tree :: ModuleTree
tree = indexTree ps
Expand All @@ -113,14 +115,16 @@ createIndexFile ps = do
nodeRow :: Sem r Html
nodeRow = case lbl of
Nothing ->
return $
Html.span ! Attr.class_ attrBare $
toHtml (prettyText s)
return
$ Html.span
! Attr.class_ attrBare
$ toHtml (prettyText s)
Just lbl' -> do
lnk <- nameIdAttrRef lbl' Nothing
return $
Html.span ! Attr.class_ attrBare $
(a ! Attr.href lnk $ toHtml (prettyText lbl'))
return
$ Html.span
! Attr.class_ attrBare
$ (a ! Attr.href lnk $ toHtml (prettyText lbl'))

attrBase :: Html.AttributeValue
attrBase = "details-toggle-control details-toggle"
Expand All @@ -136,10 +140,11 @@ createIndexFile ps = do
| null children = return row'
| otherwise = do
c' <- mapM (uncurry goChild) (HashMap.toList children)
return $
details ! Attr.open "open" $
summary row'
<> ul (mconcatMap li c')
return
$ details
! Attr.open "open"
$ summary row'
<> ul (mconcatMap li c')

writeHtml :: (Members '[Embed IO] r) => Path Abs File -> Html -> Sem r ()
writeHtml f h = Prelude.embed $ do
Expand Down Expand Up @@ -235,22 +240,27 @@ template rightMenu' content' = do
packageHeader = do
pkgName' <- toHtml <$> asks (^. entryPointPackage . packageName)
version' <- toHtml <$> asks (^. entryPointPackage . packageVersion . to prettySemVer)
return $
Html.div ! Attr.id "package-header" $
( Html.span ! Attr.class_ "caption" $
pkgName' <> " - " <> version'
return
$ Html.div
! Attr.id "package-header"
$ ( Html.span
! Attr.class_ "caption"
$ pkgName'
<> " - "
<> version'
)
<> rightMenu'
<> rightMenu'

mbody :: Sem r Html
mbody = do
bodyHeader' <- packageHeader
footer' <- htmlJuvixFooter
return $
body ! Attr.class_ "js-enabled" $
bodyHeader'
<> content'
<> footer'
return
$ body
! Attr.class_ "js-enabled"
$ bodyHeader'
<> content'
<> footer'

body' <- mbody
return $ docTypeHtml (mhead <> body')
Expand Down Expand Up @@ -296,44 +306,53 @@ goTopModule cs m = do
rightMenu :: Sem s Html
rightMenu = do
sourceRef' <- local (set htmlOptionsKind HtmlSrc) (nameIdAttrRef tmp Nothing)
return $
ul ! Attr.id "page-menu" ! Attr.class_ "links" $
li (a ! Attr.href sourceRef' $ "Source") -- TODO: review here
<> li (a ! Attr.href (fromString (toFilePath indexFileName)) $ "Index")
return
$ ul
! Attr.id "page-menu"
! Attr.class_ "links"
$ li (a ! Attr.href sourceRef' $ "Source") -- TODO: review here
<> li (a ! Attr.href (fromString (toFilePath indexFileName)) $ "Index")

content :: Sem s Html
content = do
preface' <- docPreface
interface' <- interface
return $
Html.div ! Attr.id "content" $
moduleHeader
<> toc
<> preface'
-- <> synopsis
<> interface'
interface' <- moduleInterface
return
$ Html.div
! Attr.id "content"
$ moduleHeader
<> toc
<> preface'
-- <> synopsis
<> interface'

docPreface :: Sem s Html
docPreface = do
pref <- goJudocMay (m ^. moduleDoc)
return $
Html.div ! Attr.id "description" $
Html.div ! Attr.class_ "doc" $
( a ! Attr.id "sec:description" ! Attr.href "sec:description" $
h1 "Description"
)
<> pref
return
$ Html.div
! Attr.id "description"
$ Html.div
! Attr.class_ "doc"
$ ( a
! Attr.id "sec:description"
! Attr.href "sec:description"
$ h1 "Description"
)
<> pref

toc :: Html
toc =
Html.div ! Attr.id "table-of-contents" $
Html.div ! Attr.id "contents-list" $
( p
Html.div
! Attr.id "table-of-contents"
$ Html.div
! Attr.id "contents-list"
$ ( p
! Attr.class_ "caption"
! Attr.onclick "window.scrollTo(0,0)"
$ "Contents"
)
<> tocEntries
<> tocEntries
where
tocEntries :: Html
tocEntries =
Expand All @@ -343,18 +362,22 @@ goTopModule cs m = do

moduleHeader :: Html
moduleHeader =
Html.div ! Attr.id "module-header" $
(p ! Attr.class_ "caption" $ toHtml (prettyText tmp))
Html.div
! Attr.id "module-header"
$ (p ! Attr.class_ "caption" $ toHtml (prettyText tmp))

interface :: Sem s Html
interface = do
moduleInterface :: Sem s Html
moduleInterface = do
sigs' <- mconcatMapM goStatement (m ^. moduleBody)
return $
Html.div ! Attr.id "interface" $
( a ! Attr.id "sec:interface" ! Attr.href "sec:interface" $
h1 "Definitions"
return
$ Html.div
! Attr.id "interface"
$ ( a
! Attr.id "sec:interface"
! Attr.href "sec:interface"
$ h1 "Definitions"
)
<> sigs'
<> sigs'

goJudocMay :: (Members '[Reader HtmlOptions, Reader NormalizedTable] r) => Maybe (Judoc 'Scoped) -> Sem r Html
goJudocMay = maybe (return mempty) goJudoc
Expand Down Expand Up @@ -382,13 +405,14 @@ goJudoc (Judoc bs) = mconcatMapM goGroup bs
goExample ex = do
e' <- ppCodeHtml defaultOptions (ex ^. exampleExpression)
norm' <- asks @NormalizedTable (^?! at (ex ^. exampleId) . _Just) >>= ppCodeHtmlInternal
return $
Html.pre ! Attr.class_ "screen" $
(Html.code ! Attr.class_ "prompt" $ Str.judocExample)
<> " "
<> e'
<> "\n"
<> norm'
return
$ Html.pre
! Attr.class_ "screen"
$ (Html.code ! Attr.class_ "prompt" $ Str.judocExample)
<> " "
<> e'
<> "\n"
<> norm'

goAtom :: JudocAtom 'Scoped -> Sem r Html
goAtom = \case
Expand All @@ -411,12 +435,26 @@ goLocalModule :: forall r. (Members '[Reader HtmlOptions, Reader NormalizedTable
goLocalModule def = fmap (fromMaybe mempty) . runFail $ do
failWhen (def ^. moduleInductive)
sig' <- ppHelper (ppModuleHeader def)
defHeader tmp uid sig' (def ^. moduleDoc)
header' <- defHeader tmp uid sig' (def ^. moduleDoc)
body' <-
( Html.div
! Attr.class_ "subs"
)
<$> mconcatMap goStatement (def ^. moduleBody)
return (header' <> body')
where
uid :: NameId
uid = def ^. modulePath . S.nameId
tmp :: TopModulePath
tmp = def ^. modulePath . S.nameDefinedIn . S.absTopModulePath
uid :: NameId
uid = def ^. modulePath . S.nameId
tmp :: TopModulePath
tmp = def ^. modulePath . S.nameDefinedIn . S.absTopModulePath

-- goConstructors :: forall r. (Members '[Reader HtmlOptions, Reader NormalizedTable] r) => NonEmpty (ConstructorDef 'Scoped) -> Sem r Html
-- goConstructors cc = do
-- tbl' <- table . tbody <$> mconcatMapM goConstructor cc
-- return $
-- Html.div ! Attr.class_ "subs constructors" $
-- (p ! Attr.class_ "caption" $ "Constructors")
-- <> tbl'

goOpen :: forall r. (Members '[Reader HtmlOptions] r) => OpenModule 'Scoped -> Sem r Html
goOpen op
Expand Down Expand Up @@ -467,10 +505,11 @@ ppHelper = docToHtml . run . runReader defaultOptions . execExactPrint Nothing
goConstructors :: forall r. (Members '[Reader HtmlOptions, Reader NormalizedTable] r) => NonEmpty (ConstructorDef 'Scoped) -> Sem r Html
goConstructors cc = do
tbl' <- table . tbody <$> mconcatMapM goConstructor cc
return $
Html.div ! Attr.class_ "subs constructors" $
(p ! Attr.class_ "caption" $ "Constructors")
<> tbl'
return
$ Html.div
! Attr.class_ "subs constructors"
$ (p ! Attr.class_ "caption" $ "Constructors")
<> tbl'
where
goConstructor :: ConstructorDef 'Scoped -> Sem r Html
goConstructor c = do
Expand All @@ -480,15 +519,17 @@ goConstructors cc = do
where
docPart :: Sem r Html
docPart = do
td ! Attr.class_ "doc"
td
! Attr.class_ "doc"
<$> goJudocMay (c ^. constructorDoc)

srcPart :: Sem r Html
srcPart = do
sig' <- ppCodeHtml defaultOptions (set constructorDoc Nothing c)
return $
td ! Attr.class_ "src" $
sig'
return
$ td
! Attr.class_ "src"
$ sig'

noDefHeader :: Html -> Html
noDefHeader = p ! Attr.class_ "src"
Expand All @@ -497,10 +538,11 @@ defHeader :: forall r. (Members '[Reader HtmlOptions, Reader NormalizedTable] r)
defHeader tmp uid sig mjudoc = do
funHeader' <- functionHeader
judoc' <- judoc
return $
Html.div ! Attr.class_ "top" $
funHeader'
<> judoc'
return
$ Html.div
! Attr.class_ "top"
$ funHeader'
<> judoc'
where
judoc :: Sem r Html
judoc = do
Expand Down
22 changes: 11 additions & 11 deletions src/Juvix/Compiler/Concrete/Print/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ instance (SingI s) => PrettyPrint (Iterator s) where
b'
| _iteratorBodyBraces = braces (oneLineOrNextNoIndent b)
| otherwise = line <> b
parensIf _iteratorParens
$ hang (n <+?> is' <+?> rngs' <> b')
parensIf _iteratorParens $
hang (n <+?> is' <+?> rngs' <> b')

instance PrettyPrint S.AName where
ppCode n = annotated (AnnKind (S.getNameKind n)) (noLoc (pretty (n ^. S.anameVerbatim)))
Expand Down Expand Up @@ -663,9 +663,9 @@ instance PrettyPrint Expression where
instance PrettyPrint (WithSource Pragmas) where
ppCode pragma = do
b <- asks (^. optPrintPragmas)
when b
$ let txt = pretty (Str.pragmasStart <> pragma ^. withSourceText <> Str.pragmasEnd)
in annotated AnnComment (noLoc txt) <> line
when b $
let txt = pretty (Str.pragmasStart <> pragma ^. withSourceText <> Str.pragmasEnd)
in annotated AnnComment (noLoc txt) <> line

instance PrettyPrint (WithSource IteratorAttribs) where
ppCode = braces . noLoc . pretty . (^. withSourceText)
Expand All @@ -674,15 +674,15 @@ ppJudocStart :: (Members '[ExactPrint, Reader Options] r) => Sem r (Maybe ())
ppJudocStart = do
inBlock <- asks (^. optInJudocBlock)
if
| inBlock -> return Nothing
| otherwise -> ppCode Kw.delimJudocStart $> Just ()
| inBlock -> return Nothing
| otherwise -> ppCode Kw.delimJudocStart $> Just ()

instance (SingI s) => PrettyPrint (Example s) where
ppCode e =
ppJudocStart
<??+> ppCode Kw.delimJudocExample
<+> ppExpressionType (e ^. exampleExpression)
<> semicolon
<> semicolon

instance (PrettyPrint a) => PrettyPrint (WithLoc a) where
ppCode a = morphemeM (getLoc a) (ppCode (a ^. withLocParam))
Expand Down Expand Up @@ -805,7 +805,7 @@ ppFunctionSignature FunctionDef {..} = do
?<> termin'
?<> ( name'
<+?> args'
<> type'
<> type'
)

instance (SingI s) => PrettyPrint (FunctionDef s) where
Expand Down Expand Up @@ -1060,8 +1060,8 @@ instance (SingI s) => PrettyPrint (InductiveDef s) where
?<> pragmas'
?<> sig'
<+> ppCode _inductiveAssignKw
<> line
<> indent constrs'
<> line
<> indent constrs'
where
ppConstructorBlock :: NonEmpty (ConstructorDef s) -> Sem r ()
ppConstructorBlock cs = vsep (ppCode <$> cs)
Expand Down

0 comments on commit 092cf8f

Please sign in to comment.