use regex replace syntax for arg replacement

pull/152/head
phiresky 1 year ago
parent 6f3488682f
commit 3efc0a727c

@ -2,7 +2,7 @@ use super::{
spawning::{SpawningFileAdapter, SpawningFileAdapterTrait},
AdapterMeta, GetMetadata,
};
use crate::matching::{FastFileMatcher, FileMatcher};
use crate::{matching::{FastFileMatcher, FileMatcher}, expand::expand_str_ez};
use anyhow::Result;
use lazy_static::lazy_static;
use regex::{Captures, Regex};
@ -88,7 +88,7 @@ lazy_static! {
// simpler markown (with more information loss but plainer text)
//.arg("--to=commonmark-header_attributes-link_attributes-fenced_divs-markdown_in_html_blocks-raw_html-native_divs-native_spans-bracketed_spans")
args: strs(&[
"--from={file_extension}",
"--from=$file_extension",
"--to=plain",
"--wrap=none",
"--markdown-headings=atx"
@ -126,40 +126,12 @@ impl GetMetadata for CustomSpawningFileAdapter {
}
}
fn arg_replacer(arg: &str, filepath_hint: &Path) -> Result<String> {
lazy_static::lazy_static! {
static ref ARG_REP: Regex = Regex::new(r"\{([a-z_]+)\}").unwrap();
}
let mut err = None;
let r = ARG_REP.replace_all(arg, |m: &Captures| -> String {
let idx = m.get(0).unwrap().range();
if arg.chars().nth(idx.start - 1) == Some('{') {
// skip
return m.get(0).unwrap().as_str().to_string();
}
if arg.chars().nth(idx.end + 1) == Some('}') {
// skip
return m.get(0).unwrap().as_str().to_string();
}
let key = m.get(1).unwrap().as_str();
if key == "file_extension" {
return filepath_hint
.extension()
.map(|e| e.to_string_lossy().to_string())
.unwrap_or("".to_string());
}
err = Some(anyhow::anyhow!(
"Unknown arg replacement key '{}' in '{}'",
key,
arg
));
"".to_string()
//let
});
if let Some(err) = err {
Err(err)
} else {
Ok(r.to_string())
}
Ok(expand_str_ez(arg, |s| match s {
"file_extension" => &filepath_hint
.extension()
.map(|e| e.to_string_lossy())
.unwrap_or("".into()),
}))
}
impl SpawningFileAdapterTrait for CustomSpawningFileAdapter {
fn get_exe(&self) -> &str {

@ -35,11 +35,6 @@ impl GetMetadata for SpawningFileAdapter {
}
}
/*impl<T: SpawningFileAdapterTrait> From<T> for SpawningFileAdapter {
fn from(e: dyn T) -> Self {
SpawningFileAdapter { inner: Box::new(e) }
}
}*/
/// replace a Command.spawn() error "File not found" with a more readable error
/// to indicate some program is not installed

@ -1,46 +0,0 @@
use super::*;
use lazy_static::lazy_static;
use spawning::{SpawningFileAdapter, SpawningFileAdapterTrait};
use std::process::Command;
static EXTENSIONS: &[&str] = &["jpg", "png"];
lazy_static! {
static ref METADATA: AdapterMeta = AdapterMeta {
name: "tesseract".to_owned(),
version: 1,
description: "Uses tesseract to run OCR on images to make them searchable. May need -j1 to prevent overloading the system. Make sure you have tesseract installed.".to_owned(),
recurses: false,
fast_matchers: EXTENSIONS
.iter()
.map(|s| FastFileMatcher::FileExtension(s.to_string()))
.collect(),
slow_matchers: None,
keep_fast_matchers_if_accurate: true,
disabled_by_default: true
};
}
#[derive(Default)]
pub struct TesseractAdapter {}
impl TesseractAdapter {
pub fn new() -> TesseractAdapter {
TesseractAdapter {}
}
}
impl GetMetadata for TesseractAdapter {
fn metadata(&self) -> &AdapterMeta {
&METADATA
}
}
impl SpawningFileAdapterTrait for TesseractAdapter {
fn get_exe(&self) -> &str {
"tesseract"
}
fn command(&self, _filepath_hint: &Path, mut cmd: Command) -> Command {
// rg already does threading
cmd.env("OMP_THREAD_LIMIT", "1").arg("-").arg("-");
Some(cmd)
}
}
Loading…
Cancel
Save