Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ci checks #59

Merged
merged 8 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ jobs:
- name: Checkout 🛎
uses: actions/checkout@v4

- name: check formatting
run: cargo fmt -- --check

- name: Cache Cargo dependencies
uses: Swatinem/rust-cache@v2

- name: Clippy
run: cargo clippy -- -W clippy::pedantic

- name: Build 🔧
run: cargo build --release

Expand Down
29 changes: 9 additions & 20 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ pub fn get_title<'a>(frontmatter: &'a Frontmatter, html: &'a str) -> String {
Some(Value::String(t)) => t.to_string(),
_ => html
.lines()
.filter(|line| !line.is_empty())
.next()
.find(|line| !line.is_empty())
.unwrap_or("")
.trim_start_matches("#")
.trim_start_matches('#')
.trim()
.to_string(),
}
Expand All @@ -42,9 +41,7 @@ pub fn get_slug<'a>(frontmatter: &'a Frontmatter, path: &'a Path) -> String {

let slug = path.file_stem().and_then(|stem| stem.to_str()).unwrap();
if let Some(date) = extract_date_from_filename(path) {
return slug
.replace(&format!("{}-", date.date().to_string()), "")
.to_string();
return slug.replace(&format!("{}-", date.date()), "").to_string();
}

slug.to_string()
Expand All @@ -57,11 +54,7 @@ pub fn get_tags(frontmatter: &Frontmatter) -> Vec<String> {
.map(Value::to_string)
.map(|t| t.trim_matches('"').to_string())
.collect(),
Some(Value::String(tags)) => tags
.split(",")
.map(|t| t.trim())
.map(String::from)
.collect(),
Some(Value::String(tags)) => tags.split(',').map(str::trim).map(String::from).collect(),
_ => Vec::new(),
};
tags
Expand All @@ -72,14 +65,11 @@ pub fn group_by_tags(posts: Vec<Content>) -> Vec<(String, Vec<Content>)> {
let mut tag_map: HashMap<String, Vec<Content>> = HashMap::new();

// Iterate over the posts
for post in posts.into_iter() {
for post in posts {
// For each tag in the current post
for tag in post.tags.clone() {
// Insert the tag into the map or push the post into the existing vector
tag_map
.entry(tag)
.or_insert_with(Vec::new)
.push(post.clone());
tag_map.entry(tag).or_default().push(post.clone());
}
}

Expand All @@ -90,15 +80,14 @@ pub fn group_by_tags(posts: Vec<Content>) -> Vec<(String, Vec<Content>)> {
pub fn get_date(frontmatter: &Frontmatter, path: &Path) -> Option<NaiveDateTime> {
if let Some(input) = frontmatter.get("date") {
if let Ok(date) =
NaiveDateTime::parse_from_str(&input.as_str().unwrap(), "%Y-%m-%d %H:%M:%S")
NaiveDateTime::parse_from_str(input.as_str().unwrap(), "%Y-%m-%d %H:%M:%S")
{
return Some(date);
}
if let Ok(date) = NaiveDateTime::parse_from_str(&input.as_str().unwrap(), "%Y-%m-%d %H:%M")
{
if let Ok(date) = NaiveDateTime::parse_from_str(input.as_str().unwrap(), "%Y-%m-%d %H:%M") {
return Some(date);
}
if let Ok(date) = NaiveDate::parse_from_str(&input.as_str().unwrap(), "%Y-%m-%d") {
if let Ok(date) = NaiveDate::parse_from_str(input.as_str().unwrap(), "%Y-%m-%d") {
return date.and_hms_opt(0, 0, 0);
}
error!(
Expand Down
8 changes: 4 additions & 4 deletions src/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ lazy_static! {
}

pub fn generate_static(static_folder: &Path) {
if let Err(e) = fs::create_dir_all(&static_folder) {
if let Err(e) = fs::create_dir_all(static_folder) {
error!("Unable to create static directory: {}", e);
return;
}

for (name, file_data) in EMBEDDED_STATIC.iter() {
let file_path = static_folder.join(name); // static/foo.ext

match write_bytes_to_file(file_path.as_path(), &file_data) {
Ok(_) => info!("Generated {}", &file_path.display()),
Err(e) => eprintln!("Error writing file: {}", e),
match write_bytes_to_file(file_path.as_path(), file_data) {
Ok(()) => info!("Generated {}", &file_path.display()),
Err(e) => error!("Error writing file: {}", e),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ fn main() {
error!("Logger already initialized: {}", e);
}

site::generate_site(config_path, input_folder, &output_folder);
site::generate(&config_path, &input_folder, &output_folder);

// Serve the site if the flag was provided
if serve {
info!("Starting built-in HTTP server...");
server::start_server(&bind_address, output_folder.clone().into());
server::start(bind_address, &output_folder);
}
}
12 changes: 6 additions & 6 deletions src/markdown.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::content::{get_date, get_slug, get_tags, get_title, Content};
use crate::site::SiteData;
use crate::site::Data;
use comrak::{markdown_to_html, ComrakOptions};
use frontmatter_gen::{extract, Frontmatter};
use std::fs;
use std::path::Path;

pub fn process_file(path: &Path, site_data: &mut SiteData) -> Result<(), String> {
pub fn process_file(path: &Path, site_data: &mut Data) -> Result<(), String> {
let file_content = fs::read_to_string(path).map_err(|e| e.to_string())?;
let (frontmatter, markdown) = parse_front_matter(&file_content)?;

Expand All @@ -15,14 +15,14 @@ pub fn process_file(path: &Path, site_data: &mut SiteData) -> Result<(), String>

let title = get_title(&frontmatter, markdown);
let tags = get_tags(&frontmatter);
let slug = get_slug(&frontmatter, &path);
let date = get_date(&frontmatter, &path);
let slug = get_slug(&frontmatter, path);
let date = get_date(&frontmatter, path);

let content = Content {
title,
slug,
tags,
html,
tags,
date,
};

Expand All @@ -36,7 +36,7 @@ pub fn process_file(path: &Path, site_data: &mut SiteData) -> Result<(), String>

fn parse_front_matter(content: &str) -> Result<(Frontmatter, &str), String> {
if content.starts_with("---") {
extract(&content).map_err(|e| e.to_string())
extract(content).map_err(|e| e.to_string())
} else {
Ok((Frontmatter::new(), content))
}
Expand Down
26 changes: 10 additions & 16 deletions src/robots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,18 @@ const DEFAULT_ROBOTS: &str = "User-agent: *
Disallow: /private
Allow: /public";

pub fn handle_robots(content_dir: &Path, output_path: &Path) {
pub fn handle(content_dir: &Path, output_path: &Path) {
let robots_src = content_dir.join(ROBOTS_SRC);
let robots_dst = output_path.join(ROBOTS_SRC);

match robots_src.exists() {
true => {
if let Err(e) = fs::copy(&robots_src, &robots_dst) {
error!("Failed to copy robots.txt: {}", e);
} else {
info!("Copied robots.txt to output folder");
}
}
false => {
if let Err(e) = fs::write(&robots_dst, DEFAULT_ROBOTS) {
error!("Failed to create default robots.txt: {}", e);
} else {
info!("Generated default robots.txt in output folder");
}
if robots_src.exists() {
if let Err(e) = fs::copy(&robots_src, &robots_dst) {
error!("Failed to copy robots.txt: {}", e);
} else {
info!("Copied robots.txt to output folder");
}
} else if let Err(e) = fs::write(&robots_dst, DEFAULT_ROBOTS) {
error!("Failed to create default robots.txt: {}", e);
} else {
info!("Generated default robots.txt in output folder");
}
}
10 changes: 5 additions & 5 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use log::{error, info};
use std::fs::File;
use std::io::Cursor;
use std::path::PathBuf;
use std::sync::Arc;
use std::{fs::File, path::Path};
use tiny_http::{Response, Server};

pub fn start_server(bind_address: &str, output_folder: Arc<PathBuf>) {
pub fn start(bind_address: &str, output_folder: &Arc<PathBuf>) {
let server = Server::http(bind_address).unwrap();

info!(
Expand All @@ -14,7 +14,7 @@ pub fn start_server(bind_address: &str, output_folder: Arc<PathBuf>) {
);

for request in server.incoming_requests() {
let response = match handle_request(&request, &output_folder) {
let response = match handle_request(&request, output_folder) {
Ok(response) => response,
Err(err) => {
error!("Error handling request: {}", err);
Expand All @@ -30,7 +30,7 @@ pub fn start_server(bind_address: &str, output_folder: Arc<PathBuf>) {

fn handle_request(
request: &tiny_http::Request,
output_folder: &PathBuf,
output_folder: &Path,
) -> Result<Response<Cursor<Vec<u8>>>, String> {
let request_path = match request.url() {
"/" => "index.html",
Expand All @@ -48,7 +48,7 @@ fn handle_request(
}
Err(err) => {
error!("Failed to read file {}: {}", file_path.display(), err);
Err(format!("Error reading file: {}", err))
Err(format!("Error reading file: {err}"))
}
}
} else {
Expand Down
Loading