4. Parsing A Page

A single weewiki page is parsed with parse_file.

At this point, it is assumed the page name key and it's contents val are loaded up into memory.

<<funcdefs>>=
static void parse_file(const char *key, size_t keysz,
                       const char *val, size_t valsz,
                       FILE *fp);

Parsing happens on a line-by-line. It reads up to the next line break, then parses that chunk. At the end, anything remaining will be parsed.

Each line is checked for enclosing equal signs characters. The left equal must not have a space following it, and the right equal must not have a space preceding it.

If such a pattern is found, the content inside is captured. This is considered to be a "word", even if there are multiple words contained in it. The data is then written to the specified filehandle, presumably stdout.

Content in between source blocks in worgle are ignored. A global ignore flag will be used. It will be set when a line finds a BEGIN_SRC declaration, and will turn off when it finds an END_SRC declaration.

<<funcs>>=
static void parse_file(const char *key, size_t keysz,
                       const char *val, size_t valsz,
                       FILE *fp)
{
    size_t i;
    size_t start;
    int linepos;
    int ignore;
    const char *fs;

    start = 0;
    linepos = 0;
    ignore = 0;

    fs = "\t";

    for (i = 0; i < valsz; i++) {
        char c;
        c = val[i];

        if (c == '\n') {
            size_t k;
            size_t wordlen;
            int wordstarted;
            size_t wordpos;
            linepos++;

            if (!strncmp(&val[start], "#+BEGIN_SRC", 11)) {
                ignore = 1;
            } else if (!strncmp(&val[start], "#+END_SRC", 9)) {
                ignore = 0;
                start = i + 1;
                continue;
            };

            if (ignore) {
                start = i + 1;
                continue;
            }

            wordlen = 0;
            wordstarted = 0;
            wordpos = 0;

            for (k = start; k < i; k++) {
                char s;
                s = val[k];
                if (s == '=') {
                    if (wordstarted) {
                        wordstarted = 0;
                        fwrite(key, 1, keysz, fp);
                        fprintf(fp, fs);
                        fwrite(&val[wordpos], 1, wordlen, fp);
                        fprintf(fp, "%s%d\n", fs, linepos);
                    } else {
                        if ((k + 1) < valsz && val[k + 1] != ' ') {
                            wordstarted = 1;
                            wordlen = 0;
                            wordpos = k + 1;
                        }
                    }
                } else if (wordstarted) {
                    wordlen++;
                }
            }

            start = i + 1;
        }
    }
}



prev | home | next