Example #1
0
File: blg.c Project: kingiol/cmoon
static NEOERR* rend_blog(HASH *dbh, HASH *tplh, int bid)
{
    CSPARSE *cs = NULL;
    HDF *hdf, *dhdf;
    STRING str;
    NEOERR *err;
    char fname[_POSIX_PATH_MAX];
    
    if (!dbh || !tplh || bid < 0) return nerr_raise(NERR_ASSERT, "paramter null");
    
    cs = (CSPARSE*)hash_lookup(tplh, "blog");
    dhdf = (HDF*)hash_lookup(tplh, "blog_hdf");
    if (!cs || !dhdf) return nerr_raise(LERR_MISS_TPL, "blog_index not found");

    err = hdf_init(&hdf);
    if (err != STATUS_OK) return nerr_pass(err);

    hdf_copy(hdf, NULL, dhdf);

    ltpl_prepare_rend(hdf, "layout.html");
    
    hdf_set_int_value(hdf, PRE_QUERY".bid", bid);

    err = blog_static_get(hdf, dbh);
    if (err != STATUS_OK) goto done;
    
    hdf_set_copy(hdf, PRE_LAYOUT".title", PRE_OUTPUT".blog.title");
    
    cs->hdf = hdf;

    string_init(&str);
    err = cs_render(cs, &str, mcs_strcb);
    if (err != STATUS_OK) goto done;
    
    snprintf(fname, sizeof(fname), "%s%d/%d.html",
             PATH_BLOG, bid%BLOG_SUBDIR_NUM, bid);

    err = mutil_makesure_dir(fname);
    if (err != STATUS_OK) goto done;

    err = mcs_str2file(str, fname);
    if (err != STATUS_OK) goto done;

#ifdef DEBUG_HDF
    hdf_write_file(hdf, TC_ROOT"hdf.blg");
#endif

done:
    hdf_destroy(&hdf);
    cs->hdf = NULL;
    string_clear(&str);
    return nerr_pass(err);
}
Example #2
0
File: ltpl.c Project: adderly/cmoon
/*
 * we don't pass tplh parameter to it,
 * because walker don't have it. so, tplh stored in the g_datah
 */
NEOERR* ltpl_render2file(CGI *cgi, char *render, char *fname)
{
    STRING str; string_init(&str);
    HASH *tplh;
    CSPARSE *cs;
    HDF *dhdf;
    NEOERR *err;

    MCS_NOT_NULLC(cgi->hdf, render, fname);
    
    tplh = hash_lookup(g_datah, "runtime_templates");
    MCS_NOT_NULLA(tplh);

    cs = hash_lookup(tplh, render);
    MCS_NOT_NULLA(cs);

    dhdf = hash_lookupf(tplh, "%s_hdf", render);
    if (dhdf) hdf_copy(cgi->hdf, NULL, dhdf);

    ltpl_prepare_rend(cgi->hdf, hdf_get_value(cgi->hdf,
                                              PRE_RESERVE"."PRE_CFG_LAYOUT,
                                              "layout.html"));
    cs->hdf = cgi->hdf;

    err = cs_render(cs, &str, mcs_strcb);
    if (err != STATUS_OK) return nerr_pass(err);

    err = mfile_makesure_dir(fname);
    if (err != STATUS_OK) return nerr_pass(err);
    
    err = mcs_str2file(str, fname);
    if (err != STATUS_OK) return nerr_pass(err);

    string_clear(&str);

    return STATUS_OK;
}
Example #3
0
File: ltpl.c Project: adderly/cmoon
NEOERR* ltpl_render(CGI *cgi, HASH *tplh, session_t *ses)
{
    STRING str; string_init(&str);
    CSPARSE *cs;
    HDF *dhdf;
    NEOERR *err;

    char *render = NULL;

    render = ses->render;
    cs = (CSPARSE*)hash_lookup(tplh, render);
    dhdf = (HDF*)hash_lookupf(tplh, "%s_hdf", render);

    if (!cs) return nerr_raise(LERR_MISS_TPL, "render %s not found", render);
    if (dhdf) hdf_copy(cgi->hdf, NULL, dhdf);
    
    ltpl_prepare_rend(cgi->hdf, hdf_get_value(cgi->hdf,
                                              PRE_RESERVE"."PRE_CFG_LAYOUT,
                                              "layout.html"));
    
    if (ses->tm_cache_browser > 0) {
        hdf_set_valuef(cgi->hdf, "cgiout.other.cache=Cache-Control: max-age=%lu",
                       ses->tm_cache_browser);
    }
    cs->hdf = cgi->hdf;

    err = cs_render(cs, &str, mcs_strcb);
    if (err != STATUS_OK) return nerr_pass(err);

    err = cgi_output(cgi, &str);
    if (err != STATUS_OK) return nerr_pass(err);

    cs->hdf = NULL;
    string_clear(&str);

    return STATUS_OK;
}
Example #4
0
File: blg.c Project: kingiol/cmoon
static NEOERR* rend_blog_index(HASH *dbh, HASH *tplh, int pageid, int *pgttr)
{
    CSPARSE *cs = NULL;
    HDF *hdf, *dhdf;
    STRING str;
    NEOERR *err = STATUS_OK;
    char fname[_POSIX_PATH_MAX];
    
    if (!dbh || !tplh) return nerr_raise(NERR_ASSERT, "paramter null");
    
    cs = (CSPARSE*)hash_lookup(tplh, "blog_index");
    dhdf = (HDF*)hash_lookup(tplh, "blog_index_hdf");
    if (!cs || !dhdf) return nerr_raise(LERR_MISS_TPL, "blog_index not found");

    err = hdf_init(&hdf);
    if (err != STATUS_OK) return nerr_pass(err);

    hdf_copy(hdf, NULL, dhdf);

    ltpl_prepare_rend(hdf, "layout.html");

    hdf_set_int_value(hdf, PRE_QUERY".pageid", pageid);

    err = blog_index_static_get(hdf, dbh);
    if (err != STATUS_OK) goto done;

    int ntt = hdf_get_int_value(hdf, PRE_OUTPUT".ntt", 0);
    int pgtt = hdf_get_int_value(hdf, PRE_OUTPUT".pgtt", 1);
    if (pgttr) *pgttr = pgtt;
    if (pageid == 0) {
        if (pgtt > 1) {
            err = hdf_set_int_value(hdf, PRE_OUTPUT".pgprev", pgtt-1);
            TRACE_NOK(err);

            if (ntt % BLOG_NUM_PERPAGE == 1) {
                err = rend_blog_index(dbh, tplh, pgtt-1, NULL);
                TRACE_NOK(err);

                if (pgtt > 2) {
                    /* origin 1.html's nex is index.html, change them into 2.html */
                    err = rend_blog_index(dbh, tplh, pgtt-2, NULL);
                    TRACE_NOK(err);
                }
            }
        }
    } else {
        if (pageid > 1 && pgtt > 1)
            hdf_set_int_value(hdf, PRE_OUTPUT".pgprev", pageid-1);
        if (pgtt == pageid+1)
            hdf_set_value(hdf, PRE_OUTPUT".pgnext", "index");
        else if (pgtt > pageid)
            hdf_set_int_value(hdf, PRE_OUTPUT".pgnext", pageid+1);
    }
    
    cs->hdf = hdf;

    string_init(&str);
    err = cs_render(cs, &str, mcs_strcb);
    if (err != STATUS_OK) goto done;

    if (pageid == 0)
        snprintf(fname, sizeof(fname), "%sindex.html", PATH_BLOG);
    else
        snprintf(fname, sizeof(fname), "%s%d.html", PATH_BLOG, pageid);
    
    err = mutil_makesure_dir(fname);
    if (err != STATUS_OK) goto done;
    
    err = mcs_str2file(str, fname);
    if (err != STATUS_OK) goto done;

#ifdef DEBUG_HDF
    hdf_write_file(hdf, TC_ROOT"hdf.blg.index");
#endif

done:
    hdf_destroy(&hdf);
    cs->hdf = NULL;
    string_clear(&str);
    return nerr_pass(err);
}
Example #5
0
File: ltpl.c Project: adderly/cmoon
NEOERR* ltpl_parse_file(HASH *dbh, HASH *evth,
                        void *lib, char *dir, char *name, HASH *outhash)
{
    char *tp = NULL, *tpl = NULL, *val = NULL;
    HDF *node = NULL, *dhdf = NULL, *child = NULL, *thdf = NULL;
    CSPARSE *cs = NULL;
    STRING str;
    char fname[_POSIX_PATH_MAX], tok[64], *outfile;
    NEOERR* (*data_handler)(HDF *hdf, HASH *dbh, HASH *evth);
    NEOERR *err;
    
    memset(fname, 0x0, sizeof(fname));
    snprintf(fname, sizeof(fname), "%s/%s", dir, name);
    err = hdf_init(&node);
    if (err != STATUS_OK) return nerr_pass(err);
 
    err = hdf_read_file(node, fname);
    if (err != STATUS_OK) return nerr_pass(err);

    child = hdf_obj_child(node);
    while (child != NULL) {
        mtc_dbg("parse node %s", hdf_obj_name(child));
        string_init(&str);

        val = mcs_obj_attr(child, "merge");
        if (val) {
            ULIST *list;
            string_array_split(&list, val, ",", 10);
            ITERATE_MLIST(list) {
                snprintf(fname, sizeof(fname), "%s/%s",
                         dir, neos_strip((char*)list->items[t_rsv_i]));
                err = hdf_init(&dhdf);
                JUMP_NOK(err, wnext);
                err = hdf_read_file(dhdf, fname);
                JUMP_NOK(err, wnext);
                err = hdf_copy(child, NULL, dhdf);
                JUMP_NOK(err, wnext);
            }
            uListDestroy(&list, ULIST_FREE);
        }

        /*
         * can't use dataset directly, because we'll destroy the whole node
         */
        err = hdf_init(&dhdf);
        JUMP_NOK(err, wnext);
        err = hdf_get_node(child, PRE_CFG_DATASET, &thdf);
        JUMP_NOK(err, wnext);
        err = hdf_copy(dhdf, NULL, thdf);
        JUMP_NOK(err, wnext);
        
        err = cs_init(&cs, dhdf);
        JUMP_NOK(err, wnext);

        hdf_set_value(cs->hdf, "hdf.loadpaths.tpl", PATH_TPL);
        hdf_set_value(cs->hdf, "hdf.loadpaths.local", dir);

        err = cgi_register_strfuncs(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_bitop_functions(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_mkd_functions(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_string_uslice(cs);
        JUMP_NOK(err, wnext);

        tpl = hdf_get_value(child, PRE_CFG_LAYOUT, "null.html");
        snprintf(fname, sizeof(fname), "%s/%s", PATH_TPL, tpl);
        err = cs_parse_file(cs, fname);
        JUMP_NOK(err, wnext);

        if (outhash != NULL) {
            /*
             * store template for rend stage use
             */
            hdf_set_value(cs->hdf, PRE_RESERVE"."PRE_CFG_LAYOUT, tpl);
            
            /*
             * strdup the key, baby, because we'll free the hdf later
             */
            err = hash_insert(outhash, (void*)strdup(hdf_obj_name(child)), (void*)cs);
            JUMP_NOK(err, wnext);

            snprintf(tok, sizeof(tok), "%s_hdf", hdf_obj_name(child));
            err = hash_insert(outhash, (void*)strdup(tok), (void*)cs->hdf);
            JUMP_NOK(err, wnext);
        }

        if ((outfile = hdf_get_value(child, PRE_CFG_OUTPUT, NULL)) != NULL) {
            ltpl_prepare_rend(cs->hdf, tpl);
                
            /*
             * get_data
             */
            val = hdf_get_value(child, PRE_CFG_DATAER, NULL);
            if (val != NULL && lib) {
                data_handler = dlsym(lib, val);
                if( (tp = dlerror()) != NULL) {
                    mtc_err("%s", tp);
                    //continue;
                } else {
                    err = (*data_handler)(cs->hdf, dbh, evth);
                    TRACE_NOK(err);
                }
            }

            err = cs_render(cs, &str, mcs_strcb);
            JUMP_NOK(err, wnext);

            /*
             * produce output filename
             */
            val = mcs_hdf_attr(child, PRE_CFG_OUTPUT, "ftime");
            if (val) {
                char tm[LEN_TM];
                mutil_getdatetime(tm, sizeof(tm), val, 0);
                outfile = mstr_repstr(1, outfile, "$ftime$", tm);
            }
            snprintf(fname, sizeof(fname), PATH_DOC"%s", outfile);

            /*
             * output file
             */
            err = mfile_makesure_dir(fname);
            JUMP_NOK(err, wnext);

            err = mcs_str2file(str, fname);
            JUMP_NOK(err, wnext);
#ifdef DEBUG_HDF
            snprintf(fname, sizeof(fname), "%s/hdf.%s",
                     TC_ROOT, hdf_obj_name(child));
            hdf_write_file(child, fname);
#endif
        }

    wnext:
        if (cs != NULL && outhash == NULL)
            cs_destroy(&cs);
        string_clear(&str);
        child = hdf_obj_next(child);
    }
        
    if (node != NULL) hdf_destroy(&node);

    return STATUS_OK;
}
Example #6
0
File: ltpl.c Project: kingiol/cmoon
NEOERR* ltpl_parse_file(HASH *dbh, void *lib, char *dir, char *name, HASH *outhash)
{
    char *tp = NULL, *tpl = NULL, *val = NULL;
    HDF *node = NULL, *dhdf = NULL, *child = NULL;
    CSPARSE *cs = NULL;
    STRING str;
    char fname[_POSIX_PATH_MAX], tok[64];
    NEOERR* (*data_handler)(HDF *hdf, HASH *dbh);
    NEOERR *err;
    
    memset(fname, 0x0, sizeof(fname));
    snprintf(fname, sizeof(fname), "%s/%s", dir, name);
    err = hdf_init(&node);
    if (err != STATUS_OK) return nerr_pass(err);

    err = hdf_read_file(node, fname);
    if (err != STATUS_OK) return nerr_pass(err);

    child = hdf_obj_child(node);
    while (child != NULL) {
        mtc_dbg("parse node %s", hdf_obj_name(child));
        string_init(&str);

        val = mutil_obj_attr(child, "merge");
        if (val) {
            snprintf(fname, sizeof(fname), "%s/%s", dir, val);
            err = hdf_init(&dhdf);
            JUMP_NOK(err, wnext);
            err = hdf_read_file(dhdf, fname);
            JUMP_NOK(err, wnext);
            err = hdf_copy(child, NULL, dhdf);
            JUMP_NOK(err, wnext);
        }
        
        err = cs_init(&cs, hdf_get_obj(child, PRE_CFG_DATASET));
        JUMP_NOK(err, wnext);
            
        hdf_set_value(cs->hdf, "hdf.loadpaths.local", dir);

        err = cgi_register_strfuncs(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_bitop_functions(cs);
        JUMP_NOK(err, wnext);
        err = mcs_register_mkd_functions(cs);
        JUMP_NOK(err, wnext);

        tpl = hdf_get_value(child, PRE_CFG_LAYOUT, "null.html");
        snprintf(fname, sizeof(fname), "%s/%s", PATH_TPL, tpl);
        err = cs_parse_file(cs, fname);
        JUMP_NOK(err, wnext);

        if (outhash != NULL) {
            /*
             * strdup the key, baby, because we'll free the hdf later
             */
            err = hash_insert(outhash, (void*)strdup(hdf_obj_name(child)), (void*)cs);
            JUMP_NOK(err, wnext);
            if (hdf_get_obj(child, PRE_CFG_DATASET)) {
                err = hdf_init(&dhdf);
                JUMP_NOK(err, wnext);
                err = hdf_copy(dhdf, NULL, hdf_get_obj(child, PRE_CFG_DATASET));
                JUMP_NOK(err, wnext);
                snprintf(tok, sizeof(tok), "%s_hdf", hdf_obj_name(child));
                err = hash_insert(outhash, (void*)strdup(tok), (void*)dhdf);
                JUMP_NOK(err, wnext);
            }
        }
            
        if (hdf_get_value(child, PRE_CFG_OUTPUT, NULL) != NULL) {
            ltpl_prepare_rend(hdf_get_obj(child, PRE_CFG_DATASET), tpl);
                
            /*
             * get_data
             */
            val = hdf_get_value(child, PRE_CFG_DATAER, NULL);
            if (val != NULL && lib) {
                data_handler = dlsym(lib, val);
                if( (tp = dlerror()) != NULL) {
                    mtc_err("%s", tp);
                    //continue;
                } else {
                    err = (*data_handler)(hdf_get_obj(child, PRE_CFG_DATASET), dbh);
                    TRACE_NOK(err);
                }
            }
                
            err = cs_render(cs, &str, mcs_strcb);
            JUMP_NOK(err, wnext);
                
            snprintf(fname, sizeof(fname), PATH_DOC"%s",
                     hdf_get_value(child, PRE_CFG_OUTPUT, "null.html"));
            err = mutil_makesure_dir(fname);
            JUMP_NOK(err, wnext);

            err = mcs_str2file(str, fname);
            JUMP_NOK(err, wnext);
#ifdef DEBUG_HDF
            snprintf(fname, sizeof(fname), "%s/hdf.%s",
                     TC_ROOT, hdf_obj_name(child));
            hdf_write_file(child, fname);
#endif
        }

    wnext:
        if (cs != NULL && outhash == NULL)
            cs_destroy(&cs);
        string_clear(&str);
        child = hdf_obj_next(child);
    }
        
    if (node != NULL) hdf_destroy(&node);

    return STATUS_OK;
}