Skip to content

Commit

Permalink
sidepanel as a template
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Oct 28, 2023
1 parent 6d4c3aa commit b8cfb31
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 49 deletions.
98 changes: 49 additions & 49 deletions src/html/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,7 @@ 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>
<head>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
"#;
const HTML_TAIL: &str = r#"
</body>
<script>
Expand Down Expand Up @@ -82,7 +63,11 @@ pub fn generate(
) -> Result<HashMap<String, String>, anyhow::Error> {
let mut tt = TinyTemplate::new();
tt.set_default_formatter(&tinytemplate::format_unescaped);
tt.add_template("html", HTML_TEMPLATE)?;
tt.add_template("main.html", include_str!("./templates/main.html"))?;
tt.add_template(
"sidepanel.html",
include_str!("./templates/sidepanel.html"),
)?;

let mut files = HashMap::new();

Expand All @@ -92,13 +77,13 @@ pub fn generate(
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 sidepanel = render_sidepanel(&ctx, &mut tt, &partitions)?;
let index_content =
render_index(&ctx, &sidepanel, partitions, current_symbols.clone());
files.insert(
"index".to_string(),
tt.render(
"html",
"main.html",
&serde_json::json!({
"content": index_content
}),
Expand Down Expand Up @@ -179,7 +164,6 @@ fn render_index(
) -> String {
let mut content = String::with_capacity(32 * 1024);

content.push_str(HTML_HEAD);
content.push_str(&format!(
r#"<div style="display: flex;">{sidepanel}<div style="padding: 30px; width: 100%;"><h1>Index</h1><div>{SEARCH_BAR}</div>"#
));
Expand Down Expand Up @@ -211,7 +195,7 @@ fn render_index(
}
content.push_str(r#"</main><div id="searchResults"></div>"#);

content.push_str(&format!(r#"</div></div>{HTML_TAIL}"#));
content.push_str(&format!(r#"</div></div>"#));

content
}
Expand Down Expand Up @@ -276,39 +260,55 @@ fn render_page(
let backs = name.split('.').skip(1).map(|_| "../").collect::<String>();

format!(
r##"<html><head><link rel="stylesheet" href="./{backs}{STYLESHEET_FILENAME}"></head><style>{}</style><div style="display: flex;">{sidepanel}<div style="padding: 30px; width: 100%;"><a href="{}"><- Index</a><div>{SEARCH_BAR}</div>{symbol_group}<div id="searchResults"></div></div></div>{HTML_TAIL}"##,
r##"<html>
<head>
<link rel="stylesheet" href="./{backs}{STYLESHEET_FILENAME}">
</head>
<style>{}</style>
<div style="display: flex;">
{sidepanel}
<div style="padding: 30px; width: 100%;">
<a href="{}"><- Index</a>
<div>
{SEARCH_BAR}
</div>
{symbol_group}
<div id="searchResults"></div>
</div>
</div>
{HTML_TAIL}"##,
context.additional_css.borrow(),
ctx.base_url,
)
}

fn render_sidepanel(
ctx: &GenerateCtx,
tt: &mut tinytemplate::TinyTemplate,
partitions: &IndexMap<DocNodeKind, Vec<crate::DocNode>>,
) -> String {
let mut sidepanel = String::with_capacity(1024);

sidepanel.push_str(r#"<div id="sidepanel">"#);
sidepanel.push_str(&format!(
r#"<h2><a href="{}">{}</a></h2>"#,
ctx.base_url, ctx.package_name
));
for (kind, doc_nodes) in partitions.iter() {
sidepanel.push_str(&format!(r#"<h3>{:?}</h3><ul>"#, kind));

for doc_node in doc_nodes {
sidepanel.push_str(&format!(
r##"<li><a href="{}.html">{}</a></li>"##,
ctx.url(doc_node.name.to_string()),
doc_node.name
));
}

sidepanel.push_str(r#"</ul>"#);
}
sidepanel.push_str(r#"</div>"#);

sidepanel
) -> Result<String, anyhow::Error> {
let partitions: Vec<serde_json::Value> = partitions
.into_iter()
.map(|(kind, doc_nodes)| {
serde_json::json!({
"kind": format!("{:?}", kind),
"doc_nodes": doc_nodes.iter().map(|doc_node| {
serde_json::json!({
"url": ctx.url(doc_node.name.to_string()),
"name": doc_node.name.to_string()
})
}).collect::<Vec<serde_json::Value>>()
})
})
.collect();

let render_ctx = serde_json::json!({
"base_url": ctx.base_url.to_string(),
"package_name": ctx.package_name.to_string(),
"partitions": partitions,
});
let content = tt.render("sidepanel.html", &render_ctx)?;
Ok(content)
}

#[derive(Clone, Debug, Serialize)]
Expand Down
10 changes: 10 additions & 0 deletions src/html/templates/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<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>
13 changes: 13 additions & 0 deletions src/html/templates/sidepanel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div id="sidepanel">
<h2><a href="{base_url}">{package_name}</a></h2>
{{ for partition in partitions }}
<h3>{partition.kind}</h3>
<ul>
{{ for doc_node in partition.doc_nodes }}
<li>
<a href="{doc_node.url}">{doc_node.name}</a>
</li>
{{ endfor }}
</ul>
{{ endfor }}
</div>

0 comments on commit b8cfb31

Please sign in to comment.