diff --git a/DocGen4/Output.lean b/DocGen4/Output.lean
index 4f549c2..7351530 100644
--- a/DocGen4/Output.lean
+++ b/DocGen4/Output.lean
@@ -10,6 +10,7 @@ import DocGen4.Output.Index
import DocGen4.Output.Module
import DocGen4.Output.NotFound
import DocGen4.Output.Find
+import DocGen4.Output.Semantic
namespace DocGen4
@@ -65,28 +66,46 @@ def htmlOutput (result : AnalyzerResult) (root : String) : IO Unit := do
let config := { root := root, result := result, currentName := none, sourceLinker := ←sourceLinker}
let basePath := FilePath.mk "." / "build" / "doc"
let indexHtml := ReaderT.run index config
+ let findHtml := ReaderT.run find config
let notFoundHtml := ReaderT.run notFound config
FS.createDirAll basePath
FS.createDirAll (basePath / "find")
+ FS.createDirAll (basePath / "semantic")
let mut declList := #[]
for (_, mod) in result.moduleInfo.toArray do
for decl in filterMapDocInfo mod.members do
- let findHtml := ReaderT.run (findRedirectHtml decl.getName) config
- let findDir := basePath / "find" / decl.getName.toString
- FS.createDirAll findDir
- FS.writeFile (findDir / "index.html") findHtml.toString
- let obj := Json.mkObj [("name", decl.getName.toString), ("description", decl.getDocString.getD "")]
+ let name := decl.getName.toString
+ let doc := decl.getDocString.getD ""
+ let link := root ++ s!"semantic/{decl.getName.hash}.xml#"
+ let docLink := Id.run <| ReaderT.run (declNameToLink decl.getName) config
+ let sourceLink := Id.run <| ReaderT.run (getSourceUrl mod.name decl.getDeclarationRange) config
+ let obj := Json.mkObj [("name", name), ("doc", doc), ("link", link), ("docLink", docLink), ("sourceLink", sourceLink)]
declList := declList.push obj
+ let xml := toString <| Id.run <| ReaderT.run (semanticXml decl) config
+ FS.writeFile (basePath / "semantic" / s!"{decl.getName.hash}.xml") xml
let json := Json.arr declList
- FS.writeFile (basePath / "searchable_data.bmp") json.compress
+ FS.writeFile (basePath / "semantic" / "docgen4.xml") <| toString <| Id.run <| ReaderT.run schemaXml config
+
FS.writeFile (basePath / "index.html") indexHtml.toString
- FS.writeFile (basePath / "style.css") styleCss
FS.writeFile (basePath / "404.html") notFoundHtml.toString
+ FS.writeFile (basePath / "find" / "index.html") findHtml.toString
+
+ FS.writeFile (basePath / "style.css") styleCss
+
+ let declarationDataPath := basePath / "declaration-data.bmp"
+ FS.writeFile declarationDataPath json.compress
+ FS.writeFile (basePath / "declaration-data.timestamp") <| toString (←declarationDataPath.metadata).modified.sec
+
+ FS.writeFile (basePath / "site-root.js") (siteRootJs.replace "{siteRoot}" config.root)
+ FS.writeFile (basePath / "declaration-data.js") declarationDataCenterJs
FS.writeFile (basePath / "nav.js") navJs
+ FS.writeFile (basePath / "find" / "find.js") findJs
+ FS.writeFile (basePath / "how-about.js") howAboutJs
FS.writeFile (basePath / "search.js") searchJs
FS.writeFile (basePath / "mathjax-config.js") mathjaxConfigJs
+
for (module, content) in result.moduleInfo.toArray do
let moduleHtml := ReaderT.run (moduleToHtml content) config
let path := moduleNameToFile basePath module
diff --git a/DocGen4/Output/Base.lean b/DocGen4/Output/Base.lean
index 68812a8..2a28b17 100644
--- a/DocGen4/Output/Base.lean
+++ b/DocGen4/Output/Base.lean
@@ -47,8 +47,12 @@ def moduleNameToDirectory (basePath : FilePath) (n : Name) : FilePath :=
section Static
def styleCss : String := include_str "./static/style.css"
+ def siteRootJs : String := include_str "./static/site-root.js"
+ def declarationDataCenterJs : String := include_str "./static/declaration-data.js"
def navJs : String := include_str "./static/nav.js"
+ def howAboutJs : String := include_str "./static/how-about.js"
def searchJs : String := include_str "./static/search.js"
+ def findJs : String := include_str "./static/find/find.js"
def mathjaxConfigJs : String := include_str "./static/mathjax-config.js"
end Static
diff --git a/DocGen4/Output/Find.lean b/DocGen4/Output/Find.lean
index 41b82dd..95bbd8e 100644
--- a/DocGen4/Output/Find.lean
+++ b/DocGen4/Output/Find.lean
@@ -6,10 +6,16 @@ namespace Output
open scoped DocGen4.Jsx
open Lean
-def findRedirectHtml (decl : Name) : HtmlM Html := do
- let res ← getResult
- let url ← declNameToLink decl
- let contentString := s!"0;url={url}"
- pure $ Html.element "meta" false #[("http-equiv", "refresh"), ("content", contentString)] #[]
+def find : HtmlM Html := do
+ pure
+
+
+
+
+
+
+
+
end Output
end DocGen4
+
diff --git a/DocGen4/Output/Semantic.lean b/DocGen4/Output/Semantic.lean
new file mode 100644
index 0000000..7b22cdf
--- /dev/null
+++ b/DocGen4/Output/Semantic.lean
@@ -0,0 +1,58 @@
+import DocGen4.Output.Template
+import DocGen4.Output.DocString
+import Lean.Data.Xml
+
+open Lean Xml
+
+namespace DocGen4
+namespace Output
+
+instance : ToString $ Array Element where
+ toString xs := xs.map toString |>.foldl String.append ""
+
+instance : Coe Element Content where
+ coe e := Content.Element e
+
+-- TODO: syntax metaprogramming and basic semantic data
+
+def semanticXml (i : DocInfo) : HtmlM $ Array Element := do
+ pure #[
+ Element.Element
+ "rdf:RDF"
+ (Std.RBMap.fromList [
+ ("xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
+ ("xmlns:docgen4", s!"{←getRoot}semactic/docgen4.xml#")
+ ] _)
+ #[
+ Element.Element
+ "rdf:Description"
+ (Std.RBMap.fromList [
+ ("rdf:about", s!"{←getRoot}semactic/{i.getName.hash}.xml#")
+ ] _)
+ #[]
+ ]
+ ]
+
+def schemaXml : HtmlM $ Array Element := do
+ pure #[
+ Element.Element
+ "rdf:RDF"
+ (Std.RBMap.fromList [
+ ("xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
+ ("xmlns:docgen4", s!"{←getRoot}semactic/docgen4.xml#")
+ ] _)
+ #[
+ Element.Element
+ "docgen4:hasInstance"
+ Std.RBMap.empty
+ #[
+ Element.Element
+ "rdfs:type"
+ Std.RBMap.empty
+ #[Content.Character "rdf:Property"]
+ ]
+ ]
+ ]
+
+end Output
+end DocGen4
diff --git a/DocGen4/Output/Template.lean b/DocGen4/Output/Template.lean
index 8ed58ee..05390fe 100644
--- a/DocGen4/Output/Template.lean
+++ b/DocGen4/Output/Template.lean
@@ -15,46 +15,48 @@ def baseHtmlArray (title : String) (site : Array Html) : HtmlM Html := do
pure
+
+ {title}
+
+
+
+
- {title}
-
-
+
+
+
+
+
+
+
+
+
+
-
+
-
- Documentation
- {title}
- -- TODO: Replace this form with our own search
-
-
+
+ Documentation
+ {title}
+ -- TODO: Replace this form with our own search
+
+
- [site]
-
- {←navbar}
+ [site]
+
+ {←navbar}
- -- Lean in JS in HTML in Lean...very meta
-
-
- -- TODO Add more js stuff
-
-
- -- mathjax
-
-
-
+
diff --git a/static/declaration-data.js b/static/declaration-data.js
new file mode 100644
index 0000000..cb7a90c
--- /dev/null
+++ b/static/declaration-data.js
@@ -0,0 +1,243 @@
+/**
+ * This module is a wrapper that facilitates manipulating the declaration data.
+ *
+ * Please see {@link DeclarationDataCenter} for more information.
+ */
+
+import { SITE_ROOT } from "./site-root.js";
+
+const CACHE_DB_NAME = "declaration-data";
+const CACHE_DB_VERSION = 1;
+
+/**
+ * The DeclarationDataCenter is used for declaration searching.
+ *
+ * For usage, see the {@link init} and {@link search} methods.
+ */
+export class DeclarationDataCenter {
+ /**
+ * The declaration data. Users should not interact directly with this field.
+ *
+ * *NOTE:* This is not made private to support legacy browsers.
+ */
+ declarationData = null;
+
+ /**
+ * Used to implement the singleton, in case we need to fetch data mutiple times in the same page.
+ */
+ static singleton = null;
+
+ /**
+ * Construct a DeclarationDataCenter with given data.
+ *
+ * Please use {@link DeclarationDataCenter.init} instead, which automates the data fetching process.
+ * @param {*} declarationData
+ */
+ constructor(declarationData) {
+ this.declarationData = declarationData;
+ }
+
+ /**
+ * The actual constructor of DeclarationDataCenter
+ * @returns {Promise}
+ */
+ static async init() {
+ if (!DeclarationDataCenter.singleton) {
+ const timestampUrl = new URL(
+ `${SITE_ROOT}declaration-data.timestamp`,
+ window.location
+ );
+ const dataUrl = new URL(
+ `${SITE_ROOT}declaration-data.bmp`,
+ window.location
+ );
+
+ const timestampRes = await fetch(timestampUrl);
+ const timestamp = await timestampRes.text();
+
+ // try to use cache first
+ const data = await fetchCachedDeclarationData(timestamp);
+ if (data) {
+ // if data is defined, use the cached one.
+ DeclarationDataCenter.singleton = new DeclarationDataCenter(data);
+ } else {
+ // undefined. then fetch the data from the server.
+ const dataRes = await fetch(dataUrl);
+ const dataJson = await dataRes.json();
+ // the data is a map of name (original case) to declaration data.
+ const data = new Map(
+ dataJson.map(({ name, doc, link, docLink, sourceLink }) => [
+ name,
+ {
+ name,
+ lowerName: name.toLowerCase(),
+ lowerDoc: doc.toLowerCase(),
+ link,
+ docLink,
+ sourceLink,
+ },
+ ])
+ );
+ await cacheDeclarationData(timestamp, data);
+ DeclarationDataCenter.singleton = new DeclarationDataCenter(data);
+ }
+ }
+ return DeclarationDataCenter.singleton;
+ }
+
+ /**
+ * Search for a declaration.
+ * @returns {Array}
+ */
+ search(pattern, strict = true) {
+ if (!pattern) {
+ return [];
+ }
+ if (strict) {
+ let decl = this.declarationData.get(pattern);
+ return decl ? [decl] : [];
+ } else {
+ return getMatches(this.declarationData, pattern);
+ }
+ }
+}
+
+function isSeparater(char) {
+ return char === "." || char === "_";
+}
+
+// HACK: the fuzzy matching is quite hacky
+
+function matchCaseSensitive(declName, lowerDeclName, pattern) {
+ let i = 0,
+ j = 0,
+ err = 0,
+ lastMatch = 0;
+ while (i < declName.length && j < pattern.length) {
+ if (pattern[j] === declName[i] || pattern[j] === lowerDeclName[i]) {
+ err += (isSeparater(pattern[j]) ? 0.125 : 1) * (i - lastMatch);
+ if (pattern[j] !== declName[i]) err += 0.5;
+ lastMatch = i + 1;
+ j++;
+ } else if (isSeparater(declName[i])) {
+ err += 0.125 * (i + 1 - lastMatch);
+ lastMatch = i + 1;
+ }
+ i++;
+ }
+ err += 0.125 * (declName.length - lastMatch);
+ if (j === pattern.length) {
+ return err;
+ }
+}
+
+function getMatches(declarations, pattern, maxResults = 30) {
+ const lowerPats = pattern.toLowerCase().split(/\s/g);
+ const patNoSpaces = pattern.replace(/\s/g, "");
+ const results = [];
+ for (const {
+ name,
+ lowerName,
+ lowerDoc,
+ link,
+ docLink,
+ sourceLink,
+ } of declarations.values()) {
+ let err = matchCaseSensitive(name, lowerName, patNoSpaces);
+ // match all words as substrings of docstring
+ if (
+ err >= 3 &&
+ pattern.length > 3 &&
+ lowerPats.every((l) => lowerDoc.indexOf(l) != -1)
+ ) {
+ err = 3;
+ }
+ if (err !== undefined) {
+ results.push({
+ name,
+ err,
+ lowerName,
+ lowerDoc,
+ link,
+ docLink,
+ sourceLink,
+ });
+ }
+ }
+ return results.sort(({ err: a }, { err: b }) => a - b).slice(0, maxResults);
+}
+
+// TODO: refactor the indexedDB part to be more robust
+
+/**
+ * Get the indexedDB database, automatically initialized.
+ * @returns {Promise}
+ */
+async function getDeclarationDatabase() {
+ return new Promise((resolve, reject) => {
+ const request = indexedDB.open(CACHE_DB_NAME, CACHE_DB_VERSION);
+
+ request.onerror = function (event) {
+ reject(
+ new Error(
+ `fail to open indexedDB ${CACHE_DB_NAME} of version ${CACHE_DB_VERSION}`
+ )
+ );
+ };
+ request.onupgradeneeded = function (event) {
+ let db = event.target.result;
+ // We only need to store one object, so no key path or increment is needed.
+ db.createObjectStore("declaration");
+ };
+ request.onsuccess = function (event) {
+ resolve(event.target.result);
+ };
+ });
+}
+
+/**
+ * Store data in indexedDB object store.
+ * @param {string} timestamp
+ * @param {Map} data
+ */
+async function cacheDeclarationData(timestamp, data) {
+ let db = await getDeclarationDatabase();
+ let store = db
+ .transaction("declaration", "readwrite")
+ .objectStore("declaration");
+ return new Promise((resolve, reject) => {
+ let clearRequest = store.clear();
+ let addRequest = store.add(data, timestamp);
+
+ addRequest.onsuccess = function (event) {
+ resolve();
+ };
+ addRequest.onerror = function (event) {
+ reject(new Error(`fail to store declaration data`));
+ };
+ clearRequest.onerror = function (event) {
+ reject(new Error("fail to clear object store"));
+ };
+ });
+}
+
+/**
+ * Retrieve data from indexedDB database.
+ * @param {string} timestamp
+ * @returns {Promise