-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlib.rs
More file actions
180 lines (163 loc) · 5.49 KB
/
lib.rs
File metadata and controls
180 lines (163 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use edgeparse_core::api::config::{
ImageFormat, ImageOutput, OutputFormat, ProcessingConfig, ReadingOrder, TableMethod,
};
use edgeparse_core::output;
use std::path::Path;
/// Convert a single PDF file and return the extracted content as a string.
///
/// Arguments:
/// input_path: Path to the PDF file.
/// format: Output format — "markdown", "json", "html", or "text". Default: "markdown".
/// pages: Optional page range string, e.g. "1,3,5-7".
/// password: Optional password for encrypted PDFs.
/// reading_order: Reading order algorithm — "xycut" or "off". Default: "xycut".
/// table_method: Table detection method — "default" or "cluster". Default: "default".
/// image_output: Image output mode — "off", "embedded", or "external". Default: "off".
///
/// Returns:
/// The extracted content as a string in the requested format.
#[pyfunction]
#[pyo3(signature = (
input_path,
*,
format = "markdown",
pages = None,
password = None,
reading_order = "xycut",
table_method = "default",
image_output = "off",
))]
fn convert(
input_path: &str,
format: &str,
pages: Option<&str>,
password: Option<&str>,
reading_order: &str,
table_method: &str,
image_output: &str,
) -> PyResult<String> {
let pdf_path = Path::new(input_path);
if !pdf_path.exists() {
return Err(PyRuntimeError::new_err(format!(
"File not found: {input_path}"
)));
}
let output_format = match format {
"json" => OutputFormat::Json,
"html" => OutputFormat::Html,
"text" => OutputFormat::Text,
"markdown" | "md" => OutputFormat::Markdown,
other => {
return Err(PyRuntimeError::new_err(format!(
"Unknown format: {other}. Valid: markdown, json, html, text"
)));
}
};
let config = ProcessingConfig {
formats: vec![output_format],
pages: pages.map(|s| s.to_string()),
password: password.map(|s| s.to_string()),
reading_order: match reading_order {
"off" => ReadingOrder::Off,
_ => ReadingOrder::XyCut,
},
table_method: match table_method {
"cluster" => TableMethod::Cluster,
_ => TableMethod::Default,
},
image_output: match image_output {
"embedded" => ImageOutput::Embedded,
"external" => ImageOutput::External,
_ => ImageOutput::Off,
},
image_format: ImageFormat::Png,
..ProcessingConfig::default()
};
let doc = edgeparse_core::convert(pdf_path, &config)
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
let stem = pdf_path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("output");
let content = match output_format {
OutputFormat::Json => output::legacy_json::to_legacy_json_string(&doc, stem)
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?,
OutputFormat::Html => {
output::html::to_html(&doc).map_err(|e| PyRuntimeError::new_err(e.to_string()))?
}
OutputFormat::Text => {
output::text::to_text(&doc).map_err(|e| PyRuntimeError::new_err(e.to_string()))?
}
OutputFormat::Markdown
| OutputFormat::MarkdownWithHtml
| OutputFormat::MarkdownWithImages => output::markdown::to_markdown(&doc)
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?,
OutputFormat::Pdf => {
return Err(PyRuntimeError::new_err("PDF output not yet implemented"));
}
};
Ok(content)
}
/// Convert a PDF file and write the output to a file.
///
/// Arguments:
/// input_path: Path to the PDF file.
/// output_dir: Directory to write the output file.
/// format: Output format — "markdown", "json", "html", or "text". Default: "markdown".
/// pages: Optional page range string, e.g. "1,3,5-7".
/// password: Optional password for encrypted PDFs.
///
/// Returns:
/// The path to the created output file.
#[pyfunction]
#[pyo3(signature = (
input_path,
output_dir,
*,
format = "markdown",
pages = None,
password = None,
))]
fn convert_file(
input_path: &str,
output_dir: &str,
format: &str,
pages: Option<&str>,
password: Option<&str>,
) -> PyResult<String> {
let content = convert(
input_path, format, pages, password, "xycut", "default", "off",
)?;
let out_dir = Path::new(output_dir);
std::fs::create_dir_all(out_dir)
.map_err(|e| PyRuntimeError::new_err(format!("Cannot create output dir: {e}")))?;
let stem = Path::new(input_path)
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("output");
let ext = match format {
"json" => "json",
"html" => "html",
"text" => "txt",
_ => "md",
};
let out_path = out_dir.join(format!("{stem}.{ext}"));
std::fs::write(&out_path, content)
.map_err(|e| PyRuntimeError::new_err(format!("Cannot write output: {e}")))?;
Ok(out_path.to_string_lossy().to_string())
}
/// Return the edgeparse version string.
#[pyfunction]
fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
/// EdgeParse — High-performance PDF extraction (Rust engine).
#[pymodule]
fn _edgeparse(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(convert, m)?)?;
m.add_function(wrap_pyfunction!(convert_file, m)?)?;
m.add_function(wrap_pyfunction!(version, m)?)?;
Ok(())
}