Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lib): re-export stage2 and stage3 token and node enums to expose them #2

Merged
merged 1 commit into from
Jul 6, 2024

Conversation

NTBBloodbath
Copy link
Member

@NTBBloodbath NTBBloodbath commented Jul 4, 2024

This change allows token and node enums to be used outside of internal parsing by exposing them in the main public API rust_norg, thus allowing them to be used for external parsing based on the chumsky parser. For example, to create a Norg -> HTML parser.

Any suggestion is welcome :)

Dummy example code for headers and paragraphs w/ preview
use rust_norg::{parse, ParagraphSegmentToken, NorgASTFlat};

trait NorgToHtml {
    fn to_html(&self) -> String;
}

fn paragraph_to_string(segment: &[ParagraphSegmentToken]) -> String {
    let mut paragraph: String = "".to_owned();
    segment.iter().for_each(|node| match node {
        ParagraphSegmentToken::Text(s) => paragraph.push_str(s),
        ParagraphSegmentToken::Whitespace => paragraph.push(' '),
        ParagraphSegmentToken::Escape(c) => paragraph.push_str(&c.to_string()),
        ParagraphSegmentToken::Special(c) => paragraph.push_str(&c.to_string()),
    });
    paragraph
}

impl NorgToHtml for NorgASTFlat {
    fn to_html(&self) -> String {
        match self {
            NorgASTFlat::Heading {level, title, ..} => {
                let mut heading_tag: String = "<".to_owned();
                match level {
                    1 => {
                        heading_tag.push_str("h1>")
                    },
                    _ => todo!()
                }
                let heading_title = paragraph_to_string(title);
                heading_tag.push_str(&heading_title);
                heading_tag.push_str("</h1>");
                heading_tag
            },
            NorgASTFlat::Paragraph(s) => {
                let mut paragraph_tag: String = "<p>".to_owned();
                let paragraph = paragraph_to_string(s);
                paragraph_tag.push_str(&paragraph);
                paragraph_tag.push_str("</p>");
                paragraph_tag
            }
            _ => todo!(),
        }
    }
}

fn norg_to_html(ast: &NorgASTFlat) -> String {
    ast.to_html()
}

fn main() -> Result<()> {
    let norg_code = "* Hello, World\n  This is a paragraph lol\n";
    println!("Norg code:\n{}\nProduced HTML:", norg_code);
    let ast = parse(norg_code).unwrap();
    for node in ast {
        let html = norg_to_html(&node);
        println!("{}", html);
    }
    Ok(())
}

demo showcase

Copy link
Member

@vhyrro vhyrro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thx!

@vhyrro vhyrro merged commit a15600c into nvim-neorg:main Jul 6, 2024
1 check passed
@NTBBloodbath NTBBloodbath deleted the reexport-enums branch July 6, 2024 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants