diff options
| author | Christine Dodrill <me@christine.website> | 2021-07-05 20:12:42 -0400 |
|---|---|---|
| committer | Christine Dodrill <me@christine.website> | 2021-07-05 20:12:51 -0400 |
| commit | 8ce69f76c79f17c5a0fea1c5d96cd006da8171d0 (patch) | |
| tree | a9dc888be43afc401e9288379ed9e65defea3cdf /src/post | |
| parent | 3c786a1c1fba1155eae9a066887479f4245e2b92 (diff) | |
| download | xesite-8ce69f76c79f17c5a0fea1c5d96cd006da8171d0.tar.xz xesite-8ce69f76c79f17c5a0fea1c5d96cd006da8171d0.zip | |
make new_post route for the android widget
Signed-off-by: Christine Dodrill <me@christine.website>
Diffstat (limited to 'src/post')
| -rw-r--r-- | src/post/mod.rs | 44 |
1 files changed, 44 insertions, 0 deletions
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<FixedOffset>, pub mentions: Vec<mi::WebMention>, + 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<jsonfeed::Item> 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<Post> { let body = fs::read_to_string(fname.clone()) .await @@ -96,12 +133,19 @@ async fn read_post(dir: &str, fname: PathBuf) -> Result<Post> { 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, }) } |
