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
|
|
|
|
|
import DocGen4.IncludeStr
|
2022-01-03 17:22:12 +00:00
|
|
|
|
import DocGen4.ToHtmlFormat
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
|
|
|
|
namespace DocGen4
|
|
|
|
|
namespace Output
|
|
|
|
|
|
2022-01-03 17:22:12 +00:00
|
|
|
|
open scoped DocGen4.Jsx
|
|
|
|
|
open Lean System Widget Elab
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
|
|
|
|
structure SiteContext where
|
|
|
|
|
result : AnalyzerResult
|
2022-02-15 11:12:17 +00:00
|
|
|
|
depthToRoot: Nat
|
2021-12-15 08:24:49 +00:00
|
|
|
|
currentName : Option Name
|
2022-01-09 15:57:19 +00:00
|
|
|
|
sourceLinker : Name → Option DeclarationRange → String
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
|
|
|
|
def setCurrentName (name : Name) (ctx : SiteContext) := {ctx with currentName := some name}
|
|
|
|
|
|
2021-12-17 14:59:04 +00:00
|
|
|
|
abbrev HtmlT := ReaderT SiteContext
|
|
|
|
|
abbrev HtmlM := HtmlT Id
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
2022-02-15 11:12:17 +00:00
|
|
|
|
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
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
|
|
|
|
def templateExtends {α β : Type} (base : α → HtmlM β) (new : HtmlM α) : HtmlM β :=
|
|
|
|
|
new >>= base
|
|
|
|
|
|
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"
|
2021-12-15 08:24:49 +00:00
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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-02-26 01:41:25 +00:00
|
|
|
|
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"
|
2021-12-15 08:24:49 +00:00
|
|
|
|
end Static
|
|
|
|
|
|
2022-01-03 17:22:12 +00:00
|
|
|
|
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
|
2022-01-03 17:22:12 +00:00
|
|
|
|
|
|
|
|
|
def splitWhitespaces (s : String) : (String × String × String) := Id.run do
|
|
|
|
|
let front := "".pushn ' ' (s.find (!Char.isWhitespace ·))
|
|
|
|
|
let mut s := s.trimLeft
|
|
|
|
|
let back := "".pushn ' ' (s.length - s.offsetOfPos (s.find Char.isWhitespace))
|
|
|
|
|
s:= s.trimRight
|
|
|
|
|
(front, s, back)
|
|
|
|
|
|
|
|
|
|
partial def infoFormatToHtml (i : CodeWithInfos) : HtmlM (Array Html) := do
|
|
|
|
|
match i with
|
2022-02-20 12:45:18 +00:00
|
|
|
|
| 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)) #[]
|
2022-01-03 17:22:12 +00:00
|
|
|
|
| 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 =>
|
2022-02-20 12:45:18 +00:00
|
|
|
|
let (front, t, back) := splitWhitespaces $ Html.escape t
|
2022-01-03 17:22:12 +00:00
|
|
|
|
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]
|
2022-01-03 17:22:12 +00:00
|
|
|
|
| _ =>
|
|
|
|
|
-- TODO: Is this ever reachable?
|
2022-02-12 14:09:13 +00:00
|
|
|
|
pure #[Html.element "a" true #[("href", ←declNameToLink name)] (←infoFormatToHtml t)]
|
2022-01-03 17:22:12 +00:00
|
|
|
|
| _ =>
|
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-01-03 17:22:12 +00:00
|
|
|
|
|
2021-12-15 08:24:49 +00:00
|
|
|
|
end Output
|
|
|
|
|
end DocGen4
|