2023-05-11 13:27:25 +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 Lean
|
|
|
|
|
import Lean.Data.HashMap
|
|
|
|
|
|
2023-05-11 13:55:25 +00:00
|
|
|
|
import DocGen4.Process.NameExt
|
|
|
|
|
|
2023-05-11 13:27:25 +00:00
|
|
|
|
def Lean.HashSet.fromArray [BEq α] [Hashable α] (xs : Array α) : Lean.HashSet α :=
|
|
|
|
|
xs.foldr (flip .insert) .empty
|
|
|
|
|
|
|
|
|
|
namespace DocGen4
|
|
|
|
|
|
|
|
|
|
open Lean Name
|
|
|
|
|
|
|
|
|
|
def getNLevels (name : Name) (levels: Nat) : Name :=
|
|
|
|
|
let components := name.componentsRev
|
|
|
|
|
(components.drop (components.length - levels)).reverse.foldl (· ++ ·) Name.anonymous
|
|
|
|
|
|
|
|
|
|
inductive Hierarchy where
|
2023-05-11 13:55:25 +00:00
|
|
|
|
| node (name : NameExt) (isFile : Bool) (children : RBNode NameExt (fun _ => Hierarchy)) : Hierarchy
|
2023-05-11 13:27:25 +00:00
|
|
|
|
|
2023-05-11 13:55:25 +00:00
|
|
|
|
instance : Inhabited Hierarchy := ⟨Hierarchy.node ⟨.anonymous, .html⟩ false RBNode.leaf⟩
|
2023-05-11 13:27:25 +00:00
|
|
|
|
|
2023-05-11 13:55:25 +00:00
|
|
|
|
abbrev HierarchyMap := RBNode NameExt (fun _ => Hierarchy)
|
2023-05-11 13:27:25 +00:00
|
|
|
|
|
|
|
|
|
-- Everything in this namespace is adapted from stdlib's RBNode
|
|
|
|
|
namespace HierarchyMap
|
|
|
|
|
|
2023-05-11 13:55:25 +00:00
|
|
|
|
def toList : HierarchyMap → List (NameExt × Hierarchy)
|
2023-05-11 13:27:25 +00:00
|
|
|
|
| t => t.revFold (fun ps k v => (k, v)::ps) []
|
|
|
|
|
|
2023-05-11 13:55:25 +00:00
|
|
|
|
def toArray : HierarchyMap → Array (NameExt × Hierarchy)
|
2023-05-11 13:27:25 +00:00
|
|
|
|
| t => t.fold (fun ps k v => ps ++ #[(k, v)] ) #[]
|
|
|
|
|
|
2023-05-11 13:55:25 +00:00
|
|
|
|
def hForIn [Monad m] (t : HierarchyMap) (init : σ) (f : (NameExt × Hierarchy) → σ → m (ForInStep σ)) : m σ :=
|
2023-05-11 13:27:25 +00:00
|
|
|
|
t.forIn init (fun a b acc => f (a, b) acc)
|
|
|
|
|
|
2023-05-11 13:55:25 +00:00
|
|
|
|
instance : ForIn m HierarchyMap (NameExt × Hierarchy) where
|
2023-05-11 13:27:25 +00:00
|
|
|
|
forIn := HierarchyMap.hForIn
|
|
|
|
|
|
|
|
|
|
end HierarchyMap
|
|
|
|
|
|
|
|
|
|
namespace Hierarchy
|
|
|
|
|
|
2023-05-11 13:55:25 +00:00
|
|
|
|
def empty (n : NameExt) (isFile : Bool) : Hierarchy :=
|
2023-05-11 13:27:25 +00:00
|
|
|
|
node n isFile RBNode.leaf
|
|
|
|
|
|
|
|
|
|
def getName : Hierarchy → Name
|
2023-05-11 13:55:25 +00:00
|
|
|
|
| node n _ _ => n.name
|
|
|
|
|
|
|
|
|
|
def getNameExt : Hierarchy → NameExt
|
2023-05-11 13:27:25 +00:00
|
|
|
|
| node n _ _ => n
|
|
|
|
|
|
|
|
|
|
def getChildren : Hierarchy → HierarchyMap
|
|
|
|
|
| node _ _ c => c
|
|
|
|
|
|
|
|
|
|
def isFile : Hierarchy → Bool
|
|
|
|
|
| node _ f _ => f
|
|
|
|
|
|
2023-05-11 13:55:25 +00:00
|
|
|
|
partial def insert! (h : Hierarchy) (n : NameExt) : Hierarchy := Id.run do
|
|
|
|
|
let hn := h.getNameExt
|
2023-05-11 13:27:25 +00:00
|
|
|
|
let mut cs := h.getChildren
|
|
|
|
|
|
2023-05-11 13:55:25 +00:00
|
|
|
|
if getNumParts hn.name + 1 == getNumParts n.name then
|
|
|
|
|
match cs.find NameExt.cmp n with
|
2023-05-11 13:27:25 +00:00
|
|
|
|
| none =>
|
2023-05-11 13:55:25 +00:00
|
|
|
|
node hn h.isFile (cs.insert NameExt.cmp n <| empty n true)
|
2023-05-11 13:27:25 +00:00
|
|
|
|
| some (node _ true _) => h
|
|
|
|
|
| some (node _ false ccs) =>
|
2023-05-11 13:55:25 +00:00
|
|
|
|
cs := cs.erase NameExt.cmp n
|
|
|
|
|
node hn h.isFile (cs.insert NameExt.cmp n <| node n true ccs)
|
2023-05-11 13:27:25 +00:00
|
|
|
|
else
|
2023-05-11 13:55:25 +00:00
|
|
|
|
let leveled := ⟨getNLevels n.name (getNumParts hn.name + 1), .html⟩
|
|
|
|
|
match cs.find NameExt.cmp leveled with
|
2023-05-11 13:27:25 +00:00
|
|
|
|
| some nextLevel =>
|
2023-05-11 13:55:25 +00:00
|
|
|
|
cs := cs.erase NameExt.cmp leveled
|
2023-05-11 13:27:25 +00:00
|
|
|
|
-- BUG?
|
2023-05-11 13:55:25 +00:00
|
|
|
|
node hn h.isFile <| cs.insert NameExt.cmp leveled (nextLevel.insert! n)
|
2023-05-11 13:27:25 +00:00
|
|
|
|
| none =>
|
2023-05-11 13:55:25 +00:00
|
|
|
|
let child := (insert! (empty leveled false) n)
|
|
|
|
|
node hn h.isFile <| cs.insert NameExt.cmp leveled child
|
2023-05-11 13:27:25 +00:00
|
|
|
|
|
|
|
|
|
partial def fromArray (names : Array Name) : Hierarchy :=
|
2023-05-11 13:55:25 +00:00
|
|
|
|
(names.map (fun n => NameExt.mk n .html)).foldl insert! (empty ⟨anonymous, .html⟩ false)
|
|
|
|
|
|
|
|
|
|
partial def fromArrayExt (names : Array NameExt) : Hierarchy :=
|
|
|
|
|
names.foldl insert! (empty ⟨anonymous, .html⟩ false)
|
2023-05-11 13:27:25 +00:00
|
|
|
|
|
|
|
|
|
def baseDirBlackList : HashSet String :=
|
|
|
|
|
HashSet.fromArray #[
|
|
|
|
|
"404.html",
|
2023-06-20 17:37:53 +00:00
|
|
|
|
"color-scheme.js",
|
2023-05-11 13:27:25 +00:00
|
|
|
|
"declaration-data.js",
|
|
|
|
|
"declarations",
|
|
|
|
|
"find",
|
|
|
|
|
"how-about.js",
|
|
|
|
|
"index.html",
|
|
|
|
|
"search.html",
|
|
|
|
|
"foundational_types.html",
|
|
|
|
|
"mathjax-config.js",
|
|
|
|
|
"navbar.html",
|
|
|
|
|
"nav.js",
|
|
|
|
|
"search.js",
|
|
|
|
|
"src",
|
|
|
|
|
"style.css"
|
|
|
|
|
]
|
|
|
|
|
|
2023-05-11 13:55:25 +00:00
|
|
|
|
partial def fromDirectoryAux (dir : System.FilePath) (previous : Name) : IO (Array NameExt) := do
|
2023-05-11 13:27:25 +00:00
|
|
|
|
let mut children := #[]
|
|
|
|
|
for entry in ← System.FilePath.readDir dir do
|
|
|
|
|
if ← entry.path.isDir then
|
|
|
|
|
children := children ++ (← fromDirectoryAux entry.path (.str previous entry.fileName))
|
|
|
|
|
else if entry.path.extension = some "html" then
|
2023-05-11 13:55:25 +00:00
|
|
|
|
children := children.push <| ⟨.str previous (entry.fileName.dropRight ".html".length), .html⟩
|
|
|
|
|
else if entry.path.extension = some "pdf" then
|
|
|
|
|
children := children.push <| ⟨.str previous (entry.fileName.dropRight ".pdf".length), .pdf⟩
|
2023-05-11 13:27:25 +00:00
|
|
|
|
return children
|
|
|
|
|
|
|
|
|
|
def fromDirectory (dir : System.FilePath) : IO Hierarchy := do
|
|
|
|
|
let mut children := #[]
|
|
|
|
|
for entry in ← System.FilePath.readDir dir do
|
|
|
|
|
if baseDirBlackList.contains entry.fileName then
|
|
|
|
|
continue
|
|
|
|
|
else if ← entry.path.isDir then
|
|
|
|
|
children := children ++ (← fromDirectoryAux entry.path (.mkSimple entry.fileName))
|
|
|
|
|
else if entry.path.extension = some "html" then
|
2023-05-11 13:55:25 +00:00
|
|
|
|
children := children.push <| ⟨.mkSimple (entry.fileName.dropRight ".html".length), .html⟩
|
|
|
|
|
else if entry.path.extension = some "pdf" then
|
|
|
|
|
children := children.push <| ⟨.mkSimple (entry.fileName.dropRight ".pdf".length), .pdf⟩
|
|
|
|
|
return Hierarchy.fromArrayExt children
|
2023-05-11 13:27:25 +00:00
|
|
|
|
|
|
|
|
|
end Hierarchy
|
|
|
|
|
end DocGen4
|