Skip to content

Commit

Permalink
On creating/publishing, don't put subseconds in date
Browse files Browse the repository at this point in the history
  • Loading branch information
Geobert Quach committed Oct 13, 2022
1 parent faca0f7 commit e509f26
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "emile"
version = "0.3.0"
version = "0.3.1"
authors = ["Geobert Quach <geobert@protonmail.com>"]
edition = "2021"

Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod scheduler;
mod watcher;

use opt::Opt;
use time::macros::format_description;
use time::{macros::format_description, OffsetDateTime};
use tracing_subscriber::{
fmt::time::UtcTime, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt,
EnvFilter,
Expand Down Expand Up @@ -94,3 +94,9 @@ fn zola_build() -> Result<()> {
},
}
}

fn format_date(date: &OffsetDateTime) -> Result<String> {
Ok(date.format(&format_description!(
"[year]-[month]-[day]T[hour]:[minute]:[second][offset_hour sign:mandatory]:[offset_minute]"
))?)
}
7 changes: 4 additions & 3 deletions src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::path::PathBuf;

use anyhow::{bail, Result};
use slug::slugify;
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use time::OffsetDateTime;

use crate::config::SiteConfig;
use crate::format_date;
use crate::post::modify_post;

pub fn create_draft(title: &str, cfg: &SiteConfig) -> Result<()> {
Expand Down Expand Up @@ -37,7 +38,7 @@ pub fn create_draft(title: &str, cfg: &SiteConfig) -> Result<()> {
Ok(format!(
"+++\ntitle = \"{}\"\ndate = {}\ndraft = true\n",
title,
date.format(&Rfc3339)?
format_date(&date)?
))
} else {
Ok(format!("{}\n", line))
Expand All @@ -47,7 +48,7 @@ pub fn create_draft(title: &str, cfg: &SiteConfig) -> Result<()> {
format!(
"+++\ntitle = \"{}\"\ndate = {}\ndraft = true\n+++\n",
title,
date.format(&Rfc3339)?
format_date(&date)?
)
};
fs::write(&dest, new_content)?;
Expand Down
5 changes: 3 additions & 2 deletions src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::fs::{self, DirEntry};
use std::path::Path;

use anyhow::{bail, Result};
use time::{format_description::well_known::Rfc3339, OffsetDateTime};
use time::OffsetDateTime;

use crate::config::SiteConfig;
use crate::format_date;
use crate::post::modify_post;

pub fn publish_post(slug: &str, src_dir: &Path, cfg: &SiteConfig) -> Result<String> {
Expand All @@ -18,7 +19,7 @@ pub fn publish_post(slug: &str, src_dir: &Path, cfg: &SiteConfig) -> Result<Stri
let new_content = modify_post(&src, |cur_line: &str, in_frontmatter| {
if in_frontmatter {
if cur_line.starts_with("date = ") {
Ok(format!("date = {}\n", date.format(&Rfc3339)?))
Ok(format!("date = {}\n", format_date(&date)?))
} else if !cur_line.starts_with("draft =") {
Ok(format!("{}\n", cur_line))
} else {
Expand Down

0 comments on commit e509f26

Please sign in to comment.