Browse Source

Markdown (only) to the Markdown gods

master
Julio Biason 4 years ago
parent
commit
9af9b3cda3
  1. 26
      src/storage/data.rs
  2. 3
      src/storage/filesystem.rs
  3. 39
      src/storage/helpers.rs
  4. 3
      src/storage/joplin.rs
  5. 1
      src/storage/mod.rs

26
src/storage/data.rs

@ -19,7 +19,6 @@
use std::convert::From;
use elefren::entities::status::Status;
use html2md;
use crate::storage::attachment::Attachment;
@ -28,6 +27,7 @@ use crate::storage::attachment::Attachment;
pub struct Data {
pub id: String,
pub account: String,
pub title: String,
pub text: String,
pub attachments: Vec<Attachment>,
pub source: String,
@ -39,7 +39,8 @@ impl From<&Status> for Data {
Self {
id: origin.id.to_string(),
account: origin.account.acct.to_string(),
text: build_text(origin),
title: origin.spoiler_text.to_string(),
text: origin.content.to_string(),
attachments: origin
.media_attachments
.iter()
@ -49,24 +50,3 @@ impl From<&Status> for Data {
}
}
}
fn build_text(status: &Status) -> String {
let base_content = html2md::parse_html(&status.content);
let source = &status.url;
let title = &status.spoiler_text;
let mut result = String::new();
if title.len() > 0 {
result.push_str(title);
result.push_str("\n\n");
}
result.push_str(&base_content);
if let Some(url) = source {
result.push_str("\n\n");
result.push_str(&url);
}
result
}

3
src/storage/filesystem.rs

@ -22,6 +22,7 @@ use std::path::Path;
use std::path::PathBuf;
use crate::storage::data::Data;
use crate::storage::helpers::make_markdown;
use crate::storage::storage::Storage;
pub struct Filesystem {}
@ -53,7 +54,7 @@ impl Filesystem {
fn save_content(&self, data: &Data) {
let filename = self.dir(data).join("toot.md");
let mut fp = File::create(filename).expect("Failed to create file");
fp.write_all(data.text.as_bytes())
fp.write_all(make_markdown(data).as_bytes())
.expect("Failed to save content");
}

39
src/storage/helpers.rs

@ -0,0 +1,39 @@
/*
DOWNFAV - Download Favourites
Copyright (C) 2020-2021 Julio Biason
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use crate::storage::data::Data;
use html2md;
pub fn make_markdown(status: &Data) -> String {
let base_content = html2md::parse_html(&status.text);
let title = &status.title;
let mut result = String::new();
if title.len() > 0 {
result.push_str(title);
result.push_str("\n\n");
}
result.push_str(&base_content);
if !status.source.is_empty() {
result.push_str("\n\n");
result.push_str(&status.source);
}
result
}

3
src/storage/joplin.rs

@ -26,6 +26,7 @@ use serde_derive::Deserialize;
use crate::config::JoplinConfig;
use crate::storage::data::Data;
use crate::storage::helpers::make_markdown;
use crate::storage::storage::Storage;
static INLINABLE: [&'static str; 4] = ["jpeg", "jpg", "png", "gif"];
@ -66,7 +67,7 @@ impl Storage for Joplin {
fn save(&self, record: &Data) {
let resources = self.save_attachments(&record);
log::debug!("Record attachments: {:?}", resources);
let mut text = record.text.to_string();
let mut text = make_markdown(record);
let title = format!("{}/{}", record.account, record.id);
Joplin::add_resources_to_text(&mut text, &resources);
self.save_content(&title, &text, &record.source);

1
src/storage/mod.rs

@ -19,6 +19,7 @@
pub mod attachment;
pub mod data;
pub mod filesystem;
pub mod helpers;
pub mod joplin;
pub mod org;
pub mod storage;

Loading…
Cancel
Save