Orgize
A Rust library for parsing orgmode files.
Parse
To parse a orgmode string, simply invoking the Org::parse function:
use Org;
parse;
or Org::parse_custom:
use ;
parse_custom;
Iter
Org::iter function will returns an iterator of Events, which is
a simple wrapper of Element.
use Org;
for event in parse.iter
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).
Render html
You can call the Org::write_html function to generate html directly, which
uses the DefaultHtmlHandler internally:
use Org;
let mut writer = Vecnew;
parse.write_html.unwrap;
assert_eq!;
Render html with custom HtmlHandler
To customize html rendering, simply implementing HtmlHandler trait and passing
it to the Org::wirte_html_custom function.
The following code demonstrates how to add a id for every headline and return own error type while rendering.
use From;
use ;
use FromUtf8Error;
use ;
use ;
use slugify;
// From<std::io::Error> trait is required for custom error type
;
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.
Serde
Org struct have already implemented serde's Serialize trait. It means you can
serialize it into any format supported by serde, such as json:
use Org;
use ;
let org = parse;
println!;
// {
// "type": "document",
// "children": [{
// "type": "section",
// "children": [{
// "type": "paragraph",
// "children":[{
// "type": "text",
// "value":"I 'm "
// }, {
// "type": "bold",
// "children":[{
// "type": "text",
// "value": "bold"
// }]
// }, {
// "type":"text",
// "value":"."
// }]
// }]
// }]
// }
Features
By now, orgize provides four features:
-
ser: adds the ability to serializeOrgand other elements usingserde, enabled by default. -
chrono: adds the ability to convertDatetimeintochronostructs, disabled by default. -
syntect: providesSyntectHtmlHandlerfor highlighting code block, disabled by default. -
indexmap: UsesIndexMapinstead ofHashMapfor properties to preserve their order, disabled by default.
License
MIT