Skip to content

Commit 0d4079c

Browse files
feat: allow examples to be open by default (#479)
This PR adds a field to the `example` block configuration allowing an example to be open by default. This feature is useful for error explanations, where we would like for this to be the case. --------- Co-authored-by: David Thrane Christiansen <[email protected]>
1 parent 944cedb commit 0d4079c

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

Manual/Meta/Example.lean

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@ open Lean Elab
1616

1717
namespace Manual
1818

19-
def Block.example (name : Option String) : Block where
19+
def Block.example (name : Option String) (opened : Bool) : Block where
2020
name := `Manual.example
21-
data := ToJson.toJson (name, (none : Option Tag))
21+
data := ToJson.toJson (name, opened, (none : Option Tag))
2222

2323
structure ExampleConfig where
2424
description : FileMap × TSyntaxArray `inline
2525
/-- Name for refs -/
2626
tag : Option String := none
2727
keep : Bool := false
28+
opened : Bool := false
2829

2930

3031
def ExampleConfig.parse [Monad m] [MonadInfoTree m] [MonadLiftT CoreM m] [MonadEnv m] [MonadError m] [MonadFileMap m] : ArgParse m ExampleConfig :=
31-
ExampleConfig.mk <$> .positional `description .inlinesString <*> .named `tag .string true <*> (.named `keep .bool true <&> (·.getD false))
32+
ExampleConfig.mk <$> .positional `description .inlinesString
33+
<*> .named `tag .string true
34+
<*> (.named `keep .bool true <&> (·.getD false))
35+
<*> (.named `open .bool true <&> (·.getD false))
3236

3337
def prioritizedElab [Monad m] (prioritize : α → m Bool) (act : α → m β) (xs : Array α) : m (Array β) := do
3438
let mut out := #[]
@@ -70,35 +74,42 @@ def «example» : DirectiveExpander
7074
withoutModifyingEnv <| prioritizedElab (isLeanBlock ·) elabBlock contents
7175
-- Examples are represented using the first block to hold the description. Storing it in the JSON
7276
-- entails repeated (de)serialization.
73-
pure #[← ``(Block.other (Block.example $(quote cfg.tag)) #[Block.para #[$description,*], $blocks,*])]
77+
pure #[← ``(Block.other (Block.example $(quote cfg.tag) (opened := $(quote cfg.opened)))
78+
#[Block.para #[$description,*], $blocks,*])]
7479

7580
@[block_extension «example»]
7681
def example.descr : BlockDescr where
7782
traverse id data contents := do
78-
match FromJson.fromJson? data (α := Option String × Option Tag) with
83+
match FromJson.fromJson? data (α := Option String × Bool × Option Tag) with
7984
| .error e => logError s!"Error deserializing example tag: {e}"; pure none
80-
| .ok (none, _) => pure none
81-
| .ok (some x, none) =>
85+
| .ok (none, _, _) => pure none
86+
| .ok (some x, opened, none) =>
8287
let path ← (·.path) <$> read
8388
let tag ← Verso.Genre.Manual.externalTag id path x
84-
pure <| some <| Block.other {Block.example none with id := some id, data := toJson (some x, some tag)} contents
85-
| .ok (some _, some _) => pure none
89+
pure <| some <| Block.other {Block.example none false with id := some id, data := toJson (some x, opened, some tag)} contents
90+
| .ok (some _, _, some _) => pure none
8691
toTeX :=
8792
some <| fun _ go _ _ content => do
8893
pure <| .seq <| ← content.mapM fun b => do
8994
pure <| .seq #[← go b, .raw "\n"]
9095
toHtml :=
9196
open Verso.Doc.Html in
9297
open Verso.Output.Html in
93-
some <| fun goI goB id _data blocks => do
98+
some <| fun goI goB id data blocks => do
9499
if h : blocks.size < 1 then
95100
HtmlT.logError "Malformed example"
96101
pure .empty
97102
else
98103
let .para description := blocks[0]
99104
| HtmlT.logError "Malformed example - description not paragraph"; pure .empty
105+
let opened ←
106+
match FromJson.fromJson? data (α := Option String × Bool × Option Tag) with
107+
| .error e => HtmlT.logError s!"Error deserializing example data: {e}"; pure false
108+
| .ok (_, opened, _) => pure opened
100109
let xref ← HtmlT.state
101-
let attrs := xref.htmlId id
110+
let mut attrs := xref.htmlId id
111+
if opened then
112+
attrs := attrs.push ("open", "")
102113
pure {{
103114
<details class="example" {{attrs}}>
104115
<summary class="description">{{← description.mapM goI}}</summary>

0 commit comments

Comments
 (0)