@@ -6,7 +6,7 @@ Author: Joachim Breitner
66
77import VersoManual
88import Manual.Meta.Figure
9- import Lean.Elab.InfoTree.Types
9+ import Lean.Elab.InfoTree
1010
1111open Verso Doc Elab
1212open Verso.Genre Manual
@@ -28,12 +28,31 @@ def Block.noVale.descr : BlockDescr where
2828 some <| fun _ goB _ _ content => do
2929 pure {{<div class ="no-vale" >{{← content.mapM goB}}</div>}}
3030
31- @[code_block_expander markdown]
32- def markdown : CodeBlockExpander
33- | _args, str => do
34- let str ← parserInputString str
35- let some ast := MD4Lean.parse str
36- | throwError "Failed to parse docstring as Markdown"
37- let content ← ast.blocks.mapM <|
38- Markdown.blockFromMarkdown (handleHeaders := Markdown.strongEmphHeaders)
39- pure #[← ``(Block.other Block.noVale #[$content,*])]
31+ /-- Closes the last-opened section, throwing an error on failure. -/
32+ def closeEnclosingSection : PartElabM Unit := do
33+ -- We use `default` as the source position because the Markdown doesn't have one
34+ if let some ctxt' := (← getThe PartElabM.State).partContext.close default then
35+ modifyThe PartElabM.State fun st => {st with partContext := ctxt'}
36+ else
37+ throwError m!"Failed to close the last-opened explanation part"
38+
39+ /-- Closes as many sections as were created by markdown processing. -/
40+ def closeEnclosingSections (headerMapping : Markdown.HeaderMapping) : PartElabM Unit := do
41+ for _ in headerMapping do
42+ closeEnclosingSection
43+
44+ @[part_command Lean.Doc.Syntax.codeblock]
45+ def markdown : PartCommand
46+ | `(Lean.Doc.Syntax.codeblock| ``` $markdown:ident $args*| $txt ``` ) => do
47+ let x ← Lean.Elab.realizeGlobalConstNoOverloadWithInfo markdown
48+ if x != by exact decl_name% then Elab.throwUnsupportedSyntax
49+ for arg in args do
50+ let h ← MessageData.hint m!"Remove it" #["" ] (ref? := arg)
51+ logErrorAt arg m!"No arguments expected{h}"
52+ let some ast := MD4Lean.parse txt.getString
53+ | throwError "Failed to parse body of markdown code block"
54+ let mut currentHeaderLevels : Markdown.HeaderMapping := default
55+ for block in ast.blocks do
56+ currentHeaderLevels ← Markdown.addPartFromMarkdown block currentHeaderLevels
57+ closeEnclosingSections currentHeaderLevels
58+ | _ => Elab.throwUnsupportedSyntax
0 commit comments