Use template engine for base page

FossilOrigin-Name: 9a7dc653376414af58c66015fcaed25cfc878e2c20be160798af07b8cce35673
This commit is contained in:
nekobit 2022-05-27 04:35:21 +00:00
parent cdbbfed221
commit 3976497ae2
5 changed files with 102 additions and 57 deletions

View file

@ -30,7 +30,7 @@
#include "global_cache.h"
// Files
#include "../static/index.chtml"
#include "../static/index.ctmpl"
#include "../static/quick_login.chtml"
#define BODY_STYLE "style=\"background:url('%s');\""
@ -45,7 +45,8 @@ void render_base_page(struct base_page* page, struct session* ssn, mastodont_t*
// Sidebar
char* sidebar_str,
* main_sidebar_str = NULL,
* account_sidebar_str = NULL;
* account_sidebar_str = NULL,
* instance_str = NULL;
// Mastodont, used for notifications sidebar
struct mstdnt_storage storage = { 0 };
struct mstdnt_notification* notifs = NULL;
@ -106,50 +107,47 @@ void render_base_page(struct base_page* page, struct session* ssn, mastodont_t*
account_sidebar_str ? account_sidebar_str : "",
main_sidebar_str ? main_sidebar_str : "");
char* data;
int len = easprintf(&data, data_index_html,
L10N[locale][L10N_APP_NAME],
ssn->config.theme,
ssn->config.themeclr ? "-dark" : "",
background_url_css ? background_url_css : "",
config_url_prefix,
L10N[locale][L10N_APP_NAME],
login_string,
config_url_prefix,
L10N[locale][L10N_SEARCH_PLACEHOLDER],
L10N[locale][L10N_SEARCH_BUTTON],
CAT_TEXT(page->category, BASE_CAT_HOME),
config_url_prefix,
L10N[locale][L10N_HOME],
CAT_TEXT(page->category, BASE_CAT_LOCAL),
config_url_prefix,
L10N[locale][L10N_LOCAL],
CAT_TEXT(page->category, BASE_CAT_FEDERATED),
config_url_prefix,
L10N[locale][L10N_FEDERATED],
CAT_TEXT(page->category, BASE_CAT_NOTIFICATIONS),
config_url_prefix,
L10N[locale][L10N_NOTIFICATIONS],
CAT_TEXT(page->category, BASE_CAT_LISTS),
config_url_prefix,
L10N[locale][L10N_LISTS],
CAT_TEXT(page->category, BASE_CAT_FAVOURITES),
config_url_prefix,
L10N[locale][L10N_FAVOURITES],
CAT_TEXT(page->category, BASE_CAT_BOOKMARKS),
config_url_prefix,
L10N[locale][L10N_BOOKMARKS],
CAT_TEXT(page->category, BASE_CAT_DIRECT),
config_url_prefix,
L10N[locale][L10N_DIRECT],
CAT_TEXT(page->category, BASE_CAT_CONFIG),
config_url_prefix,
L10N[locale][L10N_CONFIG],
page->sidebar_left ? page->sidebar_left : "",
(ssn->config.instance_panel && g_cache.panel_html.response ?
g_cache.panel_html.response : ""),
page->content,
sidebar_str ? sidebar_str : "");
// Create instance panel
easprintf(&instance_str, "<div class=\"static-html\" id=\"instance-panel\">%s</div>",
(g_cache.panel_html.response ?
g_cache.panel_html.response : ""));
struct index_template index = {
.title = L10N[locale][L10N_APP_NAME],
.theme = ssn->config.theme,
.theme_clr = ssn->config.themeclr ? "-dark" : "",
.prefix = config_url_prefix,
.background_url = background_url_css,
.name = L10N[locale][L10N_APP_NAME],
.sidebar_cnt = login_string,
.placeholder = L10N[locale][L10N_SEARCH_PLACEHOLDER],
.search_btn = L10N[locale][L10N_SEARCH_BUTTON],
.active_home = CAT_TEXT(page->category, BASE_CAT_HOME),
.home = L10N[locale][L10N_HOME],
.active_local = CAT_TEXT(page->category, BASE_CAT_LOCAL),
.local = L10N[locale][L10N_LOCAL],
.active_federated = CAT_TEXT(page->category, BASE_CAT_FEDERATED),
.federated = L10N[locale][L10N_FEDERATED],
.active_notifications = CAT_TEXT(page->category, BASE_CAT_NOTIFICATIONS),
.notifications = L10N[locale][L10N_NOTIFICATIONS],
.active_lists = CAT_TEXT(page->category, BASE_CAT_LISTS),
.lists = L10N[locale][L10N_LISTS],
.active_favourites = CAT_TEXT(page->category, BASE_CAT_FAVOURITES),
.favourites = L10N[locale][L10N_FAVOURITES],
.active_bookmarks = CAT_TEXT(page->category, BASE_CAT_BOOKMARKS),
.bookmarks = L10N[locale][L10N_BOOKMARKS],
.active_direct = CAT_TEXT(page->category, BASE_CAT_DIRECT),
.direct = L10N[locale][L10N_DIRECT],
.active_config = CAT_TEXT(page->category, BASE_CAT_CONFIG),
.config = L10N[locale][L10N_CONFIG],
.sidebar_leftbar = page->sidebar_left,
.instance_panel = ssn->config.instance_panel ? instance_str : "",
.main = page->content,
.sidebar_rightbar = sidebar_str
};
unsigned len;
char* data = tmpl_gen_index(&index, &len);
if (!data)
{
@ -167,6 +165,7 @@ cleanup:
if (main_sidebar_str != sidebar_embed) free(main_sidebar_str);
free(account_sidebar_str);
free(background_url_css);
free(instance_str);
}
void render_html(char* data, size_t data_len)

View file

@ -245,7 +245,7 @@ char* construct_interaction_buttons(struct session* ssn,
config_url_prefix,
status->id,
status->id,
reltime_to_str(status->created_at));
time_str);
if (size) *size = s;
// Cleanup
@ -561,7 +561,7 @@ char* construct_status(struct session* ssn,
}
// Delete status menu item, logged in only
if (strcmp(status->account.acct, ssn->acct.acct) == 0)
if (ssn->logged_in && strcmp(status->account.acct, ssn->acct.acct) == 0)
easprintf(&delete_status, data_menu_item_html,
config_url_prefix, status->id, "delete", "Delete status");

View file

@ -56,7 +56,7 @@ long filesize(FILE* file)
void chexput(const char* buf, size_t size)
{
for (size_t i = 0; i < size; ++i)
for (size_t i = 0; i < size && buf; ++i)
{
printf("0X%hhX,", buf[i]);
}
@ -81,6 +81,7 @@ char* tkn_typetostr(enum tmpl_type tkn)
case TMPL_INT:
return "int";
case TMPL_STR:
return "const char*";
case TMPL_STRLEN:
return "char*";
case TMPL_UINT:
@ -190,8 +191,8 @@ void print_template(char* var, char* buf)
// Print remainder if any
chexput(buf_prev, strlen(buf_prev));
printf("};\n"
"struct %s {", var);
printf("0};\n"
"struct %s_template {", var);
int should_print = 0;
// Print tokens
@ -217,12 +218,16 @@ void print_template(char* var, char* buf)
// Generate function
printf("};\n"
"char* tmpl_gen_%s(struct %s* data, unsigned* size){\n"
"char* tmpl_gen_%s(struct %s_template* data, unsigned* size){\n"
"char* ret;\n"
"unsigned s = easprintf(&ret, data_%s, ", var, var, var);
for (size_t i = 0; i < tokens_len; ++i)
{
printf("data->%s%s", tokens[i].token, i < tokens_len-1 ? ", " : "");
printf("data->%s", tokens[i].token);
// No (null) strings, make them empty
if (tokens[i].type == TMPL_STR || tokens[i].type == TMPL_STRLEN)
printf("?data->%s:\"\"", tokens[i].token);
fputs(i < tokens_len-1 ? ", " : "", stdout);
}
fputs(");\n"
"if (size) *size = s;\n"
@ -255,6 +260,7 @@ int main(int argc, char** argv)
}
fclose(file);
buf[size-1] = '\0';
print_template(argv[ARG_VARIABLE], buf);

42
static/index.ctmpl Normal file

File diff suppressed because one or more lines are too long

View file

@ -6,7 +6,7 @@
<link rel="icon" type="image/png" href="/favicon.png">
<link rel="stylesheet" type="text/css" href="/{{ %s : theme }}{{ %s : theme_clr }}.css">
</head>
<body {{ %s : not_sure_what_this_is }}>
<body {{ %s : background_url }}>
<div></div>
<div id="main-page-container">
<div id="main-page">
@ -40,9 +40,7 @@
<li><a class="sidebarbtn {{ %s : active_config }}" href="{{ %s : prefix}}/config">{{ %s : config }}</a></li>
</ul>
{{ %s : sidebar_leftbar }}
<div class="static-html" id="instance-panel">
{{ %s : instance_panel }}
</div>
{{ %s : instance_panel }}
</td>
<!-- Display for posts -->