Skip to content

Commit

Permalink
add tinytemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Oct 27, 2023
1 parent f355a95 commit 0678a11
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ wasm-bindgen-futures = { version = "=0.4.37", optional = true }
html-escape = { version = "0.2.13", optional = true }
comrak = { version = "0.19.0", optional = true }
indexmap = { version = "2.0.2", optional = true }
tinytemplate = "1.2.1"

[dev-dependencies]
anyhow = { version = "1.0.58" }
Expand Down
2 changes: 1 addition & 1 deletion examples/ddoc/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ fn generate_docs_directory(
// TODO: don't hardcode the path
base_url: "/generated_docs/".to_string(),
};
let html = deno_doc::html::generate(ctx.clone(), doc_nodes);
let html = deno_doc::html::generate(ctx.clone(), doc_nodes)?;

// TODO: don't hardcode the path
let base_path = format!(
Expand Down
29 changes: 26 additions & 3 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use indexmap::IndexMap;
use std::collections::HashMap;
use tinytemplate::TinyTemplate;

use crate::DocNodeKind;

Expand All @@ -24,6 +25,18 @@ pub const SEARCH_INDEX_FILENAME: &str = "search_index.js";
pub const SEARCH_JS: &str = include_str!("./search.js");
pub const SEARCH_FILENAME: &str = "search.js";

const HTML_TEMPLATE: &str = r#"
<html>
<head>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
{content}
</body>
<script type="module" src="./search_index.js" defer></script>
<script type="module" src="./search.js" defer></script>
</html>"#;

// TODO(bartlomieju): reference STYLESHEET_FILENAME below
const HTML_HEAD: &str = r#"
<html>
Expand Down Expand Up @@ -98,22 +111,32 @@ pub fn generate_search_index(
pub fn generate(
ctx: GenerateCtx,
doc_nodes: &[crate::DocNode],
) -> HashMap<String, String> {
) -> Result<HashMap<String, String>, anyhow::Error> {
let mut tt = TinyTemplate::new();
tt.set_default_formatter(&tinytemplate::format_unescaped);
tt.add_template("html", HTML_TEMPLATE)?;

let mut files = HashMap::new();

// FIXME(bartlomieju): functions can have duplicates because of overloads
let partitions = namespace::partition_nodes_by_kind(doc_nodes);
let name_partitions = partition_nodes_by_name(doc_nodes);

let sidepanel = render_sidepanel(&ctx, &partitions);
let index_content = render_index(&ctx, &sidepanel, partitions);
files.insert(
"index".to_string(),
render_index(&ctx, &sidepanel, partitions),
tt.render(
"html",
&serde_json::json!({
"content": index_content
}),
)?,
);

generate_pages(name_partitions, &mut files, &ctx, &sidepanel, None);

files
Ok(files)
}

fn generate_pages(
Expand Down

0 comments on commit 0678a11

Please sign in to comment.