use crate::data::{PageInfo, UserInfo};
use crate::settings::WikitextSettings;
use crate::tree::{ImageSource, LinkLabel, LinkLocation, Module};
use crate::url::BuildSiteUrl;
use std::borrow::Cow;
use std::num::NonZeroUsize;
use wikidot_normalize::normalize;
#[derive(Debug)]
pub struct Handle;
impl Handle {
pub fn render_module(&self, buffer: &mut String, module: &Module) {
debug!("Rendering module '{}'", module.name());
str_write!(buffer, "<p>TODO: module {}</p>", module.name());
}
pub fn get_page_title(&self, _site: &str, _page: &str) -> Option<String> {
debug!("Fetching page title");
Some(format!("TODO: actual title ({_site} {_page})"))
}
pub fn get_page_exists(&self, _site: &str, _page: &str) -> bool {
debug!("Checking page existence");
#[cfg(test)]
if _page == "missing" {
return false;
}
true
}
pub fn get_user_info<'a>(&self, name: &'a str) -> Option<UserInfo<'a>> {
debug!("Fetching user info (name '{name}')");
let mut info = UserInfo::dummy();
info.user_name = cow!(name);
info.user_profile_url = Cow::Owned(format!("/user:info/{name}"));
Some(info)
}
pub fn get_image_link<'a>(
&self,
source: &ImageSource<'a>,
info: &PageInfo,
settings: &WikitextSettings,
) -> Option<Cow<'a, str>> {
debug!("Getting file link for image");
let (site, page, file): (&str, &str, &str) = match source {
ImageSource::Url(url) => return Some(Cow::clone(url)),
ImageSource::File1 { .. }
| ImageSource::File2 { .. }
| ImageSource::File3 { .. }
if !settings.allow_local_paths =>
{
warn!("Specified path image source when local paths are disabled");
return None;
}
ImageSource::File1 { file } => (&info.site, &info.page, file),
ImageSource::File2 { page, file } => (&info.site, page, file),
ImageSource::File3 { site, page, file } => (site, page, file),
};
Some(Cow::Owned(format!(
"https://{site}.wjfiles.com/local--files/{page}/{file}",
)))
}
pub fn get_link_label<F>(
&self,
site: &str,
link: &LinkLocation,
label: &LinkLabel,
f: F,
) where
F: FnOnce(&str),
{
let page_title;
let label_text = match *label {
LinkLabel::Text(ref text) => text,
LinkLabel::Url(Some(ref text)) => text,
LinkLabel::Url(None) => match link {
LinkLocation::Url(url) => url,
LinkLocation::Page(page_ref) => page_ref.page(),
},
LinkLabel::Page => match link {
LinkLocation::Url(_) => {
panic!("Requested link label of page for a URL");
}
LinkLocation::Page(page_ref) => {
let (site, page) = page_ref.fields_or(site);
page_title = match self.get_page_title(site, page) {
Some(title) => title,
None => page_ref.to_string(),
};
&page_title
}
},
};
f(label_text);
}
pub fn get_message(&self, language: &str, message: &str) -> &'static str {
debug!("Fetching message (language {language}, key {message})");
let _ = language;
match message {
"button-copy-clipboard" => "Copy to Clipboard",
"collapsible-open" => "+ open block",
"collapsible-hide" => "- hide block",
"table-of-contents" => "Table of Contents",
"footnote" => "Footnote",
"footnote-block-title" => "Footnotes",
"bibliography-reference" => "Reference",
"bibliography-block-title" => "Bibliography",
"bibliography-cite-not-found" => "Bibliography item not found",
"image-context-bad" => "No images in this context",
_ => {
error!("Unknown message requested (key {message})");
"?"
}
}
}
pub fn post_html(&self, info: &PageInfo, html: &str) -> String {
debug!("Submitting HTML to create iframe-able snippet");
let _ = info;
let _ = html;
str!("https://example.com/")
}
pub fn post_code(&self, index: NonZeroUsize, code: &str) {
debug!("Submitting code snippet (index {})", index.get());
let _ = index;
let _ = code;
}
}
impl BuildSiteUrl for Handle {
fn build_url(&self, site: &str, path: &str) -> String {
let path = {
let mut path = str!(path);
normalize(&mut path);
path
};
format!("https://{site}.wikijump.com/{path}")
}
}