use std::os::unix::prelude::OsStrExt;
use hyper::http::request::Parts;
use serde::Deserialize;
use sputnik::html_escape;
use crate::{action_links, controller::Controller, get_renderer, Context, Error, Page, Response};
const FILE_EXT_WITH_SPELLCHECK: &[&[u8]] = &[b"md", b"txt"];
fn render_error(message: &str) -> String {
format!("
error: {}
", html_escape(message))
}
#[derive(Deserialize, Default)]
pub struct EditForm {
pub text: String,
pub msg: Option,
pub oid: Option,
}
pub fn edit_text_form<'a, C: Controller>(
data: &EditForm,
error: Option<&str>,
controller: &'a C,
ctx: &'a Context,
parts: &Parts,
) -> Page {
let mut page = Page {
title: format!(
"{} {}",
if data.oid.is_some() {
"Editing"
} else {
"Creating"
},
ctx.path.file_name().unwrap().to_str().unwrap()
),
header: data
.oid
.is_some()
.then(|| action_links("edit", controller, ctx, parts))
.unwrap_or_default(),
scripts: vec![include_str!("static/edit_script.js")],
script_src: vec![include_str!("static/edit_script.js.sha")],
..Default::default()
};
if let Some(access_info_html) = controller.access_info_html(&ctx, parts) {
page.body.push_str(&access_info_html);
}
if let Some(hint_html) = controller.edit_hint_html(ctx) {
page.body
.push_str(&format!("{}
", hint_html));
}
if let Some(error) = error {
page.body.push_str(&render_error(error));
}
page.body.push_str(&format!(
"",
html_escape(data.msg.as_deref().unwrap_or_default())
));
page
}
#[derive(Deserialize)]
pub struct MoveForm {
pub dest: String,
pub msg: Option,
}
pub fn move_form(
filename: &str,
data: &MoveForm,
error: Option<&str>,
controller: &C,
ctx: &Context,
parts: &Parts,
) -> Result {
let mut page = Page {
title: format!("Move {}", filename),
header: action_links("move", controller, ctx, parts),
..Default::default()
};
if let Some(error) = error {
page.body.push_str(&render_error(error));
}
page.body.push_str(&format!(
"",
html_escape(&data.dest),
data.msg.as_ref().map(html_escape).unwrap_or_default(),
));
Ok(page.into())
}
pub fn upload_form<'a, C: Controller>(
file_exists: bool,
controller: &'a C,
ctx: &'a Context,
parts: &Parts,
) -> Page {
let filename = ctx.path.file_name().unwrap().to_str().unwrap();
Page {
title: format!("Uploading {}", filename),
// TODO: add input for commit message
body: ""
.into(),
header: file_exists
.then(|| action_links("edit", controller, &ctx, parts))
.unwrap_or_default(),
..Default::default()
}
}