version history MVP
implement basic version history support; there's now an icon in the footer that lets you see the previous versions and their sources I'm a bit worried about spoilers but honestly it's yet another way to hint yourself at the cool secrets so I don't mind
This commit is contained in:
parent
46dee56331
commit
c58c07d846
28 changed files with 1066 additions and 330 deletions
|
@ -16,6 +16,7 @@ clap = { version = "4.3.22", features = ["derive"] }
|
|||
codespan-reporting = "0.11.1"
|
||||
copy_dir = "0.1.3"
|
||||
env_logger = "0.10.0"
|
||||
git2 = "0.19.0"
|
||||
handlebars = "4.3.7"
|
||||
http-body = "1.0.0"
|
||||
image = "0.24.8"
|
||||
|
|
|
@ -44,7 +44,14 @@ pub enum Command {
|
|||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct GenerateArgs {}
|
||||
pub struct GenerateArgs {
|
||||
/// Only use commits as sources. This will cause the latest revision to be taken from the
|
||||
/// Git history instead of the working tree.
|
||||
///
|
||||
/// Recommended for deployment.
|
||||
#[clap(long)]
|
||||
pub commits_only: bool,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct FixArgs {
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
use std::{ffi::OsStr, ops::Range};
|
||||
|
||||
use anyhow::Context;
|
||||
use codespan_reporting::diagnostic::Diagnostic;
|
||||
use treehouse_format::ast::Branch;
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use crate::{
|
||||
parse::{self, parse_toml_with_diagnostics, parse_tree_with_diagnostics},
|
||||
state::{FileId, Source, Treehouse},
|
||||
state::{report_diagnostics, FileId, Source, Treehouse},
|
||||
};
|
||||
|
||||
use super::{FixAllArgs, FixArgs, Paths};
|
||||
|
@ -103,26 +104,32 @@ fn fix_indent_in_generated_toml(toml: &str, min_indent_level: usize) -> String {
|
|||
|
||||
pub fn fix_file(
|
||||
treehouse: &mut Treehouse,
|
||||
diagnostics: &mut Vec<Diagnostic<FileId>>,
|
||||
file_id: FileId,
|
||||
) -> Result<String, parse::ErrorsEmitted> {
|
||||
parse_tree_with_diagnostics(treehouse, file_id).map(|roots| {
|
||||
let mut source = treehouse.source(file_id).input().to_owned();
|
||||
let mut state = State::default();
|
||||
parse_tree_with_diagnostics(treehouse, file_id)
|
||||
.map(|roots| {
|
||||
let mut source = treehouse.source(file_id).input().to_owned();
|
||||
let mut state = State::default();
|
||||
|
||||
for branch in &roots.branches {
|
||||
dfs_fix_branch(treehouse, file_id, &mut state, branch);
|
||||
}
|
||||
for branch in &roots.branches {
|
||||
dfs_fix_branch(treehouse, file_id, &mut state, branch);
|
||||
}
|
||||
|
||||
// Doing a depth-first search of the branches yields fixes from the beginning of the file
|
||||
// to its end. The most efficient way to apply all the fixes then is to reverse their order,
|
||||
// which lets us modify the source string in place because the fix ranges always stay
|
||||
// correct.
|
||||
for fix in state.fixes.iter().rev() {
|
||||
source.replace_range(fix.range.clone(), &fix.replacement);
|
||||
}
|
||||
// Doing a depth-first search of the branches yields fixes from the beginning of the file
|
||||
// to its end. The most efficient way to apply all the fixes then is to reverse their order,
|
||||
// which lets us modify the source string in place because the fix ranges always stay
|
||||
// correct.
|
||||
for fix in state.fixes.iter().rev() {
|
||||
source.replace_range(fix.range.clone(), &fix.replacement);
|
||||
}
|
||||
|
||||
source
|
||||
})
|
||||
source
|
||||
})
|
||||
.map_err(|mut new| {
|
||||
diagnostics.append(&mut new);
|
||||
parse::ErrorsEmitted
|
||||
})
|
||||
}
|
||||
|
||||
pub fn fix_file_cli(fix_args: FixArgs) -> anyhow::Result<()> {
|
||||
|
@ -130,9 +137,10 @@ pub fn fix_file_cli(fix_args: FixArgs) -> anyhow::Result<()> {
|
|||
let file = std::fs::read_to_string(&fix_args.file).context("cannot read file to fix")?;
|
||||
|
||||
let mut treehouse = Treehouse::new();
|
||||
let mut diagnostics = vec![];
|
||||
let file_id = treehouse.add_file(utf8_filename, Source::Other(file));
|
||||
|
||||
if let Ok(fixed) = fix_file(&mut treehouse, file_id) {
|
||||
if let Ok(fixed) = fix_file(&mut treehouse, &mut diagnostics, file_id) {
|
||||
if fix_args.apply {
|
||||
// Try to write the backup first. If writing that fails, bail out without overwriting
|
||||
// the source file.
|
||||
|
@ -145,7 +153,7 @@ pub fn fix_file_cli(fix_args: FixArgs) -> anyhow::Result<()> {
|
|||
println!("{fixed}");
|
||||
}
|
||||
} else {
|
||||
treehouse.report_diagnostics()?;
|
||||
report_diagnostics(&treehouse.files, &diagnostics)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -160,9 +168,10 @@ pub fn fix_all_cli(fix_all_args: FixAllArgs, paths: &Paths<'_>) -> anyhow::Resul
|
|||
let utf8_filename = entry.path().to_string_lossy();
|
||||
|
||||
let mut treehouse = Treehouse::new();
|
||||
let mut diagnostics = vec![];
|
||||
let file_id = treehouse.add_file(utf8_filename.into_owned(), Source::Other(file));
|
||||
|
||||
if let Ok(fixed) = fix_file(&mut treehouse, file_id) {
|
||||
if let Ok(fixed) = fix_file(&mut treehouse, &mut diagnostics, file_id) {
|
||||
if fixed != treehouse.source(file_id).input() {
|
||||
if fix_all_args.apply {
|
||||
println!("fixing: {:?}", entry.path());
|
||||
|
@ -174,7 +183,7 @@ pub fn fix_all_cli(fix_all_args: FixAllArgs, paths: &Paths<'_>) -> anyhow::Resul
|
|||
}
|
||||
}
|
||||
} else {
|
||||
treehouse.report_diagnostics()?;
|
||||
report_diagnostics(&treehouse.files, &diagnostics)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -193,9 +193,11 @@ async fn branch(RawQuery(named_id): RawQuery, State(state): State<Arc<Server>>)
|
|||
.or_else(|| state.treehouse.branch_redirects.get(&named_id).copied());
|
||||
if let Some(branch_id) = branch_id {
|
||||
let branch = state.treehouse.tree.branch(branch_id);
|
||||
if let Source::Tree { input, tree_path } = state.treehouse.source(branch.file_id) {
|
||||
let file_path = state.target_dir.join(format!("{tree_path}.html"));
|
||||
match std::fs::read_to_string(&file_path) {
|
||||
if let Source::Tree {
|
||||
input, target_path, ..
|
||||
} = state.treehouse.source(branch.file_id)
|
||||
{
|
||||
match std::fs::read_to_string(target_path) {
|
||||
Ok(content) => {
|
||||
let branch_markdown_content = input[branch.content.clone()].trim();
|
||||
let mut per_page_metadata =
|
||||
|
@ -212,7 +214,7 @@ async fn branch(RawQuery(named_id): RawQuery, State(state): State<Arc<Server>>)
|
|||
));
|
||||
}
|
||||
Err(e) => {
|
||||
error!("error while reading file {file_path:?}: {e:?}");
|
||||
error!("error while reading file {target_path:?}: {e:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ use walkdir::WalkDir;
|
|||
|
||||
use crate::{
|
||||
parse::parse_tree_with_diagnostics,
|
||||
state::{Source, Treehouse},
|
||||
state::{report_diagnostics, Source, Treehouse},
|
||||
};
|
||||
|
||||
use super::WcArgs;
|
||||
|
@ -53,17 +53,20 @@ pub fn wc_cli(content_dir: &Path, mut wc_args: WcArgs) -> anyhow::Result<()> {
|
|||
.to_string_lossy();
|
||||
|
||||
let file_id = treehouse.add_file(utf8_filename.into_owned(), Source::Other(file));
|
||||
if let Ok(parsed) = parse_tree_with_diagnostics(&mut treehouse, file_id) {
|
||||
let source = treehouse.source(file_id);
|
||||
let word_count = wc_roots(source.input(), &parsed);
|
||||
println!("{word_count:>8} {}", treehouse.filename(file_id));
|
||||
total += word_count;
|
||||
match parse_tree_with_diagnostics(&mut treehouse, file_id) {
|
||||
Ok(parsed) => {
|
||||
let source = treehouse.source(file_id);
|
||||
let word_count = wc_roots(source.input(), &parsed);
|
||||
println!("{word_count:>8} {}", treehouse.filename(file_id));
|
||||
total += word_count;
|
||||
}
|
||||
Err(diagnostics) => {
|
||||
report_diagnostics(&treehouse.files, &diagnostics)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("{total:>8} total");
|
||||
|
||||
treehouse.report_diagnostics()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -26,6 +26,10 @@ pub struct Config {
|
|||
/// TODO djot: Remove this once we transition to Djot fully.
|
||||
pub markup: Markup,
|
||||
|
||||
/// This is used to generate a link in the footer that links to the page's source commit.
|
||||
/// The final URL is `{commit_base_url}/{commit}/content/{tree_path}.tree`.
|
||||
pub commit_base_url: String,
|
||||
|
||||
/// User-defined keys.
|
||||
pub user: HashMap<String, String>,
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ use walkdir::WalkDir;
|
|||
use crate::{
|
||||
config::{Config, ConfigDerivedData},
|
||||
fun::seasons::Season,
|
||||
history::History,
|
||||
html::{
|
||||
breadcrumbs::breadcrumbs_to_html,
|
||||
navmap::{build_navigation_map, NavigationMap},
|
||||
|
@ -27,7 +28,7 @@ use crate::{
|
|||
import_map::ImportMap,
|
||||
include_static::IncludeStatic,
|
||||
parse::parse_tree_with_diagnostics,
|
||||
state::Source,
|
||||
state::{has_errors, report_diagnostics, RevisionInfo, Source},
|
||||
static_urls::StaticUrls,
|
||||
tree::SemaRoots,
|
||||
};
|
||||
|
@ -36,14 +37,25 @@ use crate::state::{FileId, Treehouse};
|
|||
|
||||
use super::Paths;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Generator {
|
||||
tree_files: Vec<PathBuf>,
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum LatestRevision {
|
||||
/// The working tree is treated as the latest revision.
|
||||
WorkingTree,
|
||||
/// The latest commit is treated as the latest revision. The working tree is ignored.
|
||||
LatestCommit,
|
||||
}
|
||||
|
||||
struct Build {}
|
||||
struct Generator {
|
||||
tree_files: Vec<PathBuf>,
|
||||
git: git2::Repository,
|
||||
history: History,
|
||||
latest_revision: LatestRevision,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct ParsedTree {
|
||||
source_path: String,
|
||||
root_key: String,
|
||||
tree_path: String,
|
||||
file_id: FileId,
|
||||
target_path: PathBuf,
|
||||
|
@ -63,6 +75,27 @@ pub struct Page {
|
|||
pub breadcrumbs: String,
|
||||
pub tree_path: Option<String>,
|
||||
pub tree: String,
|
||||
|
||||
pub revision: RevisionInfo,
|
||||
pub revision_url: String,
|
||||
pub source_url: String,
|
||||
pub history_url: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Commit {
|
||||
pub revision_number: usize,
|
||||
pub hash: String,
|
||||
pub hash_short: String,
|
||||
pub summary: String,
|
||||
pub body: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct HistoryPage {
|
||||
pub title: String,
|
||||
pub commits: Vec<Commit>,
|
||||
pub tree_path: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
|
@ -85,6 +118,13 @@ struct PageTemplateData<'a> {
|
|||
season: Option<Season>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct HistoryTemplateData<'a> {
|
||||
config: &'a Config,
|
||||
page: HistoryPage,
|
||||
season: Option<Season>,
|
||||
}
|
||||
|
||||
impl Generator {
|
||||
fn add_directory_rec(&mut self, directory: &Path) -> anyhow::Result<()> {
|
||||
for entry in WalkDir::new(directory) {
|
||||
|
@ -96,136 +136,7 @@ impl Generator {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn register_template(
|
||||
handlebars: &mut Handlebars<'_>,
|
||||
treehouse: &mut Treehouse,
|
||||
name: &str,
|
||||
path: &Path,
|
||||
) -> anyhow::Result<FileId> {
|
||||
let source = std::fs::read_to_string(path)
|
||||
.with_context(|| format!("cannot read template file {path:?}"))?;
|
||||
let file_id =
|
||||
treehouse.add_file(path.to_string_lossy().into_owned(), Source::Other(source));
|
||||
let source = treehouse.source(file_id);
|
||||
if let Err(error) = handlebars.register_template_string(name, source) {
|
||||
Self::wrangle_handlebars_error_into_diagnostic(
|
||||
treehouse,
|
||||
file_id,
|
||||
error.line_no,
|
||||
error.column_no,
|
||||
error.reason().to_string(),
|
||||
)?;
|
||||
}
|
||||
Ok(file_id)
|
||||
}
|
||||
|
||||
fn wrangle_handlebars_error_into_diagnostic(
|
||||
treehouse: &mut Treehouse,
|
||||
file_id: FileId,
|
||||
line: Option<usize>,
|
||||
column: Option<usize>,
|
||||
message: String,
|
||||
) -> anyhow::Result<()> {
|
||||
if let (Some(line), Some(column)) = (line, column) {
|
||||
let line_range = treehouse
|
||||
.files
|
||||
.line_range(file_id, line)
|
||||
.expect("file was added to the list");
|
||||
treehouse.diagnostics.push(Diagnostic {
|
||||
severity: Severity::Error,
|
||||
code: Some("template".into()),
|
||||
message,
|
||||
labels: vec![Label {
|
||||
style: LabelStyle::Primary,
|
||||
file_id,
|
||||
range: line_range.start + column..line_range.start + column + 1,
|
||||
message: String::new(),
|
||||
}],
|
||||
notes: vec![],
|
||||
})
|
||||
} else {
|
||||
let file = treehouse.filename(file_id);
|
||||
bail!("template error in {file}: {message}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_trees(
|
||||
&self,
|
||||
config: &Config,
|
||||
paths: &Paths<'_>,
|
||||
) -> anyhow::Result<(Treehouse, Vec<ParsedTree>)> {
|
||||
let mut treehouse = Treehouse::new();
|
||||
let mut parsed_trees = vec![];
|
||||
|
||||
for path in &self.tree_files {
|
||||
let utf8_filename = path.to_string_lossy();
|
||||
|
||||
let tree_path = path.strip_prefix(paths.content_dir).unwrap_or(path);
|
||||
let target_path = if tree_path == OsStr::new("index.tree") {
|
||||
paths.target_dir.join("index.html")
|
||||
} else {
|
||||
paths.target_dir.join(tree_path).with_extension("html")
|
||||
};
|
||||
debug!("generating: {path:?} -> {target_path:?}");
|
||||
|
||||
let source = match std::fs::read_to_string(path) {
|
||||
Ok(source) => source,
|
||||
Err(error) => {
|
||||
treehouse.diagnostics.push(Diagnostic {
|
||||
severity: Severity::Error,
|
||||
code: None,
|
||||
message: format!("{utf8_filename}: cannot read file: {error}"),
|
||||
labels: vec![],
|
||||
notes: vec![],
|
||||
});
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let tree_path = tree_path
|
||||
.with_extension("")
|
||||
.to_string_lossy()
|
||||
.replace('\\', "/");
|
||||
let file_id = treehouse.add_file(
|
||||
utf8_filename.into_owned(),
|
||||
Source::Tree {
|
||||
input: source,
|
||||
tree_path: tree_path.clone(),
|
||||
},
|
||||
);
|
||||
|
||||
if let Ok(roots) = parse_tree_with_diagnostics(&mut treehouse, file_id) {
|
||||
let roots = SemaRoots::from_roots(&mut treehouse, config, file_id, roots);
|
||||
treehouse.roots.insert(tree_path.clone(), roots);
|
||||
parsed_trees.push(ParsedTree {
|
||||
tree_path,
|
||||
file_id,
|
||||
target_path,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok((treehouse, parsed_trees))
|
||||
}
|
||||
|
||||
fn generate_all_files(
|
||||
&self,
|
||||
treehouse: &mut Treehouse,
|
||||
config: &Config,
|
||||
paths: &Paths<'_>,
|
||||
navigation_map: &NavigationMap,
|
||||
parsed_trees: Vec<ParsedTree>,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut handlebars = Handlebars::new();
|
||||
let mut config_derived_data = ConfigDerivedData {
|
||||
image_sizes: Default::default(),
|
||||
static_urls: StaticUrls::new(
|
||||
// NOTE: Allow referring to generated static assets here.
|
||||
paths.target_dir.join("static"),
|
||||
format!("{}/static", config.site),
|
||||
),
|
||||
};
|
||||
|
||||
fn init_handlebars(handlebars: &mut Handlebars<'_>, paths: &Paths<'_>, config: &Config) {
|
||||
handlebars_helper!(cat: |a: String, b: String| a + &b);
|
||||
|
||||
handlebars.register_helper("cat", Box::new(cat));
|
||||
|
@ -245,6 +156,267 @@ impl Generator {
|
|||
base_dir: paths.target_dir.join("static"),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
fn register_template(
|
||||
handlebars: &mut Handlebars<'_>,
|
||||
treehouse: &mut Treehouse,
|
||||
diagnostics: &mut Vec<Diagnostic<FileId>>,
|
||||
name: &str,
|
||||
path: &Path,
|
||||
) -> anyhow::Result<FileId> {
|
||||
let source = std::fs::read_to_string(path)
|
||||
.with_context(|| format!("cannot read template file {path:?}"))?;
|
||||
let file_id =
|
||||
treehouse.add_file(path.to_string_lossy().into_owned(), Source::Other(source));
|
||||
let source = treehouse.source(file_id);
|
||||
if let Err(error) = handlebars.register_template_string(name, source) {
|
||||
Self::wrangle_handlebars_error_into_diagnostic(
|
||||
treehouse,
|
||||
diagnostics,
|
||||
file_id,
|
||||
error.line_no,
|
||||
error.column_no,
|
||||
error.reason().to_string(),
|
||||
)?;
|
||||
}
|
||||
Ok(file_id)
|
||||
}
|
||||
|
||||
fn wrangle_handlebars_error_into_diagnostic(
|
||||
treehouse: &mut Treehouse,
|
||||
diagnostics: &mut Vec<Diagnostic<FileId>>,
|
||||
file_id: FileId,
|
||||
line: Option<usize>,
|
||||
column: Option<usize>,
|
||||
message: String,
|
||||
) -> anyhow::Result<()> {
|
||||
if let (Some(line), Some(column)) = (line, column) {
|
||||
let line_range = treehouse
|
||||
.files
|
||||
.line_range(file_id, line)
|
||||
.expect("file was added to the list");
|
||||
diagnostics.push(Diagnostic {
|
||||
severity: Severity::Error,
|
||||
code: Some("template".into()),
|
||||
message,
|
||||
labels: vec![Label {
|
||||
style: LabelStyle::Primary,
|
||||
file_id,
|
||||
range: line_range.start + column..line_range.start + column + 1,
|
||||
message: String::new(),
|
||||
}],
|
||||
notes: vec![],
|
||||
})
|
||||
} else {
|
||||
let file = treehouse.filename(file_id);
|
||||
bail!("template error in {file}: {message}");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_tree(
|
||||
treehouse: &mut Treehouse,
|
||||
config: &Config,
|
||||
source: String,
|
||||
source_path: String,
|
||||
tree_path: String,
|
||||
target_path: PathBuf,
|
||||
revision: RevisionInfo,
|
||||
) -> anyhow::Result<(Option<ParsedTree>, Vec<Diagnostic<FileId>>)> {
|
||||
let file_id = treehouse.add_file(
|
||||
format!("{source_path}@{}", revision.commit_short),
|
||||
Source::Tree {
|
||||
input: source,
|
||||
target_path: target_path.clone(),
|
||||
tree_path: tree_path.clone(),
|
||||
revision_info: revision.clone(),
|
||||
},
|
||||
);
|
||||
|
||||
match parse_tree_with_diagnostics(treehouse, file_id) {
|
||||
Ok(roots) => {
|
||||
let mut diagnostics = vec![];
|
||||
let roots =
|
||||
SemaRoots::from_roots(treehouse, &mut diagnostics, config, file_id, roots);
|
||||
|
||||
let root_key = if revision.is_latest {
|
||||
tree_path.clone()
|
||||
} else {
|
||||
format!("{tree_path}@{}", revision.number)
|
||||
};
|
||||
treehouse.roots.insert(root_key.clone(), roots);
|
||||
|
||||
Ok((
|
||||
Some(ParsedTree {
|
||||
source_path,
|
||||
root_key,
|
||||
tree_path,
|
||||
file_id,
|
||||
target_path,
|
||||
}),
|
||||
diagnostics,
|
||||
))
|
||||
}
|
||||
Err(diagnostics) => Ok((None, diagnostics)),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_trees(
|
||||
&self,
|
||||
config: &Config,
|
||||
paths: &Paths<'_>,
|
||||
) -> anyhow::Result<(Treehouse, Vec<ParsedTree>, Vec<Diagnostic<FileId>>)> {
|
||||
let mut treehouse = Treehouse::new();
|
||||
let mut diagnostics = vec![];
|
||||
let mut parsed_trees = vec![];
|
||||
|
||||
for path in &self.tree_files {
|
||||
let utf8_path = path.to_string_lossy();
|
||||
|
||||
let tree_path = path
|
||||
.strip_prefix(paths.content_dir)
|
||||
.unwrap_or(path)
|
||||
.with_extension("")
|
||||
.to_string_lossy()
|
||||
.replace('\\', "/");
|
||||
debug!("tree file: {path:?}");
|
||||
|
||||
let page_history = self.history.by_page.get(&utf8_path[..]);
|
||||
let working_revision_number = page_history
|
||||
.map(|history| history.revisions.len() + 1)
|
||||
.unwrap_or(1);
|
||||
|
||||
if self.latest_revision == LatestRevision::WorkingTree {
|
||||
let source = std::fs::read_to_string(path)?;
|
||||
let target_path = paths.target_dir.join(&tree_path).with_extension("html");
|
||||
let (parsed_tree, mut parse_diagnostics) = Self::parse_tree(
|
||||
&mut treehouse,
|
||||
config,
|
||||
source,
|
||||
utf8_path.clone().into_owned(),
|
||||
tree_path.clone(),
|
||||
target_path,
|
||||
RevisionInfo {
|
||||
is_latest: true,
|
||||
number: working_revision_number,
|
||||
commit: "working".into(),
|
||||
commit_short: "working".into(),
|
||||
},
|
||||
)?;
|
||||
diagnostics.append(&mut parse_diagnostics);
|
||||
if let Some(parsed_tree) = parsed_tree {
|
||||
parsed_trees.push(parsed_tree);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(page_history) = page_history {
|
||||
for (i, revision) in page_history.revisions.iter().enumerate() {
|
||||
let revision_number = page_history.revisions.len() - i;
|
||||
|
||||
let source = String::from_utf8(
|
||||
self.git.find_blob(revision.blob_oid)?.content().to_owned(),
|
||||
)?;
|
||||
|
||||
let target_path = paths
|
||||
.target_dir
|
||||
.join(format!("{tree_path}@{revision_number}"))
|
||||
.with_extension("html");
|
||||
|
||||
let (parsed_tree, parse_diagnostics) = Self::parse_tree(
|
||||
&mut treehouse,
|
||||
config,
|
||||
source,
|
||||
utf8_path.clone().into_owned(),
|
||||
tree_path.clone(),
|
||||
target_path,
|
||||
RevisionInfo {
|
||||
is_latest: false,
|
||||
number: revision_number,
|
||||
commit: revision.commit_oid.to_string(),
|
||||
commit_short: revision.commit_short(),
|
||||
},
|
||||
)?;
|
||||
_ = parse_diagnostics; // We don't reemit diagnostics from old revisions.
|
||||
if let Some(parsed_tree) = parsed_tree {
|
||||
// If this commit is also considered to be the latest revision, we need
|
||||
// to generate a second version of the page that will act as the
|
||||
// latest one.
|
||||
let is_latest =
|
||||
self.latest_revision == LatestRevision::LatestCommit && i == 0;
|
||||
if is_latest {
|
||||
let root_key = parsed_tree.tree_path.clone();
|
||||
treehouse.roots.insert(
|
||||
root_key.clone(),
|
||||
treehouse.roots.get(&parsed_tree.root_key).unwrap().clone(),
|
||||
);
|
||||
|
||||
let target_path =
|
||||
paths.target_dir.join(&tree_path).with_extension("html");
|
||||
let file_id = {
|
||||
let file = treehouse.files.get(parsed_tree.file_id).unwrap();
|
||||
let filename = file.name().clone();
|
||||
let Source::Tree {
|
||||
input,
|
||||
tree_path,
|
||||
target_path,
|
||||
revision_info,
|
||||
} = file.source().clone()
|
||||
else {
|
||||
panic!(".tree files must have Tree sources")
|
||||
};
|
||||
treehouse.add_file(
|
||||
filename,
|
||||
Source::Tree {
|
||||
input,
|
||||
tree_path,
|
||||
target_path: target_path.clone(),
|
||||
revision_info: RevisionInfo {
|
||||
is_latest: true,
|
||||
..revision_info
|
||||
},
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
parsed_trees.push(ParsedTree {
|
||||
root_key,
|
||||
target_path,
|
||||
file_id,
|
||||
..parsed_tree.clone()
|
||||
})
|
||||
}
|
||||
|
||||
parsed_trees.push(parsed_tree);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok((treehouse, parsed_trees, diagnostics))
|
||||
}
|
||||
|
||||
fn generate_all_files(
|
||||
&self,
|
||||
treehouse: &mut Treehouse,
|
||||
config: &Config,
|
||||
paths: &Paths<'_>,
|
||||
navigation_map: &NavigationMap,
|
||||
parsed_trees: Vec<ParsedTree>,
|
||||
) -> anyhow::Result<Vec<Diagnostic<FileId>>> {
|
||||
let mut global_diagnostics = vec![];
|
||||
|
||||
let mut config_derived_data = ConfigDerivedData {
|
||||
image_sizes: Default::default(),
|
||||
static_urls: StaticUrls::new(
|
||||
// NOTE: Allow referring to generated static assets here.
|
||||
paths.target_dir.join("static"),
|
||||
format!("{}/static", config.site),
|
||||
),
|
||||
};
|
||||
|
||||
let mut handlebars = Handlebars::new();
|
||||
Self::init_handlebars(&mut handlebars, paths, config);
|
||||
|
||||
let mut template_file_ids = HashMap::new();
|
||||
for entry in WalkDir::new(paths.template_dir) {
|
||||
|
@ -256,8 +428,13 @@ impl Generator {
|
|||
.to_string_lossy()
|
||||
.into_owned()
|
||||
.replace('\\', "/");
|
||||
let file_id =
|
||||
Self::register_template(&mut handlebars, treehouse, &relative_path, path)?;
|
||||
let file_id = Self::register_template(
|
||||
&mut handlebars,
|
||||
treehouse,
|
||||
&mut global_diagnostics,
|
||||
&relative_path,
|
||||
path,
|
||||
)?;
|
||||
template_file_ids.insert(relative_path, file_id);
|
||||
}
|
||||
}
|
||||
|
@ -277,6 +454,7 @@ impl Generator {
|
|||
Err(error) => {
|
||||
Self::wrangle_handlebars_error_into_diagnostic(
|
||||
treehouse,
|
||||
&mut global_diagnostics,
|
||||
file_id,
|
||||
error.line_no,
|
||||
error.column_no,
|
||||
|
@ -295,7 +473,7 @@ impl Generator {
|
|||
let mut feeds = HashMap::new();
|
||||
|
||||
for parsed_tree in &parsed_trees {
|
||||
let roots = &treehouse.roots[&parsed_tree.tree_path];
|
||||
let roots = &treehouse.roots[&parsed_tree.root_key];
|
||||
|
||||
if let Some(feed_name) = &roots.attributes.feed {
|
||||
let mut feed = Feed {
|
||||
|
@ -310,13 +488,15 @@ impl Generator {
|
|||
}
|
||||
|
||||
for parsed_tree in parsed_trees {
|
||||
let breadcrumbs = breadcrumbs_to_html(config, navigation_map, &parsed_tree.tree_path);
|
||||
debug!("generating: {:?}", parsed_tree.target_path);
|
||||
|
||||
let breadcrumbs = breadcrumbs_to_html(config, navigation_map, &parsed_tree.root_key);
|
||||
|
||||
let mut tree = String::new();
|
||||
// Temporarily steal the tree out of the treehouse.
|
||||
let roots = treehouse
|
||||
.roots
|
||||
.remove(&parsed_tree.tree_path)
|
||||
.remove(&parsed_tree.root_key)
|
||||
.expect("tree should have been added to the treehouse");
|
||||
branches_to_html(
|
||||
&mut tree,
|
||||
|
@ -328,6 +508,9 @@ impl Generator {
|
|||
&roots.branches,
|
||||
);
|
||||
|
||||
let revision = treehouse
|
||||
.revision_info(parsed_tree.file_id)
|
||||
.expect(".tree files should have Tree sources");
|
||||
let template_data = PageTemplateData {
|
||||
config,
|
||||
page: Page {
|
||||
|
@ -347,6 +530,14 @@ impl Generator {
|
|||
.tree_path(parsed_tree.file_id)
|
||||
.map(|s| s.to_owned()),
|
||||
tree,
|
||||
|
||||
revision_url: format!("{}/{}", config.site, parsed_tree.root_key),
|
||||
source_url: format!(
|
||||
"{}/{}/{}",
|
||||
config.commit_base_url, revision.commit, parsed_tree.source_path,
|
||||
),
|
||||
history_url: format!("{}/h/{}", config.site, parsed_tree.tree_path),
|
||||
revision: revision.clone(),
|
||||
},
|
||||
feeds: &feeds,
|
||||
season: Season::current(),
|
||||
|
@ -357,13 +548,16 @@ impl Generator {
|
|||
.clone()
|
||||
.unwrap_or_else(|| "_tree.hbs".into());
|
||||
|
||||
treehouse.roots.insert(parsed_tree.tree_path, roots);
|
||||
// Reinsert the stolen roots.
|
||||
treehouse.roots.insert(parsed_tree.root_key, roots);
|
||||
|
||||
let templated_html = match handlebars.render(&template_name, &template_data) {
|
||||
Ok(html) => html,
|
||||
Err(error) => {
|
||||
Self::wrangle_handlebars_error_into_diagnostic(
|
||||
treehouse,
|
||||
// TODO: This should dump diagnostics out somewhere else.
|
||||
&mut global_diagnostics,
|
||||
template_file_ids[&template_name],
|
||||
error.line_no,
|
||||
error.column_no,
|
||||
|
@ -382,11 +576,78 @@ impl Generator {
|
|||
std::fs::write(parsed_tree.target_path, templated_html)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
for (path, page_history) in &self.history.by_page {
|
||||
let tree_path = path
|
||||
.strip_prefix("content/")
|
||||
.unwrap_or(path)
|
||||
.strip_suffix(".tree")
|
||||
.unwrap_or(path);
|
||||
let target_path = paths
|
||||
.target_dir
|
||||
.join("h")
|
||||
.join(path.strip_prefix("content/").unwrap_or(path))
|
||||
.with_extension("html");
|
||||
std::fs::create_dir_all(target_path.parent().unwrap())?;
|
||||
|
||||
let template_data = HistoryTemplateData {
|
||||
config,
|
||||
page: HistoryPage {
|
||||
title: format!("page history: {tree_path}"),
|
||||
commits: page_history
|
||||
.revisions
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, revision)| Commit {
|
||||
revision_number: page_history.revisions.len() - i,
|
||||
hash: revision.commit_oid.to_string(),
|
||||
hash_short: revision.commit_short(),
|
||||
summary: self
|
||||
.history
|
||||
.commits
|
||||
.get(&revision.commit_oid)
|
||||
.map(|c| c.summary.as_str())
|
||||
.unwrap_or("<no summary available>")
|
||||
.to_owned(),
|
||||
body: self
|
||||
.history
|
||||
.commits
|
||||
.get(&revision.commit_oid)
|
||||
.map(|c| c.body.as_str())
|
||||
.unwrap_or("<no body available>")
|
||||
.to_owned(),
|
||||
})
|
||||
.collect(),
|
||||
tree_path: tree_path.to_owned(),
|
||||
},
|
||||
season: Season::current(),
|
||||
};
|
||||
let templated_html = match handlebars.render("_history.hbs", &template_data) {
|
||||
Ok(html) => html,
|
||||
Err(error) => {
|
||||
Self::wrangle_handlebars_error_into_diagnostic(
|
||||
treehouse,
|
||||
// TODO: This should dump diagnostics out somewhere else.
|
||||
&mut global_diagnostics,
|
||||
template_file_ids["_history.hbs"],
|
||||
error.line_no,
|
||||
error.column_no,
|
||||
error.desc,
|
||||
)?;
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
std::fs::write(target_path, templated_html)?;
|
||||
}
|
||||
|
||||
Ok(global_diagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate(paths: &Paths<'_>) -> anyhow::Result<(Config, Treehouse)> {
|
||||
pub fn generate(
|
||||
paths: &Paths<'_>,
|
||||
latest_revision: LatestRevision,
|
||||
) -> anyhow::Result<(Config, Treehouse)> {
|
||||
let start = Instant::now();
|
||||
|
||||
info!("loading config");
|
||||
|
@ -406,10 +667,23 @@ pub fn generate(paths: &Paths<'_>) -> anyhow::Result<(Config, Treehouse)> {
|
|||
info!("creating static/generated directory");
|
||||
std::fs::create_dir_all(paths.target_dir.join("static/generated"))?;
|
||||
|
||||
info!("getting history");
|
||||
let git = git2::Repository::open(".")?;
|
||||
let history = History::get(&git)?;
|
||||
|
||||
info!("parsing tree");
|
||||
let mut generator = Generator::default();
|
||||
let mut generator = Generator {
|
||||
tree_files: vec![],
|
||||
git,
|
||||
history,
|
||||
latest_revision,
|
||||
};
|
||||
generator.add_directory_rec(paths.content_dir)?;
|
||||
let (mut treehouse, parsed_trees) = generator.parse_trees(&config, paths)?;
|
||||
let (mut treehouse, parsed_trees, diagnostics) = generator.parse_trees(&config, paths)?;
|
||||
report_diagnostics(&treehouse.files, &diagnostics)?;
|
||||
if has_errors(&diagnostics) {
|
||||
bail!("diagnostics emitted during parsing");
|
||||
}
|
||||
|
||||
// NOTE: The navigation map is a legacy feature that is lazy-loaded when fragment-based
|
||||
// navigation is used.
|
||||
|
@ -431,30 +705,34 @@ pub fn generate(paths: &Paths<'_>) -> anyhow::Result<(Config, Treehouse)> {
|
|||
)?;
|
||||
|
||||
info!("generating standalone pages");
|
||||
generator.generate_all_files(
|
||||
let diagnostics = generator.generate_all_files(
|
||||
&mut treehouse,
|
||||
&config,
|
||||
paths,
|
||||
&navigation_map,
|
||||
parsed_trees,
|
||||
)?;
|
||||
report_diagnostics(&treehouse.files, &diagnostics)?;
|
||||
|
||||
treehouse.report_diagnostics()?;
|
||||
info!("generating change history pages");
|
||||
|
||||
let duration = start.elapsed();
|
||||
info!("generation done in {duration:?}");
|
||||
|
||||
if !treehouse.has_errors() {
|
||||
if !has_errors(&diagnostics) {
|
||||
Ok((config, treehouse))
|
||||
} else {
|
||||
bail!("generation errors occurred; diagnostics were emitted with detailed descriptions");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn regenerate_or_report_error(paths: &Paths<'_>) -> anyhow::Result<(Config, Treehouse)> {
|
||||
pub fn regenerate_or_report_error(
|
||||
paths: &Paths<'_>,
|
||||
latest_revision: LatestRevision,
|
||||
) -> anyhow::Result<(Config, Treehouse)> {
|
||||
info!("regenerating site content");
|
||||
|
||||
let result = generate(paths);
|
||||
let result = generate(paths, latest_revision);
|
||||
if let Err(e) = &result {
|
||||
error!("{e:?}");
|
||||
}
|
||||
|
|
106
crates/treehouse/src/history.rs
Normal file
106
crates/treehouse/src/history.rs
Normal file
|
@ -0,0 +1,106 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use log::debug;
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct History {
|
||||
// Sorted from newest to oldest.
|
||||
pub commits: IndexMap<git2::Oid, Commit>,
|
||||
pub by_page: HashMap<String, PageHistory>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Commit {
|
||||
pub summary: String,
|
||||
pub body: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct PageHistory {
|
||||
// Sorted from newest to oldest, so revision 0 is the current version.
|
||||
// On the website these are sorted differently: 1 is the oldest revision, succeeding numbers are later revisions.
|
||||
pub revisions: Vec<Revision>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Revision {
|
||||
pub commit_oid: git2::Oid,
|
||||
pub blob_oid: git2::Oid,
|
||||
}
|
||||
|
||||
impl History {
|
||||
pub fn get(git: &git2::Repository) -> anyhow::Result<Self> {
|
||||
debug!("reading git history");
|
||||
|
||||
let mut history = History::default();
|
||||
|
||||
let mut revwalk = git.revwalk()?;
|
||||
revwalk.push_head()?;
|
||||
|
||||
for commit_oid in revwalk {
|
||||
let commit_oid = commit_oid?;
|
||||
let commit = git.find_commit(commit_oid)?;
|
||||
history.commits.insert(
|
||||
commit_oid,
|
||||
Commit {
|
||||
summary: String::from_utf8_lossy(commit.summary_bytes().unwrap_or(&[]))
|
||||
.into_owned(),
|
||||
body: String::from_utf8_lossy(commit.body_bytes().unwrap_or(&[])).into_owned(),
|
||||
},
|
||||
);
|
||||
|
||||
let tree = commit.tree()?;
|
||||
tree.walk(git2::TreeWalkMode::PreOrder, |parent_path, entry| {
|
||||
if parent_path.is_empty() && entry.name() != Some("content") {
|
||||
// This is content-only history, so skip all directories that don't contain content.
|
||||
git2::TreeWalkResult::Skip
|
||||
} else if entry.kind() == Some(git2::ObjectType::Blob)
|
||||
&& entry.name().is_some_and(|name| name.ends_with(".tree"))
|
||||
{
|
||||
let path = format!(
|
||||
"{parent_path}{}",
|
||||
String::from_utf8_lossy(entry.name_bytes())
|
||||
);
|
||||
let page_history = history.by_page.entry(path).or_default();
|
||||
|
||||
let unchanged = page_history
|
||||
.revisions
|
||||
.last()
|
||||
.is_some_and(|rev| rev.blob_oid == entry.id());
|
||||
if unchanged {
|
||||
// Note again that the history is reversed as we're walking from HEAD
|
||||
// backwards, so we need to find the _earliest_ commit with this revision.
|
||||
// Therefore we update that current revision's commit oid with the
|
||||
// current commit.
|
||||
page_history.revisions.last_mut().unwrap().commit_oid = commit_oid;
|
||||
} else {
|
||||
page_history.revisions.push(Revision {
|
||||
commit_oid,
|
||||
blob_oid: entry.id(),
|
||||
});
|
||||
}
|
||||
git2::TreeWalkResult::Ok
|
||||
} else {
|
||||
git2::TreeWalkResult::Ok
|
||||
}
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok(history)
|
||||
}
|
||||
|
||||
pub fn read_revision(
|
||||
&self,
|
||||
git: &git2::Repository,
|
||||
revision: &Revision,
|
||||
) -> anyhow::Result<Vec<u8>> {
|
||||
Ok(git.find_blob(revision.blob_oid)?.content().to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl Revision {
|
||||
pub fn commit_short(&self) -> String {
|
||||
self.commit_oid.to_string()[0..6].to_owned()
|
||||
}
|
||||
}
|
|
@ -33,7 +33,12 @@ pub struct Renderer<'a> {
|
|||
}
|
||||
|
||||
impl<'a> Renderer<'a> {
|
||||
pub fn render(self, events: &[(Event, Range<usize>)], out: &mut String) {
|
||||
#[must_use]
|
||||
pub fn render(
|
||||
self,
|
||||
events: &[(Event, Range<usize>)],
|
||||
out: &mut String,
|
||||
) -> Vec<Diagnostic<FileId>> {
|
||||
let mut writer = Writer {
|
||||
renderer: self,
|
||||
raw: Raw::None,
|
||||
|
@ -42,6 +47,7 @@ impl<'a> Renderer<'a> {
|
|||
list_tightness: vec![],
|
||||
not_first_line: false,
|
||||
ignore_next_event: false,
|
||||
diagnostics: vec![],
|
||||
};
|
||||
|
||||
for (event, range) in events {
|
||||
|
@ -49,6 +55,8 @@ impl<'a> Renderer<'a> {
|
|||
.render_event(event, range.clone(), out)
|
||||
.expect("formatting event into string should not fail");
|
||||
}
|
||||
|
||||
writer.diagnostics
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,6 +93,8 @@ struct Writer<'a> {
|
|||
list_tightness: Vec<bool>,
|
||||
not_first_line: bool,
|
||||
ignore_next_event: bool,
|
||||
|
||||
diagnostics: Vec<Diagnostic<FileId>>,
|
||||
}
|
||||
|
||||
impl<'a> Writer<'a> {
|
||||
|
@ -95,7 +105,7 @@ impl<'a> Writer<'a> {
|
|||
out: &mut String,
|
||||
) -> std::fmt::Result {
|
||||
if let Event::Start(Container::Footnote { label: _ }, ..) = e {
|
||||
self.renderer.treehouse.diagnostics.push(Diagnostic {
|
||||
self.diagnostics.push(Diagnostic {
|
||||
severity: Severity::Error,
|
||||
code: Some("djot".into()),
|
||||
message: "Djot footnotes are not supported".into(),
|
||||
|
@ -523,7 +533,7 @@ impl<'a> Writer<'a> {
|
|||
Raw::Other => {}
|
||||
},
|
||||
Event::FootnoteReference(_label) => {
|
||||
self.renderer.treehouse.diagnostics.push(Diagnostic {
|
||||
self.diagnostics.push(Diagnostic {
|
||||
severity: Severity::Error,
|
||||
code: Some("djot".into()),
|
||||
message: "Djot footnotes are unsupported".into(),
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use std::{borrow::Cow, fmt::Write};
|
||||
|
||||
use jotdown::Render;
|
||||
use pulldown_cmark::{BrokenLink, LinkType};
|
||||
use treehouse_format::pull::BranchKind;
|
||||
|
||||
|
@ -183,7 +182,7 @@ pub fn branch_to_html(
|
|||
let events: Vec<_> = jotdown::Parser::new(&final_markup)
|
||||
.into_offset_iter()
|
||||
.collect();
|
||||
djot::Renderer {
|
||||
let render_diagnostics = djot::Renderer {
|
||||
page_id: treehouse
|
||||
.tree_path(file_id)
|
||||
.expect(".tree file expected")
|
||||
|
@ -226,7 +225,7 @@ pub fn branch_to_html(
|
|||
write!(
|
||||
s,
|
||||
"<a class=\"icon icon-permalink\" href=\"/b?{}\" title=\"permalink\"></a>",
|
||||
EscapeAttribute(&branch.attributes.id)
|
||||
EscapeAttribute(&branch.named_id)
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
|
|
@ -7,13 +7,14 @@ use cli::{
|
|||
wc::wc_cli,
|
||||
Command, Paths, ProgramArgs,
|
||||
};
|
||||
use generate::regenerate_or_report_error;
|
||||
use generate::{regenerate_or_report_error, LatestRevision};
|
||||
use log::{error, info, warn};
|
||||
|
||||
mod cli;
|
||||
mod config;
|
||||
mod fun;
|
||||
mod generate;
|
||||
mod history;
|
||||
mod html;
|
||||
mod import_map;
|
||||
mod include_static;
|
||||
|
@ -40,16 +41,24 @@ async fn fallible_main() -> anyhow::Result<()> {
|
|||
};
|
||||
|
||||
match args.command {
|
||||
Command::Generate(_generate_args) => {
|
||||
Command::Generate(generate_args) => {
|
||||
info!("regenerating using directories: {paths:#?}");
|
||||
regenerate_or_report_error(&paths)?;
|
||||
let latest_revision = match generate_args.commits_only {
|
||||
true => LatestRevision::LatestCommit,
|
||||
false => LatestRevision::WorkingTree,
|
||||
};
|
||||
regenerate_or_report_error(&paths, latest_revision)?;
|
||||
warn!("`generate` is for debugging only and the files cannot be fully served using a static file server; use `treehouse serve` if you wish to start a treehouse server");
|
||||
}
|
||||
Command::Serve {
|
||||
generate: _,
|
||||
generate: generate_args,
|
||||
serve: serve_args,
|
||||
} => {
|
||||
let (config, treehouse) = regenerate_or_report_error(&paths)?;
|
||||
let latest_revision = match generate_args.commits_only {
|
||||
true => LatestRevision::LatestCommit,
|
||||
false => LatestRevision::WorkingTree,
|
||||
};
|
||||
let (config, treehouse) = regenerate_or_report_error(&paths, latest_revision)?;
|
||||
serve(config, treehouse, &paths, serve_args.port).await?;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,10 +10,10 @@ pub struct ErrorsEmitted;
|
|||
pub fn parse_tree_with_diagnostics(
|
||||
treehouse: &mut Treehouse,
|
||||
file_id: FileId,
|
||||
) -> Result<Roots, ErrorsEmitted> {
|
||||
) -> Result<Roots, Vec<Diagnostic<FileId>>> {
|
||||
let input = &treehouse.source(file_id).input();
|
||||
Roots::parse(&mut treehouse_format::pull::Parser { input, position: 0 }).map_err(|error| {
|
||||
treehouse.diagnostics.push(Diagnostic {
|
||||
vec![Diagnostic {
|
||||
severity: Severity::Error,
|
||||
code: Some("tree".into()),
|
||||
message: error.kind.to_string(),
|
||||
|
@ -24,8 +24,7 @@ pub fn parse_tree_with_diagnostics(
|
|||
message: String::new(),
|
||||
}],
|
||||
notes: vec![],
|
||||
});
|
||||
ErrorsEmitted
|
||||
}]
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -33,17 +32,14 @@ pub fn parse_toml_with_diagnostics(
|
|||
treehouse: &mut Treehouse,
|
||||
file_id: FileId,
|
||||
range: Range<usize>,
|
||||
) -> Result<toml_edit::Document, ErrorsEmitted> {
|
||||
) -> Result<toml_edit::Document, Vec<Diagnostic<FileId>>> {
|
||||
let input = &treehouse.source(file_id).input()[range.clone()];
|
||||
toml_edit::Document::from_str(input).map_err(|error| {
|
||||
treehouse
|
||||
.diagnostics
|
||||
.push(toml_error_to_diagnostic(TomlError {
|
||||
message: error.message().to_owned(),
|
||||
span: error.span(),
|
||||
file_id,
|
||||
input_range: range.clone(),
|
||||
}));
|
||||
ErrorsEmitted
|
||||
vec![toml_error_to_diagnostic(TomlError {
|
||||
message: error.message().to_owned(),
|
||||
span: error.span(),
|
||||
file_id,
|
||||
input_range: range.clone(),
|
||||
})]
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use std::{collections::HashMap, ops::Range};
|
||||
use std::{collections::HashMap, ops::Range, path::PathBuf};
|
||||
|
||||
use anyhow::Context;
|
||||
use codespan_reporting::{
|
||||
|
@ -6,13 +6,27 @@ use codespan_reporting::{
|
|||
files::SimpleFiles,
|
||||
term::termcolor::{ColorChoice, StandardStream},
|
||||
};
|
||||
use serde::Serialize;
|
||||
use ulid::Ulid;
|
||||
|
||||
use crate::tree::{SemaBranchId, SemaRoots, SemaTree};
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct RevisionInfo {
|
||||
pub is_latest: bool,
|
||||
pub number: usize,
|
||||
pub commit: String,
|
||||
pub commit_short: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Source {
|
||||
Tree { input: String, tree_path: String },
|
||||
Tree {
|
||||
input: String,
|
||||
tree_path: String,
|
||||
target_path: PathBuf,
|
||||
revision_info: RevisionInfo,
|
||||
},
|
||||
Other(String),
|
||||
}
|
||||
|
||||
|
@ -37,7 +51,6 @@ pub type FileId = <Files as codespan_reporting::files::Files<'static>>::FileId;
|
|||
/// Treehouse compilation context.
|
||||
pub struct Treehouse {
|
||||
pub files: Files,
|
||||
pub diagnostics: Vec<Diagnostic<FileId>>,
|
||||
|
||||
pub tree: SemaTree,
|
||||
pub branches_by_named_id: HashMap<String, SemaBranchId>,
|
||||
|
@ -52,7 +65,6 @@ impl Treehouse {
|
|||
pub fn new() -> Self {
|
||||
Self {
|
||||
files: Files::new(),
|
||||
diagnostics: vec![],
|
||||
|
||||
tree: SemaTree::default(),
|
||||
branches_by_named_id: HashMap::new(),
|
||||
|
@ -91,15 +103,11 @@ impl Treehouse {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn report_diagnostics(&self) -> anyhow::Result<()> {
|
||||
let writer = StandardStream::stderr(ColorChoice::Auto);
|
||||
let config = codespan_reporting::term::Config::default();
|
||||
for diagnostic in &self.diagnostics {
|
||||
codespan_reporting::term::emit(&mut writer.lock(), &config, &self.files, diagnostic)
|
||||
.context("could not emit diagnostic")?;
|
||||
pub fn revision_info(&self, file_id: FileId) -> Option<&RevisionInfo> {
|
||||
match self.source(file_id) {
|
||||
Source::Tree { revision_info, .. } => Some(revision_info),
|
||||
Source::Other(_) => None,
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn next_missingno(&mut self) -> Ulid {
|
||||
|
@ -107,12 +115,6 @@ impl Treehouse {
|
|||
.generate()
|
||||
.expect("just how much disk space do you have?")
|
||||
}
|
||||
|
||||
pub fn has_errors(&self) -> bool {
|
||||
self.diagnostics
|
||||
.iter()
|
||||
.any(|diagnostic| diagnostic.severity == Severity::Error)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TomlError {
|
||||
|
@ -140,3 +142,18 @@ pub fn toml_error_to_diagnostic(error: TomlError) -> Diagnostic<FileId> {
|
|||
notes: vec![],
|
||||
}
|
||||
}
|
||||
|
||||
pub fn report_diagnostics(files: &Files, diagnostics: &[Diagnostic<FileId>]) -> anyhow::Result<()> {
|
||||
let writer = StandardStream::stderr(ColorChoice::Auto);
|
||||
let config = codespan_reporting::term::Config::default();
|
||||
for diagnostic in diagnostics {
|
||||
codespan_reporting::term::emit(&mut writer.lock(), &config, files, diagnostic)
|
||||
.context("could not emit diagnostic")?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn has_errors(diagnostics: &[Diagnostic<FileId>]) -> bool {
|
||||
diagnostics.iter().any(|d| d.severity == Severity::Error)
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ use std::{
|
|||
fs::File,
|
||||
io::{self, BufReader},
|
||||
path::PathBuf,
|
||||
sync::RwLock,
|
||||
sync::{Mutex, RwLock},
|
||||
};
|
||||
|
||||
use handlebars::{Context, Handlebars, Helper, HelperDef, RenderContext, RenderError, ScopedJson};
|
||||
|
@ -18,6 +18,11 @@ pub struct StaticUrls {
|
|||
// and required you to clone it over to different threads.
|
||||
// Stuff like this is why I really want to implement my own templating engine...
|
||||
hash_cache: RwLock<HashMap<String, String>>,
|
||||
missing_files: Mutex<Vec<MissingFile>>,
|
||||
}
|
||||
|
||||
pub struct MissingFile {
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
impl StaticUrls {
|
||||
|
@ -26,6 +31,7 @@ impl StaticUrls {
|
|||
base_dir,
|
||||
base_url,
|
||||
hash_cache: RwLock::new(HashMap::new()),
|
||||
missing_files: Mutex::new(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -53,6 +59,10 @@ impl StaticUrls {
|
|||
}
|
||||
Ok(hash)
|
||||
}
|
||||
|
||||
pub fn take_missing_files(&self) -> Vec<MissingFile> {
|
||||
std::mem::take(&mut self.missing_files.lock().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl HelperDef for StaticUrls {
|
||||
|
@ -65,9 +75,12 @@ impl HelperDef for StaticUrls {
|
|||
) -> Result<ScopedJson<'reg, 'rc>, RenderError> {
|
||||
if let Some(param) = helper.param(0).and_then(|v| v.value().as_str()) {
|
||||
return Ok(ScopedJson::Derived(Value::String(
|
||||
self.get(param).map_err(|error| {
|
||||
RenderError::new(format!("cannot get asset url for {param}: {error}"))
|
||||
})?,
|
||||
self.get(param).unwrap_or_else(|_| {
|
||||
self.missing_files.lock().unwrap().push(MissingFile {
|
||||
path: param.to_owned(),
|
||||
});
|
||||
format!("{}/{}", self.base_url, param)
|
||||
}),
|
||||
)));
|
||||
}
|
||||
|
||||
|
|
|
@ -46,22 +46,24 @@ pub struct SemaRoots {
|
|||
impl SemaRoots {
|
||||
pub fn from_roots(
|
||||
treehouse: &mut Treehouse,
|
||||
diagnostics: &mut Vec<Diagnostic<FileId>>,
|
||||
config: &Config,
|
||||
file_id: FileId,
|
||||
roots: Roots,
|
||||
) -> Self {
|
||||
Self {
|
||||
attributes: Self::parse_attributes(treehouse, config, file_id, &roots),
|
||||
attributes: Self::parse_attributes(treehouse, diagnostics, config, file_id, &roots),
|
||||
branches: roots
|
||||
.branches
|
||||
.into_iter()
|
||||
.map(|branch| SemaBranch::from_branch(treehouse, file_id, branch))
|
||||
.map(|branch| SemaBranch::from_branch(treehouse, diagnostics, file_id, branch))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_attributes(
|
||||
treehouse: &mut Treehouse,
|
||||
diagnostics: &mut Vec<Diagnostic<FileId>>,
|
||||
config: &Config,
|
||||
file_id: FileId,
|
||||
roots: &Roots,
|
||||
|
@ -72,14 +74,12 @@ impl SemaRoots {
|
|||
let mut attributes = if let Some(attributes) = &roots.attributes {
|
||||
toml_edit::de::from_str(&source.input()[attributes.data.clone()]).unwrap_or_else(
|
||||
|error| {
|
||||
treehouse
|
||||
.diagnostics
|
||||
.push(toml_error_to_diagnostic(TomlError {
|
||||
message: error.message().to_owned(),
|
||||
span: error.span(),
|
||||
file_id,
|
||||
input_range: attributes.data.clone(),
|
||||
}));
|
||||
diagnostics.push(toml_error_to_diagnostic(TomlError {
|
||||
message: error.message().to_owned(),
|
||||
span: error.span(),
|
||||
file_id,
|
||||
input_range: attributes.data.clone(),
|
||||
}));
|
||||
successfully_parsed = false;
|
||||
RootAttributes::default()
|
||||
},
|
||||
|
@ -98,7 +98,7 @@ impl SemaRoots {
|
|||
|
||||
if let Some(thumbnail) = &attributes.thumbnail {
|
||||
if thumbnail.alt.is_none() {
|
||||
treehouse.diagnostics.push(Diagnostic {
|
||||
diagnostics.push(Diagnostic {
|
||||
severity: Severity::Warning,
|
||||
code: Some("sema".into()),
|
||||
message: "thumbnail without alt text".into(),
|
||||
|
@ -116,7 +116,7 @@ impl SemaRoots {
|
|||
}
|
||||
|
||||
if !config.pics.contains_key(&thumbnail.id) {
|
||||
treehouse.diagnostics.push(Diagnostic {
|
||||
diagnostics.push(Diagnostic {
|
||||
severity: Severity::Warning,
|
||||
code: Some("sema".into()),
|
||||
message: format!(
|
||||
|
@ -149,20 +149,30 @@ pub struct SemaBranch {
|
|||
pub content: Range<usize>,
|
||||
|
||||
pub html_id: String,
|
||||
pub named_id: String,
|
||||
pub attributes: Attributes,
|
||||
pub children: Vec<SemaBranchId>,
|
||||
}
|
||||
|
||||
impl SemaBranch {
|
||||
pub fn from_branch(treehouse: &mut Treehouse, file_id: FileId, branch: Branch) -> SemaBranchId {
|
||||
let attributes = Self::parse_attributes(treehouse, file_id, &branch);
|
||||
pub fn from_branch(
|
||||
treehouse: &mut Treehouse,
|
||||
diagnostics: &mut Vec<Diagnostic<FileId>>,
|
||||
file_id: FileId,
|
||||
branch: Branch,
|
||||
) -> SemaBranchId {
|
||||
let attributes = Self::parse_attributes(treehouse, diagnostics, file_id, &branch);
|
||||
|
||||
let named_id = attributes.id.clone();
|
||||
let revision_info = treehouse
|
||||
.revision_info(file_id)
|
||||
.expect(".tree files must have Tree-type sources");
|
||||
let named_id = match revision_info.is_latest {
|
||||
true => attributes.id.to_owned(),
|
||||
false => format!("{}@{}", attributes.id, revision_info.commit_short),
|
||||
};
|
||||
let html_id = format!(
|
||||
"{}:{}",
|
||||
treehouse
|
||||
.tree_path(file_id)
|
||||
.expect("file should have a tree path"),
|
||||
treehouse.tree_path(file_id).unwrap(),
|
||||
attributes.id
|
||||
);
|
||||
|
||||
|
@ -175,11 +185,12 @@ impl SemaBranch {
|
|||
kind_span: branch.kind_span,
|
||||
content: branch.content,
|
||||
html_id,
|
||||
named_id: named_id.clone(),
|
||||
attributes,
|
||||
children: branch
|
||||
.children
|
||||
.into_iter()
|
||||
.map(|child| Self::from_branch(treehouse, file_id, child))
|
||||
.map(|child| Self::from_branch(treehouse, diagnostics, file_id, child))
|
||||
.collect(),
|
||||
};
|
||||
let new_branch_id = treehouse.tree.add_branch(branch);
|
||||
|
@ -191,7 +202,7 @@ impl SemaBranch {
|
|||
let new_branch = treehouse.tree.branch(new_branch_id);
|
||||
let old_branch = treehouse.tree.branch(old_branch_id);
|
||||
|
||||
treehouse.diagnostics.push(
|
||||
diagnostics.push(
|
||||
Diagnostic::warning()
|
||||
.with_code("sema")
|
||||
.with_message(format!("two branches share the same id `{}`", named_id))
|
||||
|
@ -220,7 +231,7 @@ impl SemaBranch {
|
|||
let new_branch = treehouse.tree.branch(new_branch_id);
|
||||
let old_branch = treehouse.tree.branch(old_branch_id);
|
||||
|
||||
treehouse.diagnostics.push(
|
||||
diagnostics.push(
|
||||
Diagnostic::warning()
|
||||
.with_code("sema")
|
||||
.with_message(format!(
|
||||
|
@ -247,21 +258,24 @@ impl SemaBranch {
|
|||
new_branch_id
|
||||
}
|
||||
|
||||
fn parse_attributes(treehouse: &mut Treehouse, file_id: FileId, branch: &Branch) -> Attributes {
|
||||
fn parse_attributes(
|
||||
treehouse: &mut Treehouse,
|
||||
diagnostics: &mut Vec<Diagnostic<FileId>>,
|
||||
file_id: FileId,
|
||||
branch: &Branch,
|
||||
) -> Attributes {
|
||||
let source = treehouse.source(file_id);
|
||||
|
||||
let mut successfully_parsed = true;
|
||||
let mut attributes = if let Some(attributes) = &branch.attributes {
|
||||
toml_edit::de::from_str(&source.input()[attributes.data.clone()]).unwrap_or_else(
|
||||
|error| {
|
||||
treehouse
|
||||
.diagnostics
|
||||
.push(toml_error_to_diagnostic(TomlError {
|
||||
message: error.message().to_owned(),
|
||||
span: error.span(),
|
||||
file_id,
|
||||
input_range: attributes.data.clone(),
|
||||
}));
|
||||
diagnostics.push(toml_error_to_diagnostic(TomlError {
|
||||
message: error.message().to_owned(),
|
||||
span: error.span(),
|
||||
file_id,
|
||||
input_range: attributes.data.clone(),
|
||||
}));
|
||||
successfully_parsed = false;
|
||||
Attributes::default()
|
||||
},
|
||||
|
@ -282,7 +296,7 @@ impl SemaBranch {
|
|||
// Check that every block has an ID.
|
||||
if attributes.id.is_empty() {
|
||||
attributes.id = format!("treehouse-missingno-{}", treehouse.next_missingno());
|
||||
treehouse.diagnostics.push(Diagnostic {
|
||||
diagnostics.push(Diagnostic {
|
||||
severity: Severity::Warning,
|
||||
code: Some("attr".into()),
|
||||
message: "branch does not have an `id` attribute".into(),
|
||||
|
@ -305,7 +319,7 @@ impl SemaBranch {
|
|||
// Check that link-type blocks are `+`-type to facilitate lazy loading.
|
||||
if let Content::Link(_) = &attributes.content {
|
||||
if branch.kind == BranchKind::Expanded {
|
||||
treehouse.diagnostics.push(Diagnostic {
|
||||
diagnostics.push(Diagnostic {
|
||||
severity: Severity::Warning,
|
||||
code: Some("attr".into()),
|
||||
message: "`content.link` branch is expanded by default".into(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue