#include"abc_html.h"
typedef struct {
- char* name;
- char* value;
- int type;
- int flags;
+ char** names;
+ char* value;
+ int type;
+ int flags;
} html_attr_t;
typedef struct {
- char* name;
+ char** names;
int type;
int n_attrs;
#define abc_number_of(__array) (sizeof(__array) / sizeof(__array[0]))
+static char* src_keys[] = {"src", "源", NULL};
+static char* href_keys[] = {"href", "地址", NULL};
+
+static char* width_keys[] = {"width", "宽度", NULL};
+static char* height_keys[] = {"height", "高度", NULL};
+
+static char* font_keys[] = {"font", "字体", NULL};
+static char* font_size_keys[] = {"font-size", "字号", NULL};
+static char* font_color_keys[] = {"font-color", "字体颜色", NULL};
+
+static char* type_keys[] = {"type", "类型", NULL};
+static char* name_keys[] = {"name", "名字", NULL};
+static char* for_keys[] = {"for", "关于", NULL};
+static char* id_keys[] = {"id", "编号", NULL};
+
+static char* value_keys[] = {"value", "值", NULL};
+static char* action_keys[] = {"action", "动作", NULL};
+static char* method_keys[] = {"method", "方法", NULL};
+
+static char* html_keys[] = {"html", "网页", NULL};
+static char* head_keys[] = {"head", "头部", NULL};
+static char* body_keys[] = {"body", "主体", NULL};
+static char* form_keys[] = {"form", "表单", NULL};
+static char* div_keys[] = {"div", "分区", NULL};
+static char* img_keys[] = {"img", "图片", NULL};
+
+static char* title_keys[] = {"title", "标题", NULL};
+static char* input_keys[] = {"input", "输入", NULL};
+static char* label_keys[] = {"label", "标签", NULL};
+
+static char* br_keys[] = {"br", "换行", NULL};
+static char* h1_keys[] = {"h1", "标题1", NULL};
+static char* h2_keys[] = {"h2", "标题2", NULL};
+static char* h3_keys[] = {"h3", "标题3", NULL};
+static char* h4_keys[] = {"h4", "标题4", NULL};
+static char* h5_keys[] = {"h5", "标题5", NULL};
+static char* h6_keys[] = {"h6", "标题6", NULL};
+static char* p_keys[] = {"p", "段落", NULL};
+static char* a_keys[] = {"a", "超链接", NULL};
+
+
static html_attr_t h1_attrs[] =
{
- {"font", "SimHei", ABC_HTML_ATTR_FONT, 0},
- {"font-size", "40", ABC_HTML_ATTR_FONT_SIZE, 0},
+ {font_keys, "SimHei", ABC_HTML_ATTR_FONT, 0},
+ {font_size_keys, "40", ABC_HTML_ATTR_FONT_SIZE, 0},
};
static html_attr_t h2_attrs[] =
{
- {"font", "SimHei", ABC_HTML_ATTR_FONT, 0},
- {"font-size", "32", ABC_HTML_ATTR_FONT_SIZE, 0},
+ {font_keys, "SimHei", ABC_HTML_ATTR_FONT, 0},
+ {font_size_keys, "32", ABC_HTML_ATTR_FONT_SIZE, 0},
};
static html_attr_t h3_attrs[] =
{
- {"font", "SimHei", ABC_HTML_ATTR_FONT, 0},
- {"font-size", "28", ABC_HTML_ATTR_FONT_SIZE, 0},
+ {font_keys, "SimHei", ABC_HTML_ATTR_FONT, 0},
+ {font_size_keys, "28", ABC_HTML_ATTR_FONT_SIZE, 0},
};
static html_attr_t h4_attrs[] =
{
- {"font", "SimHei", ABC_HTML_ATTR_FONT, 0},
- {"font-size", "24", ABC_HTML_ATTR_FONT_SIZE, 0},
+ {font_keys, "SimHei", ABC_HTML_ATTR_FONT, 0},
+ {font_size_keys, "24", ABC_HTML_ATTR_FONT_SIZE, 0},
};
static html_attr_t h5_attrs[] =
{
- {"font", "SimHei", ABC_HTML_ATTR_FONT, 0},
- {"font-size", "20", ABC_HTML_ATTR_FONT_SIZE, 0},
+ {font_keys, "SimHei", ABC_HTML_ATTR_FONT, 0},
+ {font_size_keys, "20", ABC_HTML_ATTR_FONT_SIZE, 0},
};
static html_attr_t h6_attrs[] =
{
- {"font", "SimHei", ABC_HTML_ATTR_FONT, 0},
- {"font-size", "16", ABC_HTML_ATTR_FONT_SIZE, 0},
+ {font_keys, "SimHei", ABC_HTML_ATTR_FONT, 0},
+ {font_size_keys, "16", ABC_HTML_ATTR_FONT_SIZE, 0},
};
static html_attr_t p_attrs[] =
{
- {"font", "SimSong", ABC_HTML_ATTR_FONT, 0},
- {"font-size", "16", ABC_HTML_ATTR_FONT_SIZE, 0},
+ {font_keys, "SimSong", ABC_HTML_ATTR_FONT, 0},
+ {font_size_keys, "16", ABC_HTML_ATTR_FONT_SIZE, 0},
};
static html_attr_t a_attrs[] =
{
- {"href", "", ABC_HTML_ATTR_HREF, ABC_HTML_FLAG_SHOW},
- {"font", "SimHei", ABC_HTML_ATTR_FONT, 0},
- {"font-size", "24", ABC_HTML_ATTR_FONT_SIZE, 0},
- {"font-color", "blue", ABC_HTML_ATTR_FONT_COLOR, 0},
+ {href_keys, "", ABC_HTML_ATTR_HREF, ABC_HTML_FLAG_SHOW},
+ {font_keys, "SimHei", ABC_HTML_ATTR_FONT, 0},
+ {font_size_keys, "24", ABC_HTML_ATTR_FONT_SIZE, 0},
+ {font_color_keys, "blue", ABC_HTML_ATTR_FONT_COLOR, 0},
};
static html_attr_t img_attrs[] =
{
- {"src", "", ABC_HTML_ATTR_SRC, ABC_HTML_FLAG_SHOW},
- {"width", "100", ABC_HTML_ATTR_WIDTH, ABC_HTML_FLAG_SHOW},
- {"height", "100", ABC_HTML_ATTR_HEIGHT, ABC_HTML_FLAG_SHOW},
+ {src_keys, "", ABC_HTML_ATTR_SRC, ABC_HTML_FLAG_SHOW},
+ {width_keys, "100", ABC_HTML_ATTR_WIDTH, ABC_HTML_FLAG_SHOW},
+ {height_keys, "100", ABC_HTML_ATTR_HEIGHT, ABC_HTML_FLAG_SHOW},
};
static html_attr_t input_attrs[] =
{
- {"type", "text", ABC_HTML_ATTR_TYPE, ABC_HTML_FLAG_SHOW},
- {"id", "", ABC_HTML_ATTR_ID, ABC_HTML_FLAG_SHOW},
- {"name", "", ABC_HTML_ATTR_NAME, ABC_HTML_FLAG_SHOW},
- {"value", "", ABC_HTML_ATTR_VALUE, ABC_HTML_FLAG_SHOW},
+ {type_keys, "text", ABC_HTML_ATTR_TYPE, ABC_HTML_FLAG_SHOW},
+ {id_keys, "", ABC_HTML_ATTR_ID, ABC_HTML_FLAG_SHOW},
+ {name_keys, "", ABC_HTML_ATTR_NAME, ABC_HTML_FLAG_SHOW},
+ {value_keys, "", ABC_HTML_ATTR_VALUE, ABC_HTML_FLAG_SHOW},
- {"font", "SimSong", ABC_HTML_ATTR_FONT, 0},
- {"font-size", "16", ABC_HTML_ATTR_FONT_SIZE, 0},
+ {font_keys, "SimSong", ABC_HTML_ATTR_FONT, 0},
+ {font_size_keys, "16", ABC_HTML_ATTR_FONT_SIZE, 0},
};
static html_attr_t label_attrs[] =
{
- {"for", "", ABC_HTML_ATTR_FOR, ABC_HTML_FLAG_SHOW},
+ {for_keys, "", ABC_HTML_ATTR_FOR, ABC_HTML_FLAG_SHOW},
- {"font", "SimSong", ABC_HTML_ATTR_FONT, 0},
- {"font-size", "16", ABC_HTML_ATTR_FONT_SIZE, 0},
+ {font_keys, "SimSong", ABC_HTML_ATTR_FONT, 0},
+ {font_size_keys, "16", ABC_HTML_ATTR_FONT_SIZE, 0},
};
static html_attr_t form_attrs[] =
{
- {"action", "/", ABC_HTML_ATTR_ACTION, ABC_HTML_FLAG_SHOW},
- {"method", "post", ABC_HTML_ATTR_METHOD, ABC_HTML_FLAG_SHOW},
+ {action_keys, "/", ABC_HTML_ATTR_ACTION, ABC_HTML_FLAG_SHOW},
+ {method_keys, "post", ABC_HTML_ATTR_METHOD, ABC_HTML_FLAG_SHOW},
- {"font", "SimSong", ABC_HTML_ATTR_FONT, 0},
- {"font-size", "16", ABC_HTML_ATTR_FONT_SIZE, 0},
+ {font_keys, "SimSong", ABC_HTML_ATTR_FONT, 0},
+ {font_size_keys, "16", ABC_HTML_ATTR_FONT_SIZE, 0},
};
static html_label_t html_labels[] =
{
- {"html", ABC_HTML, 0, NULL, ABC_HTML_FLAG_CLOSE},
- {"title", ABC_HTML_TITLE, 0, NULL, ABC_HTML_FLAG_CLOSE},
- {"head", ABC_HTML_HEAD, 0, NULL, ABC_HTML_FLAG_CLOSE},
- {"body", ABC_HTML_BODY, 0, NULL, ABC_HTML_FLAG_CLOSE},
+ {html_keys, ABC_HTML, 0, NULL, ABC_HTML_FLAG_CLOSE},
+ {title_keys, ABC_HTML_TITLE, 0, NULL, ABC_HTML_FLAG_CLOSE},
+ {head_keys, ABC_HTML_HEAD, 0, NULL, ABC_HTML_FLAG_CLOSE},
+ {body_keys, ABC_HTML_BODY, 0, NULL, ABC_HTML_FLAG_CLOSE},
- {"div", ABC_HTML_DIV, 0, NULL, ABC_HTML_FLAG_CLOSE},
+ {div_keys, ABC_HTML_DIV, 0, NULL, ABC_HTML_FLAG_CLOSE},
- {"h1", ABC_HTML_H1, abc_number_of(h1_attrs), h1_attrs, ABC_HTML_FLAG_CLOSE},
- {"h2", ABC_HTML_H2, abc_number_of(h2_attrs), h2_attrs, ABC_HTML_FLAG_CLOSE},
- {"h3", ABC_HTML_H3, abc_number_of(h3_attrs), h3_attrs, ABC_HTML_FLAG_CLOSE},
- {"h4", ABC_HTML_H4, abc_number_of(h4_attrs), h4_attrs, ABC_HTML_FLAG_CLOSE},
- {"h5", ABC_HTML_H5, abc_number_of(h5_attrs), h5_attrs, ABC_HTML_FLAG_CLOSE},
- {"h6", ABC_HTML_H6, abc_number_of(h6_attrs), h6_attrs, ABC_HTML_FLAG_CLOSE},
+ {h1_keys, ABC_HTML_H1, abc_number_of(h1_attrs), h1_attrs, ABC_HTML_FLAG_CLOSE},
+ {h2_keys, ABC_HTML_H2, abc_number_of(h2_attrs), h2_attrs, ABC_HTML_FLAG_CLOSE},
+ {h3_keys, ABC_HTML_H3, abc_number_of(h3_attrs), h3_attrs, ABC_HTML_FLAG_CLOSE},
+ {h4_keys, ABC_HTML_H4, abc_number_of(h4_attrs), h4_attrs, ABC_HTML_FLAG_CLOSE},
+ {h5_keys, ABC_HTML_H5, abc_number_of(h5_attrs), h5_attrs, ABC_HTML_FLAG_CLOSE},
+ {h6_keys, ABC_HTML_H6, abc_number_of(h6_attrs), h6_attrs, ABC_HTML_FLAG_CLOSE},
- {"p", ABC_HTML_P, abc_number_of(p_attrs), p_attrs, ABC_HTML_FLAG_CLOSE},
- {"br", ABC_HTML_BR, 0, NULL, ABC_HTML_FLAG_OPEN},
+ {p_keys, ABC_HTML_P, abc_number_of(p_attrs), p_attrs, ABC_HTML_FLAG_CLOSE},
+ {br_keys, ABC_HTML_BR, 0, NULL, ABC_HTML_FLAG_OPEN},
- {"a", ABC_HTML_A, abc_number_of(a_attrs), a_attrs, ABC_HTML_FLAG_CLOSE},
- {"img", ABC_HTML_IMG, abc_number_of(img_attrs), img_attrs, ABC_HTML_FLAG_CLOSE | ABC_HTML_FLAG_SINGLE},
+ {a_keys, ABC_HTML_A, abc_number_of(a_attrs), a_attrs, ABC_HTML_FLAG_CLOSE},
+ {img_keys, ABC_HTML_IMG, abc_number_of(img_attrs), img_attrs, ABC_HTML_FLAG_CLOSE | ABC_HTML_FLAG_SINGLE},
- {"form", ABC_HTML_FORM, abc_number_of(form_attrs), form_attrs, ABC_HTML_FLAG_CLOSE},
- {"input", ABC_HTML_INPUT, abc_number_of(input_attrs), input_attrs, ABC_HTML_FLAG_OPEN},
- {"label", ABC_HTML_LABEL, abc_number_of(label_attrs), label_attrs, ABC_HTML_FLAG_CLOSE},
+ {form_keys, ABC_HTML_FORM, abc_number_of(form_attrs), form_attrs, ABC_HTML_FLAG_CLOSE},
+ {input_keys, ABC_HTML_INPUT, abc_number_of(input_attrs), input_attrs, ABC_HTML_FLAG_OPEN},
+ {label_keys, ABC_HTML_LABEL, abc_number_of(label_attrs), label_attrs, ABC_HTML_FLAG_CLOSE},
};
static int __html_parse_obj(abc_html_t* html, abc_char_t* c);
html_label_t* label;
int i;
+ int j;
+
for (i = 0; i < sizeof(html_labels) / sizeof(html_labels[0]); i++) {
label = &html_labels[i];
- if (!strcmp(label->name, name))
- return label;
+ for (j = 0; label->names[j]; j++) {
+
+ if (!strcmp(label->names[j], name))
+ return label;
+ }
}
return NULL;
}
-static int __html_add_attr(abc_obj_t* obj, int type, const char* name, const char* value, int flags)
+static int __html_add_attr(abc_obj_t* obj, int type, char** names, const char* value, int flags)
{
abc_obj_t* attr = abc_obj_alloc(NULL, 0, 0, type);
if (!attr)
return -ENOMEM;
- attr->key = scf_string_cstr(name);
- if (!attr->key) {
- abc_obj_free(attr);
- return -ENOMEM;
- }
+ attr->keys = names;
attr->value = scf_string_cstr(value);
if (!attr->value) {
if (attrs && n_attrs > 0) {
for (i = 0; i < n_attrs; i++) {
- ret = __html_add_attr(obj, attrs[i].type, attrs[i].name, attrs[i].value, attrs[i].flags);
+ ret = __html_add_attr(obj, attrs[i].type, attrs[i].names, attrs[i].value, attrs[i].flags);
if (ret < 0)
return ret;
}
for (l = scf_list_head(&obj->parent->attrs); l != scf_list_sentinel(&obj->parent->attrs); l = scf_list_next(l)) {
attr = scf_list_data(l, abc_obj_t, list);
- ret = __html_add_attr(obj, attr->type, attr->key->data, attr->value->data, 0);
+ ret = __html_add_attr(obj, attr->type, attr->keys, attr->value->data, 0);
if (ret < 0)
return ret;
}
free(c);
c = NULL;
- int ret = 0;
- if (scf_string_cmp(obj->key, end)) {
- ret = -1;
+ int j;
+ for (j = 0; obj->keys[j]; j++) {
- scf_loge("end label '%s' file: %s, line: %d, NOT for label '%s' line: %d\n",
- end->data, html->file->data, html->n_lines, obj->key->data, obj->line);
+ if (!strcmp(obj->keys[j], end->data)) {
+ scf_string_free(end);
+ return 0;
+ }
}
+ scf_loge("end label '%s' file: %s, line: %d, NOT for label '%s' line: %d\n",
+ end->data, html->file->data, html->n_lines, obj->keys[0], obj->line);
+
scf_string_free(end);
- return ret;
+ return -1;
}
static int __html_parse_text(abc_html_t* html, abc_obj_t* obj)
for (l = scf_list_head(&obj->attrs); l != scf_list_sentinel(&obj->attrs); l = scf_list_next(l)) {
attr = scf_list_data(l, abc_obj_t, list);
- if (!strcmp(attr->key->data, key->data))
- break;
+ int j;
+ for (j = 0; attr->keys[j]; j++) {
+
+ if (!strcmp(attr->keys[j], key->data))
+ goto found;
+ }
}
if (l == scf_list_sentinel(&obj->attrs)) {
return -1;
}
+found:
scf_string_free(key);
key = NULL;
return -1;
}
+ scf_string_free(key);
+ key = NULL;
+
obj = abc_obj_alloc(html->file, html->n_lines, html->pos, label->type);
- if (!obj) {
- scf_string_free(key);
+ if (!obj)
return -ENOMEM;
- }
obj->flags = label->flags;
- obj->key = key;
- key = NULL;
+ obj->keys = label->names;
obj->parent = html->current;
return ret;
}
- scf_logi("key: %s\n", obj->key->data);
+ scf_logi("key: %s\n", obj->keys[0]);
switch (tmp) {
case ' ':
if ('>' != tmp) {
scf_loge("HTML label '%s' (%d) not closed, in file: %s, line: %d\n",
- obj->key->data, tmp, html->file->data, html->n_lines);
+ obj->keys[0], tmp, html->file->data, html->n_lines);
return -1;
}
} else
continue;
}
- if ('a' <= c->c && 'z' >= c->c) {
+ if (('a' <= c->c && 'z' >= c->c)
+ || (0x4e00 <= c->c && 0x9fa5 >= c->c)) {
int ret = __html_parse_obj(html, c);
if (ret < 0) {