feat: basic mod doc without attributes

main
Xubai Wang 2022-02-16 01:06:46 +08:00
parent 8da2e2a63d
commit 385a38a003
3 changed files with 51 additions and 16 deletions

View File

@ -71,7 +71,7 @@ def htmlOutput (result : AnalyzerResult) (root : String) : IO Unit := do
let mut declList := #[] let mut declList := #[]
for (_, mod) in result.moduleInfo.toArray do for (_, mod) in result.moduleInfo.toArray do
for decl in mod.members do for decl in mod.members.filter ModuleMember.isDocInfo do
let findHtml := ReaderT.run (findRedirectHtml decl.getName) config let findHtml := ReaderT.run (findRedirectHtml decl.getName) config
let findDir := basePath / "find" / decl.getName.toString let findDir := basePath / "find" / decl.getName.toString
FS.createDirAll findDir FS.createDirAll findDir

View File

@ -3,6 +3,7 @@ Copyright (c) 2021 Henrik Böving. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE. Released under Apache 2.0 license as described in the file LICENSE.
Authors: Henrik Böving Authors: Henrik Böving
-/ -/
import CMark
import DocGen4.Output.Template import DocGen4.Output.Template
import DocGen4.Output.Inductive import DocGen4.Output.Inductive
import DocGen4.Output.Structure import DocGen4.Output.Structure
@ -101,6 +102,17 @@ def docInfoToHtml (module : Name) (doc : DocInfo) : HtmlM Html := do
</div> </div>
</div> </div>
def modDocToHtml (module : Name) (mdoc : ModuleDoc) : HtmlM Html := do
pure
<div «class»="mod_doc">
{Html.text (CMark.renderHtml mdoc.doc)}
</div>
def moduleMemberToHtml (module : Name) (member : ModuleMember) : HtmlM Html :=
match member with
| ModuleMember.docInfo d => docInfoToHtml module d
| ModuleMember.modDoc d => modDocToHtml module d
def declarationToNavLink (module : Name) : Html := def declarationToNavLink (module : Name) : Html :=
<div «class»="nav_link"> <div «class»="nav_link">
<a «class»="break_within" href={s!"#{module.toString}"}>{module.toString}</a> <a «class»="break_within" href={s!"#{module.toString}"}>{module.toString}</a>
@ -162,9 +174,9 @@ def internalNav (members : Array Name) (moduleName : Name) : HtmlM Html := do
</nav> </nav>
def moduleToHtml (module : Module) : HtmlM Html := withReader (setCurrentName module.name) do def moduleToHtml (module : Module) : HtmlM Html := withReader (setCurrentName module.name) do
let docInfos ← module.members.mapM (λ i => docInfoToHtml module.name i) let docInfos ← module.members.mapM (λ i => moduleMemberToHtml module.name i)
templateExtends (baseHtmlArray module.name.toString) $ pure #[ templateExtends (baseHtmlArray module.name.toString) $ pure #[
←internalNav (module.members.map DocInfo.getName) module.name, ←internalNav (module.members.filter ModuleMember.isDocInfo |>.map ModuleMember.getName) module.name,
Html.element "main" false #[] docInfos Html.element "main" false #[] docInfos
] ]

View File

@ -101,16 +101,17 @@ def getDeclarationRange : DocInfo → DeclarationRange
| classInfo i => i.declarationRange | classInfo i => i.declarationRange
| classInductiveInfo i => i.declarationRange | classInductiveInfo i => i.declarationRange
def lineOrder (l r : DocInfo) : Bool :=
l.getDeclarationRange.pos.line < r.getDeclarationRange.pos.line
end DocInfo end DocInfo
inductive ModuleMember where
| docInfo (info : DocInfo) : ModuleMember
| modDoc (doc : ModuleDoc) : ModuleMember
deriving Inhabited
structure Module where structure Module where
name : Name name : Name
doc : Option String
-- Sorted according to their line numbers -- Sorted according to their line numbers
members : Array DocInfo members : Array ModuleMember
deriving Inhabited deriving Inhabited
partial def typeToArgsType (e : Expr) : (Array (Name × Expr × BinderInfo) × Expr) := partial def typeToArgsType (e : Expr) : (Array (Name × Expr × BinderInfo) × Expr) :=
@ -459,6 +460,29 @@ def getDocString : DocInfo → Option String
end DocInfo end DocInfo
namespace ModuleMember
def getDeclarationRange : ModuleMember → DeclarationRange
| docInfo i => i.getDeclarationRange
| modDoc i => i.declarationRange
def order (l r : ModuleMember) : Bool :=
Position.lt l.getDeclarationRange.pos r.getDeclarationRange.pos
def isDocInfo : ModuleMember → Bool
| docInfo _ => true
| _ => false
def getName : ModuleMember → Name
| docInfo i => i.getName
| modDoc i => Name.anonymous
def getDocString : ModuleMember → Option String
| docInfo i => i.getDocString
| modDoc i => i.doc
end ModuleMember
structure AnalyzerResult where structure AnalyzerResult where
name2ModIdx : HashMap Name ModuleIdx name2ModIdx : HashMap Name ModuleIdx
moduleNames : Array Name moduleNames : Array Name
@ -472,13 +496,12 @@ def process : MetaM AnalyzerResult := do
let env ← getEnv let env ← getEnv
let mut res := mkHashMap env.header.moduleNames.size let mut res := mkHashMap env.header.moduleNames.size
for module in env.header.moduleNames do for module in env.header.moduleNames do
-- TODO: Check why modules can have multiple doc strings and add that later on let modDocs := match getModuleDoc? env module with
let moduleDoc := match getModuleDoc? env module with | none => #[]
| none => none | some ds => ds
| some #[] => none |>.map (λ doc => ModuleMember.modDoc doc)
| some doc => doc.get! 0
res := res.insert module (Module.mk module moduleDoc #[]) res := res.insert module (Module.mk module modDocs)
for cinfo in env.constants.toList do for cinfo in env.constants.toList do
try try
@ -487,7 +510,7 @@ def process : MetaM AnalyzerResult := do
let some modidx := env.getModuleIdxFor? dinfo.getName | unreachable! let some modidx := env.getModuleIdxFor? dinfo.getName | unreachable!
let moduleName := env.allImportedModuleNames.get! modidx let moduleName := env.allImportedModuleNames.get! modidx
let module := res.find! moduleName let module := res.find! moduleName
res := res.insert moduleName {module with members := module.members.push dinfo} res := res.insert moduleName {module with members := module.members.push (ModuleMember.docInfo dinfo)}
catch e => catch e =>
IO.println s!"WARNING: Failed to obtain information for: {cinfo.fst}: {←e.toMessageData.toString}" IO.println s!"WARNING: Failed to obtain information for: {cinfo.fst}: {←e.toMessageData.toString}"
@ -495,7 +518,7 @@ def process : MetaM AnalyzerResult := do
let mut adj := Array.mkArray res.size (Array.mkArray res.size false) let mut adj := Array.mkArray res.size (Array.mkArray res.size false)
-- TODO: This could probably be faster if we did an insertion sort above instead -- TODO: This could probably be faster if we did an insertion sort above instead
for (moduleName, module) in res.toArray do for (moduleName, module) in res.toArray do
res := res.insert moduleName {module with members := module.members.qsort DocInfo.lineOrder} res := res.insert moduleName {module with members := module.members.qsort ModuleMember.order}
let some modIdx := env.getModuleIdx? moduleName | unreachable! let some modIdx := env.getModuleIdx? moduleName | unreachable!
let moduleData := env.header.moduleData.get! modIdx let moduleData := env.header.moduleData.get! modIdx
for imp in moduleData.imports do for imp in moduleData.imports do