use std::fs;
use std::path::Path;
use rhwp::provenance;
use rhwp::schema_registry::ENVELOPE_SCHEMA_VERSION;
use crate::{load_document, tables_json_value, EXIT_OK, EXIT_RUNTIME, EXIT_USAGE};
pub(crate) fn export_tables(args: &[String]) -> i32 {
use rhwp::document_core::queries::table_extract::extract_tables;
let mut file_path: Option<&str> = None;
let mut out_path: Option<String> = None;
let mut json_mode = false;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"--json" => json_mode = true,
"-o" | "--out" | "--output" => {
i += 1;
match args.get(i) {
Some(p) => out_path = Some(p.clone()),
None => {
eprintln!("오류: -o 뒤에 출력 파일 경로가 필요합니다.");
return EXIT_USAGE;
}
}
}
other if other.starts_with('-') => {
eprintln!("알 수 없는 옵션: {other}");
return EXIT_USAGE;
}
other => {
if file_path.replace(other).is_some() {
eprintln!("오류: 입력 파일은 하나만 지정할 수 있습니다.");
return EXIT_USAGE;
}
}
}
i += 1;
}
let Some(file_path) = file_path else {
eprintln!("사용법: rhwp export-tables <파일.hwp|파일.hwpx> [--json] [-o <출력.json>]");
return EXIT_USAGE;
};
let data = match fs::read(file_path) {
Ok(d) => d,
Err(e) => {
eprintln!("오류: 파일을 읽을 수 없습니다 - {}: {}", file_path, e);
return EXIT_RUNTIME;
}
};
let doc = match load_document(&data) {
Ok(d) => d,
Err(e) => return e.report(),
};
let tables = extract_tables(doc.document());
let envelope = tables_json_value(file_path, &tables);
if let Some(p) = out_path {
let json = match serde_json::to_string_pretty(&envelope) {
Ok(j) => j,
Err(e) => {
eprintln!("오류: JSON 직렬화 실패 - {}", e);
return EXIT_RUNTIME;
}
};
return match fs::write(&p, &json) {
Ok(_) => {
println!("표 추출 완료: {}개 → {}", tables.len(), p);
EXIT_OK
}
Err(e) => {
eprintln!("오류: 출력 쓰기 실패 - {}: {}", p, e);
EXIT_RUNTIME
}
};
}
if json_mode {
println!("{}", provenance::marked(envelope, "export-tables"));
return EXIT_OK;
}
println!("문서 로드: {} (표 {}개)", file_path, tables.len());
for t in &tables {
let merged = t
.cells
.iter()
.filter(|c| c.row_span > 1 || c.col_span > 1)
.count();
let nested = t.cells.iter().filter(|c| !c.nested.is_empty()).count();
println!(
" 표{} [구역{}:문단{}]: {}행×{}열, 셀 {}개 (병합 {}개, 중첩 {}개)",
t.index, t.section, t.paragraph, t.rows, t.cols, t.cell_count, merged, nested
);
}
EXIT_OK
}
pub(crate) fn table_to_csv(args: &[String]) -> i32 {
use rhwp::document_core::queries::table_csv::grid_to_csv;
use rhwp::document_core::queries::table_extract::extract_tables;
let mut file_path: Option<&str> = None;
let mut table_arg: Option<usize> = None;
let mut out_path: Option<String> = None;
let mut bom = false;
let mut json_mode = false;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"--json" => json_mode = true,
"--bom" => bom = true,
"--table" => {
i += 1;
match args.get(i).map(|v| v.parse::<usize>()) {
Some(Ok(value)) => table_arg = Some(value),
_ => {
eprintln!("오류: --table 뒤에 0 이상의 정수가 필요합니다.");
return EXIT_USAGE;
}
}
}
"-o" | "--out" | "--output" => {
i += 1;
match args.get(i) {
Some(p) => out_path = Some(p.clone()),
None => {
eprintln!("오류: -o 뒤에 출력 경로가 필요합니다.");
return EXIT_USAGE;
}
}
}
other if other.starts_with('-') => {
eprintln!("알 수 없는 옵션: {other}");
return EXIT_USAGE;
}
other => {
if file_path.replace(other).is_some() {
eprintln!("오류: 입력 파일은 하나만 지정할 수 있습니다: {other}");
return EXIT_USAGE;
}
}
}
i += 1;
}
let Some(file_path) = file_path else {
eprintln!(
"사용법: rhwp table-to-csv <파일.hwp|파일.hwpx> [--table <번호>] [-o <경로>] [--bom] [--json]"
);
return EXIT_USAGE;
};
let data = match fs::read(file_path) {
Ok(d) => d,
Err(e) => {
eprintln!("오류: 파일을 읽을 수 없습니다 - {}: {}", file_path, e);
return EXIT_RUNTIME;
}
};
let doc = match rhwp::wasm_api::HwpDocument::from_bytes(&data) {
Ok(d) => d,
Err(e) => {
eprintln!("오류: HWP 파싱 실패 - {}", e);
return EXIT_RUNTIME;
}
};
let grids = extract_tables(doc.document());
let top_level: Vec<&_> = grids
.iter()
.filter(|g| g.container_path.is_empty())
.collect();
let selected: Vec<&_> = match table_arg {
Some(n) => match top_level.iter().find(|g| g.index == n) {
Some(g) => vec![*g],
None => {
eprintln!(
"오류: 본문 최상위 표 {} 번이 없습니다 (최상위 표 {}개; 중첩 표는 v1 범위 밖).",
n,
top_level.len()
);
return EXIT_RUNTIME;
}
},
None => top_level.clone(),
};
let bodies: Vec<(usize, u16, u16, String)> = selected
.iter()
.map(|g| (g.index, g.rows, g.cols, grid_to_csv(g)))
.collect();
let mut written: Vec<Option<String>> = vec![None; bodies.len()];
if let Some(dest) = out_path.as_deref() {
if table_arg.is_some() {
let body = &bodies[0].3;
if let Err(e) = write_csv_file(dest, body, bom) {
eprintln!("오류: 출력 쓰기 실패 - {}: {}", dest, e);
return EXIT_RUNTIME;
}
written[0] = Some(dest.to_string());
} else {
if let Err(e) = fs::create_dir_all(dest) {
eprintln!("오류: 출력 폴더 생성 실패 - {}: {}", dest, e);
return EXIT_RUNTIME;
}
for (slot, (index, _, _, body)) in written.iter_mut().zip(bodies.iter()) {
let path = Path::new(dest).join(format!("table{index}.csv"));
let shown = path.to_string_lossy().to_string();
if let Err(e) = write_csv_file(&shown, body, bom) {
eprintln!("오류: 출력 쓰기 실패 - {}: {}", shown, e);
return EXIT_RUNTIME;
}
*slot = Some(shown);
}
}
}
if json_mode {
let tables: Vec<serde_json::Value> = bodies
.iter()
.zip(written.iter())
.map(|((index, rows, cols, body), out)| {
let mut entry = serde_json::json!({
"index": index,
"rowCount": rows,
"colCount": cols,
"csv": body,
});
if let Some(p) = out {
entry["output"] = serde_json::Value::String(p.clone());
}
entry
})
.collect();
let mut envelope = serde_json::json!({
"schemaVersion": ENVELOPE_SCHEMA_VERSION,
"source": file_path,
"tableCount": tables.len(),
"tables": tables,
"bom": bom,
});
if let Some(p) = out_path {
envelope["output"] = serde_json::Value::String(p);
envelope["outputFormat"] = serde_json::Value::String("csv".to_string());
}
println!("{}", provenance::marked(envelope, "table-to-csv"));
return EXIT_OK;
}
if out_path.is_some() {
println!("CSV 내보내기 완료: {} (표 {}개)", file_path, bodies.len());
for out in written.iter().flatten() {
println!(" {out}");
}
return EXIT_OK;
}
for (index, rows, cols, body) in &bodies {
if bodies.len() > 1 {
println!("# table{index} ({rows}x{cols})");
}
print!("{body}");
}
EXIT_OK
}
fn chart_matrix_from_envelope(
read: &serde_json::Value,
) -> (Vec<String>, Vec<String>, Vec<Vec<String>>, bool) {
let strings = |v: &serde_json::Value| -> Vec<String> {
v.as_array()
.map(|a| {
a.iter()
.map(|x| x.as_str().unwrap_or_default().to_string())
.collect()
})
.unwrap_or_default()
};
let labels = strings(&read["labels"]);
let series = read["series"].as_array().cloned().unwrap_or_default();
let names: Vec<String> = series
.iter()
.map(|s| s["name"].as_str().unwrap_or_default().to_string())
.collect();
let values: Vec<Vec<String>> = series.iter().map(|s| strings(&s["values"])).collect();
let scatter = read["axis"].as_str() == Some("scatter");
(labels, names, values, scatter)
}
pub(crate) fn chart_to_csv(args: &[String]) -> i32 {
use rhwp::document_core::queries::chart_csv::to_csv;
use rhwp::document_core::queries::chart_extract::collect_charts;
let mut file_path: Option<&str> = None;
let mut chart_arg: Option<usize> = None;
let mut out_path: Option<String> = None;
let mut bom = false;
let mut json_mode = false;
let mut i = 0;
while i < args.len() {
match args[i].as_str() {
"--json" => json_mode = true,
"--bom" => bom = true,
"--chart" => {
i += 1;
match args.get(i).map(|v| v.parse::<usize>()) {
Some(Ok(value)) if value >= 1 => chart_arg = Some(value),
_ => {
eprintln!("오류: --chart 뒤에 1 이상의 정수가 필요합니다.");
return EXIT_USAGE;
}
}
}
"-o" | "--out" | "--output" => {
i += 1;
match args.get(i) {
Some(p) => out_path = Some(p.clone()),
None => {
eprintln!("오류: -o 뒤에 출력 경로가 필요합니다.");
return EXIT_USAGE;
}
}
}
other if other.starts_with('-') => {
eprintln!("알 수 없는 옵션: {other}");
return EXIT_USAGE;
}
other => {
if file_path.replace(other).is_some() {
eprintln!("오류: 입력 파일은 하나만 지정할 수 있습니다: {other}");
return EXIT_USAGE;
}
}
}
i += 1;
}
let Some(file_path) = file_path else {
eprintln!(
"사용법: rhwp chart-to-csv <파일.hwp|파일.hwpx> [--chart <번호>] [-o <경로>] [--bom] [--json]"
);
return EXIT_USAGE;
};
let data = match fs::read(file_path) {
Ok(d) => d,
Err(e) => {
eprintln!("오류: 파일을 읽을 수 없습니다 - {}: {}", file_path, e);
return EXIT_RUNTIME;
}
};
let doc = match rhwp::wasm_api::HwpDocument::from_bytes(&data) {
Ok(d) => d,
Err(e) => {
eprintln!("오류: HWP 파싱 실패 - {}", e);
return EXIT_RUNTIME;
}
};
let chart_count = collect_charts(doc.document()).len();
let selected: Vec<usize> = match chart_arg {
Some(n) if n <= chart_count => vec![n - 1],
Some(n) => {
eprintln!("오류: 차트 {} 번이 없습니다 (차트 {}개).", n, chart_count);
return EXIT_RUNTIME;
}
None => (0..chart_count).collect(),
};
let mut bodies: Vec<(usize, usize, usize, String)> = Vec::new();
for index in selected {
let read: serde_json::Value = match doc.get_chart_data_by_index_native(index).map(|s| {
serde_json::from_str::<serde_json::Value>(&s).unwrap_or(serde_json::Value::Null)
}) {
Ok(v) => v,
Err(e) => {
eprintln!("오류: 차트 {} 읽기 실패 - {:?}", index + 1, e);
return EXIT_RUNTIME;
}
};
if read["ok"] != true {
eprintln!(
"오류: 차트 {} 를 읽을 수 없습니다 - {}",
index + 1,
read["invalid"][0]["message"]
.as_str()
.unwrap_or("사유 미상")
);
return EXIT_RUNTIME;
}
if read["labelsShared"] != true {
let axis = if read["axis"].as_str() == Some("scatter") {
"X 값"
} else {
"카테고리 라벨"
};
eprintln!(
"오류: 차트 {}는 계열마다 {}이 달라 CSV 한 열로 안전하게 표현할 수 없습니다.",
index + 1,
axis
);
return EXIT_RUNTIME;
}
let (labels, names, values, scatter) = chart_matrix_from_envelope(&read);
let rows = values
.iter()
.map(|s| s.len())
.chain(std::iter::once(labels.len()))
.max()
.unwrap_or(0);
bodies.push((
index + 1,
rows,
names.len(),
to_csv(&labels, &names, &values, scatter),
));
}
let mut written: Vec<Option<String>> = vec![None; bodies.len()];
if let Some(dest) = out_path.as_deref() {
if chart_arg.is_some() {
if let Err(e) = write_csv_file(dest, &bodies[0].3, bom) {
eprintln!("오류: 출력 쓰기 실패 - {}: {}", dest, e);
return EXIT_RUNTIME;
}
written[0] = Some(dest.to_string());
} else {
if let Err(e) = fs::create_dir_all(dest) {
eprintln!("오류: 출력 폴더 생성 실패 - {}: {}", dest, e);
return EXIT_RUNTIME;
}
for (slot, (number, _, _, body)) in written.iter_mut().zip(bodies.iter()) {
let path = Path::new(dest).join(format!("chart{number}.csv"));
let shown = path.to_string_lossy().to_string();
if let Err(e) = write_csv_file(&shown, body, bom) {
eprintln!("오류: 출력 쓰기 실패 - {}: {}", shown, e);
return EXIT_RUNTIME;
}
*slot = Some(shown);
}
}
}
if json_mode {
let charts: Vec<serde_json::Value> = bodies
.iter()
.zip(written.iter())
.map(|((number, rows, cols, body), out)| {
let mut entry = serde_json::json!({
"chart": number,
"rowCount": rows,
"colCount": cols,
"csv": body,
});
if let Some(p) = out {
entry["output"] = serde_json::Value::String(p.clone());
}
entry
})
.collect();
let mut envelope = serde_json::json!({
"schemaVersion": ENVELOPE_SCHEMA_VERSION,
"source": file_path,
"chartCount": charts.len(),
"charts": charts,
"bom": bom,
});
if let Some(p) = out_path {
envelope["output"] = serde_json::Value::String(p);
envelope["outputFormat"] = serde_json::Value::String("csv".to_string());
}
println!("{}", provenance::marked(envelope, "chart-to-csv"));
return EXIT_OK;
}
if out_path.is_some() {
println!(
"차트 CSV 내보내기 완료: {} (차트 {}개)",
file_path,
bodies.len()
);
for out in written.iter().flatten() {
println!(" {out}");
}
return EXIT_OK;
}
for (number, rows, cols, body) in &bodies {
if bodies.len() > 1 {
println!("# chart{number} ({rows}x{cols})");
}
print!("{body}");
}
EXIT_OK
}
fn write_csv_file(path: &str, body: &str, bom: bool) -> std::io::Result<()> {
use rhwp::document_core::queries::table_csv::UTF8_BOM;
let mut bytes = Vec::with_capacity(body.len() + 3);
if bom {
bytes.extend_from_slice(UTF8_BOM.as_bytes());
}
bytes.extend_from_slice(body.as_bytes());
fs::write(path, bytes)
}