5. Main Interface

Orgparse is a callback interface. These functions will handle the various parts of the org file.

5.1. Struct

5.1.1. Declaration

<<typedefs>>=
typedef struct orgparse orgparse;
<<structs>>=
struct orgparse {
<<interface>>
};

5.1.2. Init

<<funcdefs>>=
void orgparse_init(orgparse *op);
<<functions>>=
void orgparse_init(orgparse *op)
{
<<init>>
}

5.1.3. Size

When using is opaquely.

<<funcdefs>>=
size_t orgparse_size(void);
<<functions>>=
size_t orgparse_size(void)
{
    return sizeof(orgparse);
}

5.2. Callbacks

All callbacks have the same first 3 arguments: a generic pointer, the string, and the length of the string.

5.2.1. Header

An org header. In addition to the header name, also supplies the header level.

<<typedefs>>=
typedef void (*orgparse_header)(void *,
                                const char *,
                                size_t,
                                int);
<<interface>>=
orgparse_header header;
<<init>>=
op->header = NULL;
<<funcdefs>>=
void orgparse_set_header(orgparse *op, orgparse_header f);
<<functions>>=
void orgparse_set_header(orgparse *op, orgparse_header f)
{
    op->header = f;
}

5.2.2. Text

This function is anything that isn't formatted text.

<<typedefs>>=
typedef void (*orgparse_text)(void *,
                              const char *,
                              size_t);
<<interface>>=
orgparse_text text;
<<init>>=
op->text = NULL;
<<funcdefs>>=
void orgparse_set_text(orgparse *op, orgparse_text f);
<<functions>>=
void orgparse_set_text(orgparse *op, orgparse_text f)
{
    op->text = f;
}

5.2.3. Code

Text that is defined inside the codeblock tags.

<<interface>>=
orgparse_text code;
<<init>>=
op->code = NULL;
<<funcdefs>>=
void orgparse_set_code(orgparse *op, orgparse_text f);
<<functions>>=
void orgparse_set_code(orgparse *op, orgparse_text f)
{
    op->code = f;
}

5.2.4. Bold

Text that is defined inside the bold tags.

<<interface>>=
orgparse_text bold;
<<init>>=
op->bold = NULL;
<<funcdefs>>=
void orgparse_set_bold(orgparse *op, orgparse_text f);
<<functions>>=
void orgparse_set_bold(orgparse *op, orgparse_text f)
{
    op->bold = f;
}

5.2.5. Italic

Text that is defined inside the italic tags.

<<interface>>=
orgparse_text italic;
<<init>>=
op->italic = NULL;
<<funcdefs>>=
void orgparse_set_italic(orgparse *op, orgparse_text f);
<<functions>>=
void orgparse_set_italic(orgparse *op, orgparse_text f)
{
    op->italic = f;
}

5.2.6. Underline

Text that is contained inside the tags.

<<interface>>=
orgparse_text underline;
<<init>>=
op->underline = NULL;
<<funcdefs>>=
void orgparse_set_underline(orgparse *op, orgparse_text f);
<<functions>>=
void orgparse_set_underline(orgparse *op, orgparse_text f)
{
    op->underline = f;
}

5.2.7. Code Block

Text inside of a code block.

<<interface>>=
orgparse_text codeblock;
<<init>>=
op->codeblock = NULL;
<<funcdefs>>=
void orgparse_set_codeblock(orgparse *op, orgparse_text f);
<<functions>>=
void orgparse_set_codeblock(orgparse *op, orgparse_text f)
{
    op->codeblock = f;
}

5.2.8. Aux

A special non-org tag, used for interpolated code in tags @!and !@.

<<interface>>=
orgparse_text aux;
<<init>>=
op->aux = NULL;
<<funcdefs>>=
void orgparse_set_aux(orgparse *op, orgparse_text f);
<<functions>>=
void orgparse_set_aux(orgparse *op, orgparse_text f)
{
    op->aux = f;
}

5.2.9. Newline

The newline callback gets called anytime there is an empty line, which means an explicit line break is needed. For convenience, the orgparse_text callback is used, though the arguments will be unused and set to NULL.

<<interface>>=
orgparse_text newline;
<<init>>=
op->newline = NULL;
<<funcdefs>>=
void orgparse_set_newline(orgparse *op, orgparse_text f);
<<functions>>=
void orgparse_set_newline(orgparse *op, orgparse_text f)
{
    op->newline = f;
}

5.2.10. Name

Handles a 'NAME' command.

<<interface>>=
orgparse_text name;
<<init>>=
op->name = NULL;
<<funcdefs>>=
void orgparse_set_name(orgparse *op, orgparse_text f);
<<functions>>=
void orgparse_set_name(orgparse *op, orgparse_text f)
{
    op->name = f;
}

5.2.11. Title

Handles a titlecommand.

<<interface>>=
orgparse_text title;
<<init>>=
op->title = NULL;
<<funcdefs>>=
void orgparse_set_title(orgparse *op, orgparse_text f);
<<functions>>=
void orgparse_set_title(orgparse *op, orgparse_text f)
{
    op->title = f;
}

5.2.12. Link

<<typedefs>>=
typedef void (*orgparse_link)(void *,
                              const char *,
                              size_t,
                              const char *,
                              size_t);
<<interface>>=
orgparse_link link;
<<init>>=
op->link = NULL;
<<funcdefs>>=
void orgparse_set_link(orgparse *op, orgparse_link f);
<<functions>>=
void orgparse_set_link(orgparse *op, orgparse_link f)
{
    op->link = f;
}

5.2.13. Paragraph

The pargraph callback gets called anytime a paragraph block starts or end. For HTML generation, this will be in charge of generating p-tags.

A paragraph starts when a new text block begins, and ends with a line break, or major mode change (such as for a code block or header).

<<typedefs>>=
typedef void (*orgparse_pgrph)(void *, int);
<<interface>>=
orgparse_pgrph pgrph;
<<init>>=
op->pgrph = NULL;
<<funcdefs>>=
void orgparse_set_pgrph(orgparse *op, orgparse_pgrph f);
<<functions>>=
void orgparse_set_pgrph(orgparse *op, orgparse_pgrph f)
{
    op->pgrph = f;
}

A paragraph begins with orgparse_pgrph_begin. A paragraph ends with orgparse_pgrph_end. Note that neither of these functions actually check to see if they are supposed to be beginning or ending. That logic is done elsewhere.

<<funcdefs>>=
void orgparse_pgrph_begin(orgparse *op, void *ud);
void orgparse_pgrph_end(orgparse *op, void *ud);
<<functions>>=
void orgparse_pgrph_begin(orgparse *op, void *ud)
{
    if (op->pgrph != NULL) {
        op->pgrph(ud, 0);
    }
}

void orgparse_pgrph_end(orgparse *op, void *ud)
{
    if (op->pgrph != NULL) {
        op->pgrph(ud, 1);
    }
}

Paragraphs do not end at the end of a file. It will need to be ended explicitely. This used to be done with a function called orgparse_wrapup, which worked by checking a return flag and adding a paragraph. This ended up being too simplistic and limiting, and has since been removed..

A new version of orgparse_wrapup is used, this time taking in an orgparse_state variable. We'll call it orgparse_end.

This was created because the single rc flag (txtmode) alone was giving some false positives, and rogue paragraph ends were happening.

This new function checks both the txtmodeand newline flags. A paragraph end will only happen when the newline flag is not set.

<<funcdefs>>=
void orgparse_end(orgparse *op,
                  void *ud,
                  orgparse_state *state);
<<functions>>=
void orgparse_end(orgparse *op,
                  void *ud,
                  orgparse_state *state)
{
    int rc;
    rc =
        state->flags->txtmode &&
        state->flags->newline == 0;

    if (rc) {
        orgparse_pgrph_end(op, ud);
    }
}



prev | home | next