Commit d2136fb8 authored by Sebastian Ramacher's avatar Sebastian Ramacher

Add regex based template engine for CSS

Signed-off-by: default avatarSebastian Ramacher <sebastian@ramacher.at>
parent a21bb420
......@@ -3,6 +3,6 @@
#ifndef GIRARA_CSS_DEFINITIONS_H
#define GIRARA_CSS_DEFINITIONS_H
const char* CSS_TEMPLATE;
extern const char* CSS_TEMPLATE;
#endif
......@@ -240,6 +240,11 @@ struct girara_session_private_s
*/
girara_list_t* settings;
/**
* Template enginge for CSS.
*/
GiraraTemplate* csstemplate;
struct
{
GtkWidget* overlay; /**< So we can overlay bottom_box on top of view */
......
......@@ -4,16 +4,18 @@
#include <glib/gi18n-lib.h>
#include "session.h"
#include "settings.h"
#include "callbacks.h"
#include "commands.h"
#include "config.h"
#include "css-definitions.h"
#include "datastructures.h"
#include "input-history.h"
#include "internal.h"
#include "commands.h"
#include "callbacks.h"
#include "settings.h"
#include "shortcuts.h"
#include "config.h"
#include "template.h"
#include "utils.h"
#include "input-history.h"
#include "css-definitions.h"
#if defined(__GNUC__) || defined(__clang__)
#define DO_PRAGMA(x) _Pragma(#x)
......@@ -184,6 +186,7 @@ girara_session_create()
(girara_free_function_t) girara_setting_free);
/* CSS style provider */
session->private_data->csstemplate = girara_template_new(CSS_TEMPLATE);
session->private_data->gtk.cssprovider = gtk_css_provider_new();
/* init modes */
......@@ -472,10 +475,14 @@ girara_session_private_free(girara_session_private_t* session)
g_return_if_fail(session != NULL);
/* clean up CSS style provider */
if (session->gtk.cssprovider == NULL) {
if (session->gtk.cssprovider != NULL) {
g_object_unref(session->gtk.cssprovider);
}
session->gtk.cssprovider = NULL;
if (session->csstemplate != NULL) {
g_object_unref(session->csstemplate);
}
session->csstemplate = NULL;
/* clean up settings */
girara_list_free(session->settings);
......
This diff is collapsed.
/* See LICENSE file for license and copyright information */
#ifndef GIRARA_TEMPLATE_H
#define GIRARA_TEMPLATE_H
#include <glib-object.h>
#include "types.h"
struct girara_template_s {
GObject parent;
};
struct girara_template_class_s {
GObjectClass parent_class;
void (*base_changed)(GiraraTemplate*);
void (*variable_changed)(GiraraTemplate*, const char* name);
void (*changed)(GiraraTemplate*);
};
#define GIRARA_TYPE_TEMPLATE \
(girara_template_get_type())
#define GIRARA_TEMPLATE(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), GIRARA_TYPE_TEMPLATE, GiraraTemplate))
#define GIRARA_TEMPLATE_CLASS(obj) \
(G_TYPE_CHECK_CLASS_CAST((obj), GIRARA_TYPE_TEMPLATE, GiraraTemplateClass))
#define GIRARA_IS_TEMPLATE(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj), GIRARA_TYPE_TEMPLATE))
#define GIRARA_IS_TEMPLATE_CLASS(obj) \
(G_TYPE_CHECK_CLASS_TYPE((obj), GIRARA_TYPE_TEMPLATE))
#define GIRARA_TEMPLATE_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), GIRARA_TYPE_TEMPLATE, GiraraTemplateClass))
/**
* Returns the type of the template.
*
* @return the type
*/
GType girara_template_get_type(void);
/**
* Create new template object.
*
* @param template a string that is used as template
* @returns a templot object
*/
GiraraTemplate* girara_template_new(const char* base);
void girara_template_set_base(GiraraTemplate* object, const char* base);
const char* girara_template_get_base(GiraraTemplate* object);
girara_list_t* girara_template_referenced_variables(GiraraTemplate* object);
void girara_template_add_variable(GiraraTemplate* object, const char* name);
void girara_template_set_variable_value(GiraraTemplate* object, const char* name, const char* value);
char* girara_template_evaluate(GiraraTemplate* object);
#endif
xdg_test_helper
core
girara_test
/* See LICENSE file for license and copyright information */
#include <check.h>
#include "../template.h"
#include "../datastructures.h"
START_TEST(test_new) {
GiraraTemplate* obj = girara_template_new(NULL);
ck_assert_ptr_ne(obj, NULL);
g_object_unref(obj);
obj = girara_template_new("base");
ck_assert_ptr_ne(obj, NULL);
g_object_unref(obj);
} END_TEST
START_TEST(test_new_with_null) {
GiraraTemplate* obj = girara_template_new(NULL);
ck_assert_ptr_ne(obj, NULL);
const char* base = girara_template_get_base(obj);
ck_assert_str_eq((char*) base, "");
g_object_unref(obj);
} END_TEST
START_TEST(test_new_with_base) {
GiraraTemplate* obj = girara_template_new("base");
ck_assert_ptr_ne(obj, NULL);
const char* base = girara_template_get_base(obj);
ck_assert_str_eq((char*) base, "base");
g_object_unref(obj);
} END_TEST
START_TEST(test_base_variables_none) {
GiraraTemplate* obj = girara_template_new("base");
ck_assert_ptr_ne(obj, NULL);
girara_list_t* variables = girara_template_referenced_variables(obj);
ck_assert_uint_eq(girara_list_size(variables), 0);
g_object_unref(obj);
} END_TEST
START_TEST(test_base_variables_one) {
GiraraTemplate* obj = girara_template_new("@test@");
ck_assert_ptr_ne(obj, NULL);
girara_list_t* variables = girara_template_referenced_variables(obj);
ck_assert_uint_eq(girara_list_size(variables), 1);
char* variable = girara_list_nth(variables, 0);
ck_assert_str_eq(variable, "test");
g_object_unref(obj);
} END_TEST
START_TEST(test_base_variables_one_twice) {
GiraraTemplate* obj = girara_template_new("@test@ @test@");
ck_assert_ptr_ne(obj, NULL);
girara_list_t* variables = girara_template_referenced_variables(obj);
ck_assert_uint_eq(girara_list_size(variables), 1);
g_object_unref(obj);
} END_TEST
START_TEST(test_variable_add) {
GiraraTemplate* obj = girara_template_new(NULL);
ck_assert_ptr_ne(obj, NULL);
girara_template_add_variable(obj, "name");
g_object_unref(obj);
} END_TEST
START_TEST(test_variable_set) {
GiraraTemplate* obj = girara_template_new(NULL);
ck_assert_ptr_ne(obj, NULL);
girara_template_add_variable(obj, "name");
girara_template_set_variable_value(obj, "name", "value");
g_object_unref(obj);
} END_TEST
START_TEST(test_full_1) {
GiraraTemplate* obj = girara_template_new("name = @name@");
ck_assert_ptr_ne(obj, NULL);
girara_template_add_variable(obj, "name");
girara_template_set_variable_value(obj, "name", "value");
char* result = girara_template_evaluate(obj);
ck_assert_ptr_ne(result, NULL);
ck_assert_str_eq(result, "name = value");
g_free(result);
g_object_unref(obj);
} END_TEST
START_TEST(test_full_2) {
GiraraTemplate* obj = girara_template_new("name = @name@; test = @test@");
ck_assert_ptr_ne(obj, NULL);
girara_template_add_variable(obj, "name");
girara_template_set_variable_value(obj, "name", "value");
char* result = girara_template_evaluate(obj);
ck_assert_ptr_eq(result, NULL);
g_object_unref(obj);
} END_TEST
extern void setup(void);
Suite* suite_template()
{
TCase* tcase = NULL;
Suite* suite = suite_create("Template");
/* girara_template_new */
tcase = tcase_create("object creation");
tcase_add_checked_fixture(tcase, setup, NULL);
tcase_add_test(tcase, test_new);
tcase_add_test(tcase, test_new_with_null);
tcase_add_test(tcase, test_new_with_base);
suite_add_tcase(suite, tcase);
/* base processing */
tcase = tcase_create("base processing");
tcase_add_checked_fixture(tcase, setup, NULL);
tcase_add_test(tcase, test_base_variables_none);
tcase_add_test(tcase, test_base_variables_one);
tcase_add_test(tcase, test_base_variables_one_twice);
suite_add_tcase(suite, tcase);
/* basic variable operations */
tcase = tcase_create("variables");
tcase_add_checked_fixture(tcase, setup, NULL);
tcase_add_test(tcase, test_variable_add);
tcase_add_test(tcase, test_variable_set);
suite_add_tcase(suite, tcase);
/* full processing */
tcase = tcase_create("full");
tcase_add_checked_fixture(tcase, setup, NULL);
tcase_add_test(tcase, test_full_1);
tcase_add_test(tcase, test_full_2);
suite_add_tcase(suite, tcase);
return suite;
}
......@@ -9,6 +9,7 @@ Suite* suite_datastructures();
Suite* suite_settings();
Suite* suite_session();
Suite* suite_config();
Suite* suite_template();
void setup(void)
{
......@@ -56,5 +57,13 @@ int main()
number_failed += srunner_ntests_failed(suite_runner);
srunner_free(suite_runner);
/* test template */
suite = suite_template();
suite_runner = srunner_create(suite);
srunner_run_all(suite_runner, CK_NORMAL);
number_failed += srunner_ntests_failed(suite_runner);
srunner_free(suite_runner);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
......@@ -215,4 +215,7 @@ typedef struct girara_input_history_io_interface_s GiraraInputHistoryIOInterface
typedef struct girara_input_history_s GiraraInputHistory;
typedef struct girara_input_history_class_s GiraraInputHistoryClass;
typedef struct girara_template_s GiraraTemplate;
typedef struct girara_template_class_s GiraraTemplateClass;
#endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment