Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust document style #521

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 75 additions & 7 deletions crates/veryl/src/doc_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const SUMMARY_TMPL: &str = r###"
# Summary

[{{name}}](index.md)
- [{{version}}]()

---

- [Modules](modules.md)
{{#each modules}}
Expand All @@ -37,6 +40,7 @@ const SUMMARY_TMPL: &str = r###"
#[derive(Serialize)]
struct SummaryData {
name: String,
version: String,
modules: Vec<String>,
interfaces: Vec<String>,
packages: Vec<String>,
Expand All @@ -45,14 +49,28 @@ struct SummaryData {
const INDEX_TMPL: &str = r###"
# {{name}}

## Project

{{description}}

* Version: {{version}}
<table align="center" class="table_list">
<tbody>
<tr>
<th class="table_list_item">Version</th>
<td class="table_list_item">{{version}}</td>
</tr>
{{#if repository}}
* Repository: [{{repository}}]({{repository}})
<tr>
<th class="table_list_item">Repository</th>
<td class="table_list_item"><a href="{{repository}}">{{repository}}</a></td>
</tr>
{{/if}}
{{#if license}}
<tr>
<th class="table_list_item">License</th>
<td class="table_list_item">{{license}}</td>
</tr>
{{/if}}
</tbody>
</table>

{{{{raw}}}}
{{#include modules.md}}
Expand All @@ -67,15 +85,24 @@ struct IndexData {
description: Option<String>,
version: String,
repository: Option<String>,
license: Option<String>,
}

const LIST_TMPL: &str = r###"
## {{name}}
---

<table class="table_list">
<tbody>
{{#each items}}
[{{this.name}}]({{this.name}}.md) {{this.description}}

<tr>
<th class="table_list_item"><a href="{{this.name}}.html">{{this.name}}</a></th>
<td class="table_list_item">{{this.description}}</td>
</tr>
{{/each}}
</tbody>
</table>

"###;

#[derive(Serialize)]
Expand Down Expand Up @@ -178,6 +205,7 @@ pub struct DocBuilder {
temp_dir: TempDir,
root_dir: PathBuf,
src_dir: PathBuf,
theme_dir: PathBuf,
modules: BTreeMap<String, Symbol>,
interfaces: BTreeMap<String, Symbol>,
packages: BTreeMap<String, Symbol>,
Expand All @@ -193,20 +221,25 @@ impl DocBuilder {
let temp_dir = tempfile::tempdir().into_diagnostic()?;
let root_dir = temp_dir.path().to_path_buf();
let src_dir = temp_dir.path().join("src");
let theme_dir = temp_dir.path().join("theme");
fs::create_dir(&src_dir).into_diagnostic()?;
fs::create_dir(&theme_dir).into_diagnostic()?;

Ok(Self {
metadata: metadata.clone(),
temp_dir,
root_dir,
src_dir,
theme_dir,
modules,
interfaces,
packages,
})
}

pub fn build(&self) -> Result<()> {
self.build_theme()?;

self.build_component("SUMMARY.md", self.build_summary())?;
self.build_component("index.md", self.build_index())?;
self.build_component("modules.md", self.build_modules())?;
Expand Down Expand Up @@ -237,13 +270,46 @@ impl DocBuilder {
.join(&self.metadata.doc.path);
cfg.set("output.html.no-section-label", true).unwrap();
cfg.set("output.html.fold.enable", true).unwrap();
cfg.set("output.html.fold.level", 0).unwrap();
cfg.set("output.html.fold.level", 1).unwrap();
cfg.set("output.html.additional-css", vec!["theme/custom.css"])
.unwrap();

let md = MDBook::load_with_config(&self.root_dir, cfg).unwrap();
md.build().unwrap();
Ok(())
}

fn build_theme(&self) -> Result<()> {
let custom_css = r##"
.affix {
font-weight: bold;
font-size: 1.8em;
}

.table_list {
margin-left: 0;
margin-right: auto;
}

.table_list_item {
text-align: left;
border: unset;
background-color: var(--bg);
}
"##;

let file = self.theme_dir.join("custom.css");
let mut file = File::create(file).into_diagnostic()?;
write!(file, "{}", custom_css).into_diagnostic()?;

let favicon = include_bytes!("../../../support/logo/favicon.png");
let file = self.theme_dir.join("favicon.png");
let mut file = File::create(file).into_diagnostic()?;
file.write(favicon).into_diagnostic()?;

Ok(())
}

fn build_component(&self, name: &str, content: String) -> Result<()> {
let file = self.src_dir.join(name);
let mut file = File::create(file).into_diagnostic()?;
Expand All @@ -257,6 +323,7 @@ impl DocBuilder {
let packages: Vec<_> = self.packages.keys().cloned().collect();
let data = SummaryData {
name: self.metadata.project.name.clone(),
version: format!("{}", self.metadata.project.version),
modules,
interfaces,
packages,
Expand All @@ -272,6 +339,7 @@ impl DocBuilder {
version: format!("{}", self.metadata.project.version),
description: self.metadata.project.description.clone(),
repository: self.metadata.project.repository.clone(),
license: self.metadata.project.license.clone(),
};

let handlebars = Handlebars::new();
Expand Down
Binary file added support/logo/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading