Allow any file extension, not just .html for the template

master
Araozu 2023-08-11 12:03:19 -05:00
parent 1da85c07f5
commit 8b4bc594e3
6 changed files with 27 additions and 19 deletions

View File

@ -7,16 +7,16 @@ All configuration is done via the `md-docs.config.yaml` file.
Values:
```yaml
{
String input # Path to the input folder
String output # Path to the output folder
String input # Path to the input folder
String output # Path to the output folder
String template # Path to the template file
String? extension = "html" # File extension of the output files
headings: {
String? h1 # Classes to add to h1 elements
String? h2 # Classes to add to h2 elements
String? h3 # Classes to add to h3 elements
String? h4 # Classes to add to h4 elements
String? h5 # Classes to add to h5 elements
}
headings: {
String? h1 # Classes to add to h1 elements
String? h2 # Classes to add to h2 elements
String? h3 # Classes to add to h3 elements
String? h4 # Classes to add to h4 elements
String? h5 # Classes to add to h5 elements
}
```

View File

@ -11,6 +11,7 @@ pub struct Config {
pub input: String,
pub output: String,
pub template: String,
pub extension: String,
pub headings: Headings,
}
@ -48,6 +49,11 @@ pub fn parse(yaml_str: &String) -> Result<Config, String> {
}
};
let file_extension = match config_yaml.get(ystr!("extension")) {
Some(Yaml::String(input)) => input.clone(),
_ => String::from("html"),
};
let input_folder = match config_yaml.get(ystr!("input")) {
Some(Yaml::String(input)) => input,
Some(_) => {
@ -132,6 +138,7 @@ pub fn parse(yaml_str: &String) -> Result<Config, String> {
input: input_folder.clone(),
output: output_folder.clone(),
template: template_file.clone(),
extension: file_extension,
headings: headings_yaml,
})
}

View File

@ -10,7 +10,7 @@ mod utils;
fn main() {
let config_file = Path::new(CONFIG_NAME);
if !config_file.is_dir() {
if !config_file.is_file() {
eprintln!("A {} file was not found. Aborting.", CONFIG_NAME);
return;
}

View File

@ -31,7 +31,7 @@ pub fn compile(config: &Config, file: &PathBuf, file_tree_html: &String) {
let mut output_file = output_folder.clone();
output_file.push(relative_input_file);
output_file.set_extension("html");
output_file.set_extension(config.extension.clone());
//
// Read MD from disk
@ -47,8 +47,8 @@ pub fn compile(config: &Config, file: &PathBuf, file_tree_html: &String) {
let sidebar_html = md_ast.generate_sidebar();
// Read template.html
let mut template_path = output_folder.clone();
template_path.push("template.html");
let template_path = Path::new(&config.template.clone()).canonicalize().unwrap();
// template_path.push("template.html");
let template_contents = fs::read(template_path).unwrap();
let template_contents = String::from_utf8(template_contents).unwrap();

View File

@ -82,7 +82,7 @@ pub fn parse_yaml(values: &Yaml) -> Node {
}
}
pub fn generate_pages_html(file_tree: &Node, current_path: &Path) -> String {
pub fn generate_pages_html(file_tree: &Node, current_path: &Path, config: &Config) -> String {
match file_tree {
Node::File(file) => {
if file.path == "index" {
@ -97,11 +97,12 @@ pub fn generate_pages_html(file_tree: &Node, current_path: &Path) -> String {
} else {
format!(
"<li class=\"my-1\">
<a class=\"inline-block rounded-md w-full hover:text-c2-primary p-1\" href=\"/{}/{}.html\">{}</a>
<a class=\"inline-block rounded-md w-full hover:text-c2-primary p-1\" href=\"/{}/{}.{}\">{}</a>
</li>",
current_path.to_str().unwrap(),
file.path,
file.name
config.extension,
file.name,
)
}
}
@ -112,7 +113,7 @@ pub fn generate_pages_html(file_tree: &Node, current_path: &Path) -> String {
let sub_nodes_html: Vec<String> = folder
.children
.iter()
.map(|n| generate_pages_html(n, &new_path))
.map(|n| generate_pages_html(n, &new_path, config))
.collect();
// This is true for the root of the YAML file

View File

@ -94,7 +94,7 @@ fn process_yaml(config: &Config, current_path: &Path) {
let yaml_folder_temp = current_path.canonicalize().unwrap();
let web_absolute_path = yaml_folder_temp.strip_prefix(input_folder).unwrap();
generate_pages_html(&file_tree, web_absolute_path)
generate_pages_html(&file_tree, web_absolute_path, config)
};
//