docs Syntax highlight function set up, and test set up for syntax highlighting

This commit is contained in:
Chadtech 2021-08-09 20:21:03 -04:00
parent 39be77f786
commit 68147e7703
2 changed files with 143 additions and 3 deletions

View file

@ -14,6 +14,7 @@ use roc_collections::all::MutMap;
use roc_load::docs::DocEntry::DocDef; use roc_load::docs::DocEntry::DocDef;
use roc_region::all::Region; use roc_region::all::Region;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::Split;
pub fn generate(filenames: Vec<PathBuf>, std_lib: StdLib, build_dir: &Path) { pub fn generate(filenames: Vec<PathBuf>, std_lib: StdLib, build_dir: &Path) {
let files_docs = files_to_documentations(filenames, std_lib); let files_docs = files_to_documentations(filenames, std_lib);
@ -75,7 +76,7 @@ pub fn generate(filenames: Vec<PathBuf>, std_lib: StdLib, build_dir: &Path) {
fs::create_dir_all(&module_dir) fs::create_dir_all(&module_dir)
.expect("TODO gracefully handle not being able to create the module dir"); .expect("TODO gracefully handle not being able to create the module dir");
let rendered_module = template_html let mut rendered_module = template_html
.replace( .replace(
"<!-- Package Name and Version -->", "<!-- Package Name and Version -->",
render_name_and_version(package.name.as_str(), package.version.as_str()) render_name_and_version(package.name.as_str(), package.version.as_str())
@ -86,7 +87,10 @@ pub fn generate(filenames: Vec<PathBuf>, std_lib: StdLib, build_dir: &Path) {
render_main_content(&loaded_module.interns, &exports, module).as_str(), render_main_content(&loaded_module.interns, &exports, module).as_str(),
); );
fs::write(module_dir.join("index.html"), rendered_module) fs::write(
module_dir.join("index.html"),
syntax_highlight_code(rendered_module),
)
.expect("TODO gracefully handle failing to write html"); .expect("TODO gracefully handle failing to write html");
} }
} }
@ -94,6 +98,45 @@ pub fn generate(filenames: Vec<PathBuf>, std_lib: StdLib, build_dir: &Path) {
println!("🎉 Docs generated in {}", build_dir.display()); println!("🎉 Docs generated in {}", build_dir.display());
} }
pub fn syntax_highlight_code(mut html: String) -> String {
let mut result = html.clone();
if let Some(start_index) = html.find("<code>") {
let (before_open_tag, after_open_tag_str) = html.split_at(start_index);
let mut after_open_tag = after_open_tag_str.to_string();
drop_letters(&mut after_open_tag, 6);
if let Some(end_index) = after_open_tag.find("</code>") {
let (code_str, after_close_tag_str) = after_open_tag.split_at(end_index);
// TODO change code html
let new_code_string = code_str.to_string();
result = [
before_open_tag,
"<code>",
new_code_string.as_str(),
after_close_tag_str,
]
.concat()
}
}
result
}
fn drop_letters(s: &mut String, pos: usize) {
match s.char_indices().nth(pos) {
Some((pos, _)) => {
s.drain(..pos);
}
None => {
s.clear();
}
}
}
fn render_main_content( fn render_main_content(
interns: &Interns, interns: &Interns,
exposed_values: &[String], exposed_values: &[String],

View file

@ -0,0 +1,97 @@
#[macro_use]
extern crate pretty_assertions;
#[cfg(test)]
mod insert_doc_syntax_highlighting {
use roc_can::env::Env;
use roc_can::scope::Scope;
use roc_collections::all::MutMap;
use roc_docs::{insert_doc_links, syntax_highlight_code};
use roc_module::symbol::{IdentIds, Interns, ModuleIds};
use roc_types::subs::VarStore;
#[test]
fn simple_code_block() {
// let home = ModuleIds::default().get_or_insert(&"Test".into());
//
// let module_ids = ModuleIds::default();
//
// let dep_idents = IdentIds::exposed_builtins(0);
//
// let env = Env::new(home, dep_idents, &module_ids, IdentIds::default());
//
// let all_ident_ids = MutMap::default();
//
// let interns = Interns {
// module_ids: env.module_ids.clone(),
// all_ident_ids,
// };
//
// let var_store = &mut VarStore::default();
// let scope = &mut Scope::new(home, var_store);
//
// let markdown = r#"
// # Hello
// Hello thanks for using my package
// "#;
let html = r#"
<html>
<code>
x : Nat
x =
4
</code>
</html>
"#
.to_string();
let expectation = r#"
<html>
<code>
x : Nat
x =
4
</code>
</html>
"#
.to_string();
assert_eq!(syntax_highlight_code(html.clone()), expectation);
}
#[test]
fn no_code_blocks() {
// let home = ModuleIds::default().get_or_insert(&"Test".into());
//
// let module_ids = ModuleIds::default();
//
// let dep_idents = IdentIds::exposed_builtins(0);
//
// let env = Env::new(home, dep_idents, &module_ids, IdentIds::default());
//
// let all_ident_ids = MutMap::default();
//
// let interns = Interns {
// module_ids: env.module_ids.clone(),
// all_ident_ids,
// };
//
// let var_store = &mut VarStore::default();
// let scope = &mut Scope::new(home, var_store);
//
// let markdown = r#"
// # Hello
// Hello thanks for using my package
// "#;
let html = r#"
<html>
<p> Hello </p>
</html>
"#
.to_string();
assert_eq!(html, syntax_highlight_code(html.clone()),);
}
}