2022-06-19 22:31:09 +00:00
|
|
|
/-
|
|
|
|
Copyright (c) 2022 Henrik Böving. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
Authors: Henrik Böving, Xubai Wang
|
|
|
|
-/
|
|
|
|
import DocGen4.Output.Base
|
|
|
|
import DocGen4.Output.ToHtmlFormat
|
|
|
|
import DocGen4.LeanInk.Process
|
|
|
|
import Lean.Data.Json
|
|
|
|
import LeanInk.Annotation.Alectryon
|
|
|
|
|
|
|
|
namespace LeanInk.Annotation.Alectryon
|
|
|
|
|
|
|
|
open DocGen4 Output
|
|
|
|
open scoped DocGen4.Jsx
|
|
|
|
|
2022-06-20 16:39:55 +00:00
|
|
|
structure AlectryonContext where
|
|
|
|
counter : Nat
|
|
|
|
|
|
|
|
abbrev AlectryonM := StateT AlectryonContext HtmlM
|
|
|
|
|
|
|
|
def getNextButtonLabel : AlectryonM String := do
|
|
|
|
let val ← get
|
|
|
|
let newCounter := val.counter + 1
|
|
|
|
set { val with counter := newCounter }
|
|
|
|
pure s!"plain-lean4-lean-chk{val.counter}"
|
|
|
|
|
2022-06-20 17:21:50 +00:00
|
|
|
def TypeInfo.toHtml (tyi : TypeInfo) : AlectryonM Html := do
|
|
|
|
pure
|
|
|
|
<div class="alectryon-type-info-wrapper">
|
|
|
|
<small class="alectryon-type-info">
|
|
|
|
<div class="alectryon-goals">
|
|
|
|
<blockquote class="alectryon-goal">
|
|
|
|
<div class="goal-hyps">
|
|
|
|
<span class="hyp-type">
|
2022-06-21 18:54:29 +00:00
|
|
|
<var>{tyi.name}</var>
|
|
|
|
<b>: </b>
|
|
|
|
<span>{tyi.type}</span>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</blockquote>
|
|
|
|
</div>
|
|
|
|
</small>
|
|
|
|
</div>
|
2022-06-20 16:39:55 +00:00
|
|
|
|
2022-06-20 20:06:24 +00:00
|
|
|
def Token.processSemantic (t : Token) : Html :=
|
|
|
|
match t.semanticType with
|
2022-06-21 18:54:29 +00:00
|
|
|
| some "Name.Attribute" => <span class="na">{t.raw}</span>
|
|
|
|
| some "Name.Variable" => <span class="nv">{t.raw}</span>
|
|
|
|
| some "Keyword" => <span class="k">{t.raw}</span>
|
2022-06-20 20:06:24 +00:00
|
|
|
| _ => Html.text t.raw
|
|
|
|
|
2022-06-20 16:39:55 +00:00
|
|
|
def Token.toHtml (t : Token) : AlectryonM Html := do
|
2022-06-20 17:21:50 +00:00
|
|
|
-- Right now t.link is always none from LeanInk, ignore it
|
|
|
|
-- TODO: render docstring
|
|
|
|
let mut parts := #[]
|
|
|
|
if let some tyi := t.typeinfo then
|
2022-07-23 11:01:25 +00:00
|
|
|
parts := parts.push <| ←tyi.toHtml
|
2022-06-20 16:39:55 +00:00
|
|
|
|
2022-06-20 20:06:24 +00:00
|
|
|
parts := parts.push t.processSemantic
|
|
|
|
|
2022-06-20 17:21:50 +00:00
|
|
|
pure
|
|
|
|
-- TODO: Show rest of token
|
2022-06-20 20:06:24 +00:00
|
|
|
<span class="alectryon-token">
|
2022-06-20 17:21:50 +00:00
|
|
|
[parts]
|
2022-06-21 18:54:29 +00:00
|
|
|
</span>
|
2022-06-20 17:21:50 +00:00
|
|
|
|
|
|
|
def Contents.toHtml : Contents → AlectryonM Html
|
|
|
|
| .string value =>
|
|
|
|
pure
|
|
|
|
<span class="alectryon-wsp">
|
|
|
|
{value}
|
2022-06-21 18:54:29 +00:00
|
|
|
</span>
|
2022-06-20 17:21:50 +00:00
|
|
|
| .experimentalTokens values => do
|
|
|
|
let values ← values.mapM Token.toHtml
|
|
|
|
pure
|
|
|
|
<span class="alectryon-wsp">
|
|
|
|
[values]
|
2022-06-21 18:54:29 +00:00
|
|
|
</span>
|
2022-06-20 16:39:55 +00:00
|
|
|
|
|
|
|
def Hypothesis.toHtml (h : Hypothesis) : AlectryonM Html := do
|
2022-06-21 18:54:29 +00:00
|
|
|
let mut hypParts := #[<var>[h.names.intersperse ", " |>.map Html.text |>.toArray]</var>]
|
2022-06-20 16:39:55 +00:00
|
|
|
if h.body != "" then
|
|
|
|
hypParts := hypParts.push
|
|
|
|
<span class="hyp-body">
|
2022-06-21 18:54:29 +00:00
|
|
|
<b>:= </b>
|
|
|
|
<span>{h.body}</span>
|
|
|
|
</span>
|
2022-06-20 16:39:55 +00:00
|
|
|
hypParts := hypParts.push
|
|
|
|
<span class="hyp-type">
|
2022-06-21 18:54:29 +00:00
|
|
|
<b>: </b>
|
|
|
|
<span >{h.type}</span>
|
|
|
|
</span>
|
2022-06-20 16:39:55 +00:00
|
|
|
|
|
|
|
pure
|
|
|
|
<span>
|
|
|
|
[hypParts]
|
2022-06-21 18:54:29 +00:00
|
|
|
</span>
|
2022-06-20 16:39:55 +00:00
|
|
|
|
|
|
|
def Goal.toHtml (g : Goal) : AlectryonM Html := do
|
|
|
|
let mut hypotheses := #[]
|
|
|
|
for hyp in g.hypotheses do
|
|
|
|
let rendered ← hyp.toHtml
|
|
|
|
hypotheses := hypotheses.push rendered
|
|
|
|
hypotheses := hypotheses.push <br/>
|
|
|
|
pure
|
|
|
|
<blockquote class="alectryon-goal">
|
|
|
|
<div class="goal-hyps">
|
|
|
|
[hypotheses]
|
2022-06-21 18:54:29 +00:00
|
|
|
</div>
|
2022-06-20 16:39:55 +00:00
|
|
|
<span class="goal-separator">
|
2022-06-21 18:54:29 +00:00
|
|
|
<hr><span class="goal-name">{g.name}</span></hr>
|
|
|
|
</span>
|
2022-06-20 16:39:55 +00:00
|
|
|
<div class="goal-conclusion">
|
|
|
|
{g.conclusion}
|
2022-06-21 18:54:29 +00:00
|
|
|
</div>
|
|
|
|
</blockquote>
|
2022-06-20 16:39:55 +00:00
|
|
|
|
|
|
|
def Message.toHtml (m : Message) : AlectryonM Html := do
|
|
|
|
pure
|
|
|
|
<blockquote class="alectryon-message">
|
|
|
|
-- TODO: This might have to be done in a fancier way
|
|
|
|
{m.contents}
|
2022-06-21 18:54:29 +00:00
|
|
|
</blockquote>
|
2022-06-20 16:39:55 +00:00
|
|
|
|
|
|
|
def Sentence.toHtml (s : Sentence) : AlectryonM Html := do
|
|
|
|
let messages :=
|
|
|
|
if s.messages.size > 0 then
|
|
|
|
#[
|
|
|
|
<div class="alectryon-messages">
|
|
|
|
[←s.messages.mapM Message.toHtml]
|
2022-06-21 18:54:29 +00:00
|
|
|
</div>
|
2022-06-20 16:39:55 +00:00
|
|
|
]
|
|
|
|
else
|
|
|
|
#[]
|
|
|
|
|
|
|
|
let goals :=
|
|
|
|
if s.goals.size > 0 then
|
|
|
|
-- TODO: Alectryon has a "alectryon-extra-goals" here, implement it
|
|
|
|
#[
|
|
|
|
<div class="alectryon-goals">
|
|
|
|
[←s.goals.mapM Goal.toHtml]
|
2022-06-21 18:54:29 +00:00
|
|
|
</div>
|
2022-06-20 16:39:55 +00:00
|
|
|
]
|
|
|
|
else
|
|
|
|
#[]
|
|
|
|
|
|
|
|
let buttonLabel ← getNextButtonLabel
|
|
|
|
|
|
|
|
pure
|
|
|
|
<span class="alectryon-sentence">
|
|
|
|
<input class="alectryon-toggle" id={buttonLabel} style="display: none" type="checkbox"/>
|
|
|
|
<label class="alectryon-input" for={buttonLabel}>
|
2022-06-20 17:21:50 +00:00
|
|
|
{←s.contents.toHtml}
|
2022-06-21 18:54:29 +00:00
|
|
|
</label>
|
2022-06-20 16:39:55 +00:00
|
|
|
<small class="alectryon-output">
|
|
|
|
[messages]
|
|
|
|
[goals]
|
2022-06-21 18:54:29 +00:00
|
|
|
</small>
|
|
|
|
</span>
|
2022-06-20 16:39:55 +00:00
|
|
|
|
2022-06-20 17:21:50 +00:00
|
|
|
def Text.toHtml (t : Text) : AlectryonM Html := t.contents.toHtml
|
2022-06-20 16:39:55 +00:00
|
|
|
|
|
|
|
def Fragment.toHtml : Fragment → AlectryonM Html
|
|
|
|
| .text value => value.toHtml
|
|
|
|
| .sentence value => value.toHtml
|
|
|
|
|
|
|
|
def baseHtml (content : Array Html) : AlectryonM Html := do
|
|
|
|
let banner :=
|
|
|
|
<div «class»="alectryon-banner">
|
2022-06-21 18:54:29 +00:00
|
|
|
Built with <a href="https://github.com/leanprover/doc-gen4">doc-gen4</a>, running Lean4.
|
|
|
|
Bubbles (<span class="alectryon-bubble"></span>) indicate interactive fragments: hover for details, tap to reveal contents.
|
|
|
|
Use <kbd>Ctrl+↑</kbd> <kbd>Ctrl+↓</kbd> to navigate, <kbd>Ctrl+🖱️</kbd> to focus.
|
|
|
|
On Mac, use <kbd>Cmd</kbd> instead of <kbd>Ctrl</kbd>.
|
2022-06-20 16:39:55 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
pure
|
|
|
|
<html lang="en" class="alectryon-standalone">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8"/>
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
|
|
|
|
|
|
<link rel="stylesheet" href={s!"{←getRoot}src/alectryon.css"}/>
|
2022-06-20 20:06:24 +00:00
|
|
|
<link rel="stylesheet" href={s!"{←getRoot}src/pygments.css"}/>
|
2022-06-20 16:39:55 +00:00
|
|
|
<link rel="stylesheet" href={s!"{←getRoot}src/docutils_basic.css"}/>
|
|
|
|
<link rel="shortcut icon" href={s!"{←getRoot}favicon.ico"}/>
|
|
|
|
|
|
|
|
<script defer="true" src={s!"{←getRoot}src/alectryon.js"}></script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<article class="alectryon-root alectryon-centered">
|
|
|
|
{banner}
|
|
|
|
<pre class="alectryon-io highlight">
|
|
|
|
[content]
|
|
|
|
</pre>
|
|
|
|
</article>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|
|
|
|
def renderFragments (fs : Array Fragment) : AlectryonM Html :=
|
|
|
|
fs.mapM Fragment.toHtml >>= baseHtml
|
2022-06-19 22:31:09 +00:00
|
|
|
|
|
|
|
end LeanInk.Annotation.Alectryon
|
|
|
|
|
|
|
|
namespace DocGen4.Output.LeanInk
|
|
|
|
|
|
|
|
open Lean
|
|
|
|
open LeanInk.Annotation.Alectryon
|
|
|
|
open scoped DocGen4.Jsx
|
|
|
|
|
2022-07-20 23:40:04 +00:00
|
|
|
def moduleToHtml (module : Process.Module) (inkPath : System.FilePath) (sourceFilePath : System.FilePath) : HtmlT IO Html := withTheReader SiteBaseContext (setCurrentName module.name) do
|
2022-06-19 22:31:09 +00:00
|
|
|
let json ← runInk inkPath sourceFilePath
|
2022-06-20 16:39:55 +00:00
|
|
|
let fragments := fromJson? json
|
2022-06-19 22:31:09 +00:00
|
|
|
match fragments with
|
2022-06-20 16:39:55 +00:00
|
|
|
| .ok fragments =>
|
|
|
|
let render := StateT.run (LeanInk.Annotation.Alectryon.renderFragments fragments) { counter := 0 }
|
|
|
|
let ctx ← read
|
2022-07-20 23:40:04 +00:00
|
|
|
let baseCtx ← readThe SiteBaseContext
|
|
|
|
let (html, _) := render |>.run ctx baseCtx
|
2022-06-20 16:39:55 +00:00
|
|
|
pure html
|
2022-07-23 11:01:25 +00:00
|
|
|
| .error err => throw <| IO.userError s!"Error while parsing LeanInk Output: {err}"
|
2022-06-19 22:31:09 +00:00
|
|
|
|
|
|
|
end DocGen4.Output.LeanInk
|