dokugile/src/main.rs

273 lines
11 KiB
Rust
Raw Normal View History

2024-06-06 19:53:10 +02:00
use std::io;
use std::fs;
use std::path::Path;
use std::env;
use walkdir::WalkDir;
use std::process::Command;
use std::ffi::OsStr;
use colorize::AnsiColor;
use dirs;
use pandoc::PandocError;
use dokugile::template;
2024-06-06 20:04:00 +02:00
use std::env;
use rust_i18n::t;
rust_i18n::i18n!("locales");
2024-06-06 19:53:10 +02:00
struct Project {
wiki_path: String,
doc_path: String,
doc_title: String,
output_dir: String,
}
impl Project {
fn new(wiki_path: String, doc_path: String, doc_title:String, output_dir: String) -> Project {
Project {
wiki_path,
doc_path,
doc_title,
output_dir,
}
}
}
fn main() {
let mut wiki_path = String::new();
let mut doc_path = String::new();
let mut doc_title = String::new();
let mut output_dir = String::new();
let mut attempts: i8 = 0;
while !Path::new(&doc_path).exists() || doc_title.trim().is_empty(){
doc_path = String::new();
let mut input = String::new();
println!("Proiektuaren izena: ");
io::stdin().read_line(&mut input).expect("Failed to read line");
let root = env::var("HOME").unwrap().to_string()+"/";
let user_documents_path = dirs::document_dir().unwrap().display().to_string();
let user_doc_dir = user_documents_path.split("/").collect::<Vec<_>>().last().unwrap().to_string();
while !Path::new(&wiki_path).exists() {
let mut project_dir = String::new();
println!("Proiektuaren kokapena:[{}/Wiki] ", &user_doc_dir);
io::stdin().read_line(&mut project_dir).expect("Failed to read line");
if project_dir.trim().is_empty() { project_dir = String::from(user_doc_dir.clone()+"/Wiki") };
wiki_path = root.clone()+project_dir.trim();
if !Path::new(&wiki_path).exists(){
let mut ans = String::new();
println!("{} dokumentuentzako kokapen berria sortu nahi duzu?(y/n)[Y]", &project_dir.trim().to_owned().green().bold());
io::stdin().read_line(&mut ans).expect("Failed to read line");
if ans.trim().is_empty() { ans = String::from("Y") };
match ans.trim() {
"y" | "yes" | "Y" | "B" | "Bai" => { //let _ = fs::create_dir(&wiki_path);
let _ = mkdir(&wiki_path);
println!("{} kokapena sortu da.", &project_dir.trim().to_owned().green().bold());
},
_ => continue,
};
}
}
if input.trim().is_empty(){
attempts += 1;
}
if attempts == 3 {
break
};
doc_title = input.to_string();
output_dir = input.trim_end().to_owned()+"_html";
doc_path.push_str(&wiki_path);
doc_path.push_str("/");
doc_path.push_str(&doc_title.trim_end());
//doc_path.push_str(&capitalize_first_letter(&doc_title.trim_end()));
let res_path = wiki_path.clone()+"/res";
if !Path::new(&doc_path).exists() || !Path::new(&res_path).exists(){
let mut ans = String::new();
if !Path::new(&doc_path).exists() {
println!("{} proiektua ez da aurkitu. Sortu egin nahi duzu?(y/n)[Y]", &input.trim().to_owned().green().bold());
io::stdin().read_line(&mut ans).expect("Failed to read line");
}
if ans.trim().is_empty() { ans = String::from("Y") };
match ans.trim() {
"y" | "yes" | "Y" | "B" | "Bai" => {
let _ = mkdir(&doc_path);
// create resources
for entry in WalkDir::new("assets").into_iter().filter_map(|e| e.ok()) {
let input_entry = String::from(entry.path().strip_prefix("assets").expect("Path not found").display().to_string());
let subdirs = wiki_path.to_string()+"/"+&input_entry;
let images = wiki_path.to_string()+"/"+&doc_title.trim()+"/"+&input_entry;
if entry.path().is_dir(){
match Path::new(&input_entry).to_str() {
Some("images") => { //create images directory
let _ = mkdir(&images);
},
Some(..) => { //create resources and its subdirectories
let _ = mkdir(&subdirs);
},
_ => continue
};
}else if entry.path().is_file(){
let source = String::from(entry.path().display().to_string());
match Path::new(&input_entry).to_str() {
Some(d) if d.contains("images/")=>{ //copy sample-image to images folder
let _ = copy(&source, &images);
},
Some(..) => { //copy files from all other directories
let _ = copy(&source, &subdirs);
},
_ => continue
};
}
}
println!("{} proiektua sortu da. Orain index.md fitxategia editatu eta edukiak \
sor ditzakezu", &input.trim().to_owned().green().bold());
},
_ => continue,
};
}
let index = doc_path.to_string()+"/index.md";
if !Path::new(&index).exists(){
let _ = touch(&index, &template());
};
}
let res = processing(Project::new(wiki_path.clone(), doc_path.clone(), doc_title, output_dir));
match res {
Err(e) => println!("The operation failed with error: {}", e),
Ok(e) => println!("{}", e)
};
}
fn mkdir(dir: &String) -> std::io::Result<()>{
let _ = fs::create_dir(dir)?;
Ok(())
}
fn touch(file: &String, content: &String) -> std::io::Result<()>{
fs::write(file, content)?;
Ok(())
}
fn copy(source: &String, dest: &String) -> std::io::Result<()> {
fs::copy(source, dest)?;
Ok(())
}
/*
fn capitalize_first_letter(s: &str) -> String {
s[0..1].to_uppercase() + &s[1..]
}
*/
fn processing(project: Project) -> Result<String, String>{
let mut md_count :i32 = 0;
let mut error = String::new();
for entry in WalkDir::new(&project.doc_path).into_iter().filter_map(|e| e.ok()) {
if entry.path().is_dir(){
let input_entry = String::from(entry.path().display().to_string());
let doc_title: String = "/".to_string()+&project.doc_title.trim_end();
let output_dir: String = "/".to_string()+ &project.output_dir.trim_end();
//replace project-directory to project-directory_html(e.g. "Test" to "Test_html")
let new_entry = input_entry.replace(&doc_title, &output_dir);
//create output _html directory and subdirectories except images dir
match &entry.path().file_name() {
Some(p) if *p != "images"=> {
let _ = mkdir(&new_entry);
},
_ => continue,
};
}else if entry.path().is_file(){
if let Some(extension) = entry.path().extension().and_then(OsStr::to_str) {
//filter md files
match extension {
"md" => {
//md_count += 1;
println!("found: {}", entry.path().display());
let conv = md_to_html(entry.path(), &project.doc_title, &project.output_dir, &project.wiki_path);
match conv {
Ok(()) => md_count += 1,
Err(e) => { //println!("{}", e);
error = e.to_string();
break;
},
};
},
_ => (),
};
};
};
};
if error.is_empty() {
if md_count == 0 {
println!("No markdown files found!");
println!("Pleade edit first your documents in markdown format and come back to convert them to html. Bye!");
}else{
println!("Processed {} markdown files.", md_count);
}
let mut index = project.wiki_path.to_owned()+"/"+&project.output_dir.trim_end();
index.push_str("/index.html");
sanitize(Path::new(&index));
//Ok("Success")
let link = String::from("file://".to_owned()+&index).cyan();
let message:String = String::from("Congrats! Check your documentation at.. ".to_owned()+&link);
Ok(message)
}else{
//Err(std::stringify!(error))
Err(error)
}
}
fn md_to_html(input_file: &Path, doc_title: &String, output_dir: &String, wiki_path: &String) -> Result<(), PandocError>{
let page_title: String = "pagetitle=".to_string()+*&doc_title.trim_end();
let doc_title: String = "/".to_string()+*&doc_title.trim_end();
let output_dir: String = "/".to_string()+ *&output_dir.trim_end();
let output_file = input_file.display().to_string().replace(&doc_title, &output_dir).replace(".md", ".html");
let base_path = wiki_path.to_owned()+&doc_title;
let res_path = wiki_path.to_owned()+&"/res:".to_string();
let mut pandoc = pandoc::new();
pandoc.add_input(&input_file);
pandoc.arg("resource-path", &res_path); //for css and javascript resources
pandoc.arg("resource-path", &base_path); //for image search
pandoc.arg("include-after-body", "buttons.html");
pandoc.arg("css", "css/theme.css");
pandoc.arg("css", "css/sidebar.css");
pandoc.arg("css", "css/buttons.css");
pandoc.arg("embed-resources", "true");
pandoc.arg("standalone", "true");
pandoc.arg("shift-heading-level-by", "-1");
pandoc.set_number_sections();
pandoc.set_toc();
pandoc.arg("toc-depth", "4");
pandoc.arg("metadata", &page_title);
pandoc.set_output(pandoc::OutputKind::File(output_file.into()));
//pandoc.execute().unwrap();
match pandoc.execute() {
Ok(..) => Ok(()),
Err(PandocError::PandocNotFound) => Err(PandocError::PandocNotFound),
Err(PandocError::IoErr(e)) => Err(PandocError::IoErr(e)),
Err(PandocError::Err(e)) => Err(PandocError::Err(e)),
Err(PandocError::NoInputSpecified) => Err(PandocError::NoInputSpecified),
Err(PandocError::BadUtf8Conversion(e)) => Err(PandocError::BadUtf8Conversion(e)),
Err(PandocError::NoOutputSpecified) => Err(PandocError::NoOutputSpecified)
}
}
fn sanitize(index: &Path){
if index.exists(){
//convert page links from ".md" to ".html" in the index.html file
Command::new("sed")
.args(["-i", r"-e s/\.md/\.html/g", &index.display().to_string()])
.status()
.expect("sed command failed start");
}else{
println!("Please consider renaming your main page to index.md and restart the document conversion again. See you later!")
};
}