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

Does not support tables (patch included) #18

Open
bztsrc opened this issue Jan 17, 2022 · 0 comments
Open

Does not support tables (patch included) #18

bztsrc opened this issue Jan 17, 2022 · 0 comments

Comments

@bztsrc
Copy link

bztsrc commented Jan 17, 2022

Hi,

I couldn't find the very much needed table support, so I've added a Parser for it. It is using a minimalistic state-machine in the spirit of simplicity. Known issues:

  1. you have to add it before the doparagraph parser, otherwise your tables will be surrounded by <p></p>
  2. only handles 32 coloumns' alignments (more supported, just always will be left-aligned)
  3. it does not right-trim the table cells. Implementing this would require lot more complexity, and I've thought simplicity is more important here.

Supports normal coloumns, |---|, right aligned |--:| and centered |:-:|. Licensed under MIT, use it as you wish.

/* smu table parser, Copyright(C) bzt 2022 MIT */

static char intable = 0, inrow, incell;                         /* table state */
static long int calign;

int
dotable(const char *begin, const char *end, int newblock) {
    const char *p;
    int i, l = (int)sizeof(calign) * 4;

    if(*begin != '|')
        return 0;
    if(inrow && (begin + 1 >= end || begin[1] == '\n')) {       /* close cell and row and if ends, table too */
        fprintf(stdout, "</t%c></tr>", inrow == -1 ? 'h' : 'd');
        inrow = 0;
        if(begin + 2 >= end || begin[2] == '\n') {
            intable = 0;
            fputs("\n</table>", stdout);
            return 2;
        }
        return 1;
    }
    if(begin < end && (begin[1] == '-' || begin[1] == ':')) {   /* only load cell aligns from 2nd line */
        for(i = -1, p = begin; p < end && *p != '\n'; p++)
            if(*p == '|') {
                i++;
                if(i < l && p[1] == ':') {
                    calign |= 1 << (i * 2); p++;
                }
            } else
            if(i < l && *p == ':')
                calign |= 1 << (i * 2 + 1);
        return p - begin + 1;
    }
    if(!intable) {                                              /* open table */
        intable = 1; inrow = -1; incell = 0; calign = 0;
        fputs("<table>\n<tr>", stdout);
    }
    if(!inrow) {                                                /* open row */
        inrow = 1; incell = 0;
        fputs("<tr>", stdout);
    }
    if(incell)                                                  /* close cell */
        fprintf(stdout, "</t%c>", inrow == -1 ? 'h' : 'd');
    l = incell < l ? (calign >> (incell * 2)) & 3 : 0;          /* open cell */
    fprintf(stdout, "<t%c%s>", inrow == -1 ? 'h' : 'd',
        l == 2 ? " class=\"right\"" : (l == 3 ? " class=\"center\"" : ""));
    incell++;
    for(p = begin + 1; p < end && *p == ' '; p++);
    return p - begin;
}

Cheers,
bzt

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

No branches or pull requests

1 participant