bookshelf-doc/DocGen4/Output/Base.lean

191 lines
6.6 KiB
Plaintext
Raw Normal View History

/-
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
import DocGen4.IncludeStr
2022-05-19 18:36:35 +00:00
import DocGen4.Output.ToHtmlFormat
2022-05-19 19:05:17 +00:00
namespace DocGen4.Output
open scoped DocGen4.Jsx
open Lean System Widget Elab Process
2022-05-19 19:05:17 +00:00
/--
The context used in the `HtmlM` monad for HTML templating.
-/
structure SiteContext where
2022-05-19 19:05:17 +00:00
/--
The full analysis result from the Process module.
-/
result : AnalyzerResult
2022-05-19 19:05:17 +00:00
/--
How far away we are from the page root, used for relative links to the root.
-/
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.
-/
currentName : Option Name
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
/--
Whether LeanInk is enabled
-/
leanInkEnabled : Bool
def setCurrentName (name : Name) (ctx : SiteContext) := {ctx with currentName := some name}
abbrev HtmlT := ReaderT SiteContext
abbrev HtmlM := HtmlT Id
2022-05-19 19:05:17 +00:00
/--
Obtains the root URL as a relative one to the current depth.
-/
def getRoot : HtmlM String := do
let rec go: Nat -> String
| 0 => "./"
| Nat.succ n' => "../" ++ go n'
let d <- SiteContext.depthToRoot <$> read
return (go d)
2022-02-12 14:09:13 +00:00
def getResult : HtmlM AnalyzerResult := do pure (←read).result
def getCurrentName : HtmlM (Option Name) := do pure (←read).currentName
def getSourceUrl (module : Name) (range : Option DeclarationRange): HtmlM String := do pure $ (←read).sourceLinker module range
def leanInkEnabled? : HtmlM Bool := do pure (←read).leanInkEnabled
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.
-/
def templateExtends {α β : Type} (base : α → HtmlM β) (new : HtmlM α) : HtmlM β :=
new >>= base
2022-05-19 19:05:17 +00:00
/--
Returns the doc-gen4 link to a module name.
-/
2021-12-17 16:20:44 +00:00
def moduleNameToLink (n : Name) : HtmlM String := do
let parts := n.components.map Name.toString
2022-04-06 23:53:06 +00:00
pure $ (← getRoot) ++ (parts.intersperse "/").foldl (· ++ ·) "" ++ ".html"
/--
Returns the HTML doc-gen4 link to a module name.
-/
def moduleToHtmlLink (module : Name) : HtmlM Html := do
pure <a href={←moduleNameToLink module}>{module.toString}</a>
/--
Returns the LeanInk link to a module name.
-/
def moduleNameToInkLink (n : Name) : HtmlM String := do
let parts := "src" :: n.components.map Name.toString
pure $ (← getRoot) ++ (parts.intersperse "/").foldl (· ++ ·) "" ++ ".html"
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 ".")
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.
-/
def styleCss : String := include_str "../../static/style.css"
def declarationDataCenterJs : String := include_str "../../static/declaration-data.js"
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"
def findJs : String := include_str "../../static/find/find.js"
def mathjaxConfigJs : String := include_str "../../static/mathjax-config.js"
end Static
2022-05-19 19:05:17 +00:00
/--
Returns the doc-gen4 link to a declaration name.
-/
def declNameToLink (name : Name) : HtmlM String := do
let res ← getResult
let module := res.moduleNames[res.name2ModIdx.find! name]
2022-02-12 14:09:13 +00:00
pure $ (←moduleNameToLink module) ++ "#" ++ name.toString
/--
Returns the HTML doc-gen4 link to a declaration name.
-/
def declNameToHtmlLink (name : Name) : HtmlM Html := do
pure <a href={←declNameToLink name}>{name.toString}</a>
/--
Returns the LeanInk link to a declaration name.
-/
def declNameToInkLink (name : Name) : HtmlM String := do
let res ← getResult
let module := res.moduleNames[res.name2ModIdx.find! name]
pure $ (←moduleNameToInkLink module) ++ "#" ++ name.toString
/--
Returns the HTML doc-gen4 link to a declaration name with "break_within"
set as class.
-/
def declNameToHtmlBreakWithinLink (name : Name) : HtmlM Html := do
pure <a class="break_within" href={←declNameToLink name}>{name.toString}</a>
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.
-/
def splitWhitespaces (s : String) : (String × String × String) := Id.run do
2022-04-09 17:18:21 +00:00
let front := "".pushn ' ' $ s.offsetOfPos (s.find (!Char.isWhitespace ·))
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
(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.
-/
partial def infoFormatToHtml (i : CodeWithInfos) : HtmlM (Array Html) := do
match i with
| TaggedText.text t => pure #[Html.escape t]
2022-02-12 14:09:13 +00:00
| TaggedText.append tt => tt.foldlM (λ acc t => do pure $ acc ++ (←infoFormatToHtml t)) #[]
| TaggedText.tag a t =>
match a.info.val.info with
| Info.ofTermInfo i =>
match i.expr.consumeMData with
| Expr.const name _ _ =>
match t with
| TaggedText.text t =>
let (front, t, back) := splitWhitespaces $ Html.escape t
let elem := Html.element "a" true #[("href", ←declNameToLink name)] #[t]
2022-02-12 14:09:13 +00:00
pure #[Html.text front, elem, Html.text back]
| _ =>
-- TODO: Is this ever reachable?
2022-02-12 14:09:13 +00:00
pure #[Html.element "a" true #[("href", ←declNameToLink name)] (←infoFormatToHtml t)]
| _ =>
2022-02-12 14:09:13 +00:00
pure #[Html.element "span" true #[("class", "fn")] (←infoFormatToHtml t)]
| _ => pure #[Html.element "span" true #[("class", "fn")] (←infoFormatToHtml t)]
2022-05-19 19:05:17 +00:00
end DocGen4.Output