aboutsummaryrefslogtreecommitdiff
path: root/examples/main.c
blob: 2b3c3ca64e7cf2a55c61965dcdf0be69eb1a0f9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <typdown.h>

void foo(char *v) {
    struct typdown_Document doc = typdown_parse(v);
    if (doc.errors != NULL) {
        for (int i = 0; i < doc.errors_len; i++) {
            struct typdown_Error error = doc.errors[i];
            printf("cannot parse '%s', error: %s (%d)\n", v, typdown_getErrorString(error.code), error.code);
            printf("line %d: ", error.location.line);
            for (int j = error.location.beg; j < error.location.end; j++)
                printf("%c", v[j]);
            printf("\n");
        }
        typdown_free(doc);
        return;
    }
    uint8_t code;
    char *res = typdown_renderHTML(doc, &code);
    if (code != 0) {
        printf("cannot render '%s', error: %s (%d)\n", v, typdown_getErrorString(code), code);
        typdown_free(doc);
        return;
    }
    printf("%s\n", res);
    free(res);
    typdown_free(doc);
}

int main() {
    // valid
    foo("hello world");
    foo("he*ll*o world");
    foo("# he*ll*o world");

    // invalid
    foo("hello *world");
    foo("hello world :::");
    foo("# hello :::");
    return 0;
}