use regex::Regex;
use std::sync::LazyLock;
use crate::parser::{FormatParser, Region, flush_prose};
static HEADLINE_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(\*+\s+(?:TODO\s+|DONE\s+|NEXT\s+|WAIT\s+)?)(.*)$").unwrap());
static LIST_ITEM_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^(\s*(?:[-+]|\d+[.)]) )(.*)$").unwrap());
static LATEX_BEGIN_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^\s*\\begin\{([^}]+)\}").unwrap());
static LATEX_END_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^\s*\\end\{([^}]+)\}").unwrap());
static EXPORT_SNIPPET_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"@@[a-zA-Z]+:[^@]*@@").unwrap());
pub struct OrgParser;
impl OrgParser {
fn is_block_begin(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.to_ascii_uppercase().starts_with("#+BEGIN_")
}
fn is_src_begin(line: &str) -> Option<Option<String>> {
let trimmed = line.trim_start();
let upper = trimmed.to_ascii_uppercase();
if !upper.starts_with("#+BEGIN_SRC") {
return None;
}
let rest = trimmed["#+BEGIN_SRC".len()..].trim_start();
if rest.is_empty() {
return Some(None);
}
let lang = rest.split_whitespace().next().map(|s| s.to_string());
Some(lang)
}
fn is_block_end(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.to_ascii_uppercase().starts_with("#+END_")
}
fn is_src_end(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.to_ascii_uppercase().starts_with("#+END_SRC")
}
fn is_drawer_begin(line: &str) -> bool {
let trimmed = line.trim();
trimmed.starts_with(':') && trimmed.ends_with(':') && trimmed.len() > 2
}
fn is_drawer_end(line: &str) -> bool {
line.trim().eq_ignore_ascii_case(":END:")
}
fn is_keyword(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.starts_with("#+") && !Self::is_block_begin(line) && !Self::is_block_end(line)
}
fn is_comment(line: &str) -> bool {
let trimmed = line.trim_start();
trimmed.starts_with('#') && !trimmed.starts_with("#+")
}
fn is_table_row(line: &str) -> bool {
line.trim_start().starts_with('|')
}
fn is_latex_begin(line: &str) -> Option<String> {
LATEX_BEGIN_RE
.captures(line)
.map(|caps| caps.get(1).unwrap().as_str().to_string())
}
fn is_latex_end(line: &str, env: &str) -> bool {
LATEX_END_RE
.captures(line)
.is_some_and(|caps| caps.get(1).unwrap().as_str() == env)
}
fn is_display_math_open(line: &str) -> bool {
line.trim() == r"\["
}
fn is_display_math_close(line: &str) -> bool {
line.trim() == r"\]"
}
fn is_export_snippet_line(line: &str) -> bool {
let trimmed = line.trim();
EXPORT_SNIPPET_RE.is_match(trimmed) && trimmed.starts_with("@@")
}
}
impl FormatParser for OrgParser {
fn parse(&self, input: &str) -> Vec<Region> {
let mut regions: Vec<Region> = Vec::new();
let mut current_prose = String::new();
let mut in_block = false;
let mut in_src_block = false;
let mut src_lang: Option<String> = None;
let mut src_header = String::new();
let mut src_body = String::new();
let mut in_drawer = false;
let mut in_latex_env: Option<String> = None;
let mut in_display_math = false;
let mut pragma_off = false;
let mut list_item_indent: Option<usize> = None;
for line in input.lines() {
if !in_src_block {
if let Some(on) = super::check_pragma(line) {
flush_prose(&mut current_prose, &mut regions);
pragma_off = !on;
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if pragma_off {
flush_prose(&mut current_prose, &mut regions);
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
}
if in_src_block {
flush_prose(&mut current_prose, &mut regions);
if Self::is_src_end(line) {
in_src_block = false;
in_block = false;
regions.push(Region::Code {
lang: src_lang.take(),
header: std::mem::take(&mut src_header),
body: std::mem::take(&mut src_body),
footer: format!("{line}\n"),
});
} else {
src_body.push_str(line);
src_body.push('\n');
}
continue;
}
if in_block {
flush_prose(&mut current_prose, &mut regions);
if Self::is_block_end(line) {
in_block = false;
}
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if in_drawer {
flush_prose(&mut current_prose, &mut regions);
if Self::is_drawer_end(line) {
in_drawer = false;
}
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if let Some(ref env) = in_latex_env {
flush_prose(&mut current_prose, &mut regions);
let done = Self::is_latex_end(line, env);
regions.push(Region::Structure(format!("{line}\n")));
if done {
in_latex_env = None;
}
continue;
}
if in_display_math {
flush_prose(&mut current_prose, &mut regions);
if Self::is_display_math_close(line) {
in_display_math = false;
}
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if let Some(lang) = Self::is_src_begin(line) {
flush_prose(&mut current_prose, &mut regions);
in_block = true;
in_src_block = true;
src_lang = lang;
src_header = format!("{line}\n");
src_body.clear();
continue;
}
if Self::is_block_begin(line) {
flush_prose(&mut current_prose, &mut regions);
in_block = true;
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if Self::is_drawer_begin(line) {
flush_prose(&mut current_prose, &mut regions);
in_drawer = true;
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if let Some(env) = Self::is_latex_begin(line) {
flush_prose(&mut current_prose, &mut regions);
in_latex_env = Some(env);
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if Self::is_display_math_open(line) {
flush_prose(&mut current_prose, &mut regions);
in_display_math = true;
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if Self::is_export_snippet_line(line) {
flush_prose(&mut current_prose, &mut regions);
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if line.trim().is_empty() {
flush_prose(&mut current_prose, &mut regions);
list_item_indent = None;
regions.push(Region::BlankLines(format!("{line}\n")));
continue;
}
if Self::is_keyword(line) {
flush_prose(&mut current_prose, &mut regions);
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if Self::is_comment(line) {
flush_prose(&mut current_prose, &mut regions);
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if Self::is_table_row(line) {
flush_prose(&mut current_prose, &mut regions);
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if line.trim_start().starts_with("file:")
|| line.trim_start().starts_with("http://")
|| line.trim_start().starts_with("https://")
{
flush_prose(&mut current_prose, &mut regions);
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if HEADLINE_RE.is_match(line) {
flush_prose(&mut current_prose, &mut regions);
regions.push(Region::Structure(format!("{line}\n")));
continue;
}
if let Some(caps) = LIST_ITEM_RE.captures(line) {
flush_prose(&mut current_prose, &mut regions);
let marker = caps.get(1).unwrap().as_str();
let text = caps.get(2).unwrap().as_str();
list_item_indent = Some(marker.len());
regions.push(Region::Structure(marker.to_string()));
if !text.is_empty() {
regions.push(Region::Prose(text.to_string()));
}
regions.push(Region::Structure("\n".to_string()));
continue;
}
if let Some(indent) = list_item_indent {
let leading = line.len() - line.trim_start().len();
if leading >= indent && !line.trim().is_empty() {
if let Some(Region::Structure(s)) = regions.last() {
if s == "\n" {
regions.pop(); if let Some(Region::Prose(prose)) = regions.last_mut() {
prose.push(' ');
prose.push_str(line.trim());
}
regions.push(Region::Structure("\n".to_string()));
continue;
}
}
}
list_item_indent = None;
}
if !current_prose.is_empty() {
current_prose.push(' ');
}
current_prose.push_str(line.trim());
}
flush_prose(&mut current_prose, &mut regions);
if in_src_block {
regions.push(Region::Code {
lang: src_lang.take(),
header: std::mem::take(&mut src_header),
body: std::mem::take(&mut src_body),
footer: String::new(),
});
}
regions
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simple_prose() {
let input = "Hello world. This is a test.\nAnother line here.";
let regions = OrgParser.parse(input);
assert_eq!(
regions,
vec![Region::Prose(
"Hello world. This is a test. Another line here.".to_string()
)]
);
}
#[test]
fn preserves_blocks() {
let input = "Some prose.\n#+BEGIN_SRC python\nprint('hello')\n#+END_SRC\nMore prose.";
let regions = OrgParser.parse(input);
assert_eq!(regions.len(), 3);
assert!(matches!(®ions[0], Region::Prose(_)));
match ®ions[1] {
Region::Code {
lang,
header,
body,
footer,
} => {
assert_eq!(lang.as_deref(), Some("python"));
assert_eq!(header, "#+BEGIN_SRC python\n");
assert_eq!(body, "print('hello')\n");
assert_eq!(footer, "#+END_SRC\n");
}
other => panic!("expected Region::Code, got {other:?}"),
}
assert!(matches!(®ions[2], Region::Prose(_)));
}
#[test]
fn preserves_keywords() {
let input = "#+TITLE: My Document\n#+AUTHOR: Someone\n\nSome text here.";
let regions = OrgParser.parse(input);
assert!(matches!(®ions[0], Region::Structure(_)));
assert!(matches!(®ions[1], Region::Structure(_)));
}
#[test]
fn headline_is_structure_not_prose() {
let input = "* TODO This is a headline";
let regions = OrgParser.parse(input);
assert_eq!(regions.len(), 1);
assert_eq!(
regions[0],
Region::Structure("* TODO This is a headline\n".to_string())
);
}
#[test]
fn multi_sentence_headline_stays_one_line() {
use crate::format::Format;
use crate::{FormatConfig, format_text};
let input = "** Multi sentence. Second sentence in title\nbody prose. Second body.\n";
let cfg = FormatConfig {
format: Format::Org,
..Default::default()
};
let out = format_text(input, &cfg).unwrap();
assert!(
out.lines()
.any(|l| l == "** Multi sentence. Second sentence in title"),
"headline must stay one line, got:\n{out}"
);
assert!(
!out.contains("** Multi sentence.\nSecond"),
"must not orphan second title sentence without stars:\n{out}"
);
assert_eq!(format_text(&out, &cfg).unwrap(), out);
}
#[test]
fn headline_trailing_angle_bracket_round_trips() {
use crate::format::Format;
use crate::{FormatConfig, format_text};
let input = "* TODO R4 :: snapshot field is Box[T], not Vec[T]\nbody\n";
let cfg = FormatConfig {
format: Format::Org,
..Default::default()
};
let out = format_text(input, &cfg).unwrap();
assert!(
out.contains("Vec[T]"),
"trailing `>` must survive formatting, got:\n{out}"
);
assert_eq!(format_text(&out, &cfg).unwrap(), out);
}
#[test]
fn bold_emphasis_with_period_does_not_become_headline() {
use crate::format::Format;
use crate::{FormatConfig, format_text};
let input = "End of first. *Bold spans period. Continues* after.\n";
let cfg = FormatConfig {
format: Format::Org,
..Default::default()
};
let out = format_text(input, &cfg).unwrap();
let bold_lines: Vec<_> = out
.lines()
.filter(|l| l.contains("*Bold") || l.contains("Continues*"))
.collect();
assert_eq!(
bold_lines.len(),
1,
"bold emphasis must not split across lines, got:\n{out}"
);
assert!(bold_lines[0].contains("*Bold spans period. Continues*"));
for line in out.lines() {
let stars = line.chars().take_while(|c| *c == '*').count();
if stars > 0 {
let rest = &line[stars..];
assert!(
!rest.starts_with(' ') || rest.trim().is_empty() || line.starts_with("* "),
"unexpected star-line: {line}"
);
}
}
assert_eq!(format_text(&out, &cfg).unwrap(), out);
}
#[test]
fn table_preserved() {
let input = "| Name | Age |\n|------+-----|\n| Alice | 30 |";
let regions = OrgParser.parse(input);
assert!(regions.iter().all(|r| matches!(r, Region::Structure(_))));
}
#[test]
fn list_item_split() {
let input = "- First item text\n- Second item text";
let regions = OrgParser.parse(input);
assert_eq!(regions.len(), 6);
assert_eq!(regions[0], Region::Structure("- ".to_string()));
assert_eq!(regions[1], Region::Prose("First item text".to_string()));
}
#[test]
fn list_item_continuation() {
let input = "- First sentence of item.\n Continuation of the same item.\n- Second item";
let regions = OrgParser.parse(input);
assert_eq!(regions[0], Region::Structure("- ".to_string()));
assert_eq!(
regions[1],
Region::Prose("First sentence of item. Continuation of the same item.".to_string())
);
assert_eq!(regions[2], Region::Structure("\n".to_string()));
assert_eq!(regions[3], Region::Structure("- ".to_string()));
assert_eq!(regions[4], Region::Prose("Second item".to_string()));
}
#[test]
fn drawer_preserved() {
let input = ":PROPERTIES:\n:ID: abc123\n:END:\nSome text.";
let regions = OrgParser.parse(input);
assert!(matches!(®ions[0], Region::Structure(_))); assert!(matches!(®ions[1], Region::Structure(_))); assert!(matches!(®ions[2], Region::Structure(_))); }
#[test]
fn latex_environment_preserved() {
let input = "Some text.\n\\begin{equation}\nx = 5\n\\end{equation}\nMore text.";
let regions = OrgParser.parse(input);
assert!(matches!(®ions[0], Region::Prose(_)));
assert!(matches!(®ions[1], Region::Structure(s) if s.contains("\\begin{equation}")));
assert!(matches!(®ions[2], Region::Structure(s) if s.contains("x = 5")));
assert!(matches!(®ions[3], Region::Structure(s) if s.contains("\\end{equation}")));
assert!(matches!(®ions[4], Region::Prose(_)));
}
#[test]
fn display_math_preserved() {
let input = "Some text.\n\\[\nx = 5\n\\]\nMore text.";
let regions = OrgParser.parse(input);
assert!(matches!(®ions[0], Region::Prose(_)));
assert!(matches!(®ions[1], Region::Structure(s) if s.contains("\\[")));
assert!(matches!(®ions[2], Region::Structure(s) if s.contains("x = 5")));
assert!(matches!(®ions[3], Region::Structure(s) if s.contains("\\]")));
assert!(matches!(®ions[4], Region::Prose(_)));
}
#[test]
fn export_snippet_preserved() {
let input = "Text before.\n@@latex:\\newpage@@\nText after.";
let regions = OrgParser.parse(input);
assert!(matches!(®ions[0], Region::Prose(_)));
assert!(matches!(®ions[1], Region::Structure(s) if s.contains("@@latex:")));
assert!(matches!(®ions[2], Region::Prose(_)));
}
#[test]
fn nested_latex_envs() {
let input = "Prose.\n\\begin{align}\na &= b \\\\\nc &= d\n\\end{align}\nMore prose.";
let regions = OrgParser.parse(input);
assert!(matches!(®ions[0], Region::Prose(_)));
let struct_count = regions
.iter()
.filter(|r| matches!(r, Region::Structure(_)))
.count();
assert!(struct_count >= 4); }
}