1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-08 02:33:12 +02:00

api: add regex replace feature in function string_eval_expression

This commit is contained in:
Sébastien Helleu
2014-10-22 21:19:54 +02:00
parent 972bd26e5e
commit 633a32ccd3
8 changed files with 927 additions and 174 deletions
+142 -19
View File
@@ -1822,11 +1822,12 @@ str3 = weechat.string_input_for_buffer("//test") # "/test"
==== weechat_string_eval_expression
_WeeChat バージョン 0.4.0 以上で利用可、バージョン 0.4.2 で更新。_
// TRANSLATION MISSING
_WeeChat ≥ 0.4.0, updated in 0.4.2 and 1.1._
式を評価して文字列として返す。`${variable}`
という書式で書かれた特殊変数は展開される
('WeeChat ユーザガイド' のコマンド `/eval` を参照)。
// TRANSLATION MISSING
Evaluate an expression and return result as a string.
Special variables with format `${variable}` are expanded (see table below).
[NOTE]
バージョン 1.0 以降、入れ子変数を使えるようになりました、例:
@@ -1844,10 +1845,16 @@ char *weechat_string_eval_expression (const char *expr,
引数:
* 'expr': 評価する式
// TRANSLATION MISSING
* 'expr': 評価する式 (see table below)
* 'pointers': ポインタを含むハッシュテーブル (キーは文字列、値はポインタ);
(現在のウィンドウやバッファへのポインタを持つ) ハッシュテーブルが "window" と
"buffer" ポインタを持たない場合はこれらは自動的に追加される (NULL でも可)
"buffer" ポインタを持たない場合はこれらは自動的に追加される (NULL でも可):
// TRANSLATION MISSING
** 'regex': pointer to a regular expression ('regex_t' structure) compiled with
WeeChat function <<_weechat_string_regcomp,weechat_string_regcomp>> or
regcomp (see `man regcomp`); this option is similar to 'regex' in hashtable
'options' (below), but is used for better performance
* 'extra_vars': 展開される追加変数 (NULL でも可)
* 'options': いくつかのオプションを含むハッシュテーブル (キーと値は必ず文字列)
(NULL でも可):
@@ -1857,39 +1864,155 @@ char *weechat_string_eval_expression (const char *expr,
演算子と括弧が使われます、結果はブール値 ("0" または "1") です
** 'prefix': 置換する変数のプレフィックス (デフォルト: "${")
** 'suffix': 置換する変数のサフィックス (デフォルト: "}")
// TRANSLATION MISSING
** 'regex': a regex used to replace text in 'expr' (which is then not
evaluated)
// TRANSLATION MISSING
** 'regex_replace': the replacement text to use with 'regex', to replace
text in 'expr' (the 'regex_replace' is evaluated on each match of 'regex'
against 'expr', until no match is found)
戻り値:
* 評価された式 (使用後には必ず "free" を呼び出して領域を開放してください)、失敗した場合は
NULL (式が不正な場合やメモリが不足している場合)
// TRANSLATION MISSING
List of variables expanded in expression (by order of priority, from first
expanded to last):
// TRANSLATION MISSING
[width="100%",cols="2,8,3,3",options="header"]
|===
| Format | Description | Examples | Results
| `${name}` | Variable `name` from hashtable 'extra_vars' |
`${name}` | `value`
| `${esc:xxx}` +
`${\xxx}` | String with escaped chars |
`${esc:prefix\tmessage}` +
`${\ua9}` |
`prefix<TAB>message` +
`©`
| `${hide:x,value}` |
String with hidden chars (all chars in `value` replaced `x`) |
`${hide:*,password}` |
`********`
| `${re:N}` |
Regex captured group: 0 = whole string matching, 1 to 99 = group captured,
`+` = last group captured |
`${re:1}` |
`test`
| `${color:name}` |
WeeChat color code (the name of color has optional attributes) |
`${color:red}red text` +
`${color:*214}bold orange text` |
`red text` (in red) +
`bold orange text` (in bold orange)
| `${info:name}` +
`${indo:name,arguments}` |
Info from WeeChat or a plugin, see function
<<_weechat_info_get,weechat_info_get>> |
`${info:version}` +
`${info:irc_nick_color_name,foo}` |
`1.0` +
`lightblue`
| `${sec.data.name}` |
Value of the secured data `name` |
`${sec.data.freenode_pass}` |
`my_password`
| `${file.section.option}` |
Value of the option |
`${weechat.look.buffer_time_format}` |
`%H:%M:%S`
| `${name}` |
Value of local variable `name` in buffer |
`${nick}` |
`FlashCode`
| `${hdata.var1.var2...}` +
`${hdata[list].var1.var2...}` |
Hdata value (pointers `window` and `buffer` are set by default with current
window/buffer) |
`${buffer[gui_buffers].full_name}` +
`${window.buffer.number}` |
`core.weechat` +
`1`
|===
C 言語での使用例:
// TRANSLATION MISSING
[source,C]
----
struct t_hashtable *options = weechat_hashtable_new (8,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
if (options)
weechat_hashtable_set (options, "type", "condition");
char *str1 = weechat_string_eval_expression ("${buffer.full_name}", NULL, NULL, NULL); /* "core.weechat" */
char *str2 = weechat_string_eval_expression ("${window.win_width} > 100", NULL, NULL, options); /* "1" */
char *str3 = weechat_string_eval_expression ("abc =~ def", NULL, NULL, options); /* "0" */
/* conditions */
struct t_hashtable *options1 = weechat_hashtable_new (8,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
weechat_hashtable_set (options1, "type", "condition");
char *str1 = weechat_string_eval_expression ("${window.win_width} > 100", NULL, NULL, options1); /* "1" */
char *str2 = weechat_string_eval_expression ("abc =~ def", NULL, NULL, options1); /* "0" */
/* simple expression */
char *str3 = weechat_string_eval_expression ("${buffer.full_name}", NULL, NULL, NULL); /* "core.weechat" */
/* replace with regex */
struct t_hashtable *options2 = weechat_hashtable_new (8,
WEECHAT_HASHTABLE_STRING,
WEECHAT_HASHTABLE_STRING,
NULL,
NULL);
/* add brackets around URLs */
weechat_hashtable_set (options2, "regex", "\\w+://\\S+");
weechat_hashtable_set (options2, "regex_replace", "[ ${re:0} ]");
char *str4 = weechat_string_eval_expression ("test: http://weechat.org", NULL, NULL, NULL); /* "test: [ http://weechat.org ]" */
/* hide passwords */
weechat_hashtable_set (options2, "regex", "(password=)(\\S+)");
weechat_hashtable_set (options2, "regex_replace", "${re:1}${hide:*,${re:2}}");
char *str5 = weechat_string_eval_expression ("password=abc password=def", NULL, NULL, NULL); /* "password=*** password=***" */
----
スクリプト (Python) での使用例:
// TRANSLATION MISSING
[source,python]
----
# プロトタイプ
str = weechat.string_eval_expression(expr, pointers, extra_vars, options)
# 例s
str1 = weechat.string_eval_expression("${buffer.full_name}", {}, {}, {}) # "core.weechat"
str2 = weechat.string_eval_expression("${window.win_width} > 100", {}, {}, {"type": "condition"}) # "1"
str3 = weechat.string_eval_expression("abc =~ def", {}, {}, {"type": "condition"}) # "0"
# conditions
str1 = weechat.string_eval_expression("${window.win_width} > 100", {}, {}, {"type": "condition"}) # "1"
str2 = weechat.string_eval_expression("abc =~ def", {}, {}, {"type": "condition"}) # "0"
# simple expression
str3 = weechat.string_eval_expression("${buffer.full_name}", {}, {}, {}) # "core.weechat"
# replace with regex: add brackets around URLs
options = {
"regex": "\\w+://\\S+",
"regex_replace": "[ ${re:0} ]",
}
str4 = weechat.string_eval_expression("test: http://weechat.org", {}, {}, options) # "test: [ http://weechat.org ]"
# replace with regex: hide passwords
options = {
"regex": "(password=)(\\S+)",
"regex_replace": "${re:1}${hide:*,${re:2}}",
}
str5 = weechat.string_eval_expression("password=abc password=def", {}, {}, options) # "password=*** password=***"
----
[[utf-8]]