title
section
A Rust library for parsing orgmode files.
Live demo: https://orgize.herokuapp.com/
To parse a orgmode string, simply invoking the Org::parse
function:
```rust use orgize::Org;
Org::parse("* DONE Title :tag:"); ```
or Org::parse_with_config
:
``` rust use orgize::{Org, ParseConfig};
Org::parsewithconfig( "* TASK Title 1", &ParseConfig { // custom todo keywords todokeywords: vec!["TASK".tostring()], ..Default::default() }, ); ```
Org::iter
function will returns an iteractor of Event
s, which is
a simple wrapper of Element
.
```rust use orgize::Org;
for event in Org::parse("* DONE Title :tag:").iter() { // handling the event } ```
Note: whether an element is container or not, it will appears twice in one loop.
One as Event::Start(element)
, one as Event::End(element)
.
You can call the Org::html
function to generate html directly, which
uses the DefaultHtmlHandler
internally:
```rust use orgize::Org;
let mut writer = Vec::new(); Org::parse("* title\nsection").html(&mut writer).unwrap();
asserteq!(
String::fromutf8(writer).unwrap(),
" sectiontitle
HtmlHandler
To customize html rendering, simply implementing HtmlHandler
trait and passing
it to the Org::html_with_handler
function.
The following code demonstrates how to add a id for every headline and return own error type while rendering.
```rust use std::convert::From; use std::io::{Error as IOError, Write}; use std::string::FromUtf8Error;
use orgize::export::{html::Escape, DefaultHtmlHandler, HtmlHandler}; use orgize::{Element, Org}; use slugify::slugify;
enum MyError { IO(IOError), Heading, Utf8(FromUtf8Error), }
// From
impl From
struct MyHtmlHandler(DefaultHtmlHandler);
impl HtmlHandler
fn end<W: Write>(&mut self, mut w: W, element: &Element<'_>) -> Result<(), MyError> {
if let Element::Title(title) = element {
write!(w, "</a></h{}>", title.level)?;
} else {
self.0.end(w, element)?;
}
Ok(())
}
}
fn main() -> Result<(), MyError> { let mut writer = Vec::new();
let mut handler = MyHtmlHandler(DefaultHtmlHandler);
Org::parse("* title\n*section*").html_with_handler(&mut writer, &mut handler)?;
assert_eq!(
String::from_utf8(writer)?,
"<main><h1><a id=\"title\" href=\"#title\">title</a></h1>\
<section><p><b>section</b></p></section></main>"
);
Ok(())
} ```
Note: as I mentioned above, each element will appears two times while iterating. And handler will silently ignores all end events from non-container elements.
So if you want to change how a non-container element renders, just redefine the start
function and leave the end
function unchanged.
Org
struct have already implemented serde's Serialize
trait. It means you can
serialize it into any format supported by serde, such as json:
```rust use orgize::Org; use serdejson::{json, tostring};
let org = Org::parse("I 'm bold."); println!("{}", to_string(&org).unwrap());
// { // "type": "document", // "children": [{ // "type": "section", // "children": [{ // "type": "paragraph", // "children":[{ // "type": "text", // "value":"I 'm " // }, { // "type": "bold", // "children":[{ // "type": "text", // "value": "bold" // }] // }, { // "type":"text", // "value":"." // }] // }] // }] // } ```
By now, orgize provides two features:
ser
: adds the ability to serialize Org
and other elements using serde
, enabled by default.
chrono
: adds the ability to convert Datetime
into chrono
structs, disabled by default.
syntect
: provides SyntectHtmlHandler
for highlighting code block, disabled by default.
MIT