mediawiki: more follow-up cleanup
- use macros for conditional status messages - abstract out a pipe() fn
This commit is contained in:
parent
d3681e1699
commit
b04d35ee6b
27
src/lib.rs
27
src/lib.rs
|
@ -7,6 +7,33 @@ pub mod mediawiki;
|
|||
pub mod template;
|
||||
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
use std::io::{Read, Write};
|
||||
use std::process::{Command, ExitStatus, Stdio};
|
||||
|
||||
fn pipe(
|
||||
program: &str, args: &mut [&str], input: String,
|
||||
) -> Result<(String, String, ExitStatus), Box<dyn Error>> {
|
||||
let mut cmd = Command::new(program)
|
||||
.args(args)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()?;
|
||||
if let Some(mut stdin) = cmd.stdin.take() {
|
||||
stdin.write_all(input.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()?;
|
||||
Ok((output, errmsg, status))
|
||||
}
|
||||
|
||||
/// Checks environment variable `DRY_RUN` to see if any external operations
|
||||
/// should *actually* be done.
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
use std::cell::OnceCell;
|
||||
use std::error::Error;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::process::{Command, Output, Stdio};
|
||||
|
||||
use colored::Colorize;
|
||||
use reqwest::blocking::Client;
|
||||
|
@ -10,6 +7,7 @@ use serde::Deserialize;
|
|||
use serde_json::json;
|
||||
|
||||
use crate::config_spec::{CfgField, CfgGroup};
|
||||
use crate::{trace_var, verboseln};
|
||||
|
||||
pub const CONFIG: CfgGroup<'static> = CfgGroup {
|
||||
name: "wiki",
|
||||
|
@ -204,7 +202,7 @@ impl Mediawiki {
|
|||
pub fn update_plenum_page (&self, new_page_title_to_link_to: &str) -> Result<(), Box<dyn Error>> {
|
||||
// 1. Download Plenum page content
|
||||
let page_content = self.get_page_content(&self.plenum_main_page_name)?;
|
||||
println!("---\nPage Content: {}\n---", page_content.red());
|
||||
trace_var!(page_content);
|
||||
let current_year = "2024"; // TODO: Datumslogik einbauen
|
||||
let year_section = format!("=== {} ===\n", current_year);
|
||||
if page_content.contains(&year_section) {
|
||||
|
@ -265,16 +263,16 @@ impl Mediawiki {
|
|||
pub fn pad_ins_wiki(old_pad_content: String, wiki: &Mediawiki) -> Result<(), Box<dyn Error>> {
|
||||
// Login to Wiki and get required tokens for logging in and writing
|
||||
wiki.get_login_token()?;
|
||||
eprintln!("AUTH Done");
|
||||
verboseln!("Login token acquired.");
|
||||
let login_result = wiki.login()?;
|
||||
eprintln!("LOGIN Done");
|
||||
verboseln!("Login done.");
|
||||
trace_var!(login_result);
|
||||
wiki.get_csrf_token()?;
|
||||
eprintln!("CSRF Done");
|
||||
eprintln!("---LOGIN RESULT:---\n{:?}\n-----------", login_result);
|
||||
verboseln!("CSRF token acquired.");
|
||||
|
||||
// Convert to mediawiki and make new page
|
||||
let pad_converted = convert_md_to_mediawiki(old_pad_content);
|
||||
eprintln!("Das kommt ins Wiki: {}", pad_converted);
|
||||
let pad_converted = pandoc_convert(old_pad_content)?;
|
||||
trace_var!(pad_converted);
|
||||
let page_title = "Page Test 5";
|
||||
let page_title = format!("{}/{}", wiki.plenum_main_page_name, page_title); // Example: Plenum/13._August_2024
|
||||
wiki.new_wiki_page(&page_title, &pad_converted)?;
|
||||
|
@ -282,65 +280,18 @@ pub fn pad_ins_wiki(old_pad_content: String, wiki: &Mediawiki) -> Result<(), Box
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Converts one file type into another using pandoc and saves the result as a txt file
|
||||
fn pandoc_convert(old_pad_content: String) -> Result<String, Box<dyn Error>> {
|
||||
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()?;
|
||||
/// Takes a String in the Markdown format and returns a String in the mediawiki Format
|
||||
fn pandoc_convert(markdown: String) -> Result<String, Box<dyn Error>> {
|
||||
let (output, errors, status) = crate::pipe(
|
||||
"pandoc",
|
||||
&mut ["--from", "markdown", "--to", "mediawiki", "--no-highlight"],
|
||||
markdown,
|
||||
)?;
|
||||
if status.success() {
|
||||
Ok(output)
|
||||
} else {
|
||||
Err( format!("Pandoc error, exit {:?}\n{}", status, errmsg).into() )
|
||||
Err(format!("Pandoc error, exit code {:?}\n{}", status, errors).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::Pipe); // File(output_filepath.parse().unwrap()));
|
||||
p.set_output_format(output_format, vec![]);
|
||||
let output = p.execute()?;
|
||||
Ok(output.into())
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
/// Reads a text file from a specified path and returns it as a String
|
||||
fn read_txt_file(filepath: &str) -> String {
|
||||
let mut file = File::open(filepath)
|
||||
.unwrap_or_else(|_| panic!("Fehler beim öffnen der Textdatei mit Pfad {filepath}!"));
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)
|
||||
.expect("Fehler beim auslesen der MediaWiki-Textdatei!");
|
||||
contents
|
||||
}
|
||||
|
||||
/// Takes a Sting in the Markdown format and returns a String in the mediawiki Format
|
||||
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)
|
||||
.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
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in a new issue