natter/src/intermediate/regular_link.rs

151 lines
4.5 KiB
Rust
Raw Normal View History

2023-12-21 14:56:58 -05:00
use organic::types::StandardProperties;
2023-12-23 20:45:20 -05:00
use url::Url;
2023-12-21 14:56:58 -05:00
use super::get_web_path;
use super::macros::intermediate;
2023-12-23 22:12:05 -05:00
use super::IntermediateContext;
2023-10-29 22:31:29 -04:00
2023-10-27 20:43:57 -04:00
use super::IObject;
use crate::context::RenderContext;
use crate::error::CustomError;
2023-10-27 17:48:19 -04:00
2023-10-29 15:36:15 -04:00
#[derive(Debug, Clone)]
2023-10-27 20:43:57 -04:00
pub(crate) struct IRegularLink {
pub(crate) raw_link: String,
2023-10-27 20:43:57 -04:00
pub(crate) children: Vec<IObject>,
pub(crate) target: LinkTarget,
2023-12-21 14:56:58 -05:00
pub(crate) post_blank: organic::types::PostBlank,
2023-10-27 20:43:57 -04:00
}
2023-10-27 17:48:19 -04:00
intermediate!(
IRegularLink,
&'orig organic::types::RegularLink<'parse>,
original,
intermediate_context,
{
let children = {
let mut ret = Vec::new();
for obj in original.children.iter() {
ret.push(IObject::new(intermediate_context.clone(), obj).await?);
}
ret
};
let raw_link = original.get_raw_link();
2023-12-23 22:12:05 -05:00
let target =
LinkTarget::from_string(intermediate_context.clone(), raw_link.clone().into_owned())?;
Ok(IRegularLink {
raw_link: raw_link.into_owned(),
children,
target,
2023-12-21 14:56:58 -05:00
post_blank: original.get_post_blank(),
})
}
);
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum LinkTarget {
Raw(String),
2023-12-23 21:29:59 -05:00
Post {
post_id: Option<String>,
subpath: String,
},
2023-12-23 22:12:05 -05:00
Target {
target_id: String,
},
}
impl LinkTarget {
2023-12-23 22:12:05 -05:00
pub(crate) fn from_string(
intermediate_context: IntermediateContext<'_, '_>,
input: String,
) -> Result<LinkTarget, CustomError> {
let parsed = Url::parse(&input);
if let Err(url::ParseError::RelativeUrlWithoutBase) = parsed {
let target_id = {
let mut registry = intermediate_context.registry.lock().unwrap();
let target_id = registry.get_target(input).to_owned();
target_id
};
return Ok(LinkTarget::Target { target_id });
}
let parsed = parsed?;
match parsed.scheme() {
"post" => {
let post_id = parsed.host_str().map(str::to_owned);
let subpath = {
let subpath = parsed.path();
if let Some(subpath) = subpath.strip_prefix('/') {
subpath
} else {
subpath
}
};
Ok(LinkTarget::Post {
post_id,
subpath: subpath.to_owned(),
})
}
2023-12-23 22:12:05 -05:00
_ => Ok(LinkTarget::Raw(input.to_owned())),
}
}
pub(crate) fn generate_final_target(
&self,
render_context: RenderContext<'_>,
) -> Result<Option<String>, CustomError> {
match self {
LinkTarget::Raw(raw_link) => Ok(Some(raw_link.clone())),
2023-12-23 21:29:59 -05:00
LinkTarget::Post { post_id, subpath } => {
let path = post_id
.as_ref()
.map(|post_id| {
2023-12-23 21:29:59 -05:00
let path_to_post = render_context
.config
.get_relative_path_to_post(post_id)
.join(subpath);
get_web_path(
render_context.config,
render_context.output_root_directory,
render_context.output_file,
2023-12-23 21:29:59 -05:00
path_to_post,
)
})
.map_or(Ok(None), |r| r.map(Some))?;
Ok(path)
}
2023-12-23 22:12:05 -05:00
LinkTarget::Target { target_id } => Ok(Some(format!(
"#{}{}",
render_context
.id_addition
.map(|id_addition| format!("sec{}.", id_addition))
.unwrap_or_default(),
target_id
))),
}
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use std::sync::Mutex;
use crate::intermediate::Registry;
use super::*;
#[test]
fn link_target_raw() -> Result<(), CustomError> {
let registry = Registry::new();
let registry = Arc::new(Mutex::new(registry));
let intermediate_context = IntermediateContext::new(registry)?;
for inp in ["https://test.example/foo"] {
assert_eq!(
LinkTarget::from_string(intermediate_context.clone(), inp.to_owned())?,
LinkTarget::Raw(inp.to_owned())
);
}
Ok(())
}
}