better error handlers in rust and some UI improvement

This commit is contained in:
2025-09-23 18:21:23 +02:00
parent 468717038e
commit 9d0bbf0f88
3 changed files with 70 additions and 25 deletions

View File

@@ -126,7 +126,7 @@ def render_markdown(md_path: Path):
</head>
<body style="display:flex; flex-direction:column; min-height:100%; margin:0;">
<main class="container" style="flex:1;">
<h1 onclick="window.location.href='/'" style="cursor:pointer; display:flex; align-items:center; gap:8px; font-size:1.5em; margin:0;">
<h1 onclick="window.location.href=window.location.origin" style="cursor:pointer; display:flex; align-items:center; gap:8px; font-size:1.5em; margin:0;">
<img src="../css/icons/back.webp" width="32" height="32" alt="Back" style="display:block;" />
{title}
</h1>
@@ -295,7 +295,7 @@ if __name__ == "__main__":
# Update threshold if specified
RUST_PARSER_THRESHOLD = args.rust_threshold
Logger.log_obfuscation_info(f"Obfuscation is {'enabled' if obfuscate else 'disabled'}", obfuscate)
Logger.log_obfuscation_info(f"Obfuscation is {obfuscate}",obfuscate)
initial_scan(MARKDOWN_DIR)
event_handler = Handler()

View File

@@ -1,37 +1,82 @@
use std::{env, fs, process};
use pulldown_cmark::{Parser, Options, html};
use std::path::PathBuf;
fn main() {
// Get the file path from CLI args
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
eprintln!("Usage: {} <markdown_file>", args[0]);
process::exit(1);
#[derive(Debug)]
enum MarkdownError {
IoError(std::io::Error),
InvalidArgs,
InvalidFileType,
}
impl From<std::io::Error> for MarkdownError {
fn from(err: std::io::Error) -> Self {
MarkdownError::IoError(err)
}
}
let path = &args[1];
// Read file contents
let markdown = match fs::read_to_string(path) {
Ok(content) => content,
Err(e) => {
eprintln!("Error reading {}: {}", path, e);
process::exit(1);
impl std::fmt::Display for MarkdownError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MarkdownError::IoError(e) => write!(f, "IO error: {}", e),
MarkdownError::InvalidArgs => write!(f, "Invalid arguments provided"),
MarkdownError::InvalidFileType => write!(f, "File extension should be .md or .markdown"),
}
};
}
}
// Configure parser with GitHub-flavored options
fn markdown_to_html(markdown: &str, body_top_content: &str) -> String {
let mut options = Options::empty();
options.insert(Options::ENABLE_TABLES);
options.insert(Options::ENABLE_FOOTNOTES);
options.insert(Options::ENABLE_STRIKETHROUGH);
options.insert(Options::ENABLE_TASKLISTS);
// Parse and render to HTML
let parser = Parser::new_ext(&markdown, options);
let parser = Parser::new_ext(markdown, options);
let mut html_output = String::new();
html::push_html(&mut html_output, parser);
// Print only the body content (no <html>/<head>)
println!("{} <!-- this was generated from rust with pulldown_cmark ; REASON: Large File -->", html_output);
// If body_top_content is not empty, wrap the output
if !body_top_content.trim().is_empty() {
format!("{}\n{}", body_top_content, html_output)
} else {
html_output
}
}
fn validate_file_type(path: &PathBuf) -> Result<(), MarkdownError> {
if let Some(ext) = path.extension() {
if ext == "md" || ext == "markdown" {
Ok(())
} else {
Err(MarkdownError::InvalidFileType)
}
} else {
Err(MarkdownError::InvalidFileType)
}
}
fn run() -> Result<String, MarkdownError> {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
return Err(MarkdownError::InvalidArgs);
}
let path = PathBuf::from(&args[1]);
validate_file_type(&path)?;
let markdown = fs::read_to_string(&path)?;
let top_content = "<!-- This was automatically parsed with fastmd (Rust Markdown Parser) -->";
Ok(markdown_to_html(&markdown, top_content))
}
fn main() {
match run() {
Ok(html_output) => {
println!("{}", html_output);
}
Err(e) => {
eprintln!("Error: {}", e);
process::exit(1);
}
}
}

View File

@@ -10,7 +10,7 @@
#available { padding-left: 40px;}
ul { padding-left: 100px;}
#nojs { display: inline-block;color: red;transition: transform 0.7s cubic-bezier(0.215, 0.610, 0.355, 1); }
#nojs:hover { transform: skewX(-12deg);}
/*#nojs:hover { transform: skewX(-12deg);}*/
#nonenormalul { list-style: disc inside; margin: 1em 0; padding-left: 40px; background: none; }
#nonenormalul li { list-style: inherit; margin: 0; padding: 0; background: none; transition: font-size 0.5s cubic-bezier(0.075, 0.82, 0.165, 1); }
#nonenormalul li:hover { font-size: larger; }
@@ -28,6 +28,7 @@
<ul id="nonenormalul">
<li>It strips the .HTML ending from each file you see in the list below</li>
<li>It isnt necessary, but visually tweaks the page.</li>
<li>It enables the "Back" Function for Headers</li>
</ul>
</p>
</noscript>
@@ -55,6 +56,5 @@
</p>
</footer>
</body>
</html>