# HG changeset patch # User David Demelier # Date 1660579054 -7200 # Node ID 16f1c72d1635dc05ad9de8818d3ce2436a3f8ab0 # Parent c03305b39b10986989347fcd09a93e1272ff3bde scid: simplify theme code diff -r c03305b39b10 -r 16f1c72d1635 scid/page-index.c --- a/scid/page-index.c Thu Aug 11 21:24:50 2022 +0200 +++ b/scid/page-index.c Mon Aug 15 17:57:34 2022 +0200 @@ -114,7 +114,7 @@ /* First, fetch all projects. */ if ((projects = db_project_list())) { update_projects(projects); - data = theme_page_index(util_json_pack("{so}", + data = theme_render("onPageIndex", util_json_pack("{so}", "projects", projects )); pageutil_render(req, KHTTP_200, KMIME_TEXT_HTML, data); diff -r c03305b39b10 -r 16f1c72d1635 scid/page-jobresults.c --- a/scid/page-jobresults.c Thu Aug 11 21:24:50 2022 +0200 +++ b/scid/page-jobresults.c Mon Aug 15 17:57:34 2022 +0200 @@ -34,7 +34,7 @@ if (!(results = db_jobresult_list_by_job(id))) pageutil_status(r, KHTTP_404); else { - data = theme_page_jobresults(util_json_pack("{sI so}", + data = theme_render("onPageJobresults", util_json_pack("{sI so}", "job_id", (json_int_t)id, "jobresults", results )); diff -r c03305b39b10 -r 16f1c72d1635 scid/pageutil.c --- a/scid/pageutil.c Thu Aug 11 21:24:50 2022 +0200 +++ b/scid/pageutil.c Mon Aug 15 17:57:34 2022 +0200 @@ -68,7 +68,7 @@ * KHTTP_ are numbered like a standard enum, the Javascript code must * get the appropriate HTTP code instead. */ - body = theme_page_status(statustab[status], statusmsg[status]); + body = theme_status(statustab[status], statusmsg[status]); pageutil_render(req, status, KMIME_TEXT_HTML, body); free(body); } diff -r c03305b39b10 -r 16f1c72d1635 scid/theme.c --- a/scid/theme.c Thu Aug 11 21:24:50 2022 +0200 +++ b/scid/theme.c Mon Aug 15 17:57:34 2022 +0200 @@ -302,15 +302,15 @@ case JSON_FALSE: duk_push_false(ctx); break; - case JSON_NULL: - duk_push_null(ctx); - break; case JSON_OBJECT: push_object(ctx, val); break; case JSON_ARRAY: push_array(ctx, val); break; + default: + duk_push_null(ctx); + break; } } @@ -416,40 +416,24 @@ } char * -theme_page_index(json_t *json) +theme_render(const char *function, json_t *json) { - assert(json); - - return call(json, "onPageIndex"); -} + assert(function); -char * -theme_page_jobresults(json_t *json) -{ - assert(json); - - return call(json, "onPageJobresults"); + return call(json, function); } char * -theme_page_status(int status, const char *message) +theme_status(int status, const char *message) { - json_t *doc; - char *ret; - - doc = util_json_pack("{si ss}", + return theme_render("onPageStatus", util_json_pack("{si ss}", "status", status, "message", message - ); - ret = call(doc, "onPageStatus"); - - json_decref(doc); - - return ret; + )); } void -theme_free(void) +theme_finish(void) { duk_destroy_heap(context); } diff -r c03305b39b10 -r 16f1c72d1635 scid/theme.h --- a/scid/theme.h Thu Aug 11 21:24:50 2022 +0200 +++ b/scid/theme.h Mon Aug 15 17:57:34 2022 +0200 @@ -37,6 +37,66 @@ * * This module logs message with tag `theme`. * + * ### Functions + * + * The following functions can be defined in the Javascript code. + * + * #### onPageIndex + * + * Called to render the index page, usually the dashboard with most recent + * builds on every projects. + * + * ```javascript + * { + * "projects: [ + * { + * "name": "project name", + * "desc": "project short description", + * "url": "project URL or homepage", + * "jobs": [ + * { + * "job": job-id, + * "tag": "job tag / revision", + * "status": "failed / success" // failed if at least one has failed + * } + * ] + * "n-failed": number of failed jobs + * "n-succes": number of successful jobs + * } + * ] + * } + * ``` + * + * #### onPageJobresults + * + * Called to render most recent jobresults for a given job id. + * + * ```javascript + * { + * "jobresults": [ + * { + * "id": jobresult id, + * "job_id": parent job id, + * "worker_name": "worker which realized the task", + * "console": "stdout and stderr merge", + * "exitcode": process exit code, + * "sigcode": process termination code (0 means success), + * "date": job result insertion date + * } + * ] + * } + * ``` + * + * #### onPageStatus + * + * Called to render a unique status message (e.g. 404, 401). + * + * ```javascript + * { + * "status": number // Exemple: 400, 401 + * } + * ``` + * * [mustache]: https://mustache.github.io/ */ @@ -65,86 +125,29 @@ theme_path(const char *filename); /** - * Render the index page. - * - * The document requires the following properties: + * Call a function from Javascript with an optional JSON document. * - * ``` - * { - * "projects: [ - * { - * "name": "project name", - * "desc": "project short description", - * "url": "project URL or homepage", - * "jobs": [ - * { - * "job": job-id, - * "tag": "job tag / revision", - * "status": "failed / success" // failed if at least one has failed - * } - * ] - * "n-failed": number of failed jobs - * "n-succes": number of successful jobs - * } - * ] - * } - * ``` - * - * \pre doc != NULL - * \param doc the page document (borrowed) + * \pre function != NULL + * \param function the function name to call from Javascript + * \param doc the optional document to pass to the Javascript function * \return a newly allocated rendered string * \note You must free the return value */ char * -theme_page_index(json_t *doc); - -/** - * Render the jobresults page. - * - * The document requires the following properties: - * - * ``` - * { - * "jobresults": [ - * { - * "id": jobresult id, - * "job_id": parent job id, - * "worker_name": "worker which realized the task", - * "console": "stdout and stderr merge", - * "exitcode": process exit code, - * "sigcode": process termination code (0 means success), - * "date": job result insertion date - * } - * ] - * } - * ``` - * - * \pre doc != NULL - * \param doc the page document (borrowed) - * \return a newly allocated rendered string - * \note You must free the return value - */ -char * -theme_page_jobresults(json_t *doc); +theme_render(const char *function, json_t *doc); /** * Render the status page (for error code). * * The document requires the following properties: * - * ``` - * { - * "status": number // Exemple: 400, 401 - * } - * ``` - * * \param status the status code (e.g. 404) - * \param message the status message (e.g. Not found) + * \param msg the status message (e.g. Not found) * \return a newly allocated rendered string * \note You must free the return value */ char * -theme_page_status(int status, const char *msg); +theme_status(int status, const char *msg); /** * Cleanup theme resources.