Create intermediate representation for plain text.
This commit is contained in:
parent
31a3efe417
commit
ba2756c762
@ -9,6 +9,11 @@ pub(crate) struct Heading {
|
||||
|
||||
impl Heading {
|
||||
pub(crate) fn new(heading: &organic::types::Heading<'_>) -> Result<Heading, CustomError> {
|
||||
Ok(Heading { title: Vec::new() })
|
||||
let title = heading
|
||||
.title
|
||||
.iter()
|
||||
.map(Object::new)
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(Heading { title })
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,9 @@ mod element;
|
||||
mod heading;
|
||||
mod object;
|
||||
mod page;
|
||||
mod plain_text;
|
||||
mod section;
|
||||
mod util;
|
||||
pub(crate) use convert::convert_blog_post_page_to_render_context;
|
||||
pub(crate) use definition::BlogPost;
|
||||
pub(crate) use document_element::DocumentElement;
|
||||
|
@ -1,2 +1,44 @@
|
||||
use crate::error::CustomError;
|
||||
|
||||
use super::plain_text::PlainText;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum Object {}
|
||||
pub(crate) enum Object {
|
||||
PlainText(PlainText),
|
||||
}
|
||||
|
||||
impl Object {
|
||||
pub(crate) fn new(obj: &organic::types::Object<'_>) -> Result<Object, CustomError> {
|
||||
match obj {
|
||||
organic::types::Object::Bold(_) => todo!(),
|
||||
organic::types::Object::Italic(_) => todo!(),
|
||||
organic::types::Object::Underline(_) => todo!(),
|
||||
organic::types::Object::StrikeThrough(_) => todo!(),
|
||||
organic::types::Object::Code(_) => todo!(),
|
||||
organic::types::Object::Verbatim(_) => todo!(),
|
||||
organic::types::Object::PlainText(plain_text) => {
|
||||
Ok(Object::PlainText(PlainText::new(plain_text)?))
|
||||
}
|
||||
organic::types::Object::RegularLink(_) => todo!(),
|
||||
organic::types::Object::RadioLink(_) => todo!(),
|
||||
organic::types::Object::RadioTarget(_) => todo!(),
|
||||
organic::types::Object::PlainLink(_) => todo!(),
|
||||
organic::types::Object::AngleLink(_) => todo!(),
|
||||
organic::types::Object::OrgMacro(_) => todo!(),
|
||||
organic::types::Object::Entity(_) => todo!(),
|
||||
organic::types::Object::LatexFragment(_) => todo!(),
|
||||
organic::types::Object::ExportSnippet(_) => todo!(),
|
||||
organic::types::Object::FootnoteReference(_) => todo!(),
|
||||
organic::types::Object::Citation(_) => todo!(),
|
||||
organic::types::Object::CitationReference(_) => todo!(),
|
||||
organic::types::Object::InlineBabelCall(_) => todo!(),
|
||||
organic::types::Object::InlineSourceBlock(_) => todo!(),
|
||||
organic::types::Object::LineBreak(_) => todo!(),
|
||||
organic::types::Object::Target(_) => todo!(),
|
||||
organic::types::Object::StatisticsCookie(_) => todo!(),
|
||||
organic::types::Object::Subscript(_) => todo!(),
|
||||
organic::types::Object::Superscript(_) => todo!(),
|
||||
organic::types::Object::Timestamp(_) => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
17
src/blog_post/plain_text.rs
Normal file
17
src/blog_post/plain_text.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use crate::blog_post::util::coalesce_whitespace;
|
||||
use crate::error::CustomError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct PlainText {
|
||||
source: String,
|
||||
}
|
||||
|
||||
impl PlainText {
|
||||
pub(crate) fn new(
|
||||
plain_text: &organic::types::PlainText<'_>,
|
||||
) -> Result<PlainText, CustomError> {
|
||||
Ok(PlainText {
|
||||
source: coalesce_whitespace(plain_text.source).into_owned(),
|
||||
})
|
||||
}
|
||||
}
|
48
src/blog_post/util.rs
Normal file
48
src/blog_post/util.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
/// Removes all whitespace from a string.
|
||||
///
|
||||
/// Example: "foo bar" => "foobar" and "foo \n bar" => "foobar".
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn coalesce_whitespace(input: &str) -> Cow<'_, str> {
|
||||
let mut state = CoalesceWhitespace::Normal;
|
||||
for (offset, c) in input.char_indices() {
|
||||
match (&mut state, c) {
|
||||
(CoalesceWhitespace::Normal, ' ' | '\t' | '\r' | '\n') => {
|
||||
let mut ret = String::with_capacity(input.len());
|
||||
ret.push_str(&input[..offset]);
|
||||
ret.push(' ');
|
||||
state = CoalesceWhitespace::HasWhitespace {
|
||||
in_whitespace: true,
|
||||
ret,
|
||||
};
|
||||
}
|
||||
(CoalesceWhitespace::Normal, _) => {}
|
||||
(
|
||||
CoalesceWhitespace::HasWhitespace { in_whitespace, ret },
|
||||
' ' | '\t' | '\r' | '\n',
|
||||
) => {
|
||||
if !*in_whitespace {
|
||||
*in_whitespace = true;
|
||||
ret.push(' ');
|
||||
}
|
||||
}
|
||||
(CoalesceWhitespace::HasWhitespace { in_whitespace, ret }, _) => {
|
||||
*in_whitespace = false;
|
||||
ret.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
match state {
|
||||
CoalesceWhitespace::Normal => Cow::Borrowed(input),
|
||||
CoalesceWhitespace::HasWhitespace {
|
||||
in_whitespace: _,
|
||||
ret,
|
||||
} => Cow::Owned(ret),
|
||||
}
|
||||
}
|
||||
|
||||
enum CoalesceWhitespace {
|
||||
Normal,
|
||||
HasWhitespace { in_whitespace: bool, ret: String },
|
||||
}
|
Loading…
Reference in New Issue
Block a user