Skip to content

Commit

Permalink
fix: add date convert for file change and fixed #37
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Jan 4, 2022
1 parent 59a2472 commit a6c8888
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 38 deletions.
26 changes: 23 additions & 3 deletions libs/quake_processor/src/pdf_processor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::error::Error;
use std::path;
use std::path::Path;
Expand All @@ -6,13 +7,32 @@ use pdf_extract::extract_text;

use crate::process_engine::Processor;

#[derive(Default)]
pub struct PdfProcessor {}
#[derive(Default, Clone)]
pub struct PdfProcessor {
meta_data: HashMap<String, String>,
}

impl Processor for PdfProcessor {
fn content(&self, file: &Path) -> Result<String, Box<dyn Error>> {
let path = path::Path::new(&file);
let string = extract_text(path)?;

let mut string = String::new();
string.push_str("<quake-br>");
string.push_str(extract_text(path)?.as_str());

string = string.replace("\n\n", "<quake-br>").replace("\n", "");
string = string.replace("<quake-br>", "\n\n");

Ok(string)
}

fn meta_data(&self) -> HashMap<String, String> {
self.meta_data.clone()
}
}

#[cfg(test)]
mod tests {
#[test]
fn should_parse_references() {}
}
6 changes: 6 additions & 0 deletions libs/quake_processor/src/process_engine.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::collections::HashMap;
use std::error::Error;
use std::path::Path;

use crate::pdf_processor::PdfProcessor;

pub trait Processor {
fn content(&self, file: &Path) -> Result<String, Box<dyn Error>>;
fn meta_data(&self) -> HashMap<String, String>;
}

#[derive(Default)]
Expand All @@ -14,6 +16,10 @@ impl Processor for EmptyProcessor {
fn content(&self, _file: &Path) -> Result<String, Box<dyn Error>> {
Ok("".to_string())
}

fn meta_data(&self) -> HashMap<String, String> {
todo!()
}
}

pub struct ProcessEngine {}
Expand Down
28 changes: 12 additions & 16 deletions src/cli/entry_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,18 @@ fn generate_by_path(paths: &EntryPaths, define: &EntryDefine) -> Result<(), Box<
if EntryFile::is_match(name) {
let content = fs::read_to_string(path.path())?;
let mut entry_file = EntryFile::from(&*content, 1)?;
match entry_file.property(&field) {
None => {}
Some(value) => {
let file_path = paths.entry_path.join(value);
if file_path.exists() {
let ext = file_path.extension().unwrap().to_str().unwrap();
let engine = ProcessEngine::engine(ext);
let content = engine.content(&file_path)?;

println!("call {:?} engine from {:?}", ext, file_path);

entry_file.content = content;
fs::write(&path.path(), entry_file.to_string()).unwrap();
} else {
return Err(Box::new(QuakeError("cannot entry file".to_string())));
}
if let Some(value) = entry_file.property(&field) {
let file_path = paths.entry_path.join(value);
if file_path.exists() {
let ext = file_path.extension().unwrap().to_str().unwrap();
let engine = ProcessEngine::engine(ext);
let content = engine.content(&file_path)?;
info!("call {:?} engine from {:?}", ext, file_path);

entry_file.content = content;
fs::write(&path.path(), entry_file.to_string()).unwrap();
} else {
return Err(Box::new(QuakeError("cannot entry file".to_string())));
}
}
}
Expand Down
55 changes: 38 additions & 17 deletions src/helper/entry_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ use futures::channel::mpsc::{channel, Receiver};
use futures::{SinkExt, StreamExt};
use notify::event::ModifyKind;
use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use rocket::info;
use tracing::{debug, error};

use quake_core::entry::entry_file::EntryFile;
use quake_core::entry::entry_paths::EntryPaths;
use quake_core::entry::EntryDefines;
use quake_core::errors::QuakeError;
use quake_core::helper::file_filter::type_from_md_path;
use quake_core::helper::quake_time;
use quake_core::meta::MetaProperty;

use crate::helper::exec_wrapper::meili_exec::feed_document;

Expand All @@ -29,15 +34,15 @@ fn async_watcher() -> notify::Result<(RecommendedWatcher, Receiver<notify::Resul
}

// todo: add type merge for ranges
pub async fn async_watch<P: AsRef<Path>>(path: P, search_url: String) -> notify::Result<()> {
debug!("start watch: {:?}", path.as_ref());
pub async fn async_watch<P: AsRef<Path>>(workspace: P, search_url: String) -> notify::Result<()> {
debug!("start watch: {:?}", workspace.as_ref());
let (mut watcher, mut rx) = async_watcher()?;
watcher.watch(path.as_ref(), RecursiveMode::Recursive)?;
watcher.watch(workspace.as_ref(), RecursiveMode::Recursive)?;

while let Some(res) = rx.next().await {
match res {
Ok(event) => {
if let Err(err) = feed_by_event(event, &search_url) {
if let Err(err) = feed_by_event(event, &search_url, workspace.as_ref()) {
error!("watch error: {:?}", err)
};
}
Expand All @@ -48,7 +53,7 @@ pub async fn async_watch<P: AsRef<Path>>(path: P, search_url: String) -> notify:
Ok(())
}

fn feed_by_event(event: Event, search_url: &str) -> Result<(), Box<dyn Error>> {
fn feed_by_event(event: Event, search_url: &str, workspace: &Path) -> Result<(), Box<dyn Error>> {
// todo: looking for better way
match &event.kind {
EventKind::Modify(ModifyKind::Data(_data)) => {
Expand All @@ -73,28 +78,44 @@ fn feed_by_event(event: Event, search_url: &str) -> Result<(), Box<dyn Error>> {
}
}

let (typ, file) = entry_file_by_path(&path)?;
let (typ, file) = entry_file_by_path(&path, workspace)?;
feed_document(search_url, &typ, &file)?;
}

Ok(())
}

pub fn entry_file_by_path(path: &Path) -> Result<(String, EntryFile), Box<dyn Error>> {
let typ = type_from_md_path(path).ok_or("")?;
pub fn entry_file_by_path(
path: &Path,
workspace: &Path,
) -> Result<(String, EntryFile), Box<dyn Error>> {
let entry_type = type_from_md_path(path).ok_or("")?;
let file_name = path.file_name().ok_or("")?;

if file_name.is_empty() || typ.is_empty() {
if file_name.is_empty() || entry_type.is_empty() {
return Err(Box::new(QuakeError(format!(
"emtpy type {:?} or file_name {:?}",
typ, file_name
"empty type {:?} or file_name {:?}",
entry_type, file_name
))));
}

let id = EntryFile::id_from_name(file_name.to_str().unwrap().to_string().as_str())?;
let content = fs::read_to_string(&path)?;
let file = EntryFile::from(content.as_str(), id)?;
Ok((typ, file))

let mut file = EntryFile::from(content.as_str(), id)?;
let defines = EntryDefines::from_path(&*workspace.join(EntryPaths::entries_define()));
if let Some(define) = defines.find(&*entry_type) {
for (key, prop) in define.to_field_type() {
if let MetaProperty::Date(_date) = prop {
let text = &*file.property(&key).unwrap();
let value = quake_time::string_date_to_unix(text);
file.update_property(&key, &value);
info!("update {:} date: from {:} to {:}", key, text, value);
}
}
}

Ok((entry_type, file))
}

#[cfg(test)]
Expand All @@ -105,12 +126,12 @@ mod tests {

#[test]
fn entry_by_path() {
let buf = PathBuf::from("examples")
.join("todo")
.join("0001-time-support.md");
let workspace = PathBuf::from("examples");
let buf = workspace.join("todo").join("0001-time-support.md");

let (typ, file) = entry_file_by_path(&buf).unwrap();
let (typ, file) = entry_file_by_path(&buf, &workspace).unwrap();
assert_eq!(typ, "todo".to_string());
assert_eq!(1, file.id);
assert_eq!("1637781250", file.property("created_date").unwrap());
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ pub async fn process_cmd(opts: Opts) -> Result<(), Box<dyn Error>> {
}
SubCommand::Server(server) => {
let config = load_config(&server.config)?;
let path = config.workspace;
let workspace = config.workspace;
let search_url = config.search_url;

if server.watch {
block_on(async {
let (_s, _g) = future::join(
quake_rocket().launch(),
entry_watcher::async_watch(path, search_url),
entry_watcher::async_watch(workspace, search_url),
)
.await;
});
Expand Down

0 comments on commit a6c8888

Please sign in to comment.