changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/lib/obj/src/object/doc/org.rs

changeset 698: 96958d3eb5b0
parent: c7165d93a9eb
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 //! # org
2 //!
3 //! Org-mode object types
4 //!
5 //! The Org-mode format is particularly difficult to parse, but can be
6 //! done. The best resources for learning how to create a parser for
7 //! Org is available in the Org-Element API docs.
8 //!
9 //! ## Commentary
10 //! The `Org` type is the main type used for accessing Org Docs. This
11 //! object includes builder functions for the `OrgParser` type, which
12 //! handles parsing of the Org-mode format.
13 //!
14 //! This module does not intend to fully support the Org-mode format,
15 //! and is not a replacement for existing org files.
16 //!
17 //! ## Resources
18 //! - [parse org-mode in elisp](http://ergoemacs.org/emacs/elisp_parse_org_mode.html)
19 //! - [Using the Mapping API](https://orgmode.org/manual/Using-the-Mapping-API.html)
20 //! - [Using the Property API](https://orgmode.org/manual/Using-the-Property-API.html)
21 //! - [Org-element API](https://orgmode.org/worg/dev/org-element-api.html)
22 //! - [Orgnode.py](http://members.optusnet.com.au/~charles57/GTD/Orgnode.py)
23 use crate::{object::meta::Property, Objective, Result};
24 use hash::Id;
25 
26 use logger::log::info;
27 
28 use std::{
29  collections::HashMap,
30  fs,
31  path::{Path, PathBuf},
32 };
33 
34 use serde::{Deserialize, Serialize};
35 
36 /// Org object type
37 #[derive(Serialize, Deserialize, Debug)]
38 pub struct Org {
39  pub properties: Option<Vec<Property>>,
40  pub contents: HashMap<Id, String>,
41 }
42 
43 impl Org {
44  /// Create a new Org object
45  pub fn new() -> Self {
46  Org {
47  properties: None,
48  contents: HashMap::new(),
49  }
50  }
51  /// Create a new Org object from an Org-mode file
52  pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
53  let content = String::from_utf8(fs::read(PathBuf::from(path.as_ref()))?)
54  .expect("failed to read utf8 string");
55  let mut org = Org::new();
56  org.contents.insert(Id::rand(), content);
57  info!("parsed org-file: {}", path.as_ref().display());
58 
59  Ok(org)
60  }
61 
62  /// Append structured data to this Org document
63  pub fn append(self, input: String) -> Result<Self> {
64  let mut doc = self;
65  doc.contents.insert(Id::rand(), input);
66 
67  Ok(doc)
68  }
69 
70  /// Return document content
71  pub fn contents(self) -> HashMap<Id, String> {
72  self.contents
73  }
74 }
75 
76 impl Objective for Org {}