2018-03-27 07:27:52 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
|
|
|
module RenderDocs where
|
|
|
|
|
2018-10-22 17:10:13 +03:00
|
|
|
import CMark
|
2019-04-30 17:21:06 +03:00
|
|
|
import Control.Monad (when)
|
2020-12-02 18:33:37 +03:00
|
|
|
import qualified Data.List as List
|
2019-04-30 17:21:06 +03:00
|
|
|
import Data.Maybe (fromMaybe)
|
2018-03-27 10:05:58 +03:00
|
|
|
import Data.Text as Text
|
2020-12-16 17:53:55 +03:00
|
|
|
import qualified Map
|
2020-12-02 18:33:37 +03:00
|
|
|
import qualified Meta
|
2018-03-27 07:27:52 +03:00
|
|
|
import Obj
|
2020-12-02 18:33:37 +03:00
|
|
|
import Path
|
2020-05-30 12:52:37 +03:00
|
|
|
import Project
|
2020-12-02 18:33:37 +03:00
|
|
|
import Text.Blaze.Html.Renderer.Pretty (renderHtml)
|
|
|
|
import Text.Blaze.Html5 ((!))
|
2020-12-03 14:02:58 +03:00
|
|
|
import qualified Text.Blaze.Html5 as H
|
2020-12-02 18:33:37 +03:00
|
|
|
import qualified Text.Blaze.Html5.Attributes as A
|
2021-02-04 10:40:18 +03:00
|
|
|
import TypeError (typeVariablesInOrderOfAppearance)
|
2018-03-27 07:27:52 +03:00
|
|
|
import Types
|
2019-10-22 16:42:57 +03:00
|
|
|
|
2019-10-24 13:16:38 +03:00
|
|
|
-- TODO: Move the beautification to a much earlier place, preferably when the function is defined/concretized-
|
2019-10-22 16:42:57 +03:00
|
|
|
-- This might be a duplicate with the work in a PR by @jacereda
|
|
|
|
beautifyType :: Ty -> Ty
|
|
|
|
beautifyType t =
|
|
|
|
let tys = List.nub (typeVariablesInOrderOfAppearance t)
|
2020-12-02 18:33:37 +03:00
|
|
|
mappings =
|
|
|
|
Map.fromList
|
|
|
|
( List.zip
|
|
|
|
(List.map (\(VarTy name) -> name) tys)
|
|
|
|
(List.map (VarTy . (: [])) ['a' ..])
|
|
|
|
)
|
|
|
|
in replaceTyVars mappings t
|
2018-03-27 07:27:52 +03:00
|
|
|
|
2019-06-07 14:40:58 +03:00
|
|
|
saveDocsForEnvs :: Project -> [(SymPath, Binder)] -> IO ()
|
|
|
|
saveDocsForEnvs ctx pathsAndEnvBinders =
|
2019-02-02 13:59:47 +03:00
|
|
|
let dir = projectDocsDir ctx
|
|
|
|
title = projectTitle ctx
|
2019-04-29 20:06:53 +03:00
|
|
|
generateIndex = projectDocsGenerateIndex ctx
|
2021-06-11 14:02:52 +03:00
|
|
|
dependencies = getDependenciesForEnvs (Prelude.map (\(p, b) -> (p, fst (getEnvAndMetaFromBinder b))) pathsAndEnvBinders)
|
|
|
|
pathsAndEnvBinders' = pathsAndEnvBinders ++ dependencies
|
|
|
|
allEnvNames = fmap fst pathsAndEnvBinders'
|
2020-12-02 18:33:37 +03:00
|
|
|
in do
|
2021-06-11 14:02:52 +03:00
|
|
|
mapM_ (saveDocsForEnvBinder ctx allEnvNames) pathsAndEnvBinders'
|
2020-12-02 18:33:37 +03:00
|
|
|
when
|
|
|
|
generateIndex
|
|
|
|
( writeFile
|
|
|
|
(dir </> title ++ "_index.html")
|
|
|
|
(projectIndexPage ctx allEnvNames)
|
|
|
|
)
|
|
|
|
putStrLn ("Generated docs to '" ++ dir ++ "'")
|
2021-06-11 14:02:52 +03:00
|
|
|
where
|
|
|
|
getDependenciesForEnvs = Prelude.concat . Prelude.map getEnvDependencies
|
|
|
|
getEnvDependencies (SymPath ps p, e) =
|
|
|
|
let envs =
|
|
|
|
Prelude.map
|
|
|
|
(\(n, b) -> (SymPath (ps ++ [p]) n, b))
|
|
|
|
( Prelude.filter
|
|
|
|
(\(_, Binder _ x) -> isMod x)
|
|
|
|
( Prelude.filter
|
|
|
|
shouldEmitDocsForBinder
|
|
|
|
(Map.toList (envBindings e))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
in envs
|
|
|
|
++ getDependenciesForEnvs (Prelude.map (\(n, Binder _ (XObj (Mod env _) _ _)) -> (n, env)) envs)
|
2018-03-27 12:47:39 +03:00
|
|
|
|
2019-06-07 14:40:58 +03:00
|
|
|
-- | This function expects a binder that contains an environment, anything else is a runtime error.
|
|
|
|
getEnvAndMetaFromBinder :: Binder -> (Env, MetaData)
|
|
|
|
getEnvAndMetaFromBinder envBinder =
|
|
|
|
case envBinder of
|
Refactor: clean up Env module, store type environments in modules (#1207)
* refactor: major environment mgmt refactor
This big refactor primarily changes two things in terms of behavior:
1. Stores a SymPath on concretely named (non-generic) struct types;
before we stored a string.
2. The SymPath mentioned in (1.) designates where the struct is stored
in the current environment chain. Modules now carry a local type
environment in addition to their local value environments. Any types
defined in the module are added to this environment rather than the
global type environment.
To resolve a type such as `Foo.Bar` we now do the following:
- Search the *global value environment* for the Foo module.
- Get the type environment stored in the Foo module.
- Search for Bar in the Foo module's type environment.
Additionally, this commit eliminates the Lookup module entirely and
refactors the Env module to handle all aspects of environment management
in hopefully a more reusable fashion.
I also took the opportunity to refactor primitiveDeftype in Primitives
and qualifySym in Qualify, both of which were hefty functions that I
found difficult to grok and needed refactoring anyway as a result of
lookup changes (lookups now return an Either instead of a Maybe).
Subsequent commits will clean up and clarify this work further.
This does include one minor regression. Namely, an implementation of
`hash` in core/Color that was maximally generic now needs type casting.
* refactor: clean up recent Env changes
This commit removes some redundant functions, unifies some logic, and
renames some routines across the Env module in efforts to make it
cleaner. Call sites have been updated accordingly.
* chore: format code with ormolu
* fix: update lookup tests
Changes references to renamed functions in the Env module.
* refactor: style + additional improvements from eriksvedang@
- Rename arrayTy -> arrayTyA in ArrayTemplates.hs to disambiguate.
- Add maybeId util function.
- Remove commented code.
- Refactor a few functions for readability.
* fix: fix type inference regression
Recent commits introduced one minor regression whereby an instance of
type inference in core/Color.carp no longer worked and required
explicit type annotation. The problem ultimately had to do with
qualification:
- Prior to the recent changes, type inference worked because the call in
question was qualified to Color.Id.get-tag, fixing the type.
- Failing to copy over a local envs Use modules to function envs
resulted in finding more than just Color.Id.get-tag for this instance.
We now copy use modules over to function envs generated during
qualification to ensure we resolve to Use'd definitions before more
general cases.
Similarly, I made a small change to primitiveUse to support contextual
use calls (e.g. the `(use Id)` in Color.carp, which really means `(use
Color.Id)`)
* chore: Update some clarificatory comments
* chore: fix inline comment
2021-05-19 20:20:48 +03:00
|
|
|
Binder meta (XObj (Mod env _) _ _) -> (env, meta)
|
2019-06-07 14:40:58 +03:00
|
|
|
_ -> error "Binder's not a module. This should be detected in 'commandSaveDocsInternal'."
|
2019-02-02 13:59:47 +03:00
|
|
|
|
2021-06-11 14:02:52 +03:00
|
|
|
projectIndexPage :: Project -> [SymPath] -> String
|
2019-02-02 13:59:47 +03:00
|
|
|
projectIndexPage ctx moduleNames =
|
|
|
|
let logo = projectDocsLogo ctx
|
|
|
|
url = projectDocsURL ctx
|
|
|
|
css = projectDocsStyling ctx
|
2020-12-01 02:11:01 +03:00
|
|
|
htmlHeader = H.toHtml $ projectTitle ctx
|
2019-02-02 13:59:47 +03:00
|
|
|
htmlDoc = commonmarkToHtml [optSafe] $ Text.pack $ projectDocsPrelude ctx
|
2020-12-03 14:02:58 +03:00
|
|
|
html = renderHtml $
|
|
|
|
H.docTypeHtml $
|
|
|
|
do
|
|
|
|
headOfPage css
|
|
|
|
H.body $
|
|
|
|
H.div ! A.class_ "content" $
|
|
|
|
H.a ! A.href (H.stringValue url) $
|
|
|
|
do
|
|
|
|
H.div ! A.class_ "logo" $
|
|
|
|
do
|
|
|
|
H.img ! A.src (H.stringValue logo) ! A.alt "Logo"
|
|
|
|
moduleIndex moduleNames
|
|
|
|
H.div $
|
|
|
|
do
|
|
|
|
H.h1 htmlHeader
|
|
|
|
H.preEscapedToHtml htmlDoc
|
2020-12-02 18:33:37 +03:00
|
|
|
in html
|
2018-03-27 11:51:16 +03:00
|
|
|
|
2019-02-02 13:59:47 +03:00
|
|
|
headOfPage :: String -> H.Html
|
|
|
|
headOfPage css =
|
2018-07-18 13:23:02 +03:00
|
|
|
H.head $
|
2020-12-02 18:33:37 +03:00
|
|
|
do
|
|
|
|
H.meta ! A.charset "UTF-8"
|
|
|
|
H.meta ! A.name "viewport" ! A.content "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
|
|
|
|
H.link ! A.rel "stylesheet" ! A.href (H.stringValue css)
|
2018-03-28 13:37:24 +03:00
|
|
|
|
2018-03-27 11:51:16 +03:00
|
|
|
getModuleName :: Env -> String
|
2019-04-30 17:21:06 +03:00
|
|
|
getModuleName env = fromMaybe "Global" (envModuleName env)
|
2018-03-27 11:51:16 +03:00
|
|
|
|
2021-06-11 14:02:52 +03:00
|
|
|
saveDocsForEnvBinder :: Project -> [SymPath] -> (SymPath, Binder) -> IO ()
|
2020-12-01 02:11:01 +03:00
|
|
|
saveDocsForEnvBinder ctx moduleNames (envPath, envBinder) =
|
2020-12-02 18:33:37 +03:00
|
|
|
do
|
2021-06-11 14:02:52 +03:00
|
|
|
let moduleName = show envPath
|
2020-12-02 18:33:37 +03:00
|
|
|
dir = projectDocsDir ctx
|
|
|
|
fullPath = dir </> moduleName ++ ".html"
|
|
|
|
string = renderHtml (envBinderToHtml envBinder ctx (show envPath) moduleNames)
|
|
|
|
createDirectoryIfMissing False dir
|
|
|
|
writeFile fullPath string
|
2018-03-27 10:05:58 +03:00
|
|
|
|
2021-06-11 14:02:52 +03:00
|
|
|
envBinderToHtml :: Binder -> Project -> String -> [SymPath] -> H.Html
|
2019-06-07 14:40:58 +03:00
|
|
|
envBinderToHtml envBinder ctx moduleName moduleNames =
|
|
|
|
let (env, meta) = getEnvAndMetaFromBinder envBinder
|
|
|
|
title = projectTitle ctx
|
2019-02-02 13:59:47 +03:00
|
|
|
css = projectDocsStyling ctx
|
2020-07-01 06:09:37 +03:00
|
|
|
url = projectDocsURL ctx
|
|
|
|
logo = projectDocsLogo ctx
|
2020-06-09 22:00:49 +03:00
|
|
|
moduleDescription = case Meta.get "doc" meta of
|
2020-12-02 18:33:37 +03:00
|
|
|
Just (XObj (Str s) _ _) -> s
|
|
|
|
Nothing -> ""
|
2020-12-09 08:19:28 +03:00
|
|
|
_ -> error "moduledescription"
|
2019-06-07 14:40:58 +03:00
|
|
|
moduleDescriptionHtml = commonmarkToHtml [optSafe] $ Text.pack moduleDescription
|
2020-12-02 18:33:37 +03:00
|
|
|
in H.docTypeHtml $
|
|
|
|
do
|
|
|
|
headOfPage css
|
2020-12-03 14:02:58 +03:00
|
|
|
H.body $
|
|
|
|
H.div ! A.class_ "content" $
|
|
|
|
do
|
|
|
|
H.div ! A.class_ "logo" $
|
|
|
|
do
|
|
|
|
H.a ! A.href (H.stringValue url) $
|
|
|
|
H.img ! A.src (H.stringValue logo)
|
|
|
|
--span_ "CARP DOCS FOR"
|
|
|
|
H.div ! A.class_ "title" $ H.toHtml title
|
|
|
|
moduleIndex moduleNames
|
2021-06-11 14:02:52 +03:00
|
|
|
H.div ! A.class_ "module" $
|
|
|
|
do
|
|
|
|
H.h1 (H.toHtml moduleName)
|
|
|
|
H.div ! A.class_ "module-description" $ H.preEscapedToHtml moduleDescriptionHtml
|
|
|
|
mapM_ (binderToHtml moduleName . snd) (Prelude.filter shouldEmitDocsForBinder (Map.toList (envBindings env)))
|
2018-03-27 15:32:47 +03:00
|
|
|
|
|
|
|
shouldEmitDocsForBinder :: (String, Binder) -> Bool
|
2020-11-26 00:12:57 +03:00
|
|
|
shouldEmitDocsForBinder (_, Binder meta _) =
|
2018-03-27 15:32:47 +03:00
|
|
|
not (metaIsTrue meta "hidden")
|
2018-03-27 10:05:58 +03:00
|
|
|
|
2021-06-11 14:02:52 +03:00
|
|
|
moduleIndex :: [SymPath] -> H.Html
|
2018-03-27 12:47:39 +03:00
|
|
|
moduleIndex moduleNames =
|
2021-06-11 14:02:52 +03:00
|
|
|
H.div ! A.class_ "index" $ grouped moduleNames
|
|
|
|
where
|
|
|
|
grouped names = H.ul $ mapM_ gen (order names)
|
|
|
|
gen (m, subs) =
|
|
|
|
H.li $
|
|
|
|
if Prelude.null subs
|
|
|
|
then moduleLink m
|
|
|
|
else H.details $
|
|
|
|
do
|
|
|
|
H.summary (moduleLink m)
|
|
|
|
grouped subs
|
|
|
|
order [] = []
|
|
|
|
order (m : mods) =
|
|
|
|
let (isIn, isNotIn) = List.partition (symBelongsToMod m) mods
|
|
|
|
in (m, isIn) : order isNotIn
|
|
|
|
symBelongsToMod (SymPath xs x) (SymPath ys y) =
|
|
|
|
List.isPrefixOf (xs ++ [x]) (ys ++ [y])
|
2018-03-27 12:47:39 +03:00
|
|
|
|
2021-06-11 14:02:52 +03:00
|
|
|
moduleLink :: SymPath -> H.Html
|
|
|
|
moduleLink p@(SymPath _ name) =
|
|
|
|
H.a ! A.href (H.stringValue (show p ++ ".html")) $ H.toHtml name
|
2018-07-18 13:23:02 +03:00
|
|
|
|
2021-06-11 14:02:52 +03:00
|
|
|
binderToHtml :: String -> Binder -> H.Html
|
|
|
|
binderToHtml moduleName (Binder meta xobj) =
|
2018-07-17 20:00:39 +03:00
|
|
|
let name = getSimpleName xobj
|
2018-07-19 14:04:06 +03:00
|
|
|
maybeNameAndArgs = getSimpleNameWithArgs xobj
|
2018-03-27 10:05:58 +03:00
|
|
|
description = getBinderDescription xobj
|
2020-12-01 02:11:01 +03:00
|
|
|
typeSignature = case xobjTy xobj of
|
2020-12-02 18:33:37 +03:00
|
|
|
Just t -> show (beautifyType t) -- NOTE: This destroys user-defined names of type variables!
|
|
|
|
Nothing -> ""
|
2021-01-26 15:22:50 +03:00
|
|
|
isDeprecated = case Meta.get "deprecated" meta of
|
|
|
|
Just (XObj (Bol True) _ _) -> True
|
|
|
|
Just (XObj (Str _) _ _) -> True
|
|
|
|
_ -> False
|
|
|
|
deprecationStr = case Meta.get "deprecated" meta of
|
|
|
|
Just (XObj (Str s) _ _) -> commonmarkToHtml [optSafe] $ Text.pack s
|
|
|
|
_ -> ""
|
2020-06-09 22:00:49 +03:00
|
|
|
docString = case Meta.get "doc" meta of
|
2020-12-02 18:33:37 +03:00
|
|
|
Just (XObj (Str s) _ _) -> s
|
|
|
|
Just found -> pretty found
|
|
|
|
Nothing -> ""
|
2018-10-22 17:10:13 +03:00
|
|
|
htmlDoc = commonmarkToHtml [optSafe] $ Text.pack docString
|
2020-12-02 18:33:37 +03:00
|
|
|
in H.div ! A.class_ "binder" $
|
|
|
|
do
|
2020-12-03 14:02:58 +03:00
|
|
|
H.a ! A.class_ "anchor" ! A.href (H.stringValue ("#" ++ name)) $
|
|
|
|
H.h3 ! A.id (H.stringValue name) $
|
2021-01-26 15:22:50 +03:00
|
|
|
do
|
2021-06-11 14:02:52 +03:00
|
|
|
if isMod xobj
|
|
|
|
then H.a ! A.href (H.stringValue (moduleName ++ "." ++ pretty xobj ++ ".html")) $ H.toHtml (pretty xobj)
|
|
|
|
else H.toHtml name
|
2021-01-26 15:22:50 +03:00
|
|
|
when isDeprecated $
|
|
|
|
H.span ! A.class_ "deprecation-notice" $
|
|
|
|
H.toHtml ("deprecated" :: String)
|
2020-12-02 18:33:37 +03:00
|
|
|
H.div ! A.class_ "description" $ H.toHtml description
|
|
|
|
H.p ! A.class_ "sig" $ H.toHtml typeSignature
|
|
|
|
case maybeNameAndArgs of
|
|
|
|
Just nameAndArgs -> H.pre ! A.class_ "args" $ H.toHtml nameAndArgs
|
|
|
|
Nothing -> H.span $ H.toHtml ("" :: String)
|
|
|
|
H.p ! A.class_ "doc" $ H.preEscapedToHtml htmlDoc
|
2021-01-26 15:22:50 +03:00
|
|
|
when isDeprecated $
|
|
|
|
H.div ! A.class_ "deprecation-text" $
|
|
|
|
H.preEscapedToHtml deprecationStr
|
2020-12-03 14:02:58 +03:00
|
|
|
|
2020-12-02 18:33:37 +03:00
|
|
|
--p_ (toHtml (description))
|