2024-08-05 04:45:34 +02:00
|
|
|
use crate::config_spec::{CfgField, CfgGroup};
|
2024-08-19 04:45:20 +02:00
|
|
|
use regex::Regex;
|
2024-08-02 22:22:11 +02:00
|
|
|
use reqwest::blocking::Client;
|
2024-08-02 22:29:22 +02:00
|
|
|
use reqwest::blocking::Response;
|
2024-08-01 18:30:14 +02:00
|
|
|
use std::error::Error;
|
|
|
|
|
2024-08-05 04:45:34 +02:00
|
|
|
pub const CONFIG: CfgGroup<'static> = CfgGroup {
|
|
|
|
name: "hedgedoc",
|
|
|
|
description: "HedgeDoc markdown pad server settings",
|
|
|
|
fields: &[
|
|
|
|
CfgField::Default {
|
|
|
|
key: "server-url",
|
|
|
|
default: "https://md.berlin.ccc.de",
|
|
|
|
description: "Hedgedoc server storing the pads.",
|
|
|
|
},
|
|
|
|
CfgField::Default {
|
|
|
|
key: "template-name",
|
|
|
|
default: "plenum-template",
|
|
|
|
description: "Name of the pad containing the template to use.",
|
|
|
|
},
|
2024-08-05 16:10:25 +02:00
|
|
|
CfgField::Generated {
|
2024-08-05 14:30:48 +02:00
|
|
|
key: "last-id",
|
|
|
|
generator: make_pad_id,
|
|
|
|
generator_description: "Makes a new pad that's completely empty.",
|
|
|
|
description: "ID of last plenum's pad.",
|
|
|
|
},
|
2024-08-05 16:10:25 +02:00
|
|
|
CfgField::Generated {
|
2024-08-05 14:30:48 +02:00
|
|
|
key: "next-id",
|
|
|
|
generator: make_pad_id,
|
|
|
|
generator_description: "Makes a new pad that's completely empty.",
|
|
|
|
description: "ID of next plenum's pad.",
|
|
|
|
},
|
2024-08-05 04:45:34 +02:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2024-08-17 21:52:54 +02:00
|
|
|
#[derive(Debug)]
|
2024-08-01 18:30:14 +02:00
|
|
|
pub struct HedgeDoc {
|
2024-08-02 22:29:22 +02:00
|
|
|
server_url: String,
|
|
|
|
is_dry_run: bool,
|
|
|
|
client: Client,
|
2024-08-01 18:30:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HedgeDoc {
|
2024-08-02 22:29:22 +02:00
|
|
|
pub fn new(server_url: &str, is_dry_run: bool) -> Self {
|
2024-08-01 18:30:14 +02:00
|
|
|
Self { server_url: server_url.to_string(), is_dry_run, client: Client::new() }
|
|
|
|
}
|
|
|
|
|
2024-08-02 22:29:22 +02:00
|
|
|
pub fn format_url(&self, pad_name: &str) -> String {
|
|
|
|
format!("{}/{}", self.server_url, pad_name)
|
2024-08-01 18:30:14 +02:00
|
|
|
}
|
|
|
|
|
2024-08-02 22:29:22 +02:00
|
|
|
fn format_action(&self, pad_name: &str, verb: &str) -> String {
|
|
|
|
format!("{}/{}/{}", self.server_url, pad_name, verb)
|
2024-08-01 18:30:14 +02:00
|
|
|
}
|
|
|
|
|
2024-08-02 22:29:22 +02:00
|
|
|
fn do_request(&self, url: &str) -> Result<Response, Box<dyn Error>> {
|
|
|
|
Ok(self.client.get(url).send().unwrap())
|
2024-08-01 18:30:14 +02:00
|
|
|
}
|
|
|
|
|
2024-08-02 22:29:22 +02:00
|
|
|
fn get_id_from_response(&self, res: Response) -> String {
|
|
|
|
res.url().to_string().trim_start_matches(&format!("{}/", self.server_url)).to_string()
|
2024-08-01 18:30:14 +02:00
|
|
|
}
|
|
|
|
|
2024-08-02 22:29:22 +02:00
|
|
|
pub fn download(&self, pad_name: &str) -> Result<String, Box<dyn Error>> {
|
|
|
|
Ok(self.do_request(&self.format_action(pad_name, "download"))?.text()?)
|
2024-08-01 18:30:14 +02:00
|
|
|
}
|
|
|
|
|
2024-08-02 22:29:22 +02:00
|
|
|
pub fn create_pad(&self) -> Result<String, Box<dyn Error>> {
|
2024-08-05 14:30:48 +02:00
|
|
|
if self.is_dry_run {
|
|
|
|
todo!("NYI: sane dry-run behavior")
|
|
|
|
}
|
2024-08-02 22:29:22 +02:00
|
|
|
let res = self.do_request(&format!("{}/new", self.server_url)).unwrap();
|
2024-08-01 18:30:14 +02:00
|
|
|
if res.status().is_success() {
|
|
|
|
Ok(self.get_id_from_response(res))
|
|
|
|
} else {
|
2024-08-02 22:29:22 +02:00
|
|
|
Err(format!("Failed to create pad {}", res.status()).into())
|
2024-08-01 18:30:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-02 22:29:22 +02:00
|
|
|
pub fn import_note(&self, id: Option<&str>, content: String) -> Result<String, Box<dyn Error>> {
|
2024-08-05 14:30:48 +02:00
|
|
|
if self.is_dry_run {
|
|
|
|
todo!("NYI: sane dry-run behavior")
|
|
|
|
}
|
2024-08-01 18:30:14 +02:00
|
|
|
let url = match id {
|
2024-08-02 22:29:22 +02:00
|
|
|
Some(id) => self.format_url(&format!("new/{id}")),
|
2024-08-01 18:30:14 +02:00
|
|
|
None => self.format_url("new"),
|
|
|
|
};
|
|
|
|
|
2024-08-02 22:29:22 +02:00
|
|
|
let res =
|
|
|
|
self.client.post(&url).header("Content-Type", "text/markdown").body(content).send()?;
|
2024-08-01 18:30:14 +02:00
|
|
|
|
|
|
|
if res.status().is_success() {
|
|
|
|
Ok(self.get_id_from_response(res))
|
|
|
|
} else {
|
2024-08-02 22:29:22 +02:00
|
|
|
Err(format!("Failed to import note: {}", res.status()).into())
|
2024-08-01 18:30:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-08-05 14:30:48 +02:00
|
|
|
|
2024-08-19 04:45:20 +02:00
|
|
|
pub fn summarize(pad_content: String) -> String {
|
|
|
|
// 1. remove HTML comments
|
|
|
|
let re_comment = Regex::new(r"(?s)<!--.*?-->").unwrap();
|
|
|
|
let pad_content = re_comment.replace_all(&pad_content, "").to_string();
|
|
|
|
// 2. accumulate topic lines
|
|
|
|
let re_header = Regex::new(r"^\s*##(#*) TOP ([\d.]+\s*.*?)\s*#*$").unwrap();
|
|
|
|
let mut result: Vec<String> = Vec::new();
|
|
|
|
for line in pad_content.lines() {
|
|
|
|
if let Some(captures) = re_header.captures(line) {
|
|
|
|
let indent = " ".repeat(captures.get(1).unwrap().as_str().len());
|
|
|
|
let title = captures.get(2).unwrap().as_str();
|
|
|
|
result.push(format!("{}{}", indent, title));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.join("\n")
|
|
|
|
}
|
|
|
|
|
2024-08-05 14:30:48 +02:00
|
|
|
/// For the config, make a new pad ID (by actually making a pad.)
|
2024-08-20 08:38:20 +02:00
|
|
|
fn make_pad_id(
|
|
|
|
_key: &str, config: &crate::key_value::KeyValueStore, is_dry_run: bool,
|
|
|
|
) -> Result<String, Box<dyn Error>> {
|
2024-08-05 14:30:48 +02:00
|
|
|
HedgeDoc::new(&config.get("hedgedoc-server-url").unwrap(), is_dry_run).create_pad()
|
|
|
|
}
|