Add target.
This commit is contained in:
parent
c6cf5f75ac
commit
7b01230234
@ -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;
|
||||
|
@ -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
27
src/context/target.rs
Normal 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(),
|
||||
})
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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!(),
|
||||
|
@ -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();
|
||||
|
@ -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)
|
||||
|
23
src/intermediate/target.rs
Normal file
23
src/intermediate/target.rs
Normal 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(),
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user