Skip to content

Commit

Permalink
feat: add Document::title & Document::keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
PoiScript committed Apr 1, 2024
1 parent 0d8ef46 commit f918bf4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/ast/document.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use rowan::ast::AstNode;

use crate::Org;

use super::{Document, Keyword, SyntaxKind};

impl Document {
/// ```rust
/// use orgize::{Org, ast::Document};
///
/// let org = Org::parse(r#"
/// #+TITLE: hello
/// #+TITLE: world
/// #+DATE: tody
/// #+AUTHOR: poi"#);
/// let doc = org.first_node::<Document>().unwrap();
/// assert_eq!(doc.keywords().count(), 4);
/// ```
pub fn keywords(&self) -> impl Iterator<Item = Keyword> {
self.syntax
.first_child()
.filter(|c| c.kind() == SyntaxKind::SECTION)
.into_iter()
.flat_map(|section| section.children().filter_map(Keyword::cast))
}

/// Returns the value in `#+TITLE`
///
/// ```rust
/// use orgize::{Org, ast::Document};
///
/// let org = Org::parse("#+TITLE: hello\n#+TITLE: world");
/// let doc = org.first_node::<Document>().unwrap();
/// assert_eq!(doc.title().unwrap(), "hello world");
///
/// let org = Org::parse("");
/// let doc = org.first_node::<Document>().unwrap();
/// assert!(doc.title().is_none());
/// ```
pub fn title(&self) -> Option<String> {
self.keywords()
.filter(|kw| kw.key().eq_ignore_ascii_case("TITLE"))
.fold(Option::<String>::None, |acc, cur| {
let mut s = acc.unwrap_or_default();
if !s.is_empty() {
s.push(' ');
}
s.push_str(cur.value().trim());
Some(s)
})
}
}

impl Org {
/// Equals to `self.document().title()`, see [Document::title]
pub fn title(&self) -> Option<String> {
self.document().title()
}

/// Equals to `self.document().keywords()`, see [Document::keywords]
pub fn keywords(&self) -> impl Iterator<Item = Keyword> {
self.document().keywords()
}
}
1 change: 1 addition & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod affiliated_keyword;
mod block;
mod clock;
mod comment;
mod document;
mod drawer;
mod entity;
mod fixed_width;
Expand Down

0 comments on commit f918bf4

Please sign in to comment.