wayver's git archive


an obsidian renderer
git clone https://git.wayver.dev/sable

sable-markdown/src/parser/util.rs@main

raw
Date Commit Message Author Files + -
2026-02-23 01:55 initial mvp wayverd 139 17808 0
...

1use std::{collections::HashMap, sync::LazyLock};
2
3use nom::{
4    IResult, Parser,
5    branch::alt,
6    character::complete::{anychar, line_ending, not_line_ending, space0},
7    combinator::{eof, not, recognize},
8    multi::{many0, many1},
9    sequence::{preceded, terminated},
10};
11
12pub(super) static ENTITIES: LazyLock<HashMap<String, &'static entities::Entity>> =
13    LazyLock::new(|| {
14        let mut map = HashMap::new();
15        for entity in &entities::ENTITIES {
16            map.insert(entity.entity.to_string(), entity);
17        }
18        map
19    });
20
21pub(super) fn eof_or_eol(input: &str) -> IResult<&str, &str> {
22    alt((line_ending, eof)).parse(input)
23}
24
25pub(super) fn many_empty_lines0(input: &str) -> IResult<&str, Vec<&str>> {
26    many0(preceded(space0, eof_or_eol)).parse(input)
27}
28
29pub(super) fn not_eof_or_eol1(input: &str) -> IResult<&str, &str> {
30    recognize(many1(preceded(not(eof_or_eol), anychar))).parse(input)
31}
32
33pub(super) fn not_eof_or_eol0(input: &str) -> IResult<&str, &str> {
34    alt((not_line_ending, eof)).parse(input)
35}
36
37pub(super) fn line_terminated<'a, O, P>(
38    inner: P,
39) -> impl Parser<&'a str, Output = O, Error = nom::error::Error<&'a str>>
40where
41    P: Parser<&'a str, Output = O, Error = nom::error::Error<&'a str>>,
42{
43    terminated(inner, eof_or_eol)
44}
45
46// pub(crate) fn logged<'a, O, P>(
47//     message: &'static str,
48//     mut inner: P,
49// ) -> impl Parser<&'a str, Output = O, Error = nom::error::Error<&'a str>>
50// where
51//     P: Parser<&'a str, Output = O, Error = nom::error::Error<&'a str>>,
52//     O: std::fmt::Debug,
53// {
54//     move |input: &'a str| {
55//         println!("Logged: {message}: {:?}", input);
56//         let r = inner.parse(input);
57//         println!("Logged out: {message}: {:?}", r);
58//         r
59//     }
60// }
61