Carp/src/RenderDocs.hs

222 lines
8.3 KiB
Haskell
Raw Normal View History

2018-03-27 07:27:52 +03:00
{-# LANGUAGE OverloadedStrings #-}
module RenderDocs where
import CMark
2019-04-30 17:21:06 +03:00
import Control.Monad (when)
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
import qualified Map
import qualified Meta
2018-03-27 07:27:52 +03:00
import Obj
import Path
2020-05-30 12:52:37 +03:00
import Project
import Text.Blaze.Html.Renderer.Pretty (renderHtml)
import Text.Blaze.Html5 ((!))
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A
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)
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
saveDocsForEnvs :: Project -> [(SymPath, Binder)] -> IO ()
saveDocsForEnvs ctx pathsAndEnvBinders =
let dir = projectDocsDir ctx
title = projectTitle ctx
generateIndex = projectDocsGenerateIndex ctx
dependencies = getDependenciesForEnvs (Prelude.map (\(p, b) -> (p, fst (getEnvAndMetaFromBinder b))) pathsAndEnvBinders)
pathsAndEnvBinders' = pathsAndEnvBinders ++ dependencies
allEnvNames = fmap fst pathsAndEnvBinders'
in do
mapM_ (saveDocsForEnvBinder ctx allEnvNames) pathsAndEnvBinders'
when
generateIndex
( writeFile
(dir </> title ++ "_index.html")
(projectIndexPage ctx allEnvNames)
)
putStrLn ("Generated docs to '" ++ dir ++ "'")
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)
-- | 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)
_ -> error "Binder's not a module. This should be detected in 'commandSaveDocsInternal'."
projectIndexPage :: Project -> [SymPath] -> String
projectIndexPage ctx moduleNames =
let logo = projectDocsLogo ctx
url = projectDocsURL ctx
css = projectDocsStyling ctx
htmlHeader = H.toHtml $ projectTitle ctx
htmlDoc = commonmarkToHtml [optSafe] $ Text.pack $ projectDocsPrelude ctx
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
in html
2018-03-27 11:51:16 +03:00
headOfPage :: String -> H.Html
headOfPage css =
2018-07-18 13:23:02 +03:00
H.head $
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
saveDocsForEnvBinder :: Project -> [SymPath] -> (SymPath, Binder) -> IO ()
saveDocsForEnvBinder ctx moduleNames (envPath, envBinder) =
do
let moduleName = show envPath
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
envBinderToHtml :: Binder -> Project -> String -> [SymPath] -> H.Html
envBinderToHtml envBinder ctx moduleName moduleNames =
let (env, meta) = getEnvAndMetaFromBinder envBinder
title = projectTitle ctx
css = projectDocsStyling ctx
url = projectDocsURL ctx
logo = projectDocsLogo ctx
moduleDescription = case Meta.get "doc" meta of
Just (XObj (Str s) _ _) -> s
Nothing -> ""
_ -> error "moduledescription"
moduleDescriptionHtml = commonmarkToHtml [optSafe] $ Text.pack moduleDescription
in H.docTypeHtml $
do
headOfPage css
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
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
moduleIndex :: [SymPath] -> H.Html
moduleIndex moduleNames =
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])
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
binderToHtml :: String -> Binder -> H.Html
binderToHtml moduleName (Binder meta xobj) =
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
typeSignature = case xobjTy xobj of
Just t -> show (beautifyType t) -- NOTE: This destroys user-defined names of type variables!
Nothing -> ""
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
_ -> ""
docString = case Meta.get "doc" meta of
Just (XObj (Str s) _ _) -> s
Just found -> pretty found
Nothing -> ""
htmlDoc = commonmarkToHtml [optSafe] $ Text.pack docString
in H.div ! A.class_ "binder" $
do
H.a ! A.class_ "anchor" ! A.href (H.stringValue ("#" ++ name)) $
H.h3 ! A.id (H.stringValue name) $
do
if isMod xobj
then H.a ! A.href (H.stringValue (moduleName ++ "." ++ pretty xobj ++ ".html")) $ H.toHtml (pretty xobj)
else H.toHtml name
when isDeprecated $
H.span ! A.class_ "deprecation-notice" $
H.toHtml ("deprecated" :: String)
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
when isDeprecated $
H.div ! A.class_ "deprecation-text" $
H.preEscapedToHtml deprecationStr
--p_ (toHtml (description))