Add timers and enable more markdown extensions;

This commit is contained in:
Benjamin Bouvier 2019-07-01 00:10:33 +02:00
parent c9febb25d3
commit c92bdb8e17
1 changed files with 29 additions and 13 deletions

View File

@ -1,15 +1,11 @@
use std::collections::{HashMap, HashSet};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;
use std::time::Duration;
use std::time::{Duration, Instant};
use std::{fs, io};
use comrak::{self, ComrakOptions};
use notify::{
DebouncedEvent::{Create, Remove},
RecommendedWatcher, RecursiveMode, Watcher,
};
use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher};
use serde::{Deserialize, Serialize};
use toml;
@ -112,8 +108,13 @@ impl Context {
let title = content.split("\n").next().unwrap();
let mut options = ComrakOptions::default();
options.ext_autolink = true;
options.ext_tasklist = true;
options.ext_header_ids = Some("id-".into());
let html_content = TEMPLATE_HEADER.replace("{{TITLE}}", &title)
+ &comrak::markdown_to_html(&md_content, &ComrakOptions::default())
+ &comrak::markdown_to_html(&md_content, &options)
+ TEMPLATE_FOOTER;
// TODO do a better sanitize of the title.
@ -123,6 +124,8 @@ impl Context {
println!("Publishing {}", path);
let mut file = fs::File::create(path)?;
use std::io::Write;
file.write_all(html_content.as_bytes())?;
}
}
@ -146,8 +149,16 @@ fn collect_tags(notes: &NotesMap) -> TagMap {
}
fn run_hooks(context: &mut Context, notes: &NotesMap) -> Result<(), io::Error> {
println!("Running hooks...");
let start = Instant::now();
context.update_tags(notes);
context.publish_public(notes)
let ret = context.publish_public(notes);
let duration = Instant::now() - start;
println!("Took {}ms", duration.as_millis());
ret
}
fn read_file(path: &Path) -> Result<NoteMap, io::Error> {
@ -233,17 +244,22 @@ fn main() -> Result<(), io::Error> {
loop {
match rx.recv() {
Ok(evt) => {
match &evt {
Create(path) => {
use DebouncedEvent::*;
let should_run_hooks = match &evt {
Create(path) | Write(path) => {
files.insert(path.clone(), read_file(path)?);
true
}
Remove(path) => {
files.remove(path);
true
}
_ => {}
}
run_hooks(&mut context, &files)?;
_ => false,
};
println!("{:?}", evt);
if should_run_hooks {
run_hooks(&mut context, &files)?;
}
}
Err(e) => println!("watch error: {:?}", e),
}