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

Help reconsturcting comrak manually #186

Closed
kkharji opened this issue Apr 18, 2021 · 8 comments
Closed

Help reconsturcting comrak manually #186

kkharji opened this issue Apr 18, 2021 · 8 comments

Comments

@kkharji
Copy link

kkharji commented Apr 18, 2021

Hey @kivikakk I'm trying to create a manipulation library for comrak to both solve a previous challenge I told you about and to have something to be reused in other projects.

I'd like to construct the following manually for learning purpose to

The original text for these events was:
This is some text.
## Then a *header*
[And a link](https://example.com)
    let root = AstNode::from(NodeValue::Document);
    let paragraph = AstNode::from(NodeValue::Paragraph);
    root.append(&paragraph);
    println!("{:?}", &root);
    /*********************
    Node {
        data: RefCell {
            value: Ast {
                value: Document,
                start_line: 0,
                content: [],
                open: true,
                last_line_blank: false
            }
        },
        children: [
            Node {
                data: RefCell {
                    value: Ast {
                        value: Paragraph,
                        start_line: 0,
                        content: [],
                        open: true,
                        last_line_blank: false
                    }
                },
                children: []
            }
        ]
    }
    *********************/

Thanks and sorry for the inconvenience

@kkharji kkharji changed the title Help reconsturcting Help reconsturcting comrak manually Apr 18, 2021
@kivikakk
Copy link
Owner

Unfortunately, I won’t have the time to put this together for you. Maybe another user does!

@kkharji
Copy link
Author

kkharji commented Apr 18, 2021

Wow, it is that difficult to programmatically add text to node? I'm sure you miss understood what I'm asking here. I simply want know how add content/text to a node, not asking you to help me build a manipulation library, I know how busy you are.

         Node {
                data: RefCell {
                    value: Ast {
                        value: Paragraph,
                        start_line: 0,
                        content: [], //// how to access this?
                        open: true,
                        last_line_blank: false
                    }
          }

I tried the following but didn't work

    paragraph.data.borrow_mut().value.content = .....;

For example in pulldown_cmark they does it with

        let inputs = vec![
            (Event::Start(Tag::Paragraph), false),
            (Event::Text("This is some text.".into()), false),
            (Event::End(Tag::Paragraph), false),
            (Event::Start(Tag::Heading(2)), true),
            (Event::Text("Then a ".into()), true),
            (Event::Start(Tag::Emphasis), true),
            (Event::Text("header".into()), true),
            (Event::End(Tag::Emphasis), true),
            (Event::End(Tag::Heading(2)), true),
            (Event::Start(Tag::Paragraph), false),
            (
                Event::Start(Tag::Link(
                    LinkType::Inline,
                    "https://example.com".into(),
                    "".into(),
                )),
                false,
            ),
            (Event::Text("And a link".into()), false),
            (
                Event::End(Tag::Link(
                    LinkType::Inline,
                    "https://example.com".into(),
                    "".into(),
                )),
                false,
            ),
            (Event::End(Tag::Paragraph), false),
        ];

Thanks

@kivikakk
Copy link
Owner

Wow, it is that difficult to programmatically add text to node?

It was not at all clear that's what you were asking.

Per #175 (comment), you need to add a Text subnode. content is only used internally during processing and is not pub as it doesn't represent user-facing data.

@kkharji
Copy link
Author

kkharji commented Apr 18, 2021

🥲 wish if your architecture and api considered future users aiming to build upon the library to create awesome extensions and tools.

Although this still does not solve my issue, I will close and give up.

Thanks @kivikakk

@kkharji kkharji closed this as completed Apr 18, 2021
@kivikakk
Copy link
Owner

The architecture maps exactly one-to-one with the architecture of cmark-gfm, as explained in the README. It's the way it is for a reason.

Adding a Text subnode doesn't seem like that much of a hurdle for serious use; I'm not sure how it doesn't answer your question about adding text to a paragraph.

All the best ❤️

@kkharji
Copy link
Author

kkharji commented Apr 18, 2021

The architecture maps exactly one-to-one with the architecture of cmark-gfm, as explained in the README. It's the way it is for a reason.

And? so what? yours is in rust and much better, sadly limited to one-three helper function and the low level api just sucks and feels impossible.

Yah the text thing is not a serious use case, but it shows how poor the api for supporting such a small tasks.

Finally, why is it the way it is?
Why is it so dangerous to expose or modify some aspects or add more helper functions to skip boilerplates code, I really don't understand.

If I ware you, and I came with such a great tool, I'd ensure that others have idiomatic apis that make their life easier and support further development.

Anyway, I do apologize, please ignore me, I'm just a bit frustrated that's all. Thanks, all the best to you too 🤗

@kivikakk
Copy link
Owner

And? so what?

I'm just saying, the reason the node structure is as it is(text content is in a subnode and not just on the paragraph itself) is because it matches cmark-gfm, and the purpose of the design is that the internal structure will always match cmark-gfm in order to facilitate changing with the spec. (e.g. #183)

Why is it so dangerous to expose or modify some aspects or add more helper functions to skip boilerplates code

I'm not against adding helpers, I just don't have any desire to write them myself. I will accept thoughtful PRs.

(I'm not against exposing anything; the reason that field isn't exposed is because exposing to users it would make no sense. As you've seen yourself, setting content doesn't do anything. It's an internal field used during block parsing, and doesn't mean anything after parsing is complete. It might as well be called internal_temp.)

If I ware you, and I came with such a great tool, I'd ensure that others have idiomatic apis that make their life easier and support further development

Then please, do.

I haven't used Rust myself in years. I maintain CommonMark/GFM spec compatibility, fix bugs and security issues that arise, and try to help users with what time and energy I can spare, but that's not a lot these days -- my priorities have changed a lot in the almost 5 years since I started this project.

If you want to add those idiomatic APIs yourself, by all means do. I'm happy to spend proper time on reviewing larger changes. I don't have any intrinsic motivation to add such APIs myself: I am not a user of the library these days, just the author. But I am motivated to help others improve it if they would like to.

@kkharji
Copy link
Author

kkharji commented Apr 18, 2021

❤️❤️🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants