package forester
A tool for tending mathematical forests
Install
Dune Dependency
Authors
Maintainers
Sources
2.5.1.tar.gz
md5=b26b6f4e3d14b976c3d5018c97b81b94
sha512=f0c794ed4330485b5255639720ad7201488b77ef947b0736a847fa4f56f93d3f03a22fd2a3319bbe867bab827a4b5ae73b2426a1067911b29fe178d21dd99e73
doc/src/forester.core/Expand.ml.html
Source file Expand.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
open Prelude open Resolver module Set = Set.Make (String) module UnitMap = Map.Make (String) type mode = Frontmatter | Body [@@deriving show] type exports = P.data Trie.Untagged.t module U = Algaeff.Reader.Make (struct type env = exports UnitMap.t end) module Fm = Algaeff.State.Make (struct type state = Syn.frontmatter end) module Mode = struct include Algaeff.State.Make (struct type state = mode end) let protect kont = let mode = get () in let x = kont () in set mode; x end exception FrontmatterInBody of Code.t let only_frontmatter code () = match Mode.get () with | Frontmatter -> () | Body -> raise @@ FrontmatterInBody code let rec expand (code : Code.t) : Syn.t = match code with | [] -> [] | Text x :: rest -> if not (String.trim x = "") then Mode.set Body; Syn.Text x :: expand rest | Let (a, bs, def) :: rest -> let singl = Mode.protect @@ fun () -> Trie.Untagged.singleton (a, `Term (expand_lambda (bs, def))) in Resolver.Scope.section [] @@ fun _ -> Resolver.Scope.import_subtree ([], singl); expand rest | Namespace (path, body) :: rest -> only_frontmatter code (); let mode, result = Scope.section path @@ fun () -> let x = expand body in Mode.get (), x in Mode.set mode; result @ expand rest | Open path :: rest -> Scope.section [] @@ fun () -> Scope.modify_visible @@ Resolver.Lang.union [ Resolver.Lang.all; Resolver.Lang.renaming path [] ]; expand rest | Group (Squares, title) :: Group (Parens, dest) :: rest -> Mode.set Body; let dest = expand dest in let title = Option.some @@ expand title in Syn.Link {dest; title} :: expand rest | Group (Squares, [Group (Squares, dest)]) :: rest -> let dest = expand dest in Syn.Link {dest; title = None} :: expand rest | Group (d, xs) :: rest -> Mode.set Body; Syn.Group (d, expand xs) :: expand rest | Transclude addr :: rest -> Mode.set Body; Syn.Transclude addr :: expand rest | Query query :: rest -> Mode.set Body; let query = Query.map expand query in Syn.Query query :: expand rest | Embed_tex xs :: rest -> Mode.set Body; let fm = Fm.get () in Syn.Embed_tex {packages = fm.tex_packages; source = expand xs} :: expand rest | Block (xs, ys) :: rest -> Mode.set Body; Syn.Block (expand xs, expand ys) :: expand rest | Math (m, xs) :: rest -> Mode.set Body; Syn.Math (m, expand xs) :: expand rest | Ident str :: rest -> Mode.set Body; expand_ident str @ expand rest | Scope body :: rest -> let body = Scope.section [] @@ fun () -> expand body in body @ expand rest | Put (k, v) :: rest -> Mode.set Body; let k = expand_sym k in let v = expand v in [Syn.Put (k, v, expand rest)] | Default (k, v) :: rest -> Mode.set Body; let k = expand_sym k in let v = expand v in [Syn.Default (k, v, expand rest)] | Get k :: rest -> Mode.set Body; let k = expand_sym k in Syn.Get k :: expand rest | If_tex (x, y) :: rest -> Mode.set Body; let x = expand x in let y = expand y in Syn.If_tex (x, y) :: expand rest | Import (vis, dep) :: rest -> only_frontmatter code (); let import = UnitMap.find dep @@ U.read () in begin match vis with | Public -> Resolver.Scope.include_subtree ([], import) | Private -> Resolver.Scope.import_subtree ([], import) end; expand rest | Def (path, xs, body) :: rest -> only_frontmatter code (); let lam = Mode.protect @@ fun () -> expand_lambda (xs, body) in Resolver.Scope.include_singleton (path, (`Term lam, ())); expand rest | Alloc path :: rest -> only_frontmatter code (); let symbol = Symbol.fresh path in Resolver.Scope.include_singleton (path, (`Sym symbol, ())); expand rest | Title xs :: rest -> only_frontmatter code (); begin Mode.protect @@ fun () -> Fm.modify @@ fun fm -> {fm with title = Option.some @@ expand xs} end; Mode.set Frontmatter; expand rest | Author :: rest -> only_frontmatter code (); begin Fm.modify @@ fun fm -> {fm with authors = fm.authors @ [author]} end; Mode.set Frontmatter; expand rest | Tag tag :: rest -> only_frontmatter code (); begin Fm.modify @@ fun fm -> {fm with tags = fm.tags @ [tag]} end; Mode.set Frontmatter; expand rest | Taxon taxon :: rest -> only_frontmatter code (); begin Fm.modify @@ fun fm -> {fm with taxon = Some taxon} end; Mode.set Frontmatter; expand rest | Date str :: rest -> only_frontmatter code (); let date = Date.parse str in begin Fm.modify @@ fun fm -> {fm with date = Some date} end; Mode.set Frontmatter; expand rest | Meta (k, v) :: rest -> only_frontmatter code (); begin Mode.protect @@ fun () -> let v = expand v in Fm.modify @@ fun fm -> {fm with metas = fm.metas @ [k,v]} end; Mode.set Frontmatter; expand rest | TeX_package pkg :: rest -> only_frontmatter code (); begin Fm.modify @@ fun fm -> {fm with tex_packages = fm.tex_packages @ [pkg]} end; Mode.set Frontmatter; expand rest and expand_lambda : Trie.path list * Code.t -> Syn.t = fun (xs, body) -> Scope.section [] @@ fun () -> let syms = xs |> List.map @@ fun x -> let sym = Symbol.fresh x in let singlx = Trie.Untagged.singleton (x, `Term [Syn.Var sym]) in Scope.import_subtree ([], singlx); sym in [Syn.Lam (syms, expand body)] and expand_ident path = match Scope.resolve path, path with | None, [name] -> [Tag name] | Some (`Term x, ()), _ -> x | _ -> failwith "expand_ident" and expand_sym path = match Scope.resolve path, path with | None, [name] -> failwith "expand_sym" | Some (`Sym x, ()), _ -> x | _ -> failwith "expand_ident" module Builtins = struct let create path = let sym = Symbol.fresh path in sym, fun () -> Resolver.Scope.include_singleton (path, (`Sym sym, ())) module Transclude = struct let title_sym, alloc_title = create ["transclude"; "title"] let taxon_sym, alloc_taxon = create ["transclude"; "taxon"] let expanded_sym, alloc_expanded = create ["transclude"; "expanded"] let show_heading_sym, alloc_show_heading = create ["transclude"; "heading"] let toc_sym, alloc_toc = create ["transclude"; "toc"] let numbered_sym, alloc_numbered = create ["transclude"; "numbered"] let show_metadata_sym, alloc_show_metadata = create ["transclude"; "metadata"] end end let expand_doc (units : exports UnitMap.t) addr (doc : Code.doc) = let init = Syn.{addr; title = None; taxon = None; date = None; authors = []; tags = []; metas = []; tex_packages = []} in Resolver.Scope.run @@ fun () -> Builtins.Transclude.alloc_title (); Builtins.Transclude.alloc_taxon (); Builtins.Transclude.alloc_expanded (); Builtins.Transclude.alloc_show_heading (); Builtins.Transclude.alloc_toc (); Builtins.Transclude.alloc_numbered (); Builtins.Transclude.alloc_show_metadata (); U.run ~env:units @@ fun () -> Fm.run ~init @@ fun () -> Mode.run ~init:Frontmatter @@ fun () -> try let tree = expand doc in let fm = Fm.get () in let exports = Resolver.Scope.get_export () in UnitMap.add addr exports units, (fm, tree) with | FrontmatterInBody code as exn -> Format.eprintf "[%s] Encountered frontmatter-only code in body: %a@." addr Code.pp code; raise exn
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>