Skip to content

Commit

Permalink
+ CeL.wiki.prefix_page_name()
Browse files Browse the repository at this point in the history
  • Loading branch information
kanasimi committed Dec 3, 2024
1 parent 0ce9f80 commit d84aa03
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 27 deletions.
22 changes: 13 additions & 9 deletions _structure/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -1293,22 +1293,26 @@ if (typeof CeL === 'function') {
) {
// TODO: unload 時 delete .script_node
// _.script_node = node;
var env = _.env, config, matched;
var env = _.env, config;
try {
config = node.innerText || (config = node.firstChild)
&& config.nodeValue;
config = node.innerText || node.firstChild
&& node.firstChild.nodeValue;
// IE8 沒有 .innerText || .nodeValue
if (!config
&& typeof (config = node.innerHTML) === 'string')
config = (matched = config
.match(/^[\s\n]*<!--(.+?)-->[\s\n]*$/)) ? matched[1]
: config.replace(/<!--([\s\S]*?)-->/g, '');
if (!config && typeof node.innerHTML === 'string') {
config = node.innerHTML;
var matched = config
.match(/^[\s\n]*<\!--(.+?)-->[\s\n]*$/);
config = matched ? matched[1]
//
: config.replace(/<\!--[\s\S]*?-->/g, '');
}
if (config) {
// http://www.whatwg.org/specs/web-apps/current-work/multipage/scripting-1.html#inline-documentation-for-external-scripts
// If there is a src attribute, the element must be
// either empty or contain only script documentation
// that also matches script content restrictions.
if (matched = config.match(/\/\*([\s\S]+?)\*\//))
var matched = config.match(/\/\*([\s\S]+?)\*\//);
if (matched)
config = matched[1];
if (config = (typeof JSON === 'object' && JSON.parse || eval_parse)
(config.replace(/[\s\r\n]*\/\//g, '')))
Expand Down
10 changes: 10 additions & 0 deletions _test suite/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3490,6 +3490,7 @@ function test_wiki() {
assert(['深圳', CeL.wiki.wikitext_to_plain_text('<span xml:lang="zh">深圳</span>')], 'wikitext_to_plain_text() #4');
assert(['大稻埕', CeL.wiki.wikitext_to_plain_text('大<span lang="zh" xml:lang="zh">稻埕</span>')], 'wikitext_to_plain_text() #5');
assert(['H1\tH2\tH3\n11\t12\t13\n21\t22\t23', CeL.wiki.wikitext_to_plain_text('{| class="wikitable"\n|-\n! H1 !! H2 !! H3\n|-\n| 11 || 12 || 13\n|-\n| 21 || 22 || 23\n|}')], 'wikitext_to_plain_text() #6');
assert(['| =', CeL.wiki.wikitext_to_plain_text('{{ ! }} {{ = }}')], 'wikitext_to_plain_text() #7');

wikitext = '\'t\' "t"'; parsed = CeL.wiki.parse(wikitext);
assert([wikitext, parsed], 'wiki.parse: quoted text');
Expand Down Expand Up @@ -3668,6 +3669,10 @@ function test_wiki() {
assert([wikitext, parsed.toString()], 'wiki.parse.link #17');
assert(["高1%", parsed.anchor], 'wiki.parse.link #17-1');
assert(["高1%", parsed.page_title], 'wiki.parse.link #17-2');
wikitext = '[[1<!-- -->+1{{=}}1#Nouri Mabrouk and the 1+1{{=}}1 <!---->Revolution]]'; parsed = CeL.wiki.parse(wikitext);
assert([wikitext, parsed.toString()], 'wiki.parse.link #18');
assert(["Nouri Mabrouk and the 1+1=1 Revolution", parsed.anchor], 'wiki.parse.link #18-1');
assert(["1+1=1", parsed.page_title], 'wiki.parse.link #18-2');

wikitext = '[[Image:a.svg|thumb|20px|b{{c|d[[e]]f}}]]'; parsed = CeL.wiki.parser(wikitext).parse();
assert([wikitext, parsed.toString()], 'wiki.parse.file #1');
Expand Down Expand Up @@ -4022,6 +4027,11 @@ function test_wiki() {
assert(['transclusion', parsed.type], 'wiki.parse.transclusion #47-1');
assert(['plain', parsed[0].type], 'wiki.parse.transclusion #47-2');

wikitext = '{{{{=}}D}}'; parsed = CeL.wiki.parse(wikitext);
assert([wikitext, parsed.toString()], 'wiki.parse.transclusion #48');
assert(['transclusion', parsed.type], 'wiki.parse.transclusion #48-1');
assert(['=D', parsed.name], 'wiki.parse.transclusion #48-2');
assert(['Template:=D', parsed.page_title], 'wiki.parse.transclusion #48-3');

wikitext = 'a[[link]]b'; parsed = CeL.wiki.parser(wikitext).parse();
assert([wikitext, parsed.toString()]);
Expand Down
36 changes: 27 additions & 9 deletions application/net/wiki/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -1145,9 +1145,8 @@ function module_code(library_namespace) {
// console.log(namespace);
if (typeof namespace === 'string') {
var list = [];
namespace = namespace
// e.g., 'main{{!}}template' → 'main|template'
.replace(/{{\s*!\s*}}/g, '|')
namespace = prefix_page_name(namespace)
// e.g., 'User_talk' → 'User talk'
.replace(/[\s_]+/g, ' ');
(is_page_title ? [ namespace.toLowerCase() ]
Expand Down Expand Up @@ -1724,16 +1723,13 @@ function module_code(library_namespace) {
return wikitext;
}
// TODO: "《茶花女》维基百科词条'''(法语)'''"
wikitext = wikitext
// 去除註解。 Remove comments.
wikitext
// e.g., "親会社<!-- リダイレクト先の「[[子会社]]」は、[[:en:Subsidiary]] とリンク -->"
// "ロイ・トーマス<!-- 曖昧さ回避ページ -->"
.replace(/<\!--[\s\S]*?-->/g, '')
= prefix_page_name(wikitext)
// 沒先處理的話,也會去除 <br />
.replace(/\s*<br(?:[^\s\/][^<>]*)?>/ig, '\n').replace(
/<\/?[a-z][^>]*>/g, '')
// "{{=}}" → "="
.replace(/{{=\s*}}/ig, '=')
// e.g., remove "{{En icon}}"
.replace(/{{[a-z\s]+}}/ig, '')
// e.g., "[[link]]" → "link"
Expand Down Expand Up @@ -1814,6 +1810,28 @@ function module_code(library_namespace) {
});
}

// CeL.wiki.prefix_page_name(page_name)
// 前置作業,用以簡化頁面名稱。
// or prefix_parameter_name() ?
function prefix_page_name(page_name) {
page_name = String(page_name);

// 去除註解。 Remove comments. "<!-- comment -->"
page_name = page_name.replace(/<\!--[\s\S]*?-->/g, '');

// [[mw:Help:Magic words#Escaped characters]]
page_name = page_name
// '{{!}}' → '|'
.replace(/{{\s*!\s*}}/g, '|')
// '{{=}}' → '='
.replace(/{{\s*=\s*}}/g, '=');

// CeL.wiki.parse.anchor.normalize_anchor() needs preserving spaces
// page_name = page_name.trim();

return page_name;
}

/** @inner */
var PATTERN_anchor_of_page_title;
try {
Expand Down Expand Up @@ -1894,8 +1912,7 @@ function module_code(library_namespace) {

// true === /^\s$/.test('\uFEFF')

// 去除註解。 Remove comments.
page_name = page_name.replace(/<!--[\s\S]*?-->/g, '');
page_name = prefix_page_name(page_name);

// [[A&quot;A]]→[[A"A]]
// fix "&#39;". 由於裡面包含"#",所以必須在 PATTERN_anchor_of_page_title 之前處理。
Expand Down Expand Up @@ -4301,6 +4318,7 @@ function module_code(library_namespace) {
title_link_of : get_page_title_link,
revision_content : revision_content,
content_of : get_page_content,
prefix_page_name : prefix_page_name,
// normalize_page_title
normalize_title : normalize_page_name,
normalize_title_pattern : normalize_name_pattern,
Expand Down
5 changes: 4 additions & 1 deletion application/net/wiki/parser/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1295,10 +1295,13 @@ function module_code(library_namespace) {
// @ CeL.application.net.wiki.parser.wikitext
switch (token.name) {

// @see prefix_page_name(page_name) @ CeL.application.net.wiki.namespace
case '!':
// '{{!}}' → '|'
return '|';

case '=':
// '{{=}}' → '='
return '=';

// ----------------------------------------------------------------
Expand Down Expand Up @@ -1999,7 +2002,7 @@ function module_code(library_namespace) {
function Yesno(value, default_value) {
if (value === undefined)
return;
value = String(value).replace(/<!--[\s\S]*?-->/g, '').trim();
value = wiki_API.prefix_page_name(value).trim();
if (/^(?:[0nf]|no|false|off)$/i.test(value))
return false;
if (/^(?:[1yt]|yes|true|on)$/i.test(value))
Expand Down
29 changes: 24 additions & 5 deletions application/net/wiki/parser/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ function module_code(library_namespace) {
return token;
}

// 去除註解。 Remove comments. "<!-- comment -->"
if (token.type === 'comment') {
return '';
}
Expand Down Expand Up @@ -386,10 +387,25 @@ function module_code(library_namespace) {
}

// console.trace(token);
if (token.type in {
magic_word_function : true,
parameter : true
}) {

if (token.type === 'magic_word_function') {
// e.g., {{!}} {{=}}
token = wiki_API.evaluate_parser_function_token
.call(token, options);

if (typeof token !== 'object')
return token;

token.unconvertible = true;
}

/**
* TODO: check all <code>
[[mw:Help:Advanced editing#Reformatting and/or disabling wikitext interpretation|<nowiki>character</nowiki>]]
</code>
*/

if (token.type === 'parameter') {
// TODO: return token.evaluate()
token.unconvertible = true;
}
Expand Down Expand Up @@ -1710,7 +1726,9 @@ function module_code(library_namespace) {
if (anchor) {
anchor =
// '&#39;' → "'"
library_namespace.HTML_to_Unicode(anchor.toString())
library_namespace.HTML_to_Unicode(anchor.toString());

anchor = wiki_API.prefix_page_name(anchor)
/**
* 包括 "\xa0" (&nbsp), "\u206F" 在目錄的網頁錨點中都會被轉為空白字元 "_"。
*
Expand All @@ -1719,6 +1737,7 @@ function module_code(library_namespace) {
* 因此採用"P Q"。
*/
.replace(/[_\s]/g, ' ');

if (!preserve_spaces) {
// " a " → "a"
anchor = anchor.trim();
Expand Down
10 changes: 10 additions & 0 deletions application/net/wiki/parser/wikitext.js
Original file line number Diff line number Diff line change
Expand Up @@ -3025,6 +3025,16 @@ function module_code(library_namespace) {
// parameters.splice = template_token__splice;
// parameters.push = template_token__push;

if (false && matched
// [[mw:Help:Magic words#Escaped characters]]
// TODO: '{{!|}}' 其實會呼叫 [[Template:!]]
&& parameters.length > 1 && parameters.name in {
'!' : true,
'=' : true
}) {
// matched = false;
}

_set_wiki_type(parameters, matched ? 'magic_word_function'
: 'transclusion');
queue.push(parameters);
Expand Down
Binary file modified ce.js
Binary file not shown.
16 changes: 16 additions & 0 deletions extension/zh_conversion/OpenCC/additional.to_TW.phrases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@
拼理智 拚理智
// 《速通修仙!》 <p>拼情报,你就是把玉玲珑叫来, (~拚<COO>v:是 wp:<p>+v:拼+n:情报+wp:,+r:你+c:就是+p:把+n:玉玲珑+v:叫+v:来+wp:,)
拼情報 拚情報
// 《速通修仙!》 除了颜值之外,就只能拼情绪价值了。 (~拚<COO>v:没 ~拚<role.type:A0>季长生 ~拚<role.type:ARGM-ADV>除了颜值之外 ~拚<role.type:ARGM-ADV>就 ~拚<role.type:ARGM-ADV>只能 ~拚<role.type:A1>情绪价值 ~拚<role.type:ARGM-DIS>于是 ~拚<role.type:A0>季长生 ~拚<role.type:ARGM-DIS>就 p:除了+n:颜值+nd:之外+wp:,+d:就+d:只能+v:拼+n:情绪+n:价值+u:了+wp:。)
拼情緒 拚情緒
// 《开局奖励一亿条命》 拼武技, (~拚<role.type:A1>武技 v:拼+n:武技+wp:,)
拼武技 拚武技
// 《女侠且慢》 拼武艺他打不过、 (~拚<ADV>v:打 ~拚<role.type:A1>武艺 v:拼+n:武艺+r:他+v:打+d:不+v:过+wp:、)
Expand Down Expand Up @@ -672,6 +674,8 @@
命來拼 命來拚
// 《最仙遊》 我云清门不会用血本去拼苍茫盟, (~拚<COO>v:分散 ~拚<role.type:A1>苍茫盟 r:我+nh:云清门+d:不+v:会+p:用+n:血本+v:去+v:拼+nh:苍茫盟+wp:,)
血本去拼 血本去拚
// 《速通修仙!》 五五开的时候,祂会去拼, (~拚<COO>v:有 ~拚<role.type:ARGM-TMP>五五开的时候 ~拚<role.type:A0>祂 ~拚<role.type:ARGM-ADV>这样一来 m:五五开+u:的+n:时候+wp:,+r:祂+v:会+v:去+v:拼+wp:,)
祂會去拼, 祂會去拚,
// 《一品修仙》 跟小魔佛拼什么命, (~拚<role.type:A0>跟小魔佛 ~拚<role.type:A1>什么命 p:跟+a:小+n:魔佛+v:拼+r:什么+n:命+wp:,)
拼什麼命 拚什麼命
// 《苟在仙武娶妻长生》 想要出人头地,那就得去拼,去闯。 (~拚<COO>v:想 ~拚<role.type:ARGM-DIS>那 ~拚<role.type:ARGM-DIS>就 ~拚<role.type:ARGM-DIS>那 ~拚<role.type:A0>韩永春 v:想+v:要+i:出人头地+wp:,+c:那+d:就+v:得+v:去+v:拼+wp:,+v:去+v:闯+wp:。)
Expand Down Expand Up @@ -789,6 +793,8 @@
// 解析錯誤 @ 《平平无奇大师兄》 年轻就要醒着拼, (~拚<COO>v:醒 ~拚<parent.relate:eSUCC>v:醒 v:醒+u:着+v:拼) 若是不拼,这辈子也就如此了。 (c:若是+d:不+v:拼+wp:,)
醒着拼 醒著拚
若是不拼 若是不拚
// 《速通修仙!》 不拼,把希望寄托在别人手中, (~拚<COO>v:敢 ~拚<role.type:ARGM-ADV>不 ~拚<role.type:A0>诸圣 ~拚<role.type:ARGM-ADV>还是 d:不+v:拼+wp:,+p:把+n:希望+v:寄托+p:在+r:别人+nl:手中+wp:,)
不拼,把希望 不拚,把希望
// 解析錯誤 @ 《大奉打更人》 妖族没有皈依佛门,反而与佛门拼的两败俱伤, (~佛門拚<SBV>m:两败俱伤 d:反而+p:与+n:佛门拼+u:的+m:两败俱伤)
佛門拼 佛門拚
// 《剑仙三千万》 (r:这+n:门+v:蓄力杀招拼+u:得+v:乃是+n:玉石+d:俱+v:焚)
Expand Down Expand Up @@ -856,10 +862,16 @@

// 《大奉打更人》 其实拼的是底蕴。 (v:拼+u:的+v:是+n:底蕴)
拼的是 拚的是
// 《速通修仙!》 拼的不是你的长板到底有多长,而是你的短板到底有多短。 (~拚<SBV>v:是 v:拼+u:的+d:不+v:是+r:你+u:的+n:长板+d:到底+v:有+d:多+a:长+wp:,+c:而是+r:你+u:的+n:短板+d:到底+v:有+d:多+a:短+wp:。)
拼的不是 拚的不是
// 《从红月开始》 拼的不是很好看啊……” (~拼<SBV>v:是 v:拼+u:的+d:不+v:是+d:很+a:好看+u:啊+wp:……”) 單純 zh_conversion 轉換過程: 拼的不是→拚的不是
拼的不是很好看 拼的不是很好看
// 《开局奖励一亿条命》 拼的只是仙魂 (~拚<SBV>v:是 v:拼+u:的+d:只+v:是+n:仙魂)
拼的只是 拚的只是
// 解析錯誤 @ 《我师兄实在太稳健了》 (v:拼+u:的+d:就+v:是+r:这个+n:尿性+wp:。) (v:拼+u:的+d:就+v:是+m:一个+n:运气+wp:!)
拼的就是 拚的就是
// 《速通修仙!》 拼的更多的是硬实力。 (~拚<SBV>v:是 v:拼+u:的+d:更+a:多+u:的+v:是+n:硬实力+wp:。)
拼的更多的是 拚的更多的是
// 《烂柯棋缘》 钓鱼拼得就是耐心, (n:钓鱼+v:拼+u:得+d:就+v:是+a:耐心+wp:,) (v:釣魚+v:拚+u:得+d:就+v:是+a:耐心+wp:,) (~拚<role.type:ARGM-EXT>就是耐心 v:钓鱼+v:拼+u:得+d:就是+a:耐心+wp:,)
拼得就是 拚得就是
// 《中国历史那些事儿系列套装》 他能拼的只有青春、斗志、耐力以及智慧。 (v:能+v:拼+u:的+d:只+v:有+n:青春)
Expand Down Expand Up @@ -1033,6 +1045,8 @@
此時還願意拼 此時還願意拚
// 《我在修仙界长生不死》 拼起狠劲儿来, (~拚<ADV>v:认为 ~拚<role.type:A1>狠劲儿 v:拼+v:起+n:狠劲儿+v:来+wp:,)
拼起狠 拚起狠
// 《别人练级我修仙,苟到大乘再出山》 拼起神通术法! (~拚<ATT>n:术法 v:拼+v:起+n:神通+n:术法+wp:!)
拼起神通 拚起神通
// 《回到过去变成猫》 真正拼起来自己还是很吃亏的, (~拚<role.type:ARGM-ADV>真正 d:真正+v:拼+v:起来+r:自己+d:还是+d:很+a:吃亏+u:的+wp:,)
真正拼起來 真正拚起來
// 《回到过去变成猫》 总之人家拼起来的时候咱装死就好。 (~拚<ATT>n:时候 ~拚<role.type:A0>人家 c:总之+r:人家+v:拼+v:起来+u:的+n:时候+r:咱+v:装+a:死+d:就+a:好+wp:。)
Expand Down Expand Up @@ -1070,6 +1084,8 @@
匈奴血拼 匈奴血拚
// 《速通修仙!》 甚至有可能引发正魔两道的血拼。 (~血拚<VOB>v:引发 d:甚至+v:有+n:可能+v:引发+j:正魔+m:两+q:道+u:的+n:血拼+wp:。)
兩道的血拼 兩道的血拚
// 《速通修仙!》 都残血了还拼,666。 (~拚<COO>a:高 d:都+v:残+n:血+u:了+d:还+v:拼+wp:,+e:666+wp:。)
殘血了還拼 殘血了還拚


// ------------------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit d84aa03

Please sign in to comment.