Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Format DateTime in accordance to Mokk's locale
Browse files Browse the repository at this point in the history
Support for locales beyond en_US is now supported (locales pulled from glibc, see: https://sources.debian.org/src/glibc/2.31-4/localedata/locales/) experimentally (see also: chronotope/chrono#453)
  • Loading branch information
Emil Sayahi committed Nov 9, 2020
1 parent 9d48596 commit c73f25c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 27 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion .github/workflows/format_code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ jobs:
run: |
git config --global user.name 'Dokkoo'
git config --global user.email '[email protected]'
git diff --quiet && git diff --staged --quiet || git commit -am "Automatic Rust styleguide enforcement"
git diff --quiet && git diff --staged --quiet || git commit -am "Automatically enforce Rust styleguide"
git push
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ readme = "README"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = "*"
chrono = { version = "*", features = ["unstable-locales"] }
clap = { version = "3.0.0-beta.2", features = ["yaml", "wrap_help"] }
comrak = "0.9"
comrak = "*"
liquid = "*"
liquid-core = "*"
liquid-lib = { version = "*", features = ["jekyll", "shopify", "extra"] }
Expand Down
56 changes: 32 additions & 24 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::convert::TryFrom;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
/// Document:
Expand Down Expand Up @@ -159,6 +160,9 @@ pub fn get_page_object(page_path: String) -> Page {

let page_path_io = Path::new(&page_path[..]); // Turn the path into a Path object for easy manipulation (to get page.dir and page.name)
let datetime = DateTime::parse_from_rfc3339(date.unwrap().1); // Turn the date-time into a DateTime object for easy manipulation (to generate temporal Page metadata)
let global: HashMap<String, String> =
serde_yaml::from_str(&fs::read_to_string("./_global.yml").unwrap()).unwrap(); // TODO: Figure out a way to not have to get copy of Global context in get_page, save on memory
let locale: chrono::Locale = chrono::Locale::try_from(&(global.get_key_value("locale").unwrap().1)[..]).unwrap(); // Get locale from Global context

// Define our Page
let mut page = Page {
Expand All @@ -171,30 +175,30 @@ pub fn get_page_object(page_path: String) -> Page {
.unwrap()
.to_owned(),
url: "".to_owned(),
year: format!("{}", datetime.unwrap().format("%Y")),
short_year: format!("{}", datetime.unwrap().format("%y")),
month: format!("{}", datetime.unwrap().format("%m")),
i_month: format!("{}", datetime.unwrap().format("%-m")),
short_month: format!("{}", datetime.unwrap().format("%b")),
long_month: format!("{}", datetime.unwrap().format("%B")),
day: format!("{}", datetime.unwrap().format("%d")),
i_day: format!("{}", datetime.unwrap().format("%-d")),
y_day: format!("{}", datetime.unwrap().format("%j")),
w_year: format!("{}", datetime.unwrap().format("%G")),
week: format!("{}", datetime.unwrap().format("%U")),
w_day: format!("{}", datetime.unwrap().format("%u")),
short_day: format!("{}", datetime.unwrap().format("%a")),
long_day: format!("{}", datetime.unwrap().format("%A")),
hour: format!("{}", datetime.unwrap().format("%H")),
minute: format!("{}", datetime.unwrap().format("%M")),
second: format!("{}", datetime.unwrap().format("%S")),
year: format!("{}", datetime.unwrap().format_localized("%Y", locale)),
short_year: format!("{}", datetime.unwrap().format_localized("%y", locale)),
month: format!("{}", datetime.unwrap().format_localized("%m", locale)),
i_month: format!("{}", datetime.unwrap().format_localized("%-m", locale)),
short_month: format!("{}", datetime.unwrap().format_localized("%b", locale)),
long_month: format!("{}", datetime.unwrap().format_localized("%B", locale)),
day: format!("{}", datetime.unwrap().format_localized("%d", locale)),
i_day: format!("{}", datetime.unwrap().format_localized("%-d", locale)),
y_day: format!("{}", datetime.unwrap().format_localized("%j", locale)),
w_year: format!("{}", datetime.unwrap().format_localized("%G", locale)),
week: format!("{}", datetime.unwrap().format_localized("%U", locale)),
w_day: format!("{}", datetime.unwrap().format_localized("%u", locale)),
short_day: format!("{}", datetime.unwrap().format_localized("%a", locale)),
long_day: format!("{}", datetime.unwrap().format_localized("%A", locale)),
hour: format!("{}", datetime.unwrap().format_localized("%H", locale)),
minute: format!("{}", datetime.unwrap().format_localized("%M", locale)),
second: format!("{}", datetime.unwrap().format_localized("%S", locale)),
};

// Render the URL once the Page metadata has been generated
page.url = render(&page, &get_permalink(permalink.unwrap().1));

// Render Page content, set page.document.content as rendered version
//page.document.content = render(&page, &page.document.content);
page.document.content = render(&page, &page.document.content);

page
}
Expand All @@ -208,9 +212,11 @@ pub fn get_contexts(page: &Page) -> Object {
let global: HashMap<String, String> =
serde_yaml::from_str(&fs::read_to_string("./_global.yml").unwrap()).unwrap(); // Defined as variable as it required a type annotation

/*
Collections
*/
let collection_name = page.document.frontmatter.get_key_value("collection");
let collection: HashMap<String, String>;

// Import collection context if Page is in a collection
match collection_name {
None => {
Expand All @@ -225,9 +231,11 @@ pub fn get_contexts(page: &Page) -> Object {
}
}

/*
Layouts
*/
let layout_name = page.document.frontmatter.get_key_value("layout");
let layout: HashMap<String, String>;

// Import layout context if Page has a layout
match layout_name {
None => {
Expand All @@ -236,7 +244,7 @@ pub fn get_contexts(page: &Page) -> Object {
Some(_) => {
layout = serde_yaml::from_str(
&split_frontmatter(
fs::read_to_string(format!("./layouts/{}.html", layout_name.unwrap().1))
fs::read_to_string(format!("./layouts/{}.mokkf", layout_name.unwrap().1))
.unwrap(),
)
.0,
Expand All @@ -259,7 +267,7 @@ pub fn get_contexts(page: &Page) -> Object {
///
/// # Arguments
///
/// * `page` - The `.mokkf` file's context as a Page
/// * `page` - A `.mokkf` file's context as a Page
///
/// * `text_to_render` - The text to be rendered
pub fn render(page: &Page, text_to_render: &str) -> String {
Expand Down Expand Up @@ -299,7 +307,7 @@ pub fn compile(page: &Page) -> String {
// Otherwise, render with Document's contents
match layout_name {
None => {
compiled_page = render(&page, &page.document.content);
compiled_page = page.document.content.to_owned();
}
Some(_) => {
compiled_page = render(
Expand All @@ -312,7 +320,7 @@ pub fn compile(page: &Page) -> String {
}
}

// If within a collection, append to list of collection's entries
// If within a collection, append page.document.content to list of collection's entries

compiled_page
}

0 comments on commit c73f25c

Please sign in to comment.