6. Janet

6.1. Setup

<<funcdefs>>=
JanetTable * weewiki_janet_setup(void);
<<functions>>=
JanetTable * weewiki_janet_setup(void)
{
    JanetTable *env;
    janet_init();
    env = janet_core_env(NULL);
    weewiki_janet_cfuns(env);
    return env;
}

6.2. Load C Functions

All the custom C functions for janet are loaded here. This is called in setup, but is also called separately for the janet standalone in janet_main.

<<funcdefs>>=
void weewiki_janet_cfuns(JanetTable *env);
<<functions>>=
<<janet_funcs>>
void weewiki_janet_sqlite(JanetTable *env);
void weewiki_janet_btprnt(JanetTable *env);
void weewiki_janet_cfuns(JanetTable *env)
{
    janet_def(env,
              "print",
              janet_wrap_cfunction(cfun_io_print),
              NULL);
    janet_def(env,
              "prin",
              janet_wrap_cfunction(cfun_io_prin),
              NULL);
    janet_def(env,
              "org",
              janet_wrap_cfunction(cfun_org),
              NULL);
    janet_def(env,
              "ww-open",
              janet_wrap_cfunction(cfun_ww_open),
              NULL);
    janet_def(env,
              "ww-close",
              janet_wrap_cfunction(cfun_ww_close),
              NULL);
    janet_def(env,
              "ww-add-page",
              janet_wrap_cfunction(cfun_ww_add_page),
              NULL);
    janet_def(env,
              "ww-add-link",
              janet_wrap_cfunction(cfun_ww_add_link),
              NULL);
    janet_def(env,
              "ww-sync",
              janet_wrap_cfunction(cfun_ww_sync),
              NULL);
    janet_def(env,
              "ww-clear",
              janet_wrap_cfunction(cfun_ww_clear),
              NULL);
    janet_def(env,
              "ww-db",
              janet_wrap_cfunction(cfun_ww_db),
              NULL);
    janet_def(env,
              "ww-name",
              janet_wrap_cfunction(cfun_name),
              NULL);
<<janet_defs>>
    weewiki_janet_sqlite(env);
    weewiki_janet_btprnt(env);
    janet_dostring(env,
                    "(defn html-header () ())"
                    "(defn html-footer () ())"
                    "(def ww-dir \"\")",
                    NULL, NULL);

}

6.3. Cleanup

<<funcdefs>>=
void weewiki_janet_cleanup(void);
<<functions>>=
void weewiki_janet_cleanup(void)
{
    janet_deinit();
}

6.4. Load Config

<<funcdefs>>=
void weewiki_janet_loadconfig(JanetTable *env);
<<functions>>=
void weewiki_janet_loadconfig(JanetTable *env)
{
    FILE *config;
    size_t config_sz;
    char *tmp;

    config = fopen("weewiki.janet", "r");

    if (config != NULL) {
        fseek(config, 0, SEEK_END);
        config_sz = ftell(config);
        tmp = calloc(1, config_sz + 1);
        fseek(config, 0, SEEK_SET);
        fread(tmp, 1, config_sz, config);
        fclose(config);
        janet_dostring(env, tmp, NULL, NULL);
        free(tmp);
    }
}

6.5. Get www-dir

<<funcdefs>>=
const unsigned char * weewiki_janet_wwdir(JanetTable *env);
<<functions>>=
const unsigned char * weewiki_janet_wwdir(JanetTable *env)
{
    const unsigned char *dir;
    Janet out;

    dir = NULL;
    janet_dostring(env, "ww-dir", NULL, &out);
    dir = janet_unwrap_string(out);
    return dir;
}

6.6. Open/Close database

Should only be used in the janet standalone.

<<static_funcdefs>>=
static Janet cfun_ww_open(int32_t argc, Janet *argv);
static Janet cfun_ww_close(int32_t argc, Janet *argv);

Note: this used to use weewiki_open, but has been changed to use weewiki_create. The latter function will open and initialize a new database (which is what the former used to do).

<<functions>>=
static Janet cfun_ww_open(int32_t argc, Janet *argv)
{
    const uint8_t *filename;
    weewiki_d *ww;
    janet_fixarity(argc, 1);

    filename = janet_getstring(argv, 0);
    ww = weewiki_get();

    weewiki_create(ww, (const char *)filename);
    return janet_wrap_nil();
}

static Janet cfun_ww_close(int32_t argc, Janet *argv)
{
    weewiki_d *ww;
    ww = weewiki_get();
    weewiki_close(ww);
    return janet_wrap_nil();
}

6.7. Add Page

Adds a page to the database.

<<static_funcdefs>>=
static Janet cfun_ww_add_page(int32_t argc, Janet *argv);
<<functions>>=
static Janet cfun_ww_add_page(int32_t argc, Janet *argv)
{
    const char *key;
    const char *val;
    weewiki_d *ww;

    janet_fixarity(argc, 2);
    key = (const char *)janet_getstring(argv, 0);
    val = (const char *)janet_getstring(argv, 1);
    ww = weewiki_get();

    weewiki_add_page(ww, key, val);

    return janet_wrap_nil();
}

6.8. Add Link

Adds a link to the note. Note that the mtime will be set to zero, so that the page is automatically pushed by default.

<<static_funcdefs>>=
static Janet cfun_ww_add_link(int32_t argc, Janet *argv);
<<functions>>=
static Janet cfun_ww_add_link(int32_t argc, Janet *argv)
{
    const char *key;
    const char *fname;
    weewiki_d *ww;

    janet_fixarity(argc, 2);
    key = (const char *)janet_getstring(argv, 0);
    fname = (const char *)janet_getstring(argv, 1);
    ww = weewiki_get();

    weewiki_add_link(ww, key, fname);
    return janet_wrap_nil();
}

6.9. Sync Database

Equivalent to weewiki sync.

<<static_funcdefs>>=
static Janet cfun_ww_sync(int32_t argc, Janet *argv);
<<functions>>=
static Janet cfun_ww_sync(int32_t argc, Janet *argv)
{
    weewiki_d *ww;
    ww = weewiki_get();
    weewiki_sync(ww);
    return janet_wrap_nil();
}

6.10. Clear Database

Clears data from existing wiki db.

<<static_funcdefs>>=
static Janet cfun_ww_clear(int32_t argc, Janet *argv);
<<functions>>=
static Janet cfun_ww_clear(int32_t argc, Janet *argv)
{
    weewiki_d *ww;
    ww = weewiki_get();
    weewiki_clear(ww);
    return janet_wrap_nil();
}

6.11. Get Open Database

<<static_funcdefs>>=
static Janet cfun_ww_db(int32_t argc, Janet *argv);
<<functions>>=
Janet weewiki_return_db(sqlite3 *sql);
static Janet cfun_ww_db(int32_t argc, Janet *argv)
{
    weewiki_d *ww;
    ww = weewiki_get();
    return weewiki_return_db(weewiki_db(ww));
}

6.12. Getting current page name

<<static_funcdefs>>=
static Janet cfun_name(int32_t argc, Janet *argv);
<<functions>>=
static Janet cfun_name(int32_t argc, Janet *argv)
{
    weewiki_d *ww;
    const char *name;
    Janet out;
    ww = weewiki_get();

    name = weewiki_pgname(ww);

    out = janet_wrap_nil();

    if (name !=NULL) {
        out = janet_wrap_string(janet_string((const unsigned char *)name, strlen(name)));
    }

    return out;
}

6.13. Is weewiki in server mode?

Done with the janet function ww-server?

<<janet_defs>>=
janet_def(env,
          "ww-server?",
          janet_wrap_cfunction(cfun_is_server),
          NULL);
<<static_funcdefs>>=
static Janet cfun_is_server(int32_t argc, Janet *argv);
<<functions>>=
static Janet cfun_is_server(int32_t argc, Janet *argv)
{
    weewiki_d *ww;
    ww = weewiki_get();

    return janet_wrap_boolean(weewiki_is_server(ww));
}

6.14. DB name set/get

Wrappers to set/get the current database name.

<<static_funcdefs>>=
static Janet cfun_dbname_set(int32_t argc, Janet *argv);
static Janet cfun_dbname_get(int32_t argc, Janet *argv);
<<janet_defs>>=
janet_def(env,
          "ww-dbname-set",
          janet_wrap_cfunction(cfun_dbname_set),
          NULL);
janet_def(env,
          "ww-dbname-get",
          janet_wrap_cfunction(cfun_dbname_get),
          NULL);
<<functions>>=
static Janet cfun_dbname_set(int32_t argc, Janet *argv)
{
    const char *str;
    janet_fixarity(argc, 1);
    str = (const char *)janet_getstring(argv, 0);
    weewiki_dbname_set(str);
    return janet_wrap_nil();
}

static Janet cfun_dbname_get(int32_t argc, Janet *argv)
{
    janet_fixarity(argc, 0);
    return janet_wrap_string(weewiki_dbname_get());
}

6.15. Zet Resolve

ww-zet-resolve is a function that resolves a UUID. If no ID is found, an error is thrown.

<<janet_defs>>=
janet_def(env,
          "ww-zet-resolve",
          janet_wrap_cfunction(cfun_zet_resolve),
          NULL);
<<janet_funcs>>=
static Janet cfun_zet_resolve(int32_t argc, Janet *argv)
{
    const char *str;
    weewiki_d *ww;
    wwzet_uuid uuid;
    int rc;


    janet_fixarity(argc, 1);
    str = (const char *)janet_getstring(argv, 0);
    weewiki_dbname_set(str);
    ww = weewiki_get();

    rc = wwzet_uuid_resolve(ww, str, strlen(str), &uuid);

    if (rc) {
        janet_panicf("Could not resolve %s\n", str);
    }

    return janet_wrap_string(janet_string((const unsigned char *)uuid.str, 36));
}

6.16. UUID Generator

Bindings to the internal uuidv4 generator used by weewiki.

<<janet_defs>>=
janet_def(env,
          "ww-zet-uuid-init",
          janet_wrap_cfunction(cfun_zet_uuid_init),
          NULL);
janet_def(env,
          "ww-zet-uuid-gen",
          janet_wrap_cfunction(cfun_zet_uuid_gen),
          NULL);
<<janet_funcs>>=
static Janet cfun_zet_uuid_gen(int32_t argc, Janet *argv)
{
    wwzet_uuid uuid;

    janet_fixarity(argc, 0);

    wwzet_uuid_init(&uuid);
    wwzet_uuid_generate(&uuid);

    return janet_wrap_string(janet_string((const unsigned char *)uuid.str, 36));
}
<<janet_funcs>>=
static Janet cfun_zet_uuid_init(int32_t argc, Janet *argv)
{
    wwzet_uuid_rng_init();
    return janet_wrap_nil();
}



prev | home | next