aboutsummaryrefslogtreecommitdiff
path: root/src/adapter
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2026-11-19 09:29:47 +0800
committerJSDurand <mmemmew@gmail.com>2026-11-19 09:29:47 +0800
commit5b9a806af13d38163156302097e405a7b145b6cc (patch)
treec079f3c4e027dc730892630f667b0e6401736043 /src/adapter
elel: notes engine (Rust) and Gnus-like Emacs frontendHEADmaster
Tantivy full text index, link graph, inline query filters, Ollama embeddings with paragraph chunks, and an Elisp summary/article UI. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Diffstat (limited to 'src/adapter')
-rw-r--r--src/adapter/mod.rs275
-rw-r--r--src/adapter/org.rs84
-rw-r--r--src/adapter/txt.rs85
3 files changed, 444 insertions, 0 deletions
diff --git a/src/adapter/mod.rs b/src/adapter/mod.rs
new file mode 100644
index 0000000..8f56b9b
--- /dev/null
+++ b/src/adapter/mod.rs
@@ -0,0 +1,275 @@
+//! Format adapters. Each adapter turns the text of a file into notes.
+
+pub mod org;
+pub mod txt;
+
+use crate::note::{Link, Note};
+use std::path::Path;
+
+/// Parse the file at `path`, whose content is `text`. Returns `None`
+/// when no adapter handles the extension.
+pub fn parse_file(path: &Path, text: &str) -> Option<Vec<Note>> {
+ let p = path.to_string_lossy().to_string();
+ match path.extension().and_then(|e| e.to_str()) {
+ Some("org") => Some(org::parse(&p, text)),
+ Some("txt") => Some(txt::parse(&p, text)),
+ _ => None,
+ }
+}
+
+/// The fields every adapter collects before building a note.
+#[derive(Default)]
+pub struct Fields {
+ pub id: Option<String>,
+ pub title: String,
+ pub date: Option<String>,
+ pub kind: Option<String>,
+ pub tags: Vec<String>,
+ pub parent: Option<String>,
+ pub links: Option<String>,
+ pub context: Option<String>,
+ pub summary: Option<String>,
+}
+
+impl Fields {
+ /// Store the value of a metadata key. Returns false for unknown keys.
+ pub fn set(&mut self, key: &str, value: &str) -> bool {
+ let value = value.trim();
+ let some = |v: &str| {
+ if v.is_empty() {
+ None
+ } else {
+ Some(v.to_string())
+ }
+ };
+ match key.to_ascii_lowercase().as_str() {
+ "id" => self.id = some(value),
+ "title" => self.title = value.to_string(),
+ "date" => self.date = some(value),
+ "kind" => self.kind = some(value),
+ "tags" => {
+ for t in split_list(value) {
+ if !self.tags.contains(&t) {
+ self.tags.push(t);
+ }
+ }
+ }
+ "parent" => self.parent = some(value),
+ "links" => self.links = some(value),
+ "context" => self.context = some(value),
+ "summary" => self.summary = some(value),
+ _ => return false,
+ }
+ true
+ }
+}
+
+/// Assemble a note from its fields, its body, and its place in the file.
+#[allow(clippy::too_many_arguments)]
+pub fn build(
+ path: &str,
+ text: &str,
+ start: usize,
+ end: usize,
+ line: usize,
+ fields: Fields,
+ body: String,
+) -> Note {
+ let mut links = fields
+ .links
+ .as_deref()
+ .map(parse_links_prop)
+ .unwrap_or_default();
+ for l in body_links(&body) {
+ if !links.iter().any(|x| x.dst == l.dst) {
+ links.push(l);
+ }
+ }
+ Note {
+ id: fields.id,
+ path: path.to_string(),
+ start,
+ end,
+ line,
+ hash: blake3::hash(text[start..end].as_bytes()).to_hex().to_string(),
+ title: fields.title,
+ date: fields.date,
+ kind: fields.kind,
+ tags: fields.tags,
+ parent: fields.parent,
+ links,
+ context: fields.context,
+ summary: fields.summary,
+ body,
+ }
+}
+
+/// Split on whitespace, commas and colons.
+pub fn split_list(s: &str) -> Vec<String> {
+ s.split(|c: char| c.is_whitespace() || c == ',' || c == ':')
+ .filter(|t| !t.is_empty())
+ .map(|t| t.to_string())
+ .collect()
+}
+
+fn is_kind(s: &str) -> bool {
+ !s.is_empty()
+ && s
+ .chars()
+ .all(|c| c.is_ascii_alphabetic() || c == '-' || c == '_')
+}
+
+/// Parse the value of a LINKS property: tokens `ID` or `kind:ID`.
+pub fn parse_links_prop(s: &str) -> Vec<Link> {
+ let mut out = Vec::new();
+ for tok in s.split_whitespace() {
+ let link = match tok.split_once(':') {
+ Some(("notes" | "note", rest)) if !rest.is_empty() => Link {
+ dst: rest.to_string(),
+ kind: None,
+ },
+ Some((k, rest)) if is_kind(k) && !rest.is_empty() => Link {
+ dst: rest.to_string(),
+ kind: Some(k.to_string()),
+ },
+ _ => Link {
+ dst: tok.to_string(),
+ kind: None,
+ },
+ };
+ out.push(link);
+ }
+ out
+}
+
+/// Split the path of a `note:` link into its kind and its ID.
+///
+/// The path is `ID` for an untyped link and `:KIND:ID` for a typed one,
+/// so `[[note:ID]]` and `[[note::KIND:ID]]` are the two forms.
+fn split_note_path(p: &str) -> Option<(Option<String>, String)> {
+ if p.is_empty() || p.contains(char::is_whitespace) {
+ return None;
+ }
+ match p.strip_prefix(':') {
+ Some(rest) => {
+ let (k, id) = rest.split_once(':')?;
+ let ok = !k.is_empty()
+ && k.chars()
+ .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_');
+ (ok && !id.is_empty()).then(|| (Some(k.to_string()), id.to_string()))
+ }
+ None => Some((None, p.to_string())),
+ }
+}
+
+/// Find the `[[note:ID][description]]` links in a body, and their kinds.
+/// The description is for humans (usually the title of the note); only
+/// the ID is used to locate the note.
+pub fn body_links(body: &str) -> Vec<Link> {
+ let mut out: Vec<Link> = Vec::new();
+ let mut pos = 0;
+ while let Some(i) = body[pos..].find("[[note:") {
+ let start = pos + i + "[[note:".len();
+ let rest = &body[start..];
+ match rest.find(']') {
+ Some(j) if !rest[..j].contains('\n') => {
+ let closes = rest[j..].starts_with("]]") || rest[j..].starts_with("][");
+ if closes {
+ if let Some((kind, dst)) = split_note_path(&rest[..j]) {
+ match out.iter_mut().find(|l| l.dst == dst) {
+ // A typed link is more informative than an untyped one.
+ Some(l) => l.kind = l.kind.take().or(kind),
+ None => out.push(Link { dst, kind }),
+ }
+ }
+ }
+ pos = start + j;
+ }
+ _ => pos = start,
+ }
+ }
+ out
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn body_links_read_the_id_and_ignore_the_description() {
+ let l = body_links(
+ "see [[note:abc-1][A title: with colon]] and [[note:<x.y@z>][d]] or [[note:q]] twice [[note:q][again]]",
+ );
+ let got: Vec<(&str, Option<&str>)> =
+ l.iter().map(|x| (x.dst.as_str(), x.kind.as_deref())).collect();
+ assert_eq!(got, vec![("abc-1", None), ("<x.y@z>", None), ("q", None)]);
+ }
+
+ #[test]
+ fn body_links_may_be_typed() {
+ let l = body_links("[[note::supports:a1][A]] [[note::contra:<b@c>]] [[note:d]] [[note:d2][x]]");
+ let got: Vec<(&str, Option<&str>)> =
+ l.iter().map(|x| (x.dst.as_str(), x.kind.as_deref())).collect();
+ assert_eq!(
+ got,
+ vec![("a1", Some("supports")), ("<b@c>", Some("contra")), ("d", None), ("d2", None)]
+ );
+ // The typed form wins when the same note is linked twice.
+ let l = body_links("[[note:a]] and [[note::cites:a]]");
+ assert_eq!(l.len(), 1);
+ assert_eq!(l[0].kind.as_deref(), Some("cites"));
+ }
+
+ #[test]
+ fn body_links_ignore_other_links() {
+ let body = "[[https://a.b][web]] [[file:x.org]] [[*heading]] [[#custom]] [[./rel][r]] \\
+ [[two words]] [[n1][fuzzy]] [[note:]] [[note:two words]] [[note::bad]] \\
+ [[notes:x]] [[note:a\nb]]";
+ assert!(body_links(body).is_empty(), "{:?}", body_links(body));
+ }
+
+ #[test]
+ fn links_property_has_typed_and_plain_tokens() {
+ let l = parse_links_prop("a1 supports:b2 notes:c3 <d@e>");
+ let got: Vec<(&str, Option<&str>)> =
+ l.iter().map(|x| (x.dst.as_str(), x.kind.as_deref())).collect();
+ assert_eq!(
+ got,
+ vec![("a1", None), ("b2", Some("supports")), ("c3", None), ("<d@e>", None)]
+ );
+ }
+
+ #[test]
+ fn org_notes_are_top_level_headings_only() {
+ let text = "intro\n* One :a:b:\n:PROPERTIES:\n:ID: 1\n:PARENT: 0\n:END:\nbody\n** sub\nmore\n* Two\ntext\n";
+ let notes = org::parse("/x.org", text);
+ assert_eq!(notes.len(), 2);
+ assert_eq!(notes[0].id.as_deref(), Some("1"));
+ assert_eq!(notes[0].title, "One");
+ assert_eq!(notes[0].tags, vec!["a", "b"]);
+ assert_eq!(notes[0].parent.as_deref(), Some("0"));
+ assert!(notes[0].body.contains("** sub"));
+ assert_eq!(notes[0].line, 2);
+ assert_eq!(&text[notes[0].start..notes[0].end].lines().next(), &Some("* One :a:b:"));
+ assert_eq!(notes[1].id, None);
+ }
+
+ #[test]
+ fn txt_notes_are_split_at_form_feeds() {
+ let text = "ID: 1\nTitle: A\nTags: x y\n\nbody a\n\x0c\nID: 2\nTitle: B\n\nbody b\n\x0c\n\n";
+ let notes = txt::parse("/x.txt", text);
+ assert_eq!(notes.len(), 2);
+ assert_eq!(notes[0].tags, vec!["x", "y"]);
+ assert_eq!(notes[0].body, "body a");
+ assert_eq!(notes[1].id.as_deref(), Some("2"));
+ assert_eq!(notes[1].line, 7);
+ }
+
+ #[test]
+ fn txt_note_without_header_takes_its_title_from_the_text() {
+ let notes = txt::parse("/x.txt", "just a thought\nmore\n");
+ assert_eq!(notes.len(), 1);
+ assert_eq!(notes[0].title, "just a thought");
+ assert_eq!(notes[0].id, None);
+ }
+}
diff --git a/src/adapter/org.rs b/src/adapter/org.rs
new file mode 100644
index 0000000..a999814
--- /dev/null
+++ b/src/adapter/org.rs
@@ -0,0 +1,84 @@
+//! Org adapter: each top-level heading is a note. Metadata lives in
+//! the heading (title, tags) and in its property drawer.
+
+use super::{Fields, build};
+use crate::note::Note;
+
+fn is_tag_string(s: &str) -> bool {
+ s.len() > 2
+ && s.starts_with(':')
+ && s.ends_with(':')
+ && s.chars()
+ .all(|c| c.is_alphanumeric() || "_@#%:.".contains(c))
+}
+
+/// Split a heading line into its title and its tags.
+fn parse_heading(line: &str) -> (String, Vec<String>) {
+ let h = line.trim_start_matches('*').trim();
+ if let Some(idx) = h.rfind(char::is_whitespace) {
+ let last = &h[idx + 1..];
+ if is_tag_string(last) {
+ let tags = last
+ .split(':')
+ .filter(|t| !t.is_empty())
+ .map(|t| t.to_string())
+ .collect();
+ return (h[..idx].trim().to_string(), tags);
+ }
+ }
+ (h.to_string(), Vec::new())
+}
+
+fn is_planning(line: &str) -> bool {
+ let t = line.trim_start();
+ t.starts_with("SCHEDULED:") || t.starts_with("DEADLINE:") || t.starts_with("CLOSED:")
+}
+
+pub fn parse(path: &str, text: &str) -> Vec<Note> {
+ // Find the top-level headings: a single star at column 0.
+ let mut heads: Vec<(usize, usize)> = Vec::new();
+ let mut off = 0;
+ for (ln, line) in text.split_inclusive('\n').enumerate() {
+ let t = line.trim_end();
+ if line.starts_with("* ") || t == "*" {
+ heads.push((off, ln + 1));
+ }
+ off += line.len();
+ }
+ let mut notes = Vec::new();
+ for (i, &(start, line_no)) in heads.iter().enumerate() {
+ let end = heads.get(i + 1).map(|h| h.0).unwrap_or(text.len());
+ let chunk = &text[start..end];
+ let mut lines = chunk.lines();
+ let heading = lines.next().unwrap_or("");
+ let (title, tags) = parse_heading(heading);
+ let mut fields = Fields {
+ title,
+ tags,
+ ..Default::default()
+ };
+ let mut rest: Vec<&str> = lines.collect();
+ // Skip planning lines, then read the property drawer, if any.
+ let mut k = 0;
+ while k < rest.len() && is_planning(rest[k]) {
+ k += 1;
+ }
+ let mut body_from = 0;
+ if k < rest.len() && rest[k].trim() == ":PROPERTIES:" {
+ let mut j = k + 1;
+ while j < rest.len() && rest[j].trim() != ":END:" {
+ let l = rest[j].trim();
+ if let Some(r) = l.strip_prefix(':') {
+ if let Some((key, val)) = r.split_once(':') {
+ fields.set(key, val);
+ }
+ }
+ j += 1;
+ }
+ body_from = (j + 1).min(rest.len());
+ }
+ let body = rest.split_off(body_from).join("\n");
+ notes.push(build(path, text, start, end, line_no, fields, body));
+ }
+ notes
+}
diff --git a/src/adapter/txt.rs b/src/adapter/txt.rs
new file mode 100644
index 0000000..ddea799
--- /dev/null
+++ b/src/adapter/txt.rs
@@ -0,0 +1,85 @@
+//! Plain text adapter. Notes are separated by a form-feed line. A
+//! note starts with `Key: value` lines, then a blank line, then the
+//! body.
+
+use super::{Fields, build};
+use crate::note::Note;
+
+const KEYS: [&str; 9] = [
+ "id", "title", "date", "kind", "tags", "parent", "links", "context", "summary",
+];
+
+/// Parse `Key: value` if the key is a known one.
+fn header_line(line: &str) -> Option<(&str, &str)> {
+ let (k, v) = line.split_once(':')?;
+ let lk = k.trim().to_ascii_lowercase();
+ if KEYS.contains(&lk.as_str()) && k.trim().len() == k.len() {
+ Some((k, v))
+ } else {
+ None
+ }
+}
+
+pub fn parse(path: &str, text: &str) -> Vec<Note> {
+ // Split into chunks at the form-feed lines.
+ let mut chunks: Vec<(usize, usize, usize)> = Vec::new(); // start, end, line
+ let mut off = 0;
+ let mut cur_start = 0;
+ let mut cur_line = 1;
+ for (ln, line) in text.split_inclusive('\n').enumerate() {
+ if line.trim_end_matches(['\r', '\n']) == "\x0c" {
+ chunks.push((cur_start, off, cur_line));
+ cur_start = off + line.len();
+ cur_line = ln + 2;
+ }
+ off += line.len();
+ }
+ chunks.push((cur_start, text.len(), cur_line));
+
+ let mut notes = Vec::new();
+ for (start, end, line_no) in chunks {
+ let chunk = &text[start..end];
+ if chunk.trim().is_empty() {
+ continue;
+ }
+ let mut fields = Fields::default();
+ let mut body_lines: Vec<&str> = Vec::new();
+ let mut in_header = true;
+ let mut seen = 0;
+ for l in chunk.lines() {
+ if in_header {
+ if seen == 0 && l.trim().is_empty() {
+ continue;
+ }
+ if let Some((k, v)) = header_line(l) {
+ fields.set(k, v);
+ seen += 1;
+ continue;
+ }
+ in_header = false;
+ // The blank line that ends the header is not part of the body.
+ if seen > 0 && l.trim().is_empty() {
+ continue;
+ }
+ }
+ body_lines.push(l);
+ }
+ if fields.title.is_empty() {
+ fields.title = body_lines
+ .iter()
+ .find(|l| !l.trim().is_empty())
+ .map(|l| l.trim().chars().take(80).collect())
+ .unwrap_or_default();
+ }
+ notes.push(build(
+ path,
+ text,
+ start,
+ end,
+ line_no,
+ fields,
+ body_lines.join("\n"),
+ ));
+ }
+ notes
+}