[Web] Improve code generation

master
Araozu 2023-03-28 18:55:42 -05:00
parent 761de27edc
commit 3197d4ed05
1 changed files with 18 additions and 16 deletions

View File

@ -2,9 +2,8 @@ use clap::Parser;
use markdown::to_html; use markdown::to_html;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::Write;
use std::str;
use std::{ use std::{
fs::{self}, fs,
path::Path, path::Path,
}; };
@ -26,21 +25,21 @@ fn main() {
let output_folder = Path::new(&args.output); let output_folder = Path::new(&args.output);
if input_folder.is_dir() && output_folder.is_dir() { if input_folder.is_dir() && output_folder.is_dir() {
process_folder(&input_folder, input_folder, output_folder, true); process_folder(&input_folder, input_folder, output_folder);
} else { } else {
eprint!("Input folder is not a valid path to a folder") eprint!("Input folder is not a valid path to a folder")
} }
} }
fn process_folder(path: &Path, input_folder: &Path, output_folder: &Path, is_top_level: bool) { fn process_folder(path: &Path, input_folder: &Path, output_folder: &Path) {
for entry in path.read_dir().unwrap() { for entry in path.read_dir().unwrap() {
match entry { match entry {
Ok(entry) if entry.path().is_dir() => { Ok(entry) if entry.path().is_dir() => {
let path = entry.path(); let path = entry.path();
match ensure_folder_exists(&entry.path(), input_folder, output_folder, is_top_level) { match ensure_folder_exists(&entry.path(), input_folder, output_folder) {
Ok(_) => { Ok(_) => {
process_folder(&path, input_folder, output_folder, false); process_folder(&path, input_folder, output_folder);
} }
Err(reason) => panic!("{}", reason), Err(reason) => panic!("{}", reason),
} }
@ -53,7 +52,7 @@ fn process_folder(path: &Path, input_folder: &Path, output_folder: &Path, is_top
} }
} }
fn ensure_folder_exists(folder: &Path, input_folder: &Path, output_folder: &Path, is_top_level: bool) -> Result<(), String> { fn ensure_folder_exists(folder: &Path, input_folder: &Path, output_folder: &Path) -> Result<(), String> {
// /home/fernando/misti/docs/markdown // /home/fernando/misti/docs/markdown
let input_folder = input_folder.canonicalize().unwrap(); let input_folder = input_folder.canonicalize().unwrap();
@ -91,29 +90,32 @@ fn process_markdown(file: &Path, input_folder: &Path, output_folder: &Path) -> R
let input_folder = input_folder.canonicalize().unwrap(); let input_folder = input_folder.canonicalize().unwrap();
// /home/fernando/misti/docs/markdown/en/docs/latest/index.md // /home/fernando/misti/docs/markdown/en/docs/latest/index.md
let full_input_path = file.canonicalize().unwrap(); let input_file = file.canonicalize().unwrap();
// /home/fernando/misti/docs/static // /home/fernando/misti/docs/static
let output_folder = output_folder.canonicalize().unwrap(); let output_folder = output_folder.canonicalize().unwrap();
// en/docs/latests/index.md // en/docs/latests/index.md
let relative_file_path = full_input_path.strip_prefix(input_folder).unwrap(); let relative_input_file = input_file.strip_prefix(input_folder).unwrap();
let mut full_output_path = output_folder.clone(); let mut output_file = output_folder.clone();
full_output_path.push(relative_file_path); output_file.push(relative_input_file);
output_file.set_extension("html");
let file_content_bytes = fs::read(&full_input_path).unwrap(); let file_content_bytes = fs::read(&input_file).unwrap();
let markdown_text = String::from_utf8(file_content_bytes).unwrap(); let markdown_text = String::from_utf8(file_content_bytes).unwrap();
let html_text = to_html(markdown_text.as_str()); let html_text = to_html(markdown_text.as_str());
// Write the HTML to disk // Write the HTML to disk
println!("Compiling: from -> to\n{:?}\n{:?}", full_input_path, full_output_path); println!("Compiling: from -> to\n{:?}\n{:?}\n", input_file, output_file);
let mut file = File::create(&full_output_path).unwrap(); let _ = File::create(&output_file)
let _ = file.write_all(html_text.as_bytes()); .unwrap()
.write_all(html_text.as_bytes())
.unwrap();
Err(String::from("Not implemented")) Ok(())
} }