From 8ce69f76c79f17c5a0fea1c5d96cd006da8171d0 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 5 Jul 2021 20:12:42 -0400 Subject: make new_post route for the android widget Signed-off-by: Christine Dodrill --- src/post/mod.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/post') diff --git a/src/post/mod.rs b/src/post/mod.rs index b5303a8..4c54404 100644 --- a/src/post/mod.rs +++ b/src/post/mod.rs @@ -1,6 +1,7 @@ use chrono::prelude::*; use color_eyre::eyre::{eyre, Result, WrapErr}; use glob::glob; +use serde::Serialize; use std::{cmp::Ordering, path::PathBuf}; use tokio::fs; @@ -13,6 +14,15 @@ pub struct Post { pub body_html: String, pub date: DateTime, pub mentions: Vec, + pub new_post: NewPost, +} + +/// Used with the Android app to show information in a widget. +#[derive(Eq, PartialEq, Debug, Clone, Serialize)] +pub struct NewPost { + pub title: String, + pub summary: String, + pub link: String, } impl Into for Post { @@ -70,6 +80,33 @@ impl Post { } } +fn trim(string: &str) -> String { + let mut buf = String::new(); + let mut capturing = false; + + for line in string.lines() { + if line.starts_with("#") { + continue; + } + + if line == "" { + if capturing && buf.len() > 260 { + break; + } else { + capturing = true; + continue; + } + } + + if capturing { + buf.push_str(" "); + buf.push_str(line); + } + } + + buf +} + async fn read_post(dir: &str, fname: PathBuf) -> Result { let body = fs::read_to_string(fname.clone()) .await @@ -96,12 +133,19 @@ async fn read_post(dir: &str, fname: PathBuf) -> Result { Err(_) => vec![], }; + let new_post = NewPost { + title: front_matter.title.clone(), + summary: trim(body).to_string(), + link: format!("https://christine.website/{}", link), + }; + Ok(Post { front_matter, link, body_html, date, mentions, + new_post, }) } -- cgit v1.2.3