2021-12-15 08:24:49 +00:00
|
|
|
|
/-
|
|
|
|
|
Copyright (c) 2021 Henrik Böving. All rights reserved.
|
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
Authors: Henrik Böving
|
|
|
|
|
-/
|
|
|
|
|
import DocGen4.Process
|
2022-05-19 18:36:35 +00:00
|
|
|
|
import DocGen4.Output.ToHtmlFormat
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
2022-05-19 19:05:17 +00:00
|
|
|
|
namespace DocGen4.Output
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
2022-01-03 17:22:12 +00:00
|
|
|
|
open scoped DocGen4.Jsx
|
2022-05-19 22:36:43 +00:00
|
|
|
|
open Lean System Widget Elab Process
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
2022-07-26 10:52:41 +00:00
|
|
|
|
def basePath := FilePath.mk "." / "build" / "doc"
|
|
|
|
|
def srcBasePath := basePath / "src"
|
|
|
|
|
def declarationsBasePath := basePath / "declarations"
|
|
|
|
|
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
2022-07-20 23:40:04 +00:00
|
|
|
|
The context used in the `BaseHtmlM` monad for HTML templating.
|
2022-05-19 19:05:17 +00:00
|
|
|
|
-/
|
2022-07-20 23:40:04 +00:00
|
|
|
|
structure SiteBaseContext where
|
|
|
|
|
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
2022-07-20 23:40:04 +00:00
|
|
|
|
The module hierarchy as a tree structure.
|
2022-05-19 19:05:17 +00:00
|
|
|
|
-/
|
2022-07-20 23:40:04 +00:00
|
|
|
|
hierarchy : Hierarchy
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
|
|
|
|
How far away we are from the page root, used for relative links to the root.
|
|
|
|
|
-/
|
2022-02-15 11:12:17 +00:00
|
|
|
|
depthToRoot: Nat
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
|
|
|
|
The name of the current module if there is one, there exist a few
|
|
|
|
|
pages that don't have a module name.
|
|
|
|
|
-/
|
2021-12-15 08:24:49 +00:00
|
|
|
|
currentName : Option Name
|
2023-03-09 20:37:13 +00:00
|
|
|
|
/--
|
|
|
|
|
The Github URL of the project that we are building docs for.
|
|
|
|
|
-/
|
|
|
|
|
projectGithubUrl : String
|
|
|
|
|
/--
|
|
|
|
|
The commit of the project that we are building docs for.
|
|
|
|
|
-/
|
|
|
|
|
projectCommit : String
|
2022-07-20 23:40:04 +00:00
|
|
|
|
|
|
|
|
|
/--
|
|
|
|
|
The context used in the `HtmlM` monad for HTML templating.
|
|
|
|
|
-/
|
|
|
|
|
structure SiteContext where
|
|
|
|
|
/--
|
|
|
|
|
The full analysis result from the Process module.
|
|
|
|
|
-/
|
|
|
|
|
result : AnalyzerResult
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
|
|
|
|
A function to link declaration names to their source URLs, usually Github ones.
|
|
|
|
|
-/
|
2022-01-09 15:57:19 +00:00
|
|
|
|
sourceLinker : Name → Option DeclarationRange → String
|
2022-06-19 22:31:09 +00:00
|
|
|
|
/--
|
|
|
|
|
Whether LeanInk is enabled
|
|
|
|
|
-/
|
|
|
|
|
leanInkEnabled : Bool
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
2022-07-20 23:40:04 +00:00
|
|
|
|
def setCurrentName (name : Name) (ctx : SiteBaseContext) := {ctx with currentName := some name}
|
|
|
|
|
|
|
|
|
|
abbrev BaseHtmlT := ReaderT SiteBaseContext
|
|
|
|
|
abbrev BaseHtmlM := BaseHtmlT Id
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
2022-07-20 23:40:04 +00:00
|
|
|
|
abbrev HtmlT (m) := ReaderT SiteContext (BaseHtmlT m)
|
2021-12-17 14:59:04 +00:00
|
|
|
|
abbrev HtmlM := HtmlT Id
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
2022-07-20 23:40:04 +00:00
|
|
|
|
def HtmlT.run (x : HtmlT m α) (ctx : SiteContext) (baseCtx : SiteBaseContext) : m α :=
|
|
|
|
|
ReaderT.run x ctx |>.run baseCtx
|
|
|
|
|
|
|
|
|
|
def HtmlM.run (x : HtmlM α) (ctx : SiteContext) (baseCtx : SiteBaseContext) : α :=
|
|
|
|
|
ReaderT.run x ctx |>.run baseCtx |>.run
|
|
|
|
|
|
2022-07-27 18:11:41 +00:00
|
|
|
|
instance [Monad m] : MonadLift HtmlM (HtmlT m) where
|
2023-01-01 18:51:01 +00:00
|
|
|
|
monadLift x := do return x.run (← readThe SiteContext) (← readThe SiteBaseContext)
|
2022-07-27 18:11:41 +00:00
|
|
|
|
|
|
|
|
|
instance [Monad m] : MonadLift BaseHtmlM (BaseHtmlT m) where
|
2023-01-01 18:51:01 +00:00
|
|
|
|
monadLift x := do return x.run (← readThe SiteBaseContext)
|
2022-07-27 18:11:41 +00:00
|
|
|
|
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
|
|
|
|
Obtains the root URL as a relative one to the current depth.
|
|
|
|
|
-/
|
2022-07-20 23:40:04 +00:00
|
|
|
|
def getRoot : BaseHtmlM String := do
|
2022-02-15 11:12:17 +00:00
|
|
|
|
let rec go: Nat -> String
|
|
|
|
|
| 0 => "./"
|
|
|
|
|
| Nat.succ n' => "../" ++ go n'
|
2022-07-20 23:40:04 +00:00
|
|
|
|
let d <- SiteBaseContext.depthToRoot <$> read
|
2022-02-15 11:12:17 +00:00
|
|
|
|
return (go d)
|
|
|
|
|
|
2023-01-01 18:51:01 +00:00
|
|
|
|
def getHierarchy : BaseHtmlM Hierarchy := do return (← read).hierarchy
|
|
|
|
|
def getCurrentName : BaseHtmlM (Option Name) := do return (← read).currentName
|
|
|
|
|
def getResult : HtmlM AnalyzerResult := do return (← read).result
|
|
|
|
|
def getSourceUrl (module : Name) (range : Option DeclarationRange): HtmlM String := do return (← read).sourceLinker module range
|
|
|
|
|
def leanInkEnabled? : HtmlM Bool := do return (← read).leanInkEnabled
|
2023-03-09 20:37:13 +00:00
|
|
|
|
def getProjectGithubUrl : BaseHtmlM String := do return (← read).projectGithubUrl
|
|
|
|
|
def getProjectCommit : BaseHtmlM String := do return (← read).projectCommit
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
|
|
|
|
If a template is meant to be extended because it for example only provides the
|
|
|
|
|
header but no real content this is the way to fill the template with content.
|
2022-07-20 23:40:04 +00:00
|
|
|
|
This is untyped so HtmlM and BaseHtmlM can be mixed.
|
2022-05-19 19:05:17 +00:00
|
|
|
|
-/
|
2022-07-20 23:40:04 +00:00
|
|
|
|
def templateExtends {α β} {m} [Bind m] (base : α → m β) (new : m α) : m β :=
|
2021-12-15 08:24:49 +00:00
|
|
|
|
new >>= base
|
|
|
|
|
|
2022-07-20 23:40:04 +00:00
|
|
|
|
def templateLiftExtends {α β} {m n} [Bind m] [MonadLift n m] (base : α → n β) (new : m α) : m β :=
|
|
|
|
|
new >>= (monadLift ∘ base)
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
|
|
|
|
Returns the doc-gen4 link to a module name.
|
|
|
|
|
-/
|
2022-07-20 23:40:04 +00:00
|
|
|
|
def moduleNameToLink (n : Name) : BaseHtmlM String := do
|
2021-12-17 16:20:44 +00:00
|
|
|
|
let parts := n.components.map Name.toString
|
2023-01-01 18:51:01 +00:00
|
|
|
|
return (← getRoot) ++ (parts.intersperse "/").foldl (· ++ ·) "" ++ ".html"
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
2022-05-19 19:40:22 +00:00
|
|
|
|
/--
|
|
|
|
|
Returns the HTML doc-gen4 link to a module name.
|
|
|
|
|
-/
|
2022-07-20 23:40:04 +00:00
|
|
|
|
def moduleToHtmlLink (module : Name) : BaseHtmlM Html := do
|
2023-01-01 18:51:01 +00:00
|
|
|
|
return <a href={← moduleNameToLink module}>{module.toString}</a>
|
2022-05-19 19:40:22 +00:00
|
|
|
|
|
2022-06-19 22:31:09 +00:00
|
|
|
|
/--
|
|
|
|
|
Returns the LeanInk link to a module name.
|
|
|
|
|
-/
|
2022-07-20 23:40:04 +00:00
|
|
|
|
def moduleNameToInkLink (n : Name) : BaseHtmlM String := do
|
2022-06-19 22:31:09 +00:00
|
|
|
|
let parts := "src" :: n.components.map Name.toString
|
2023-01-01 18:51:01 +00:00
|
|
|
|
return (← getRoot) ++ (parts.intersperse "/").foldl (· ++ ·) "" ++ ".html"
|
2022-06-19 22:31:09 +00:00
|
|
|
|
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
|
|
|
|
Returns the path to the HTML file that contains information about a module.
|
|
|
|
|
-/
|
2021-12-15 10:59:13 +00:00
|
|
|
|
def moduleNameToFile (basePath : FilePath) (n : Name) : FilePath :=
|
2022-01-15 14:35:52 +00:00
|
|
|
|
let parts := n.components.map Name.toString
|
|
|
|
|
FilePath.withExtension (basePath / parts.foldl (· / ·) (FilePath.mk ".")) "html"
|
2021-12-15 10:59:13 +00:00
|
|
|
|
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
|
|
|
|
Returns the directory of the HTML file that contains information about a module.
|
|
|
|
|
-/
|
2021-12-15 10:59:13 +00:00
|
|
|
|
def moduleNameToDirectory (basePath : FilePath) (n : Name) : FilePath :=
|
2022-01-15 14:35:52 +00:00
|
|
|
|
let parts := n.components.dropLast.map Name.toString
|
|
|
|
|
basePath / parts.foldl (· / ·) (FilePath.mk ".")
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
|
|
|
|
section Static
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/-!
|
|
|
|
|
The following section contains all the statically included files that
|
|
|
|
|
are used in documentation generation, notably JS and CSS ones.
|
|
|
|
|
-/
|
2022-02-26 01:41:25 +00:00
|
|
|
|
def styleCss : String := include_str "../../static/style.css"
|
|
|
|
|
def declarationDataCenterJs : String := include_str "../../static/declaration-data.js"
|
2023-06-08 05:52:51 +00:00
|
|
|
|
def colorSchemeJs : String := include_str "../../static/color-scheme.js"
|
2022-02-26 01:41:25 +00:00
|
|
|
|
def navJs : String := include_str "../../static/nav.js"
|
|
|
|
|
def howAboutJs : String := include_str "../../static/how-about.js"
|
|
|
|
|
def searchJs : String := include_str "../../static/search.js"
|
2022-07-22 12:48:36 +00:00
|
|
|
|
def instancesJs : String := include_str "../../static/instances.js"
|
2022-07-22 14:15:37 +00:00
|
|
|
|
def importedByJs : String := include_str "../../static/importedBy.js"
|
2022-02-26 01:41:25 +00:00
|
|
|
|
def findJs : String := include_str "../../static/find/find.js"
|
|
|
|
|
def mathjaxConfigJs : String := include_str "../../static/mathjax-config.js"
|
2023-06-08 05:52:51 +00:00
|
|
|
|
|
2022-06-20 16:39:55 +00:00
|
|
|
|
def alectryonCss : String := include_str "../../static/alectryon/alectryon.css"
|
|
|
|
|
def alectryonJs : String := include_str "../../static/alectryon/alectryon.js"
|
|
|
|
|
def docUtilsCss : String := include_str "../../static/alectryon/docutils_basic.css"
|
2022-06-20 20:06:24 +00:00
|
|
|
|
def pygmentsCss : String := include_str "../../static/alectryon/pygments.css"
|
2021-12-15 08:24:49 +00:00
|
|
|
|
end Static
|
|
|
|
|
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
|
|
|
|
Returns the doc-gen4 link to a declaration name.
|
|
|
|
|
-/
|
2022-01-03 17:22:12 +00:00
|
|
|
|
def declNameToLink (name : Name) : HtmlM String := do
|
|
|
|
|
let res ← getResult
|
2022-07-20 14:18:57 +00:00
|
|
|
|
let module := res.moduleNames[res.name2ModIdx.find! name |>.toNat]!
|
2023-01-01 18:51:01 +00:00
|
|
|
|
return (← moduleNameToLink module) ++ "#" ++ name.toString
|
2022-01-03 17:22:12 +00:00
|
|
|
|
|
2022-05-19 19:40:22 +00:00
|
|
|
|
/--
|
|
|
|
|
Returns the HTML doc-gen4 link to a declaration name.
|
|
|
|
|
-/
|
|
|
|
|
def declNameToHtmlLink (name : Name) : HtmlM Html := do
|
2023-01-01 18:51:01 +00:00
|
|
|
|
return <a href={← declNameToLink name}>{name.toString}</a>
|
2022-05-19 19:40:22 +00:00
|
|
|
|
|
2022-06-19 22:31:09 +00:00
|
|
|
|
/--
|
|
|
|
|
Returns the LeanInk link to a declaration name.
|
|
|
|
|
-/
|
|
|
|
|
def declNameToInkLink (name : Name) : HtmlM String := do
|
|
|
|
|
let res ← getResult
|
2022-07-20 14:18:57 +00:00
|
|
|
|
let module := res.moduleNames[res.name2ModIdx.find! name |>.toNat]!
|
2023-01-01 18:51:01 +00:00
|
|
|
|
return (← moduleNameToInkLink module) ++ "#" ++ name.toString
|
2022-06-19 22:31:09 +00:00
|
|
|
|
|
2023-03-03 11:48:48 +00:00
|
|
|
|
/--
|
|
|
|
|
Returns a name splitted into parts.
|
|
|
|
|
Together with "break_within" CSS class this helps browser to break a name
|
|
|
|
|
nicely.
|
|
|
|
|
-/
|
|
|
|
|
def breakWithin (name: String) : (Array Html) :=
|
|
|
|
|
name.splitOn "."
|
|
|
|
|
|> .map (fun (s: String) => <span class="name">{s}</span>)
|
|
|
|
|
|> .intersperse "."
|
|
|
|
|
|> List.toArray
|
|
|
|
|
|
2022-05-19 19:40:22 +00:00
|
|
|
|
/--
|
|
|
|
|
Returns the HTML doc-gen4 link to a declaration name with "break_within"
|
|
|
|
|
set as class.
|
|
|
|
|
-/
|
|
|
|
|
def declNameToHtmlBreakWithinLink (name : Name) : HtmlM Html := do
|
2023-03-03 11:48:48 +00:00
|
|
|
|
return <a class="break_within" href={← declNameToLink name}>
|
|
|
|
|
[breakWithin name.toString]
|
|
|
|
|
</a>
|
2022-05-19 19:40:22 +00:00
|
|
|
|
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
|
|
|
|
In Lean syntax declarations the following pattern is quite common:
|
|
|
|
|
```
|
|
|
|
|
syntax term " + " term : term
|
|
|
|
|
```
|
|
|
|
|
that is, we place spaces around the operator in the middle. When the
|
|
|
|
|
`InfoTree` framework provides us with information about what source token
|
|
|
|
|
corresponds to which identifier it will thus say that `" + "` corresponds to
|
|
|
|
|
`HAdd.hadd`. This is however not the way we want this to be linked, in the HTML
|
|
|
|
|
only `+` should be linked, taking care of this is what this function is
|
|
|
|
|
responsible for.
|
|
|
|
|
-/
|
2022-01-03 17:22:12 +00:00
|
|
|
|
def splitWhitespaces (s : String) : (String × String × String) := Id.run do
|
2022-07-23 11:01:25 +00:00
|
|
|
|
let front := "".pushn ' ' <| s.offsetOfPos (s.find (!Char.isWhitespace ·))
|
2022-01-03 17:22:12 +00:00
|
|
|
|
let mut s := s.trimLeft
|
|
|
|
|
let back := "".pushn ' ' (s.length - s.offsetOfPos (s.find Char.isWhitespace))
|
2022-04-09 17:18:21 +00:00
|
|
|
|
s := s.trimRight
|
2022-01-03 17:22:12 +00:00
|
|
|
|
(front, s, back)
|
|
|
|
|
|
2022-05-19 19:05:17 +00:00
|
|
|
|
/--
|
|
|
|
|
Turns a `CodeWithInfos` object, that is basically a Lean syntax tree with
|
|
|
|
|
information about what the identifiers mean, into an HTML object that links
|
|
|
|
|
to as much information as possible.
|
|
|
|
|
-/
|
2022-01-03 17:22:12 +00:00
|
|
|
|
partial def infoFormatToHtml (i : CodeWithInfos) : HtmlM (Array Html) := do
|
|
|
|
|
match i with
|
2023-01-01 18:51:01 +00:00
|
|
|
|
| .text t => return #[Html.escape t]
|
|
|
|
|
| .append tt => tt.foldlM (fun acc t => do return acc ++ (← infoFormatToHtml t)) #[]
|
2022-11-20 12:10:05 +00:00
|
|
|
|
| .tag a t =>
|
2022-01-03 17:22:12 +00:00
|
|
|
|
match a.info.val.info with
|
|
|
|
|
| Info.ofTermInfo i =>
|
2022-08-11 21:19:31 +00:00
|
|
|
|
let cleanExpr := i.expr.consumeMData
|
2022-11-20 12:10:05 +00:00
|
|
|
|
match cleanExpr with
|
|
|
|
|
| .const name _ =>
|
2022-08-11 21:19:31 +00:00
|
|
|
|
-- TODO: this is some very primitive blacklisting but real Blacklisting needs MetaM
|
|
|
|
|
-- find a better solution
|
2023-01-01 18:51:01 +00:00
|
|
|
|
if (← getResult).name2ModIdx.contains name then
|
2022-08-11 21:19:31 +00:00
|
|
|
|
match t with
|
2022-11-20 12:10:05 +00:00
|
|
|
|
| .text t =>
|
2022-08-11 21:19:31 +00:00
|
|
|
|
let (front, t, back) := splitWhitespaces <| Html.escape t
|
2023-01-01 18:51:01 +00:00
|
|
|
|
let elem := <a href={← declNameToLink name}>{t}</a>
|
|
|
|
|
return #[Html.text front, elem, Html.text back]
|
2022-08-11 21:19:31 +00:00
|
|
|
|
| _ =>
|
2023-01-01 18:51:01 +00:00
|
|
|
|
return #[<a href={← declNameToLink name}>[← infoFormatToHtml t]</a>]
|
2022-08-11 21:19:31 +00:00
|
|
|
|
else
|
2023-01-01 18:51:01 +00:00
|
|
|
|
return #[<span class="fn">[← infoFormatToHtml t]</span>]
|
2022-11-20 12:10:05 +00:00
|
|
|
|
| .sort _ =>
|
|
|
|
|
match t with
|
|
|
|
|
| .text t =>
|
2023-09-10 12:44:14 +00:00
|
|
|
|
let sortPrefix :: rest := t.splitOn " " | unreachable!
|
2022-11-20 12:10:05 +00:00
|
|
|
|
let sortLink := <a href={s!"{← getRoot}foundational_types.html"}>{sortPrefix}</a>
|
2023-09-10 12:44:14 +00:00
|
|
|
|
let mut restStr := String.intercalate " " rest
|
|
|
|
|
if restStr.length != 0 then
|
|
|
|
|
restStr := " " ++ restStr
|
|
|
|
|
return #[sortLink, Html.text restStr]
|
2022-11-20 12:10:05 +00:00
|
|
|
|
| _ =>
|
2023-01-01 18:51:01 +00:00
|
|
|
|
return #[<a href={s!"{← getRoot}foundational_types.html"}>[← infoFormatToHtml t]</a>]
|
2022-11-20 12:10:05 +00:00
|
|
|
|
| _ =>
|
2023-01-01 18:51:01 +00:00
|
|
|
|
return #[<span class="fn">[← infoFormatToHtml t]</span>]
|
|
|
|
|
| _ => return #[<span class="fn">[← infoFormatToHtml t]</span>]
|
2022-01-03 17:22:12 +00:00
|
|
|
|
|
2022-07-21 21:01:15 +00:00
|
|
|
|
def baseHtmlHeadDeclarations : BaseHtmlM (Array Html) := do
|
2023-01-01 18:51:01 +00:00
|
|
|
|
return #[
|
2022-07-21 21:01:15 +00:00
|
|
|
|
<meta charset="UTF-8"/>,
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>,
|
2023-01-01 18:51:01 +00:00
|
|
|
|
<link rel="stylesheet" href={s!"{← getRoot}style.css"}/>,
|
|
|
|
|
<link rel="stylesheet" href={s!"{← getRoot}src/pygments.css"}/>,
|
|
|
|
|
<link rel="shortcut icon" href={s!"{← getRoot}favicon.ico"}/>,
|
|
|
|
|
<link rel="prefetch" href={s!"{← getRoot}/declarations/declaration-data.bmp"} as="image"/>
|
2022-07-21 21:01:15 +00:00
|
|
|
|
]
|
2022-07-21 20:06:26 +00:00
|
|
|
|
|
2022-05-19 19:05:17 +00:00
|
|
|
|
end DocGen4.Output
|