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