feat: add tools module
This commit is contained in:
parent
1c3300ca61
commit
8ba9ade62d
|
@ -13,5 +13,6 @@ keywords = ["orgmode","emacs","parser"]
|
||||||
travis-ci = { repository = "PoiScript/orgize" }
|
travis-ci = { repository = "PoiScript/orgize" }
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bytecount = "0.5.1"
|
||||||
jetscii = "0.4.3"
|
jetscii = "0.4.3"
|
||||||
memchr = "2"
|
memchr = "2"
|
||||||
|
|
|
@ -62,5 +62,6 @@ pub mod headline;
|
||||||
mod lines;
|
mod lines;
|
||||||
pub mod objects;
|
pub mod objects;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
pub mod tools;
|
||||||
|
|
||||||
pub use parser::{Event, Parser};
|
pub use parser::{Event, Parser};
|
||||||
|
|
44
src/tools.rs
Normal file
44
src/tools.rs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
use crate::elements::{fn_def, keyword, Key};
|
||||||
|
use crate::headline::Headline;
|
||||||
|
use memchr::memchr;
|
||||||
|
|
||||||
|
type Headlines<'a> = Vec<Headline<'a>>;
|
||||||
|
type Keywords<'a> = Vec<(Key<'a>, &'a str)>;
|
||||||
|
type Footnotes<'a> = Vec<&'a str>;
|
||||||
|
|
||||||
|
pub fn metadata(src: &str) -> (Headlines<'_>, Keywords<'_>, Footnotes<'_>) {
|
||||||
|
let mut headlines = Vec::new();
|
||||||
|
let mut keywords = Vec::new();
|
||||||
|
let mut footnotes = Vec::new();
|
||||||
|
|
||||||
|
for line in src.lines().filter(|l| !l.is_empty()) {
|
||||||
|
if line.starts_with('*') {
|
||||||
|
let level = memchr(b' ', line.as_bytes()).unwrap_or_else(|| line.len());
|
||||||
|
if line.as_bytes()[0..level].iter().all(|&c| c == b'*') {
|
||||||
|
headlines.push(Headline::parse(line).0)
|
||||||
|
}
|
||||||
|
} else if line.starts_with("#+") {
|
||||||
|
if let Some((key, value, _)) = keyword::parse(line) {
|
||||||
|
keywords.push((key, value))
|
||||||
|
}
|
||||||
|
} else if line.starts_with("[fn:") {
|
||||||
|
if let Some((label, _, _)) = fn_def::parse(line) {
|
||||||
|
footnotes.push(label)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(headlines, keywords, footnotes)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn toc(src: &str) -> Headlines<'_> {
|
||||||
|
metadata(src).0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn keywords(src: &str) -> Keywords<'_> {
|
||||||
|
metadata(src).1
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fn_def(src: &str) -> Footnotes<'_> {
|
||||||
|
metadata(src).2
|
||||||
|
}
|
Loading…
Reference in a new issue