pdf: add page number prefix

This commit is contained in:
phiresky 2019-06-06 14:55:27 +02:00
parent ba58151dac
commit cd1a26ffff
7 changed files with 73 additions and 12 deletions

BIN
exampledir/test.tar.gz Normal file

Binary file not shown.

Binary file not shown.

38
exampledir/wasteland.fb2 Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,7 @@
use super::*; use super::*;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use spawning::SpawningFileAdapter; use spawning::SpawningFileAdapter;
use std::io::BufReader;
use std::process::Command; use std::process::Command;
static EXTENSIONS: &[&str] = &["pdf"]; static EXTENSIONS: &[&str] = &["pdf"];
@ -29,6 +30,20 @@ impl GetMetadata for PopplerAdapter {
} }
} }
impl SpawningFileAdapter for PopplerAdapter { impl SpawningFileAdapter for PopplerAdapter {
fn postproc(line_prefix: &str, inp: &mut Read, oup: &mut Write) -> Fallible<()> {
// prepend Page X to each line
let mut page = 1;
for line in BufReader::new(inp).lines() {
let mut line = line?;
if line.contains("\x0c") {
// page break
line = line.replace("\x0c", "");
page += 1;
}
oup.write_all(format!("{}Page {}: {}\n", line_prefix, page, line).as_bytes())?;
}
Ok(())
}
fn get_exe(&self) -> &str { fn get_exe(&self) -> &str {
"pdftotext" "pdftotext"
} }

View File

@ -53,7 +53,7 @@ impl FileAdapter for ZipAdapter {
file.size(), file.size(),
file.compressed_size() file.compressed_size()
); );
let line_prefix = &format!("{}{}:/", line_prefix, file.name().clone()); let line_prefix = &format!("{}{}: ", line_prefix, file.name().clone());
rga_preproc( rga_preproc(
AdaptInfo { AdaptInfo {
filepath_hint: &file.sanitized_name(), filepath_hint: &file.sanitized_name(),

View File

@ -1,11 +1,8 @@
use failure::{format_err, Error}; use failure::{format_err, Error};
use path_clean::PathClean;
use rga::adapters::*; use rga::adapters::*;
use rga::preproc::*; use rga::preproc::*;
use rga::CachingWriter; use std::env;
use std::fs::File; use std::fs::File;
use std::path::PathBuf;
use std::rc::Rc;
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
let path = { let path = {
@ -26,5 +23,11 @@ fn main() -> Result<(), Error> {
line_prefix: "", line_prefix: "",
}; };
rga_preproc(ai, Some(open_cache_db()?)) let cache_db = match env::var("RGA_NO_CACHE") {
Ok(ref s) if s.len() > 0 => Some(open_cache_db()?),
Ok(_) => None,
Err(_) => None,
};
rga_preproc(ai, cache_db)
} }

View File

@ -14,14 +14,10 @@ fn main() -> std::io::Result<()> {
"--list-adapters 'Lists all known adapters'", "--list-adapters 'Lists all known adapters'",
)) ))
.arg(Arg::from_usage("--adapters=[commaseparated] 'Change which adapters to use and in which priority order (descending)'").require_equals(true)) .arg(Arg::from_usage("--adapters=[commaseparated] 'Change which adapters to use and in which priority order (descending)'").require_equals(true))
.arg(Arg::from_usage("--no-cache 'Disable caching of results'"))
.arg(Arg::from_usage("--rg-help 'Show help for ripgrep itself'")) .arg(Arg::from_usage("--rg-help 'Show help for ripgrep itself'"))
.arg(Arg::from_usage("--rg-version 'Show version of ripgrep itself'")); .arg(Arg::from_usage("--rg-version 'Show version of ripgrep itself'"));
app.p.create_help_and_version(); app.p.create_help_and_version();
println!("g={:#?},f={:#?}", app.p.groups, app.p.flags);
for opt in app.p.opts() {
println!("opt {:#?}", opt.s.long);
}
//
let mut firstarg = true; let mut firstarg = true;
let (our_args, mut passthrough_args): (Vec<OsString>, Vec<OsString>) = std::env::args_os() let (our_args, mut passthrough_args): (Vec<OsString>, Vec<OsString>) = std::env::args_os()
.partition(|os_arg| { .partition(|os_arg| {
@ -71,7 +67,16 @@ fn main() -> std::io::Result<()> {
println!("Adapters:"); println!("Adapters:");
for adapter in adapters { for adapter in adapters {
let meta = adapter.metadata(); let meta = adapter.metadata();
println!("{} v{}", meta.name, meta.version); let matchers = meta
.matchers
.iter()
.map(|m| match m {
Matcher::FileExtension(ext) => format!(".{}", ext),
})
.collect::<Vec<_>>()
.join(", ");
print!("{} v{}: {}", meta.name, meta.version, matchers);
println!("");
} }
return Ok(()); return Ok(());
} }