Started implementing property type fetching.

Implemented `do doc --serve`
This commit is contained in:
Samuel Guerra 2022-03-22 22:19:13 -03:00
parent ba80316a96
commit af6596dcd8
2 changed files with 93 additions and 1 deletions

View File

@ -54,7 +54,18 @@ fn install(mut args: Vec<&str>) {
}
// do doc [-o, --open] [<cargo-doc-args>]
// [-s, --serve]
//
// Generate documentation for zero-ui crates.
//
// USAGE:
// doc -o
// Generate docs, then open the `zero-ui` crate on the browser.
// doc -s -o
// Generate docs, then start `basic-http-server` on the docs and open
// the served URL on the browser.
//
// Note: `basic-http-server` can be installed with cargo, it is not by default.
fn doc(mut args: Vec<&str>) {
let custom_open = if args.contains(&"--manifest-path") {
if let Some(open) = args.iter_mut().find(|a| **a == "-o") {
@ -65,6 +76,8 @@ fn doc(mut args: Vec<&str>) {
take_flag(&mut args, &["-o", "--open"])
};
let serve = take_flag(&mut args, &["-s", "--serve"]);
cmd_env_req(
"cargo",
&["+nightly", "doc", "--all-features", "--no-deps", "--package", "zero-ui*"],
@ -75,10 +88,26 @@ fn doc(mut args: Vec<&str>) {
)],
);
let server = if serve {
Some(std::thread::spawn(|| {
let root = std::env::current_dir().unwrap().join("target/doc/");
if let Err(e) = std::process::Command::new("basic-http-server").arg(root).status() {
error(f!("couldn't serve docs: {e}\n\nYou can install the server with the command:\ncargo install basic-http-server"));
}
}))
} else {
None
};
if custom_open {
// Open the main crate.
// based on https://github.com/rust-lang/cargo/blob/master/src/cargo/ops/cargo_doc.rs
let path = std::env::current_dir().unwrap().join("target/doc/zero_ui/index.html");
let path = if serve {
// `basic-http-server` default.
"http://127.0.0.1:4000/zero_ui/index.html".to_owned()
} else {
std::env::current_dir().unwrap().join("target/doc/zero_ui/index.html").display().to_string()
};
match std::env::var_os("BROWSER") {
Some(browser) => {
if let Err(e) = std::process::Command::new(&browser).arg(path).status() {
@ -92,6 +121,10 @@ fn doc(mut args: Vec<&str>) {
}
};
}
if let Some(s) = server {
let _ = s.join();
}
}
// do test, t [-u, --unit <function-path>]

View File

@ -260,6 +260,8 @@ function onDocsIframeLoaded(docs) {
// scroll to anchor
window.location.href = window.location.href;
}
fetchPropTypes();
}
// customize sidebar of widget module page.
@ -314,4 +316,61 @@ function appendSidebarAnchor(ul, id) {
li.appendChild(a);
ul.appendChild(li);
}
}
// fetch linked property pages and edit the types with the types.
function fetchPropTypes() {
let current_page = window.location.href.split('#')[0];
if (current_page.startsWith("file:///")) {
return;
}
document.querySelectorAll('h4.wp-title').forEach(function (title) {
let url = title.querySelector('a:not(.anchor)').href;
if (url.startsWith(current_page)) {
return;
}
url = url.replace('/index.html', '/constant.__DOCS.html');
fetch(url)
.then(function (r) { return r.text(); })
.then(function (html) {
var parser = new DOMParser();
var doc = parser.parseFromString(html, "text/html");
if (url.includes('#')) {
resolvePropPage(title, url, doc);
} else {
copyPropType(title, doc);
}
})
});
}
function resolvePropPage(title, url, doc) {
let anchor = url.split('#')[1];
let inner_title = doc.getElementById(anchor);
if (inner_title == null) {
console.error("failed: " + anchor);
return;
}
let inner_url = inner_title.querySelector('a:not(.anchor)').href.replace('/index.html', '/constant.__DOCS.html');
fetch(inner_url)
.then(function (r) { return r.text() })
.then(function (html) {
var parser = new DOMParser();
var inner_doc = parser.parseFromString(html, "text/html");
if (inner_url.includes('#')) {
resolvePropPage(title, inner_url, inner_doc);
} else {
copyPropType(title, doc);
}
})
}
function copyPropType(title, doc) {
console.log(doc.querySelector('pre.rust.fn'));
}