Add support for publishing;
This commit is contained in:
parent
89a9798d48
commit
e8cd2a7c26
145
Cargo.lock
generated
145
Cargo.lock
generated
@ -1,5 +1,13 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.1.0"
|
||||
@ -14,7 +22,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
name = "entertainer"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"markdown 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"notify 4.0.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -112,6 +123,23 @@ dependencies = [
|
||||
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "markdown"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"pipeline 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mio"
|
||||
version = "0.6.19"
|
||||
@ -179,11 +207,49 @@ dependencies = [
|
||||
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pipeline"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "0.4.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "0.6.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.1.54"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "0.1.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.4"
|
||||
@ -192,11 +258,74 @@ dependencies = [
|
||||
"winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.94"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"serde_derive 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.94"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 0.15.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "0.15.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread-id"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "utf8-ranges"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.2.8"
|
||||
@ -254,6 +383,7 @@ dependencies = [
|
||||
]
|
||||
|
||||
[metadata]
|
||||
"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
|
||||
"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd"
|
||||
"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33"
|
||||
"checksum filetime 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2f8c63033fcba1f51ef744505b3cad42510432b904c062afa67ad7ece008429d"
|
||||
@ -268,14 +398,29 @@ dependencies = [
|
||||
"checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f"
|
||||
"checksum libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "6281b86796ba5e4366000be6e9e18bf35580adf9e63fbe2294aadb587613a319"
|
||||
"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
|
||||
"checksum markdown 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bdb7e864aa1dccbebb05751e899bc84c639df47490c0c24caf4b1a77770b6566"
|
||||
"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
|
||||
"checksum mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)" = "83f51996a3ed004ef184e16818edc51fadffe8e7ca68be67f9dee67d84d0ff23"
|
||||
"checksum mio-extras 2.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "46e73a04c2fa6250b8d802134d56d554a9ec2922bf977777c805ea5def61ce40"
|
||||
"checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919"
|
||||
"checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88"
|
||||
"checksum notify 4.0.12 (registry+https://github.com/rust-lang/crates.io-index)" = "3572d71f13ea8ed41867accd971fd564aa75934cf7a1fae03ddb8c74a8a49943"
|
||||
"checksum pipeline 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d15b6607fa632996eb8a17c9041cb6071cb75ac057abd45dece578723ea8c7c0"
|
||||
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
|
||||
"checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db"
|
||||
"checksum redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)" = "12229c14a0f65c4f1cb046a3b52047cdd9da1f4b30f8a39c5063c8bae515e252"
|
||||
"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
|
||||
"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
|
||||
"checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267"
|
||||
"checksum serde 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)" = "076a696fdea89c19d3baed462576b8f6d663064414b5c793642da8dfeb99475b"
|
||||
"checksum serde_derive 1.0.94 (registry+https://github.com/rust-lang/crates.io-index)" = "ef45eb79d6463b22f5f9e16d283798b7c0175ba6050bc25c1a946c122727fe7b"
|
||||
"checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
|
||||
"checksum syn 0.15.39 (registry+https://github.com/rust-lang/crates.io-index)" = "b4d960b829a55e56db167e861ddb43602c003c7be0bee1d345021703fac2fb7c"
|
||||
"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
|
||||
"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
|
||||
"checksum toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b8c96d7873fa7ef8bdeb3a9cda3ac48389b4154f32b9803b4bc26220b677b039"
|
||||
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
|
||||
"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
|
||||
"checksum walkdir 2.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c7904a7e2bb3cdf0cf5e783f44204a85a37a93151738fa349f06680f59a98b45"
|
||||
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
|
||||
"checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770"
|
||||
|
@ -6,3 +6,6 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
notify = "4.0.12"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
toml = "0.5.0"
|
||||
markdown = "0.2.0"
|
||||
|
2
config.toml
Normal file
2
config.toml
Normal file
@ -0,0 +1,2 @@
|
||||
path_in="./assets/notes"
|
||||
path_out="./output"
|
135
src/main.rs
135
src/main.rs
@ -1,25 +1,122 @@
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::mpsc::channel;
|
||||
use std::time::Duration;
|
||||
use std::{fs, io};
|
||||
|
||||
use markdown;
|
||||
use notify::{
|
||||
DebouncedEvent::{Create, NoticeRemove, NoticeWrite, Remove},
|
||||
RecommendedWatcher, RecursiveMode, Watcher,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use toml;
|
||||
|
||||
enum Type {
|
||||
Tag = 5,
|
||||
type TagName = String;
|
||||
type TagId = String;
|
||||
type TagMap = HashMap<TagName, TagId>;
|
||||
|
||||
// Maps field to value.
|
||||
type NoteMap = HashMap<String, String>;
|
||||
|
||||
type NotesMap = HashMap<PathBuf, NoteMap>;
|
||||
|
||||
const TAG_TYPE_ID: &'static str = "5";
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Configuration {
|
||||
path_in: String,
|
||||
path_out: String,
|
||||
}
|
||||
|
||||
fn read_file(path: &Path) -> Result<HashMap<String, String>, io::Error> {
|
||||
struct Context {
|
||||
config: Configuration,
|
||||
tags: Option<TagMap>,
|
||||
public_tag_id: Option<String>,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
fn new(config: Configuration) -> Self {
|
||||
Self {
|
||||
config,
|
||||
tags: None,
|
||||
public_tag_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn update_tags(&mut self, notes: &NotesMap) {
|
||||
let tags = analyze_tags(notes);
|
||||
self.public_tag_id = tags.get("public").map(|x| x.clone());
|
||||
self.tags = Some(tags);
|
||||
}
|
||||
|
||||
fn publish_public(&self, notes: &NotesMap) -> Result<(), io::Error> {
|
||||
let public_tag_id = if let Some(id) = &self.public_tag_id {
|
||||
id
|
||||
} else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let mut tagged_notes = HashSet::new();
|
||||
for note in notes.values() {
|
||||
if let Some(tag_id) = note.get("tag_id") {
|
||||
if tag_id == public_tag_id {
|
||||
tagged_notes.insert(note.get("note_id").unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let tagged_notes = tagged_notes.iter().map(|¬e_id| {
|
||||
notes
|
||||
.iter()
|
||||
.find(|(_, note)| ¬e["id"] == note_id)
|
||||
.map(|(path, _)| path)
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
for note_id in tagged_notes {
|
||||
let note = ¬es[note_id];
|
||||
if let Some(content) = note.get("markdown_content") {
|
||||
println!("Gonna publish this content: {}", content);
|
||||
|
||||
// TODO crash in markdown!
|
||||
let content = markdown::to_html(content);
|
||||
|
||||
// TODO do a better sanitize of the title.
|
||||
let title = content.split("\n").next().unwrap().replace(" ", "_");
|
||||
let path = self.config.path_out.clone() + &format!("/{}.html", title);
|
||||
fs::write(path, content)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn analyze_tags(notes: &NotesMap) -> TagMap {
|
||||
let mut tags = TagMap::new();
|
||||
|
||||
for note in notes.values() {
|
||||
if let Some(type_id) = note.get("type_") {
|
||||
if type_id == TAG_TYPE_ID {
|
||||
tags.insert(note["markdown_content"].clone(), note["id"].clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tags
|
||||
}
|
||||
|
||||
fn run_hooks(context: &mut Context, notes: &NotesMap) -> Result<(), io::Error> {
|
||||
context.update_tags(notes);
|
||||
context.publish_public(notes)
|
||||
}
|
||||
|
||||
fn read_file(path: &Path) -> Result<NoteMap, io::Error> {
|
||||
let content = fs::read_to_string(path)?;
|
||||
let content = content.trim().split("\n").collect::<Vec<_>>();
|
||||
|
||||
println!("content: {}", content.join("\n"));
|
||||
|
||||
let mut fields = HashMap::new();
|
||||
let mut fields = NoteMap::new();
|
||||
|
||||
for j in 0..content.len() {
|
||||
let i = content.len() - 1 - j;
|
||||
@ -55,10 +152,20 @@ fn read_file(path: &Path) -> Result<HashMap<String, String>, io::Error> {
|
||||
}
|
||||
|
||||
fn main() -> Result<(), io::Error> {
|
||||
let mut num_files = 0;
|
||||
let mut files = HashMap::new();
|
||||
let config_content = fs::read_to_string("./config.toml")?;
|
||||
let config: Configuration = toml::from_str(&config_content).unwrap();
|
||||
|
||||
for entry in fs::read_dir("./assets/notes")? {
|
||||
match fs::metadata(&config.path_out) {
|
||||
Ok(_) => {}
|
||||
Err(_) => {
|
||||
fs::create_dir(&config.path_out)?;
|
||||
}
|
||||
}
|
||||
|
||||
let mut num_files = 0;
|
||||
let mut files = NotesMap::new();
|
||||
|
||||
for entry in fs::read_dir(&config.path_in)? {
|
||||
let dir_entry = entry?;
|
||||
if !dir_entry.file_type()?.is_file() {
|
||||
continue;
|
||||
@ -71,6 +178,9 @@ fn main() -> Result<(), io::Error> {
|
||||
|
||||
println!("Found {} files.", num_files);
|
||||
|
||||
let mut context = Context::new(config);
|
||||
run_hooks(&mut context, &files)?;
|
||||
|
||||
println!("Now listening to disk events...");
|
||||
|
||||
let (tx, rx) = channel();
|
||||
@ -92,11 +202,10 @@ fn main() -> Result<(), io::Error> {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
run_hooks(&mut context, &files)?;
|
||||
println!("{:?}", evt);
|
||||
}
|
||||
Err(e) => println!("watch error: {:?}", e),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user