mity / md4c

C Markdown parser. Fast. SAX-like interface. Compliant to CommonMark specification.
MIT License
756 stars 138 forks source link

Text-Align for Blocs #117

Closed matwachich closed 4 years ago

matwachich commented 4 years ago

Hello, I know this is not standard extension, but I need it for a personal project. Can you advise me how to do it? I can modify your code if needed, or is there another way to do it?

What is in your opinion the best syntax for it? I thought about:

<| Normal bloc text

Juste like this one

<> Centered paragraphe

|> Right-aligned paragraphe

Thank you :)

mity commented 4 years ago

Hello, I know this is not standard extension, but I need it for a personal project.

Yes. I see it as too non-standard to make it into upstream.

Can you advise me how to do it? I can modify your code if needed, or is there another way to do it?

It depends on an exact behavior you want from it.

  1. Is it intended to behave similarly as a quote block, with support for any blocks inside of it, including for nesting blocks who also specify (possibly different) alignment?

    Then you would have to study how quote blocks are implemented and do something very similar.

    It would be quite a lot of work to do this.

  2. If you intend to support it only for paragraphs, and if it is only 1st line which (optionally) contains the alignment syntax, you may do without any modification to the parser.

    Just write a custom renderer which tests in the text() callback whether you are at the beginning of a paragraph. In such case just test whether it begins with one of those syntaxes and if yes, eat it from a normal text and act accordingly.

    This would be far the easiest way.

  3. If you intend to support it only for paragraphs, but you expect ppl write it on every line of the paragraph, you could maybe try changing md_process_inlines() to test for it.

    However note this function is called not just of a paragraph but for any block containing a text which supports Markdown inlines, including a tight list item or a table cell, for example, so you might need to pass in a new flag so you can test whether you are really just a paragraph.

    I'm not sure how complicated this would be to do or what obstacles you could encounter. There is likely not (yet) anything with similar semantics in the code.

    Also you would have to handle the crazy cases when the alignment marks in the single paragraph disagree with each other etc.

matwachich commented 4 years ago

Ok, I made it. And I think it works pretty well.

I went for the second option, with support for aligning paragraphs, headings and list items.

I dont know if I should make a pull request since this is non standard markdown?

mity commented 4 years ago

Good it works for you.

I'm afraid a PR would indeed be pointless.

I'm generally quite open to extensions beyond the CommonMark standard which are quite widely adopted elsewhere and which can be more-or-less expected to eventually land there (even though maybe with some changes in a corner case details); or which are important features of some other (de-facto) standard like the GFM.

Adopting a brand new syntax is simply beyond that scope.