Skip to content

Commit

Permalink
refactor: add render_name module for handling different patterns name…
Browse files Browse the repository at this point in the history
…s in markdown render
  • Loading branch information
Kremilly committed Aug 18, 2024
1 parent cc48b8b commit 0e786a2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 57 deletions.
1 change: 1 addition & 0 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod render;
pub mod render_io;
pub mod render_name;

// Helpers
pub mod render_inject;
Expand Down
62 changes: 62 additions & 0 deletions src/render/render_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::{
consts::uris::Uris,
utils::domains::Domains,
};

pub struct RenderName {
pub domain: String,
pub url_slices: Vec<String>,
}

impl RenderName {

pub fn new(url: &str) -> Self {
Self {
domain: Domains::get(url),
url_slices: url.split("/").map(|s| s.to_string()).collect(),
}
}

fn github_repo_name(&self) -> String {
let segments = &self.url_slices;
let repo_name = &segments[segments.len() - 3];
repo_name.to_string()
}

fn gitlab_repo_name(&self) -> String {
let segments = &self.url_slices;
let repo_name = &segments[segments.len() - 5];
repo_name.to_string()
}

fn bitbucket_repo_name(&self) -> String {
let segments = &self.url_slices;
let repo_name = &segments[segments.len() - 4];
repo_name.to_string()
}

fn codeberg_repo_name(&self) -> String {
let segments = &self.url_slices;
let repo_name = &segments[segments.len() - 5];
repo_name.to_string()
}

fn generic(&self) -> String {
if let Some(last_part) = self.url_slices.last() {
last_part.to_string()
} else {
String::new()
}
}

pub fn get_final_name(&self) -> String {
match &self.domain {
domain if domain == Uris::PROVIDERS_DOMAINS[2] => self.github_repo_name(),
domain if domain == Uris::PROVIDERS_DOMAINS[3] => self.gitlab_repo_name(),
domain if domain == Uris::PROVIDERS_DOMAINS[4] => self.bitbucket_repo_name(),
domain if domain == Uris::PROVIDERS_DOMAINS[5] => self.codeberg_repo_name(),
_ => self.generic(),
}
}

}
4 changes: 2 additions & 2 deletions src/system/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ use crate::{

render::{
render_io::RenderIO,
render_name::RenderName,
render_inject::RenderInject,
render_extras::RenderExtras,
},

utils::{
remote::Remote,
file::FileUtils,
filename::FileName,
},
};

Expand Down Expand Up @@ -91,7 +91,7 @@ impl Markdown {
let html_content = Self::render(url).await?;
let content = RenderInject::html_content(contents, html_content).await?;

let original_name = FileName::get_final_name(url);
let original_name = RenderName::new(url).get_final_name();
let new_filename = FileUtils::replace_extension(&original_name, "pdf");
let output_path = FileUtils::get_output_path(&path, &new_filename);
let output_path_str = format!("{}{}", &path, &new_filename);
Expand Down
54 changes: 0 additions & 54 deletions src/utils/filename.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ pub mod file;
pub mod remote;
pub mod base64;
pub mod domains;
pub mod filename;
pub mod validation;

0 comments on commit 0e786a2

Please sign in to comment.