index : livejrnl-rs.git

ascending towards madness

author berrysweet <berrysweet@noreply.codeberg.org> 2023-07-06 15:00:31.0 +00:00:00
committer berrysweet <berrysweet@noreply.codeberg.org> 2023-07-06 15:00:31.0 +00:00:00
commit
79fbb98486a02f3232740af7f80433e4e36039bc [patch]
tree
7f0a824404d9bdf40c03fd0106c5c02035cffd73
parent
d91b6c0f6a8a0b81ee31026e7180a8dd00b9676a
download
79fbb98486a02f3232740af7f80433e4e36039bc.tar.gz

feat: add journal parsing and rendering



Diff

 Cargo.toml             |  2 ++
 src/main.rs            | 12 ++++++++++--
 src/util/journal.rs    | 26 ++++++++++++++++++++++++++
 src/util/templating.rs |  8 +++++---
 4 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index b62dfda..499c751 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,8 @@ edition = "2021"
[dependencies]
confy = "0.5.1"
serde = { version = "1.0.164", features = ["derive"] }
serde_derive = "1.0.166"
serde_json = "1.0.100"
walkdir = "2.3.3"
read_pipe = "0.1.2"
minijinja = "1.0.3"
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index 1203baf..1c5b39e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,16 +3,24 @@ mod data {
}
mod util {
	pub mod templating;
	pub mod journal;
}

const APP_NAME: &str = "livejrnl-rs";
const ERR_JOURNAL_NOT_FOUND: &str = "ERROR: No journal data specified.\n\nUsage:\n\tjrnl --format json | livejrnl-rs";

fn main() {
	let mut jrnl_json: String = String::new();
	if let Some(pipe) = read_pipe::read_pipe() {
		jrnl_json.push_str(&pipe);
	}
	println!("{:#?}", jrnl_json);
	else {
		println!("{}", ERR_JOURNAL_NOT_FOUND);
		return;
	}

	let journal: util::journal::Journal = util::journal::parse_journal_json(&jrnl_json);
	println!("{:#?}", journal);

	let config_data = load_config();
	match config_data {
@@ -22,7 +30,7 @@ fn main() {
			let template_files = util::templating::get_templates(config_data.template.clone());
			println!("{:?}", template_files);

			let html = util::templating::render_html(config_data, &template_files);
			let html = util::templating::render_html(config_data, journal, &template_files);
			println!("{}", html);
		},
		Err(error) => println!("{:#?}", error)
diff --git a/src/util/journal.rs b/src/util/journal.rs
new file mode 100644
index 0000000..cd083aa
--- /dev/null
+++ b/src/util/journal.rs
@@ -0,0 +1,26 @@
use serde_derive::Deserialize;
use serde_derive::Serialize;
use std::collections::HashMap;

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Journal {
    pub tags: HashMap<String, i64>,
    pub entries: Vec<JournalEntry>,
	pub template: Option<String>
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct JournalEntry {
    pub title: String,
    pub body: String,
    pub date: String,
    pub time: String,
    pub tags: Vec<String>,
    pub starred: bool,
}

pub fn parse_journal_json(data: &str) -> Journal {
	return serde_json::from_str(&data).unwrap_or_default();
}
diff --git a/src/util/templating.rs b/src/util/templating.rs
index 6232061..4eb6293 100644
--- a/src/util/templating.rs
+++ b/src/util/templating.rs
@@ -4,7 +4,9 @@ use std::path::PathBuf;
use std::str::FromStr;
use walkdir::WalkDir;
use minijinja::{Environment, context};
use crate::data::formats::RenderConfig;

use crate::data::formats;
use crate::util::journal;

pub fn get_templates(template: String) -> HashMap<String, String> {
	let template_path: PathBuf = PathBuf::from_str(&template).unwrap();
@@ -22,7 +24,7 @@ pub fn get_templates(template: String) -> HashMap<String, String> {
	templates
}

pub fn render_html(config: RenderConfig, templates: &HashMap<String, String>) -> String {
pub fn render_html(config: formats::RenderConfig, journal: journal::Journal, templates: &HashMap<String, String>) -> String {
	let mut env: Environment<'_> = Environment::new();

	for (name, source) in templates {
@@ -30,5 +32,5 @@ pub fn render_html(config: RenderConfig, templates: &HashMap<String, String>) ->
	}

	let tmpl: minijinja::Template<'_, '_> = env.get_template(&config.template_index).unwrap();
	tmpl.render(context!(config => config)).unwrap_or_default()
	tmpl.render(context!(config => config, journal => journal)).unwrap_or_default()
}
\ No newline at end of file