From d3681e1699cca509f01a76c0fc59b0013d7fb992 Mon Sep 17 00:00:00 2001 From: nobody Date: Sun, 25 Aug 2024 21:14:05 +0200 Subject: [PATCH] mediawiki/pandoc: call directly instead of via lib Pandoc crate is confusing and badly documented. Using std::process::command now to avoid temporary files. --- Cargo.toml | 1 - src/mediawiki.rs | 44 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4d9bee8..1321446 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,6 @@ edition = "2021" [dependencies] stdext = "0.3.3" -pandoc = "0.8" chrono = "0.4.38" regex = "1.10.5" futures = "0.3.30" diff --git a/src/mediawiki.rs b/src/mediawiki.rs index ccf33b3..c780443 100644 --- a/src/mediawiki.rs +++ b/src/mediawiki.rs @@ -1,10 +1,10 @@ +use std::cell::OnceCell; use std::error::Error; use std::fs::File; -use std::io::Read; -use std::cell::OnceCell; +use std::io::{Read, Write}; +use std::process::{Command, Output, Stdio}; use colored::Colorize; -use pandoc::{PandocError, PandocOutput}; use reqwest::blocking::Client; use serde::Deserialize; use serde_json::json; @@ -283,18 +283,41 @@ pub fn pad_ins_wiki(old_pad_content: String, wiki: &Mediawiki) -> Result<(), Box } /// Converts one file type into another using pandoc and saves the result as a txt file -fn pandoc_convert( - old_pad_content: String, output_filepath: &str, input_format: pandoc::InputFormat, - output_format: pandoc::OutputFormat, -) -> Result { +fn pandoc_convert(old_pad_content: String) -> Result> { + let mut cmd = Command::new("pandoc") + .args(["--from", "markdown", "--to", "mediawiki", "--no-highlight"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + if let Some(mut stdin) = cmd.stdin.take() { + stdin.write_all(old_pad_content.as_bytes())?; + } + let mut output = String::new(); + if let Some(mut stdout) = cmd.stdout.take() { + stdout.read_to_string(&mut output)?; + } + let mut errmsg = String::new(); + if let Some(mut stderr) = cmd.stderr.take() { + stderr.read_to_string(&mut errmsg)?; + } + let status = cmd.wait()?; + if status.success() { + Ok(output) + } else { + Err( format!("Pandoc error, exit {:?}\n{}", status, errmsg).into() ) + } + /* //Convert Markdown into Mediawiki // Vanilla pandoc Befehl: pandoc --from markdown --to mediawiki --no-highlight let mut p = pandoc::new(); p.set_input(pandoc::InputKind::Pipe(old_pad_content)); p.set_input_format(input_format, vec![]); - p.set_output(pandoc::OutputKind::File(output_filepath.parse().unwrap())); + p.set_output(pandoc::OutputKind::Pipe); // File(output_filepath.parse().unwrap())); p.set_output_format(output_format, vec![]); - p.execute() + let output = p.execute()?; + Ok(output.into()) + */ } @@ -313,7 +336,8 @@ fn convert_md_to_mediawiki(old_pad_content: String) -> String { // TODO: use tempfile="3.3", make it a NamedTempFile::new()?; // or alternatively use piped stdout to avoid files entirely let output_filepath: &str = "./pandoc_mediawiki.txt"; - pandoc_convert(old_pad_content, output_filepath, pandoc::InputFormat::Markdown, pandoc::OutputFormat::MediaWiki).expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei"); + pandoc_convert(old_pad_content) + .expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei"); let temp = read_txt_file(output_filepath); println!("TEMP: {}", temp.purple()); temp