feat(org): add parse_string and parse_string_custom

This commit is contained in:
PoiScript 2020-04-06 16:08:07 +08:00
parent 37e27b3461
commit 19740440db

View file

@ -6,7 +6,7 @@ use crate::{
config::{ParseConfig, DEFAULT_CONFIG},
elements::{Element, Keyword},
export::{DefaultHtmlHandler, DefaultOrgHandler, HtmlHandler, OrgHandler},
parsers::{blank_lines, parse_container, Container},
parsers::{blank_lines, parse_container, Container, OwnedArena},
};
pub struct Org<'a> {
@ -33,6 +33,11 @@ impl<'a> Org<'a> {
Org::parse_custom(text, &DEFAULT_CONFIG)
}
/// Likes `parse`, but accepts `String`.
pub fn parse_string(text: String) -> Org<'static> {
Org::parse_string_custom(text, &DEFAULT_CONFIG)
}
/// Parses string `text` into `Org` struct with custom `ParseConfig`.
pub fn parse_custom(text: &'a str, config: &ParseConfig) -> Org<'a> {
let mut arena = Arena::new();
@ -54,6 +59,27 @@ impl<'a> Org<'a> {
org
}
/// Likes `parse_custom`, but accepts `String`.
pub fn parse_string_custom(text: String, config: &ParseConfig) -> Org<'static> {
let mut arena = Arena::new();
let (text, pre_blank) = blank_lines(&text);
let root = arena.new_node(Element::Document { pre_blank });
let mut org = Org { arena, root };
parse_container(
&mut OwnedArena::new(&mut org.arena),
Container::Document {
content: text,
node: org.root,
},
config,
);
org.debug_validate();
org
}
/// Returns a reference to the underlay arena.
pub fn arena(&self) -> &Arena<Element<'a>> {
&self.arena