| 24 |
#define LML_TAG_PARAM_BUF_LEN 256 |
#define LML_TAG_PARAM_BUF_LEN 256 |
| 25 |
#define LML_TAG_OUTPUT_BUF_LEN 1024 |
#define LML_TAG_OUTPUT_BUF_LEN 1024 |
| 26 |
|
|
| 27 |
typedef int (*lml_tag_filter_cb)(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len); |
typedef int (*lml_tag_filter_cb)(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode); |
| 28 |
|
|
| 29 |
static int lml_tag_color_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len) |
static int lml_tag_color_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len) |
| 30 |
{ |
{ |
| 73 |
|
|
| 74 |
static int lml_tag_quote_level = 0; |
static int lml_tag_quote_level = 0; |
| 75 |
|
|
| 76 |
static int lml_tag_quote_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len) |
static int lml_tag_quote_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode) |
| 77 |
{ |
{ |
| 78 |
if (strcasecmp(tag_name, "quote") == 0) |
if (strcasecmp(tag_name, "quote") == 0) |
| 79 |
{ |
{ |
| 97 |
return 0; |
return 0; |
| 98 |
} |
} |
| 99 |
|
|
| 100 |
|
static int lml_tag_disabled = 0; |
| 101 |
|
|
| 102 |
|
static int lml_tag_disable_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode) |
| 103 |
|
{ |
| 104 |
|
lml_tag_disabled = 1; |
| 105 |
|
|
| 106 |
|
return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "" : "[plain]")); |
| 107 |
|
} |
| 108 |
|
|
| 109 |
const static char *lml_tag_def[][4] = { |
const static char *lml_tag_def[][4] = { |
| 110 |
// Definition of tuple: {lml_tag, lml_output, default_param | lml_filter_cb, no_lml_output} |
// Definition of tuple: {lml_tag, lml_output, default_param | lml_filter_cb, quote_mode_output} |
| 111 |
|
{"plain", NULL, (const char *)lml_tag_disable_filter, NULL}, |
| 112 |
{"left", "[", "", "[left]"}, |
{"left", "[", "", "[left]"}, |
| 113 |
{"right", "]", "", "[right]"}, |
{"right", "]", "", "[right]"}, |
| 114 |
{"bold", "\033[1m", "", ""}, // does not work in Fterm |
{"bold", "\033[1m", "", ""}, // does not work in Fterm |
| 129 |
{"/quote", NULL, (const char *)lml_tag_quote_filter, ""}, |
{"/quote", NULL, (const char *)lml_tag_quote_filter, ""}, |
| 130 |
{"url", "", "", ""}, |
{"url", "", "", ""}, |
| 131 |
{"/url", "(链接: %s)", NULL, ""}, |
{"/url", "(链接: %s)", NULL, ""}, |
| 132 |
{"link", "", ""}, |
{"link", "", "", ""}, |
| 133 |
{"/link", "(链接: %s)", NULL, ""}, |
{"/link", "(链接: %s)", NULL, ""}, |
| 134 |
{"email", "", ""}, |
{"email", "", "", ""}, |
| 135 |
{"/email", "(Email: %s)", NULL, ""}, |
{"/email", "(Email: %s)", NULL, ""}, |
| 136 |
{"user", "", ""}, |
{"user", "", "", ""}, |
| 137 |
{"/user", "(用户: %s)", NULL, ""}, |
{"/user", "(用户: %s)", NULL, ""}, |
| 138 |
{"article", "", ""}, |
{"article", "", "", ""}, |
| 139 |
{"/article", "(文章: %s)", NULL, ""}, |
{"/article", "(文章: %s)", NULL, ""}, |
| 140 |
{"image", "(图片: %s)", "", "%s"}, |
{"image", "(图片: %s)", "", "%s"}, |
| 141 |
{"flash", "(Flash: %s)", "", ""}, |
{"flash", "(Flash: %s)", "", ""}, |
| 162 |
} |
} |
| 163 |
} |
} |
| 164 |
|
|
| 165 |
int lml_render(const char *str_in, char *str_out, int buf_len, int lml_tag) |
int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode) |
| 166 |
{ |
{ |
| 167 |
char c; |
char c; |
| 168 |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
| 179 |
|
|
| 180 |
lml_init(); |
lml_init(); |
| 181 |
|
|
| 182 |
|
lml_tag_disabled = 0; |
| 183 |
lml_tag_quote_level = 0; |
lml_tag_quote_level = 0; |
| 184 |
|
|
| 185 |
for (i = 0; str_in[i] != '\0'; i++) |
for (i = 0; str_in[i] != '\0'; i++) |
| 186 |
{ |
{ |
| 187 |
if (lml_tag && new_line) |
if (quote_mode && !lml_tag_disabled && new_line) |
| 188 |
{ |
{ |
| 189 |
if (fb_quote_level > 0) |
if (fb_quote_level > 0) |
| 190 |
{ |
{ |
| 262 |
continue; // ignore '\r' |
continue; // ignore '\r' |
| 263 |
} |
} |
| 264 |
|
|
| 265 |
if (str_in[i] == '[') |
if (!lml_tag_disabled && str_in[i] == '[') |
| 266 |
{ |
{ |
| 267 |
tag_start_pos = i + 1; |
tag_start_pos = i + 1; |
| 268 |
} |
} |
| 269 |
else if (str_in[i] == ']') |
else if (!lml_tag_disabled && str_in[i] == ']') |
| 270 |
{ |
{ |
| 271 |
if (tag_start_pos >= 0) |
if (tag_start_pos >= 0) |
| 272 |
{ |
{ |
| 299 |
strncpy(tag_param_buf, lml_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1); |
strncpy(tag_param_buf, lml_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1); |
| 300 |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
| 301 |
} |
} |
| 302 |
if (lml_tag) |
if (quote_mode) |
| 303 |
{ |
{ |
| 304 |
if (lml_tag_def[k][1] != NULL) |
if (lml_tag_def[k][1] != NULL) |
| 305 |
{ |
{ |
| 308 |
else |
else |
| 309 |
{ |
{ |
| 310 |
tag_output_len = ((lml_tag_filter_cb)lml_tag_def[k][2])( |
tag_output_len = ((lml_tag_filter_cb)lml_tag_def[k][2])( |
| 311 |
lml_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN); |
lml_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, quote_mode); |
| 312 |
} |
} |
| 313 |
} |
} |
| 314 |
else |
else |
| 319 |
} |
} |
| 320 |
else |
else |
| 321 |
{ |
{ |
| 322 |
tag_output_len = 0; |
tag_output_len = ((lml_tag_filter_cb)lml_tag_def[k][2])( |
| 323 |
|
lml_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, quote_mode); |
| 324 |
} |
} |
| 325 |
} |
} |
| 326 |
if (j + tag_output_len >= buf_len) |
if (j + tag_output_len >= buf_len) |
| 342 |
tag_start_pos = -1; |
tag_start_pos = -1; |
| 343 |
} |
} |
| 344 |
} |
} |
| 345 |
else if (tag_start_pos == -1) // not in LML tag |
else if (lml_tag_disabled || tag_start_pos == -1) // not in LML tag |
| 346 |
{ |
{ |
| 347 |
if (str_in[i] & 0b10000000) // head of multi-byte character |
if (str_in[i] & 0b10000000) // head of multi-byte character |
| 348 |
{ |
{ |
| 380 |
} |
} |
| 381 |
} |
} |
| 382 |
|
|
| 383 |
if (lml_tag && lml_tag_quote_level > 0) |
if (quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0) |
| 384 |
{ |
{ |
| 385 |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m"); |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m"); |
| 386 |
if (j + tag_output_len >= buf_len) |
if (j + tag_output_len >= buf_len) |