use regex replace syntax for arg replacement

This commit is contained in:
phiresky 2022-12-25 17:45:53 +01:00
parent 6f3488682f
commit 3efc0a727c
3 changed files with 8 additions and 87 deletions

View File

@ -2,7 +2,7 @@ use super::{
spawning::{SpawningFileAdapter, SpawningFileAdapterTrait}, spawning::{SpawningFileAdapter, SpawningFileAdapterTrait},
AdapterMeta, GetMetadata, AdapterMeta, GetMetadata,
}; };
use crate::matching::{FastFileMatcher, FileMatcher}; use crate::{matching::{FastFileMatcher, FileMatcher}, expand::expand_str_ez};
use anyhow::Result; use anyhow::Result;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::{Captures, Regex}; use regex::{Captures, Regex};
@ -88,7 +88,7 @@ lazy_static! {
// simpler markown (with more information loss but plainer text) // 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") //.arg("--to=commonmark-header_attributes-link_attributes-fenced_divs-markdown_in_html_blocks-raw_html-native_divs-native_spans-bracketed_spans")
args: strs(&[ args: strs(&[
"--from={file_extension}", "--from=$file_extension",
"--to=plain", "--to=plain",
"--wrap=none", "--wrap=none",
"--markdown-headings=atx" "--markdown-headings=atx"
@ -126,40 +126,12 @@ impl GetMetadata for CustomSpawningFileAdapter {
} }
} }
fn arg_replacer(arg: &str, filepath_hint: &Path) -> Result<String> { fn arg_replacer(arg: &str, filepath_hint: &Path) -> Result<String> {
lazy_static::lazy_static! { Ok(expand_str_ez(arg, |s| match s {
static ref ARG_REP: Regex = Regex::new(r"\{([a-z_]+)\}").unwrap(); "file_extension" => &filepath_hint
}
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() .extension()
.map(|e| e.to_string_lossy().to_string()) .map(|e| e.to_string_lossy())
.unwrap_or("".to_string()); .unwrap_or("".into()),
} }))
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())
}
} }
impl SpawningFileAdapterTrait for CustomSpawningFileAdapter { impl SpawningFileAdapterTrait for CustomSpawningFileAdapter {
fn get_exe(&self) -> &str { fn get_exe(&self) -> &str {

View File

@ -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 /// replace a Command.spawn() error "File not found" with a more readable error
/// to indicate some program is not installed /// to indicate some program is not installed

View File

@ -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)
}
}