Compare commits
2 Commits
e3b5f7f74f
...
7b01230234
Author | SHA1 | Date | |
---|---|---|---|
![]() |
7b01230234 | ||
![]() |
c6cf5f75ac |
@ -6,6 +6,7 @@ mod heading;
|
|||||||
mod object;
|
mod object;
|
||||||
mod plain_text;
|
mod plain_text;
|
||||||
mod section;
|
mod section;
|
||||||
|
mod target;
|
||||||
|
|
||||||
pub(crate) use blog_post_page::RenderBlogPostPage;
|
pub(crate) use blog_post_page::RenderBlogPostPage;
|
||||||
pub(crate) use document_element::RenderDocumentElement;
|
pub(crate) use document_element::RenderDocumentElement;
|
||||||
|
@ -7,11 +7,13 @@ use crate::error::CustomError;
|
|||||||
use crate::intermediate::IObject;
|
use crate::intermediate::IObject;
|
||||||
|
|
||||||
use super::plain_text::RenderPlainText;
|
use super::plain_text::RenderPlainText;
|
||||||
|
use super::target::RenderTarget;
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
#[serde(untagged)]
|
#[serde(untagged)]
|
||||||
pub(crate) enum RenderObject {
|
pub(crate) enum RenderObject {
|
||||||
PlainText(RenderPlainText),
|
PlainText(RenderPlainText),
|
||||||
|
Target(RenderTarget),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenderObject {
|
impl RenderObject {
|
||||||
@ -28,6 +30,12 @@ impl RenderObject {
|
|||||||
output_file,
|
output_file,
|
||||||
inner,
|
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(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -5,15 +5,14 @@ use tokio::task::JoinHandle;
|
|||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
use crate::error::CustomError;
|
use crate::error::CustomError;
|
||||||
|
use crate::intermediate::registry::Registry;
|
||||||
|
|
||||||
use super::BlogPostPage;
|
use super::BlogPostPage;
|
||||||
use super::IDocumentElement;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct BlogPost {
|
pub(crate) struct BlogPost {
|
||||||
pub(crate) id: String,
|
pub(crate) id: String,
|
||||||
pub(crate) pages: Vec<BlogPostPage>,
|
pub(crate) pages: Vec<BlogPostPage>,
|
||||||
pub(crate) children: Vec<IDocumentElement>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlogPost {
|
impl BlogPost {
|
||||||
@ -44,12 +43,27 @@ impl BlogPost {
|
|||||||
ret
|
ret
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut registry = Registry::new();
|
||||||
|
|
||||||
|
// Assign IDs to the targets
|
||||||
|
for (_real_path, _contents, parsed_document) in parsed_org_files.iter() {
|
||||||
|
organic::types::AstNode::from(parsed_document)
|
||||||
|
.iter_all_ast_nodes()
|
||||||
|
.for_each(|node| match node {
|
||||||
|
organic::types::AstNode::Target(target) => {
|
||||||
|
registry.get_target(target.value);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let pages = {
|
let pages = {
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
for (real_path, _contents, parsed_document) in parsed_org_files {
|
for (real_path, _contents, parsed_document) in parsed_org_files.iter() {
|
||||||
let relative_to_post_dir_path = real_path.strip_prefix(post_dir)?;
|
let relative_to_post_dir_path = real_path.strip_prefix(post_dir)?;
|
||||||
ret.push(BlogPostPage::new(
|
ret.push(BlogPostPage::new(
|
||||||
relative_to_post_dir_path,
|
relative_to_post_dir_path,
|
||||||
|
&mut registry,
|
||||||
parsed_document,
|
parsed_document,
|
||||||
)?);
|
)?);
|
||||||
}
|
}
|
||||||
@ -59,7 +73,6 @@ impl BlogPost {
|
|||||||
Ok(BlogPost {
|
Ok(BlogPost {
|
||||||
id: post_id.to_string_lossy().into_owned(),
|
id: post_id.to_string_lossy().into_owned(),
|
||||||
pages,
|
pages,
|
||||||
children: Vec::new(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
inner(root_dir.as_ref(), post_dir.as_ref()).await
|
inner(root_dir.as_ref(), post_dir.as_ref()).await
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use crate::error::CustomError;
|
use crate::error::CustomError;
|
||||||
|
|
||||||
|
use super::registry::Registry;
|
||||||
use super::IObject;
|
use super::IObject;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -9,11 +10,14 @@ pub(crate) struct IHeading {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl IHeading {
|
impl IHeading {
|
||||||
pub(crate) fn new(heading: &organic::types::Heading<'_>) -> Result<IHeading, CustomError> {
|
pub(crate) fn new<'parse>(
|
||||||
|
registry: &mut Registry<'parse>,
|
||||||
|
heading: &organic::types::Heading<'parse>,
|
||||||
|
) -> Result<IHeading, CustomError> {
|
||||||
let title = heading
|
let title = heading
|
||||||
.title
|
.title
|
||||||
.iter()
|
.iter()
|
||||||
.map(IObject::new)
|
.map(|obj| IObject::new(registry, obj))
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
Ok(IHeading {
|
Ok(IHeading {
|
||||||
title,
|
title,
|
||||||
|
@ -6,7 +6,9 @@ mod heading;
|
|||||||
mod object;
|
mod object;
|
||||||
mod page;
|
mod page;
|
||||||
mod plain_text;
|
mod plain_text;
|
||||||
|
mod registry;
|
||||||
mod section;
|
mod section;
|
||||||
|
mod target;
|
||||||
mod util;
|
mod util;
|
||||||
pub(crate) use convert::convert_blog_post_page_to_render_context;
|
pub(crate) use convert::convert_blog_post_page_to_render_context;
|
||||||
pub(crate) use definition::BlogPost;
|
pub(crate) use definition::BlogPost;
|
||||||
@ -17,3 +19,4 @@ pub(crate) use object::IObject;
|
|||||||
pub(crate) use page::BlogPostPage;
|
pub(crate) use page::BlogPostPage;
|
||||||
pub(crate) use plain_text::IPlainText;
|
pub(crate) use plain_text::IPlainText;
|
||||||
pub(crate) use section::ISection;
|
pub(crate) use section::ISection;
|
||||||
|
pub(crate) use target::ITarget;
|
||||||
|
@ -1,14 +1,20 @@
|
|||||||
use crate::error::CustomError;
|
use crate::error::CustomError;
|
||||||
|
|
||||||
use super::plain_text::IPlainText;
|
use super::plain_text::IPlainText;
|
||||||
|
use super::registry::Registry;
|
||||||
|
use super::ITarget;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) enum IObject {
|
pub(crate) enum IObject {
|
||||||
PlainText(IPlainText),
|
PlainText(IPlainText),
|
||||||
|
Target(ITarget),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IObject {
|
impl IObject {
|
||||||
pub(crate) fn new(obj: &organic::types::Object<'_>) -> Result<IObject, CustomError> {
|
pub(crate) fn new<'parse>(
|
||||||
|
registry: &mut Registry<'parse>,
|
||||||
|
obj: &organic::types::Object<'parse>,
|
||||||
|
) -> Result<IObject, CustomError> {
|
||||||
match obj {
|
match obj {
|
||||||
organic::types::Object::Bold(_) => todo!(),
|
organic::types::Object::Bold(_) => todo!(),
|
||||||
organic::types::Object::Italic(_) => todo!(),
|
organic::types::Object::Italic(_) => todo!(),
|
||||||
@ -17,7 +23,7 @@ impl IObject {
|
|||||||
organic::types::Object::Code(_) => todo!(),
|
organic::types::Object::Code(_) => todo!(),
|
||||||
organic::types::Object::Verbatim(_) => todo!(),
|
organic::types::Object::Verbatim(_) => todo!(),
|
||||||
organic::types::Object::PlainText(plain_text) => {
|
organic::types::Object::PlainText(plain_text) => {
|
||||||
Ok(IObject::PlainText(IPlainText::new(plain_text)?))
|
Ok(IObject::PlainText(IPlainText::new(registry, plain_text)?))
|
||||||
}
|
}
|
||||||
organic::types::Object::RegularLink(_) => todo!(),
|
organic::types::Object::RegularLink(_) => todo!(),
|
||||||
organic::types::Object::RadioLink(_) => todo!(),
|
organic::types::Object::RadioLink(_) => todo!(),
|
||||||
@ -34,7 +40,9 @@ impl IObject {
|
|||||||
organic::types::Object::InlineBabelCall(_) => todo!(),
|
organic::types::Object::InlineBabelCall(_) => todo!(),
|
||||||
organic::types::Object::InlineSourceBlock(_) => todo!(),
|
organic::types::Object::InlineSourceBlock(_) => todo!(),
|
||||||
organic::types::Object::LineBreak(_) => 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::StatisticsCookie(_) => todo!(),
|
||||||
organic::types::Object::Subscript(_) => todo!(),
|
organic::types::Object::Subscript(_) => todo!(),
|
||||||
organic::types::Object::Superscript(_) => todo!(),
|
organic::types::Object::Superscript(_) => todo!(),
|
||||||
|
@ -2,6 +2,7 @@ use std::path::PathBuf;
|
|||||||
|
|
||||||
use crate::error::CustomError;
|
use crate::error::CustomError;
|
||||||
|
|
||||||
|
use super::registry::Registry;
|
||||||
use super::IDocumentElement;
|
use super::IDocumentElement;
|
||||||
use super::IHeading;
|
use super::IHeading;
|
||||||
use super::ISection;
|
use super::ISection;
|
||||||
@ -17,17 +18,18 @@ pub(crate) struct BlogPostPage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BlogPostPage {
|
impl BlogPostPage {
|
||||||
pub(crate) fn new<P: Into<PathBuf>>(
|
pub(crate) fn new<'parse, P: Into<PathBuf>>(
|
||||||
path: P,
|
path: P,
|
||||||
document: organic::types::Document<'_>,
|
registry: &mut Registry<'parse>,
|
||||||
|
document: &organic::types::Document<'parse>,
|
||||||
) -> Result<BlogPostPage, CustomError> {
|
) -> Result<BlogPostPage, CustomError> {
|
||||||
let path = path.into();
|
let path = path.into();
|
||||||
let mut children = Vec::new();
|
let mut children = Vec::new();
|
||||||
if let Some(section) = document.zeroth_section.as_ref() {
|
if let Some(section) = document.zeroth_section.as_ref() {
|
||||||
children.push(IDocumentElement::Section(ISection::new(section)?));
|
children.push(IDocumentElement::Section(ISection::new(registry, section)?));
|
||||||
}
|
}
|
||||||
for heading in document.children.iter() {
|
for heading in document.children.iter() {
|
||||||
children.push(IDocumentElement::Heading(IHeading::new(heading)?));
|
children.push(IDocumentElement::Heading(IHeading::new(registry, heading)?));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(BlogPostPage {
|
Ok(BlogPostPage {
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
use crate::error::CustomError;
|
use crate::error::CustomError;
|
||||||
use crate::intermediate::util::coalesce_whitespace;
|
use crate::intermediate::util::coalesce_whitespace;
|
||||||
|
|
||||||
|
use super::registry::Registry;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct IPlainText {
|
pub(crate) struct IPlainText {
|
||||||
source: String,
|
source: String,
|
||||||
@ -8,6 +10,7 @@ pub(crate) struct IPlainText {
|
|||||||
|
|
||||||
impl IPlainText {
|
impl IPlainText {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
|
registry: &mut Registry<'_>,
|
||||||
plain_text: &organic::types::PlainText<'_>,
|
plain_text: &organic::types::PlainText<'_>,
|
||||||
) -> Result<IPlainText, CustomError> {
|
) -> Result<IPlainText, CustomError> {
|
||||||
Ok(IPlainText {
|
Ok(IPlainText {
|
||||||
|
24
src/intermediate/registry.rs
Normal file
24
src/intermediate/registry.rs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
type IdCounter = u16;
|
||||||
|
|
||||||
|
pub(crate) struct Registry<'parse> {
|
||||||
|
id_counter: IdCounter,
|
||||||
|
targets: HashMap<&'parse str, String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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: &'parse str) -> &'b String {
|
||||||
|
self.targets.entry(body).or_insert_with(|| {
|
||||||
|
self.id_counter += 1;
|
||||||
|
format!("target_{}", self.id_counter)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -1,10 +1,15 @@
|
|||||||
use crate::error::CustomError;
|
use crate::error::CustomError;
|
||||||
|
|
||||||
|
use super::registry::Registry;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct ISection {}
|
pub(crate) struct ISection {}
|
||||||
|
|
||||||
impl ISection {
|
impl ISection {
|
||||||
pub(crate) fn new(section: &organic::types::Section<'_>) -> Result<ISection, CustomError> {
|
pub(crate) fn new(
|
||||||
|
registry: &mut Registry<'_>,
|
||||||
|
section: &organic::types::Section<'_>,
|
||||||
|
) -> Result<ISection, CustomError> {
|
||||||
Ok(ISection {})
|
Ok(ISection {})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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…
x
Reference in New Issue
Block a user