Moved docs/src/main.rs to docs/src/lib.rs and imported it into the repl. Commented out doc code to avoid it deleting my entire roc project (which happened)

This commit is contained in:
Chadtech 2021-03-21 22:37:39 -04:00
parent 22f77ed966
commit 807250f3e3
7 changed files with 324 additions and 321 deletions

39
Cargo.lock generated
View file

@ -889,25 +889,6 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]]
name = "docs"
version = "0.1.0"
dependencies = [
"bumpalo",
"fs_extra",
"handlebars",
"maplit",
"pretty_assertions 0.5.1",
"pulldown-cmark",
"roc_builtins",
"roc_can",
"roc_collections",
"roc_load",
"serde",
"serde_derive",
"serde_json",
]
[[package]] [[package]]
name = "downcast-rs" name = "downcast-rs"
version = "1.2.0" version = "1.2.0"
@ -2961,6 +2942,7 @@ dependencies = [
"roc_can", "roc_can",
"roc_collections", "roc_collections",
"roc_constrain", "roc_constrain",
"roc_docs",
"roc_editor", "roc_editor",
"roc_fmt", "roc_fmt",
"roc_gen", "roc_gen",
@ -3012,6 +2994,25 @@ dependencies = [
"roc_types", "roc_types",
] ]
[[package]]
name = "roc_docs"
version = "0.1.0"
dependencies = [
"bumpalo",
"fs_extra",
"handlebars",
"maplit",
"pretty_assertions 0.5.1",
"pulldown-cmark",
"roc_builtins",
"roc_can",
"roc_collections",
"roc_load",
"serde",
"serde_derive",
"serde_json",
]
[[package]] [[package]]
name = "roc_editor" name = "roc_editor"
version = "0.1.0" version = "0.1.0"

View file

@ -33,6 +33,7 @@ target-all = [
[dependencies] [dependencies]
roc_collections = { path = "../compiler/collections" } roc_collections = { path = "../compiler/collections" }
roc_can = { path = "../compiler/can" } roc_can = { path = "../compiler/can" }
roc_docs = { path = "../docs" }
roc_parse = { path = "../compiler/parse" } roc_parse = { path = "../compiler/parse" }
roc_region = { path = "../compiler/region" } roc_region = { path = "../compiler/region" }
roc_module = { path = "../compiler/module" } roc_module = { path = "../compiler/module" }

View file

@ -8,11 +8,12 @@ use roc_build::link::LinkType;
use roc_gen::llvm::build::OptLevel; use roc_gen::llvm::build::OptLevel;
use roc_load::file::LoadingProblem; use roc_load::file::LoadingProblem;
use std::io; use std::io;
use std::path::Path; use std::path::{Path, PathBuf};
use std::process; use std::process;
use std::process::Command; use std::process::Command;
use target_lexicon::Triple; use target_lexicon::Triple;
pub mod build; pub mod build;
pub mod repl; pub mod repl;
@ -76,6 +77,36 @@ pub fn build_app<'a>() -> App<'a> {
.help("(optional) The directory or files to open on launch.") .help("(optional) The directory or files to open on launch.")
) )
) )
.subcommand(
App::new("docs")
.about("Generate documentation for Roc modules")
.arg(Arg::with_name(DIRECTORY_OR_FILES)
.index(1)
.multiple(true)
.required(true)
.help("The directory or files to build documentation for")
)
)
}
pub fn docs(files: Vec<PathBuf>) {
roc_docs::generate(
files,
// vec![
// PathBuf::from(r"./compiler/builtins/docs/Bool.roc"),
// // PathBuf::from(r"../compiler/builtins/docs/Dict.roc"),
// // Not working
// // PathBuf::from(r"../compiler/builtins/docs/List.roc"),
// // Not working
// // PathBuf::from(r"../compiler/builtins/docs/Num.roc"),
// // PathBuf::from(r"../compiler/builtins/docs/Set.roc"),
// // PathBuf::from(r"../compiler/builtins/docs/Str.roc"),
// ],
roc_builtins::std::standard_stdlib(),
Path::new("./"),
// Path::new("./build"),
)
} }
pub fn build(target: &Triple, matches: &ArgMatches, run_after_build: bool) -> io::Result<()> { pub fn build(target: &Triple, matches: &ArgMatches, run_after_build: bool) -> io::Result<()> {

View file

@ -1,6 +1,6 @@
use roc_cli::{build, build_app, repl, DIRECTORY_OR_FILES}; use roc_cli::{build, docs, build_app, repl, DIRECTORY_OR_FILES};
use std::io; use std::io;
use std::path::Path; use std::path::{Path, PathBuf};
use target_lexicon::Triple; use target_lexicon::Triple;
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
@ -35,6 +35,21 @@ fn main() -> io::Result<()> {
} }
} }
} }
Some("docs") => {
let values = matches
.subcommand_matches("docs")
.unwrap()
.values_of_os(DIRECTORY_OR_FILES)
.unwrap();
let paths = values
.map(|os_str| Path::new(os_str).to_path_buf())
.collect::<Vec<PathBuf>>();
docs(paths);
Ok(())
}
_ => unreachable!(), _ => unreachable!(),
} }
} }

View file

@ -1,5 +1,5 @@
[package] [package]
name = "docs" name = "roc_docs"
version = "0.1.0" version = "0.1.0"
authors = ["Pablo Hirafuji <pablohirafuji@gmail.com>"] authors = ["Pablo Hirafuji <pablohirafuji@gmail.com>"]
edition = "2018" edition = "2018"

253
docs/src/lib.rs Normal file
View file

@ -0,0 +1,253 @@
extern crate fs_extra;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate pulldown_cmark;
extern crate serde_json;
use roc_builtins::std::StdLib;
use roc_can::builtins::builtin_defs_map;
use roc_load::docs::Documentation;
use roc_load::docs::ModuleDocumentation;
use roc_load::file::LoadingProblem;
use std::fs;
extern crate roc_load;
use bumpalo::Bump;
use roc_collections::all::MutMap;
use std::path::{Path, PathBuf};
#[derive(Serialize)]
pub struct Template {
pub package_name: String,
pub package_version: String,
pub module_name: String,
pub module_docs: String,
pub module_entries: Vec<ModuleEntry>,
pub module_links: Vec<TemplateLink>,
}
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct ModuleEntry {
pub name: String,
pub docs: String,
}
#[derive(Serialize)]
pub struct TemplateLink {
pub name: String,
pub href: String,
pub classes: String,
pub entries: Vec<TemplateLinkEntry>,
}
#[derive(Serialize)]
pub struct TemplateLinkEntry {
name: String,
}
// fn main() {
// generate(
// vec![
// PathBuf::from(r"../compiler/builtins/docs/Bool.roc"),
// // PathBuf::from(r"../compiler/builtins/docs/Dict.roc"),
// // Not working
// // PathBuf::from(r"../compiler/builtins/docs/List.roc"),
// // Not working
// // PathBuf::from(r"../compiler/builtins/docs/Num.roc"),
// // PathBuf::from(r"../compiler/builtins/docs/Set.roc"),
// // PathBuf::from(r"../compiler/builtins/docs/Str.roc"),
// ],
// roc_builtins::std::standard_stdlib(),
// Path::new("../compiler/builtins/docs"),
// // Path::new("./build"),
// )
// }
pub fn generate(filenames: Vec<PathBuf>, std_lib: StdLib, build_dir: &Path) {
// let files_docs = files_to_documentations(filenames, std_lib);
//
// // TODO: get info from a file like "elm.json"
// let package = roc_load::docs::Documentation {
// name: "roc/builtins".to_string(),
// version: "1.0.0".to_string(),
// docs: "Package introduction or README.".to_string(),
// modules: files_docs,
// };
//
// let version_folder = build_dir
// .join(package.name.clone())
// .join(package.version.clone());
//
// println!("{}", version_folder.display());
// // Make sure the output directories exists
// fs::create_dir_all(&version_folder)
// .expect("TODO gracefully handle creating directories failing");
//
// // Register handlebars template
// let mut handlebars = handlebars::Handlebars::new();
// handlebars
// .register_template_file("page", "./docs/src/templates/page.hbs")
// .expect("TODO gracefully handle registering template failing");
//
// // Write each package's module docs html file
// for module in &package.modules {
// let template = documentation_to_template_data(&package, module);
//
// let handlebars_data = handlebars::to_json(&template);
// let filepath = version_folder.join(format!("{}.html", module.name));
// let mut output_file =
// fs::File::create(filepath).expect("TODO gracefully handle creating file failing");
// handlebars
// .render_to_write("page", &handlebars_data, &mut output_file)
// .expect("TODO gracefully handle writing file failing");
// }
//
// // Copy /static folder content to /build
// let copy_options = fs_extra::dir::CopyOptions {
// overwrite: true,
// skip_exist: false,
// buffer_size: 64000, //64kb
// copy_inside: false,
// content_only: true,
// depth: 0,
// };
// fs_extra::dir::copy("./docs/src/static/", &build_dir, &copy_options)
// .expect("TODO gracefully handle copying static content failing");
// println!("Docs generated at {}", build_dir.display());
}
pub fn files_to_documentations(
filenames: Vec<PathBuf>,
std_lib: StdLib,
) -> Vec<ModuleDocumentation> {
let arena = Bump::new();
let mut files_docs = vec![];
for filename in filenames {
let mut src_dir = filename.clone();
src_dir.pop();
match roc_load::file::load_and_typecheck(
&arena,
filename,
&std_lib,
src_dir.as_path(),
MutMap::default(),
8, // TODO: Is it okay to hardcode ptr_bytes here? I think it should be fine since we'er only type checking (also, 8 => 32bit system)
builtin_defs_map,
) {
Ok(mut loaded) => files_docs.extend(loaded.documentation.drain().map(|x| x.1)),
Err(LoadingProblem::ParsingFailedReport(report)) => {
println!("{}", report);
panic!();
}
Err(e) => panic!("{:?}", e),
}
}
files_docs
}
pub fn documentation_to_template_data(
doc: &Documentation,
module: &ModuleDocumentation,
) -> Template {
Template {
package_name: doc.name.clone(),
package_version: doc.version.clone(),
module_name: module.name.clone(),
module_docs: markdown_to_html(module.docs.clone()),
module_entries: module
.entries
.clone()
.into_iter()
.map(|entry| ModuleEntry {
name: entry.name.clone(),
docs: match entry.docs {
Some(docs) => markdown_to_html(docs),
None => String::new(),
},
})
.collect(),
module_links: doc
.modules
.clone()
.into_iter()
.map(|module_link| TemplateLink {
name: module_link.name.clone(),
href: format!("./{}.html", module_link.name),
classes: "".to_string(),
entries: module_link
.entries
.into_iter()
.map(|entry| TemplateLinkEntry { name: entry.name })
.collect(),
})
.collect(),
}
}
fn markdown_to_html(markdown: String) -> String {
use pulldown_cmark::CodeBlockKind::*;
use pulldown_cmark::CowStr::*;
use pulldown_cmark::Event::*;
use pulldown_cmark::Tag::*;
let markdown_options = pulldown_cmark::Options::all();
let mut docs_parser = vec![];
let (_, _) = pulldown_cmark::Parser::new_ext(&markdown, markdown_options).fold(
(0, 0),
|(start_quote_count, end_quote_count), event| match event {
// Replace this sequence (`>>>` syntax):
// Start(BlockQuote)
// Start(BlockQuote)
// Start(BlockQuote)
// Start(Paragraph)
// For `Start(CodeBlock(Fenced(Borrowed("roc"))))`
Start(BlockQuote) => {
docs_parser.push(event);
(start_quote_count + 1, 0)
}
Start(Paragraph) => {
if start_quote_count == 3 {
docs_parser.pop();
docs_parser.pop();
docs_parser.pop();
docs_parser.push(Start(CodeBlock(Fenced(Borrowed("roc")))));
} else {
docs_parser.push(event);
}
(0, 0)
}
// Replace this sequence (`>>>` syntax):
// End(Paragraph)
// End(BlockQuote)
// End(BlockQuote)
// End(BlockQuote)
// For `End(CodeBlock(Fenced(Borrowed("roc"))))`
End(Paragraph) => {
docs_parser.push(event);
(0, 1)
}
End(BlockQuote) => {
if end_quote_count == 3 {
docs_parser.pop();
docs_parser.pop();
docs_parser.pop();
docs_parser.push(End(CodeBlock(Fenced(Borrowed("roc")))));
(0, 0)
} else {
docs_parser.push(event);
(0, end_quote_count + 1)
}
}
_ => {
docs_parser.push(event);
(0, 0)
}
},
);
let mut docs_html = String::new();
pulldown_cmark::html::push_html(&mut docs_html, docs_parser.into_iter());
docs_html
}

View file

@ -1,298 +0,0 @@
extern crate fs_extra;
extern crate handlebars;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate pulldown_cmark;
extern crate serde_json;
use roc_builtins::std::StdLib;
use roc_can::builtins::builtin_defs_map;
use roc_load::docs::Documentation;
use roc_load::docs::ModuleDocumentation;
use roc_load::file::LoadingProblem;
use std::fs;
extern crate roc_load;
use bumpalo::Bump;
use roc_collections::all::MutMap;
use std::path::{Path, PathBuf};
#[derive(Serialize)]
pub struct Template {
package_name: String,
package_version: String,
module_name: String,
module_docs: String,
module_entries: Vec<ModuleEntry>,
module_links: Vec<TemplateLink>,
}
#[derive(Serialize, Clone, Debug, PartialEq)]
pub struct ModuleEntry {
name: String,
docs: String,
}
#[derive(Serialize)]
pub struct TemplateLink {
name: String,
href: String,
classes: String,
entries: Vec<TemplateLinkEntry>,
}
#[derive(Serialize)]
pub struct TemplateLinkEntry {
name: String,
}
fn main() {
generate(
vec![
PathBuf::from(r"../compiler/builtins/docs/Bool.roc"),
PathBuf::from(r"../compiler/builtins/docs/Dict.roc"),
// Not working
// PathBuf::from(r"../compiler/builtins/docs/List.roc"),
// Not working
// PathBuf::from(r"../compiler/builtins/docs/Num.roc"),
PathBuf::from(r"../compiler/builtins/docs/Set.roc"),
PathBuf::from(r"../compiler/builtins/docs/Str.roc"),
],
roc_builtins::std::standard_stdlib(),
Path::new("../compiler/builtins/docs"),
Path::new("./build"),
)
}
pub fn generate(filenames: Vec<PathBuf>, std_lib: StdLib, src_dir: &Path, build_dir: &Path) {
let files_docs = files_to_documentations(filenames, std_lib, src_dir);
// TODO: get info from a file like "elm.json"
let package = roc_load::docs::Documentation {
name: "roc/builtins".to_string(),
version: "1.0.0".to_string(),
docs: "Package introduction or README.".to_string(),
modules: files_docs,
};
// Remove old build folder, if exists
let _ = fs::remove_dir_all(build_dir);
let version_folder = build_dir
.join(package.name.clone())
.join(package.version.clone());
// Make sure the output directories exists
fs::create_dir_all(&version_folder)
.expect("TODO gracefully handle creating directories failing");
// Register handlebars template
let mut handlebars = handlebars::Handlebars::new();
handlebars
.register_template_file("page", "./src/templates/page.hbs")
.expect("TODO gracefully handle registering template failing");
// Write each package's module docs html file
for module in &package.modules {
let template = documentation_to_template_data(&package, module);
let handlebars_data = handlebars::to_json(&template);
let filepath = version_folder.join(format!("{}.html", module.name));
let mut output_file =
fs::File::create(filepath).expect("TODO gracefully handle creating file failing");
handlebars
.render_to_write("page", &handlebars_data, &mut output_file)
.expect("TODO gracefully handle writing file failing");
}
// Copy /static folder content to /build
let copy_options = fs_extra::dir::CopyOptions {
overwrite: true,
skip_exist: false,
buffer_size: 64000, //64kb
copy_inside: false,
content_only: true,
depth: 0,
};
fs_extra::dir::copy("./src/static/", &build_dir, &copy_options)
.expect("TODO gracefully handle copying static content failing");
println!("Docs generated at {}", build_dir.display());
}
fn files_to_documentations(
filenames: Vec<PathBuf>,
std_lib: StdLib,
src_dir: &Path,
) -> Vec<ModuleDocumentation> {
let arena = Bump::new();
let mut files_docs = vec![];
for filename in filenames {
match roc_load::file::load_and_typecheck(
&arena,
filename,
&std_lib,
src_dir,
MutMap::default(),
8, // TODO: Is it okay to hardcode ptr_bytes here? I think it should be fine since we'er only type checking (also, 8 => 32bit system)
builtin_defs_map,
) {
Ok(mut loaded) => files_docs.extend(loaded.documentation.drain().map(|x| x.1)),
Err(LoadingProblem::ParsingFailedReport(report)) => {
println!("{}", report);
panic!();
}
Err(e) => panic!("{:?}", e),
}
}
files_docs
}
fn documentation_to_template_data(doc: &Documentation, module: &ModuleDocumentation) -> Template {
Template {
package_name: doc.name.clone(),
package_version: doc.version.clone(),
module_name: module.name.clone(),
module_docs: markdown_to_html(module.docs.clone()),
module_entries: module
.entries
.clone()
.into_iter()
.map(|entry| ModuleEntry {
name: entry.name.clone(),
docs: match entry.docs {
Some(docs) => markdown_to_html(docs),
None => String::new(),
},
})
.collect(),
module_links: doc
.modules
.clone()
.into_iter()
.map(|module_link| TemplateLink {
name: module_link.name.clone(),
href: format!("./{}.html", module_link.name),
classes: "".to_string(),
entries: module_link
.entries
.into_iter()
.map(|entry| TemplateLinkEntry { name: entry.name })
.collect(),
})
.collect(),
}
}
fn markdown_to_html(markdown: String) -> String {
use pulldown_cmark::CodeBlockKind::*;
use pulldown_cmark::CowStr::*;
use pulldown_cmark::Event::*;
use pulldown_cmark::Tag::*;
let markdown_options = pulldown_cmark::Options::all();
let mut docs_parser = vec![];
let (_, _) = pulldown_cmark::Parser::new_ext(&markdown, markdown_options).fold(
(0, 0),
|(start_quote_count, end_quote_count), event| match event {
// Replace this sequence (`>>>` syntax):
// Start(BlockQuote)
// Start(BlockQuote)
// Start(BlockQuote)
// Start(Paragraph)
// For `Start(CodeBlock(Fenced(Borrowed("roc"))))`
Start(BlockQuote) => {
docs_parser.push(event);
(start_quote_count + 1, 0)
}
Start(Paragraph) => {
if start_quote_count == 3 {
docs_parser.pop();
docs_parser.pop();
docs_parser.pop();
docs_parser.push(Start(CodeBlock(Fenced(Borrowed("roc")))));
} else {
docs_parser.push(event);
}
(0, 0)
}
// Replace this sequence (`>>>` syntax):
// End(Paragraph)
// End(BlockQuote)
// End(BlockQuote)
// End(BlockQuote)
// For `End(CodeBlock(Fenced(Borrowed("roc"))))`
End(Paragraph) => {
docs_parser.push(event);
(0, 1)
}
End(BlockQuote) => {
if end_quote_count == 3 {
docs_parser.pop();
docs_parser.pop();
docs_parser.pop();
docs_parser.push(End(CodeBlock(Fenced(Borrowed("roc")))));
(0, 0)
} else {
docs_parser.push(event);
(0, end_quote_count + 1)
}
}
_ => {
docs_parser.push(event);
(0, 0)
}
},
);
let mut docs_html = String::new();
pulldown_cmark::html::push_html(&mut docs_html, docs_parser.into_iter());
docs_html
}
#[cfg(test)]
mod test_docs {
use super::*;
#[test]
fn internal() {
let files_docs = files_to_documentations(
vec![PathBuf::from(r"tests/fixtures/Interface.roc")],
roc_builtins::std::standard_stdlib(),
Path::new("tests/fixtures"),
);
let package = roc_load::docs::Documentation {
name: "roc/builtins".to_string(),
version: "1.0.0".to_string(),
docs: "Package introduction or README.".to_string(),
modules: files_docs,
};
let expected_entries = vec![
ModuleEntry {
name: "singleline".to_string(),
docs: "<p>Single line documentation.</p>\n".to_string(),
},
ModuleEntry {
name: "multiline".to_string(),
docs: "<p>Multiline documentation.\nWithout any complex syntax yet!</p>\n".to_string(),
}, ModuleEntry {
name: "multiparagraph".to_string(),
docs: "<p>Multiparagraph documentation.</p>\n<p>Without any complex syntax yet!</p>\n".to_string(),
}, ModuleEntry {
name: "codeblock".to_string(),
docs: "<p>Turns &gt;&gt;&gt; into code block for now.</p>\n<pre><code class=\"language-roc\">codeblock</code></pre>\n".to_string(),
},
];
for module in &package.modules {
let template = documentation_to_template_data(&package, module);
assert_eq!(template.module_name, "Test");
template
.module_entries
.iter()
.zip(expected_entries.iter())
.for_each(|(x, y)| assert_eq!(x, y));
}
}
}