Add target.

This commit is contained in:
Tom Alexander 2023-10-27 14:54:54 -04:00
parent c6cf5f75ac
commit 7b01230234
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
9 changed files with 80 additions and 15 deletions

View File

@ -6,6 +6,7 @@ mod heading;
mod object;
mod plain_text;
mod section;
mod target;
pub(crate) use blog_post_page::RenderBlogPostPage;
pub(crate) use document_element::RenderDocumentElement;

View File

@ -7,11 +7,13 @@ use crate::error::CustomError;
use crate::intermediate::IObject;
use super::plain_text::RenderPlainText;
use super::target::RenderTarget;
#[derive(Debug, Serialize)]
#[serde(untagged)]
pub(crate) enum RenderObject {
PlainText(RenderPlainText),
Target(RenderTarget),
}
impl RenderObject {
@ -28,6 +30,12 @@ impl RenderObject {
output_file,
inner,
)?)),
IObject::Target(inner) => Ok(RenderObject::Target(RenderTarget::new(
config,
output_directory,
output_file,
inner,
)?)),
}
}
}

27
src/context/target.rs Normal file
View File

@ -0,0 +1,27 @@
use std::path::Path;
use serde::Serialize;
use crate::config::Config;
use crate::error::CustomError;
use crate::intermediate::ITarget;
#[derive(Debug, Serialize)]
#[serde(tag = "type")]
#[serde(rename = "target")]
pub(crate) struct RenderTarget {
id: String,
}
impl RenderTarget {
pub(crate) fn new<D: AsRef<Path>, F: AsRef<Path>>(
config: &Config,
output_directory: D,
output_file: F,
target: &ITarget,
) -> Result<RenderTarget, CustomError> {
Ok(RenderTarget {
id: target.id.clone(),
})
}
}

View File

@ -10,9 +10,9 @@ pub(crate) struct IHeading {
}
impl IHeading {
pub(crate) fn new(
registry: &mut Registry<'_>,
heading: &organic::types::Heading<'_>,
pub(crate) fn new<'parse>(
registry: &mut Registry<'parse>,
heading: &organic::types::Heading<'parse>,
) -> Result<IHeading, CustomError> {
let title = heading
.title

View File

@ -8,6 +8,7 @@ mod page;
mod plain_text;
mod registry;
mod section;
mod target;
mod util;
pub(crate) use convert::convert_blog_post_page_to_render_context;
pub(crate) use definition::BlogPost;
@ -18,3 +19,4 @@ pub(crate) use object::IObject;
pub(crate) use page::BlogPostPage;
pub(crate) use plain_text::IPlainText;
pub(crate) use section::ISection;
pub(crate) use target::ITarget;

View File

@ -2,16 +2,18 @@ use crate::error::CustomError;
use super::plain_text::IPlainText;
use super::registry::Registry;
use super::ITarget;
#[derive(Debug)]
pub(crate) enum IObject {
PlainText(IPlainText),
Target(ITarget),
}
impl IObject {
pub(crate) fn new(
registry: &mut Registry<'_>,
obj: &organic::types::Object<'_>,
pub(crate) fn new<'parse>(
registry: &mut Registry<'parse>,
obj: &organic::types::Object<'parse>,
) -> Result<IObject, CustomError> {
match obj {
organic::types::Object::Bold(_) => todo!(),
@ -38,7 +40,9 @@ impl IObject {
organic::types::Object::InlineBabelCall(_) => todo!(),
organic::types::Object::InlineSourceBlock(_) => todo!(),
organic::types::Object::LineBreak(_) => todo!(),
organic::types::Object::Target(_) => todo!(),
organic::types::Object::Target(target) => {
Ok(IObject::Target(ITarget::new(registry, target)?))
}
organic::types::Object::StatisticsCookie(_) => todo!(),
organic::types::Object::Subscript(_) => todo!(),
organic::types::Object::Superscript(_) => todo!(),

View File

@ -18,10 +18,10 @@ pub(crate) struct BlogPostPage {
}
impl BlogPostPage {
pub(crate) fn new<P: Into<PathBuf>>(
pub(crate) fn new<'parse, P: Into<PathBuf>>(
path: P,
registry: &mut Registry<'_>,
document: &organic::types::Document<'_>,
registry: &mut Registry<'parse>,
document: &organic::types::Document<'parse>,
) -> Result<BlogPostPage, CustomError> {
let path = path.into();
let mut children = Vec::new();

View File

@ -2,20 +2,20 @@ use std::collections::HashMap;
type IdCounter = u16;
pub(crate) struct Registry<'p> {
pub(crate) struct Registry<'parse> {
id_counter: IdCounter,
targets: HashMap<&'p str, String>,
targets: HashMap<&'parse str, String>,
}
impl<'p> Registry<'p> {
pub(crate) fn new() -> Registry<'p> {
impl<'parse> Registry<'parse> {
pub(crate) fn new() -> Registry<'parse> {
Registry {
id_counter: 0,
targets: HashMap::new(),
}
}
pub(crate) fn get_target<'b>(&'b mut self, body: &'p str) -> &'b String {
pub(crate) fn get_target<'b>(&'b mut self, body: &'parse str) -> &'b String {
self.targets.entry(body).or_insert_with(|| {
self.id_counter += 1;
format!("target_{}", self.id_counter)

View File

@ -0,0 +1,23 @@
use crate::error::CustomError;
use crate::intermediate::util::coalesce_whitespace;
use super::registry::Registry;
#[derive(Debug)]
pub(crate) struct ITarget {
pub(crate) id: String,
value: String,
}
impl ITarget {
pub(crate) fn new<'parse>(
registry: &mut Registry<'parse>,
target: &organic::types::Target<'parse>,
) -> Result<ITarget, CustomError> {
let id = registry.get_target(target.value);
Ok(ITarget {
id: id.clone(),
value: target.value.to_owned(),
})
}
}