wayver's git archive


an obsidian renderer
git clone https://git.wayver.dev/sable

sable-renderer/src/lib.rs@2b84405277e54ab809e328cf0237374d4b4dbd0c

raw
Date Commit Message Author Files + -
2026-02-23 01:55 initial mvp wayverd 139 17808 0
...

1#![deny(rust_2018_idioms, unsafe_code)]
2#![warn(
3    absolute_paths_not_starting_with_crate,
4    ambiguous_associated_items,
5    anonymous_parameters,
6    arithmetic_overflow,
7    array_into_iter,
8    asm_sub_register,
9    bad_asm_style,
10    bindings_with_variant_name,
11    break_with_label_and_loop,
12    clashing_extern_declarations,
13    coherence_leak_check,
14    conflicting_repr_hints,
15    confusable_idents,
16    const_evaluatable_unchecked,
17    const_item_mutation,
18    dangling_pointers_from_temporaries,
19    dead_code,
20    deprecated_in_future,
21    deprecated_where_clause_location,
22    deprecated,
23    deref_into_dyn_supertrait,
24    deref_nullptr,
25    drop_bounds,
26    duplicate_macro_attributes,
27    dyn_drop,
28    ellipsis_inclusive_range_patterns,
29    enum_intrinsics_non_enums,
30    explicit_outlives_requirements,
31    exported_private_dependencies,
32    forbidden_lint_groups,
33    function_item_references,
34    future_incompatible,
35    ill_formed_attribute_input,
36    improper_ctypes_definitions,
37    improper_ctypes,
38    incomplete_features,
39    incomplete_include,
40    ineffective_unstable_trait_impl,
41    inline_no_sanitize,
42    invalid_atomic_ordering,
43    invalid_doc_attributes,
44    invalid_type_param_default,
45    invalid_value,
46    irrefutable_let_patterns,
47    keyword_idents,
48    large_assignments,
49    late_bound_lifetime_arguments,
50    legacy_derive_helpers,
51    macro_expanded_macro_exports_accessed_by_absolute_paths,
52    meta_variable_misuse,
53    missing_abi,
54    missing_copy_implementations,
55    missing_debug_implementations,
56    missing_docs,
57    mixed_script_confusables,
58    mutable_transmutes,
59    named_arguments_used_positionally,
60    named_asm_labels,
61    no_mangle_const_items,
62    no_mangle_generic_items,
63    non_ascii_idents,
64    non_camel_case_types,
65    non_fmt_panics,
66    non_shorthand_field_patterns,
67    non_snake_case,
68    non_upper_case_globals,
69    nonstandard_style,
70    noop_method_call,
71    overflowing_literals,
72    overlapping_range_endpoints,
73    path_statements,
74    patterns_in_fns_without_body,
75    proc_macro_derive_resolution_fallback,
76    pub_use_of_private_extern_crate,
77    redundant_semicolons,
78    repr_transparent_external_private_fields,
79    rust_2021_incompatible_closure_captures,
80    rust_2021_incompatible_or_patterns,
81    rust_2021_prefixes_incompatible_syntax,
82    rust_2021_prelude_collisions,
83    semicolon_in_expressions_from_macros,
84    soft_unstable,
85    stable_features,
86    text_direction_codepoint_in_comment,
87    text_direction_codepoint_in_literal,
88    trivial_bounds,
89    trivial_casts,
90    trivial_numeric_casts,
91    type_alias_bounds,
92    tyvar_behind_raw_pointer,
93    uncommon_codepoints,
94    unconditional_panic,
95    unconditional_recursion,
96    unexpected_cfgs,
97    uninhabited_static,
98    unknown_crate_types,
99    unnameable_test_items,
100    unreachable_code,
101    unreachable_patterns,
102    unreachable_pub,
103    unsafe_op_in_unsafe_fn,
104    unstable_features,
105    unstable_name_collisions,
106    unused_allocation,
107    unused_assignments,
108    unused_attributes,
109    unused_braces,
110    unused_comparisons,
111    unused_crate_dependencies,
112    unused_doc_comments,
113    unused_extern_crates,
114    unused_features,
115    unused_import_braces,
116    unused_imports,
117    unused_labels,
118    unused_lifetimes,
119    unused_macro_rules,
120    unused_macros,
121    unused_must_use,
122    unused_mut,
123    unused_parens,
124    unused_qualifications,
125    unused_unsafe,
126    unused_variables,
127    useless_deprecated,
128    while_true
129)]
130#![warn(
131    clippy::all,
132    clippy::await_holding_lock,
133    clippy::char_lit_as_u8,
134    clippy::checked_conversions,
135    clippy::cognitive_complexity,
136    clippy::dbg_macro,
137    clippy::debug_assert_with_mut_call,
138    clippy::disallowed_script_idents,
139    clippy::doc_link_with_quotes,
140    clippy::doc_markdown,
141    clippy::empty_enum,
142    clippy::empty_line_after_outer_attr,
143    clippy::empty_structs_with_brackets,
144    clippy::enum_glob_use,
145    clippy::equatable_if_let,
146    clippy::exit,
147    clippy::expl_impl_clone_on_copy,
148    clippy::explicit_deref_methods,
149    clippy::explicit_into_iter_loop,
150    clippy::fallible_impl_from,
151    clippy::filter_map_next,
152    clippy::flat_map_option,
153    clippy::float_cmp_const,
154    clippy::float_cmp,
155    clippy::float_equality_without_abs,
156    clippy::fn_params_excessive_bools,
157    clippy::fn_to_numeric_cast_any,
158    clippy::from_iter_instead_of_collect,
159    clippy::if_let_mutex,
160    clippy::implicit_clone,
161    clippy::imprecise_flops,
162    clippy::index_refutable_slice,
163    clippy::inefficient_to_string,
164    clippy::invalid_upcast_comparisons,
165    clippy::iter_not_returning_iterator,
166    clippy::large_digit_groups,
167    clippy::large_stack_arrays,
168    clippy::large_types_passed_by_value,
169    clippy::let_unit_value,
170    clippy::linkedlist,
171    clippy::lossy_float_literal,
172    clippy::macro_use_imports,
173    clippy::manual_ok_or,
174    clippy::map_err_ignore,
175    clippy::map_flatten,
176    clippy::map_unwrap_or,
177    clippy::match_same_arms,
178    clippy::match_wild_err_arm,
179    clippy::match_wildcard_for_single_variants,
180    clippy::mem_forget,
181    clippy::missing_const_for_fn,
182    clippy::missing_enforced_import_renames,
183    clippy::missing_errors_doc,
184    clippy::missing_panics_doc,
185    clippy::mut_mut,
186    clippy::mutex_integer,
187    clippy::needless_borrow,
188    clippy::needless_continue,
189    clippy::needless_for_each,
190    clippy::needless_pass_by_value,
191    clippy::negative_feature_names,
192    clippy::nonstandard_macro_braces,
193    clippy::nursery,
194    clippy::option_if_let_else,
195    clippy::option_option,
196    clippy::path_buf_push_overwrite,
197    clippy::pedantic,
198    // clippy::print_stderr,
199    clippy::print_stdout,
200    clippy::ptr_as_ptr,
201    clippy::rc_mutex,
202    clippy::ref_option_ref,
203    clippy::rest_pat_in_fully_bound_structs,
204    clippy::same_functions_in_if_condition,
205    clippy::semicolon_if_nothing_returned,
206    clippy::shadow_unrelated,
207    clippy::similar_names,
208    clippy::single_match_else,
209    clippy::string_add_assign,
210    clippy::string_add,
211    clippy::string_lit_as_bytes,
212    clippy::suspicious_operation_groupings,
213    clippy::todo,
214    clippy::trailing_empty_array,
215    clippy::trait_duplication_in_bounds,
216    clippy::trivially_copy_pass_by_ref,
217    clippy::unimplemented,
218    clippy::unnecessary_wraps,
219    clippy::unnested_or_patterns,
220    clippy::unseparated_literal_suffix,
221    clippy::unused_self,
222    // clippy::use_debug,
223    clippy::use_self,
224    clippy::used_underscore_binding,
225    clippy::useless_let_if_seq,
226    clippy::useless_transmute,
227    clippy::verbose_file_reads,
228    clippy::wildcard_dependencies,
229    clippy::wildcard_imports,
230    clippy::zero_sized_map_values
231)]
232
233//! A simple Tera + Markdown renderer for Obsidian vaults.
234
235pub mod templates;
236
237mod base;
238mod canvas;
239mod html;
240mod note;
241mod page;
242
243use std::fmt::Write as _;
244
245use sable_core::{MetaInfo, config::Config};
246use sable_vault::{File, SharedVault};
247
248use crate::templates::{Templates, TemplatesError};
249
250#[derive(Debug, miette::Diagnostic, thiserror::Error)]
251pub enum RendererError {
252    #[error("failed to create parent directories")]
253    CreateDir(#[source] std::io::Error),
254    #[error("failed to render template")]
255    RenderTemplate(#[source] TemplatesError),
256    #[error("failed to write rendered template")]
257    WriteTemplate(#[source] std::io::Error),
258}
259
260#[derive(Debug)]
261pub struct Renderer {
262    config: Config,
263    meta_info: &'static MetaInfo,
264    vault: SharedVault,
265
266    templates: Templates,
267}
268
269impl Renderer {
270    pub const fn new(
271        config: Config,
272        meta_info: &'static MetaInfo,
273        vault: SharedVault,
274        templates: Templates,
275    ) -> Self {
276        Self {
277            config,
278            meta_info,
279            vault,
280
281            templates,
282        }
283    }
284
285    pub fn render_all(&self) {
286        self.render_bases();
287        self.render_canvases();
288        self.render_notes();
289        self.render_pages();
290        self.render_htmls();
291    }
292
293    /// Copy non-'note' assets to the destination folder.
294    pub fn copy_assets(&self) {
295        let vault = self.vault.read();
296
297        for file in vault.files.values() {
298            if !file.kind.is_asset() {
299                continue;
300            }
301
302            if let Err(err) = self.copy_asset(file).into_diagnostic() {
303                tracing::error!(path=%file.path.full, "failed to copy asset");
304                eprintln!("{err:?}");
305            }
306        }
307    }
308
309    fn copy_asset(&self, file: &File) -> std::io::Result<()> {
310        let mut path = self.config.build.join(&file.path.slug);
311
312        if let Some(dir) = path.parent() {
313            std::fs::create_dir_all(dir)?;
314        }
315
316        if let Some(ext) = file.path.extension() {
317            path.add_extension(ext);
318        }
319
320        std::fs::copy(&file.path.full, &path)?;
321
322        Ok(())
323    }
324}
325
326#[derive(Debug)]
327pub(crate) struct DiagnosticError(pub(crate) Box<dyn std::error::Error + Send + Sync + 'static>);
328
329impl std::fmt::Display for DiagnosticError {
330    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
331        let msg = &self.0;
332        write!(indenter::indented(f).with_str("  "), "{msg}")
333    }
334}
335
336impl std::error::Error for DiagnosticError {
337    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
338        self.0.source()
339    }
340}
341
342impl miette::Diagnostic for DiagnosticError {}
343
344pub(crate) trait IntoDiagnostic<T, E> {
345    fn into_diagnostic(self) -> Result<T, miette::Report>;
346}
347
348impl<T, E: std::error::Error + Send + Sync + 'static> IntoDiagnostic<T, E> for Result<T, E> {
349    fn into_diagnostic(self) -> Result<T, miette::Report> {
350        self.map_err(|e| DiagnosticError(Box::new(e)).into())
351    }
352}
353
354pub(crate) fn slugify_path(mut path: &str) -> String {
355    use itertools::Itertools as _;
356
357    fn get_extension(path: &str) -> Option<&str> {
358        if !path.contains('.') {
359            return None;
360        }
361
362        if !path.contains('/') {
363            return path.split('.').rfind(|s| !s.is_empty() || *s != ".");
364        }
365
366        path.split('/')
367            .rfind(|s| !s.is_empty() || *s != "/")
368            .and_then(|s| s.split('.').rfind(|s| !s.is_empty() || *s != "."))
369    }
370
371    let ext = get_extension(path);
372
373    if let Some(ext) = &ext {
374        path = path.trim_end_matches(ext);
375        path = path.trim_end_matches('.');
376    }
377
378    let (path, has_leading_period) = if path.starts_with('.') {
379        (path.trim_start_matches('.'), true)
380    } else {
381        (path, false)
382    };
383
384    let path = path
385        .split('/')
386        .filter(|s| !(s.is_empty() || *s == "/"))
387        .map(slug::slugify)
388        .join("/");
389
390    format!(
391        "{}{}{}{}",
392        if has_leading_period { "./" } else { "" },
393        url_escape::encode_path(path.as_str()),
394        if ext.is_some() { "." } else { "" },
395        ext.map_or("", |ext| match ext {
396            "md" => "html",
397            ext => ext,
398        }),
399    )
400}
401