Skip to content

Commit af599f8

Browse files
committed
fix function get_all_anchors(wikitext, options), + 可能被用來配合網頁錨點 anchors 使用的模板。
1 parent 1dfa14f commit af599f8

File tree

5 files changed

+91
-14
lines changed

5 files changed

+91
-14
lines changed

application/net/wiki/parser/evaluate.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ function module_code(library_namespace) {
447447
return token;
448448
}
449449

450-
// 類似 wiki_API_expandtemplates()
450+
// 類似 wiki_API_expandtemplates() try_to_expand_templates
451451
// ** 僅能提供簡單的演算功能,但提供 cache,不必每次從伺服器重新取得嵌入的頁面。
452452
// [[Special:ExpandTemplates]]
453453
// 使用上注意: 應設定 options[KEY_on_page_title_option]
@@ -505,6 +505,11 @@ function module_code(library_namespace) {
505505
var promise = for_each_subelement.call(parsed, 'transclusion',
506506
//
507507
function(token) {
508+
if (options.expand_transclusion_filter
509+
&& !options.expand_transclusion_filter(token)) {
510+
return;
511+
}
512+
508513
// Error.stackTraceLimit = Infinity;
509514
// console.trace(token);
510515
token = repeatedly_expand_template_token(token, options);
@@ -525,7 +530,10 @@ function module_code(library_namespace) {
525530
function expand_template_name(token) {
526531
// console.trace(token[0]);
527532
var template_name = token[0].toString();
528-
var promise = expand_transclusion(token[0], options, level);
533+
var promise = expand_transclusion(typeof token[0] === 'string'
534+
// for "/header", token.page_title !== token[0].toString()
535+
&& token.name.startsWith('/') ? token.page_title : token[0],
536+
options, level);
529537
if (!library_namespace.is_thenable(promise)) {
530538
if (false) {
531539
Error.stackTraceLimit = Infinity;
@@ -608,12 +616,17 @@ function module_code(library_namespace) {
608616
}, options);
609617

610618
if (!session) {
611-
page_title = wiki_API.to_namespace(page_title, 'Template');
619+
if (wiki_API.is_namespace(page_title, 'main')) {
620+
page_title = wiki_API.to_namespace(page_title,
621+
'Template');
622+
}
612623
wiki_API.page(page_title, evaluate, page_options);
613624
return;
614625
}
615626

616-
page_title = session.to_namespace(page_title, 'Template');
627+
if (session.is_namespace(page_title, 'main')) {
628+
page_title = session.to_namespace(page_title, 'Template');
629+
}
617630
// console.trace(page_title);
618631

619632
// 盡量避免網路操作。

application/net/wiki/parser/section.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1769,12 +1769,32 @@ function module_code(library_namespace) {
17691769
var anchor_list = CeL.wiki.parse.anchor(wikitext);
17701770
}
17711771

1772+
var wikitext_had_expanded = typeof Symbol === 'function' ? Symbol('wikitext had expanded')
1773+
: '\0wikitext had expanded';
1774+
17721775
// CeL.wiki.parse.anchor()
17731776
function get_all_anchors(wikitext, options) {
17741777
if (!wikitext) {
17751778
return [];
17761779
}
17771780

1781+
options = library_namespace.setup_options(options);
1782+
if (options.try_to_expand_templates
1783+
&& (!options[wikitext_had_expanded] || options[wikitext_had_expanded] !== wikitext)) {
1784+
// wikitext = String(wikitext);
1785+
return Promise.resolve(
1786+
// 嵌入其他頁面時,需在 parsed.each_section() 分段前就先解開所嵌入的內容。
1787+
wiki_API.expand_transclusion(wikitext, options))
1788+
//
1789+
.then(function(wikitext) {
1790+
var _options = Object.assign(Object.create(null), options);
1791+
// Do not need expand templates any more.
1792+
delete _options.try_to_expand_templates;
1793+
_options[wikitext_had_expanded] = wikitext;
1794+
return get_all_anchors(wikitext, _options);
1795+
});
1796+
}
1797+
17781798
// const
17791799
var anchor_hash = Object.create(null), imprecise_anchor_count = 0;
17801800
function register_anchor(anchor, token, preserve_spaces) {
@@ -1827,7 +1847,6 @@ function module_code(library_namespace) {
18271847
// var latest_action_count = session.actions && session.actions.length;
18281848

18291849
parsed.each_section();
1830-
options = library_namespace.setup_options(options);
18311850
var promise
18321851
//
18331852
= parsed.each('section_title', function(section_title_token) {

application/net/wiki/parser/wikitext.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2975,19 +2975,23 @@ function module_code(library_namespace) {
29752975
// wiki_API.namespace.hash using lower case
29762976
if (namespace === wiki_API.namespace.hash.template) {
29772977
// e.g., {{Template:name|...}}
2978+
// assert: parameters.name.startsWith('/') === false
29782979
parameters.page_title = parameters.name;
29792980
parameters.name = wiki_API.remove_namespace(
29802981
parameters.name, options);
2982+
2983+
} else if (parameters.name.startsWith('/')
2984+
&& (options.on_page_title || options.target_array
2985+
&& options.target_array.page
2986+
&& options.target_array.page.title)) {
2987+
// e.g., "{{/header}}" @ [[w:zh:Wikipedia:新条目推荐/候选]]
2988+
// "{{/header}}" → "Wikipedia:新条目推荐/候选/header"
2989+
// "{{/topic list}}" → "base page title/topic list"
2990+
parameters.page_title = (options.on_page_title || options.target_array.page.title)
2991+
+ parameters.name;
2992+
29812993
} else if (namespace === wiki_API.namespace.hash.main) {
2982-
if (parameters.name.startsWith('/')
2983-
&& options.target_array
2984-
&& options.target_array.page
2985-
&& options.target_array.page.title) {
2986-
// e.g., "{{/topic list}}"
2987-
// → "base page title/topic list"
2988-
parameters.page_title = options.target_array.page.title
2989-
+ parameters.name;
2990-
} else if (/^[\s_]*:/.test(parameters[0])) {
2994+
if (/^[\s_]*:/.test(parameters[0])) {
29912995
parameters.page_title = parameters.name;
29922996
} else {
29932997
parameters.page_title
@@ -2996,6 +3000,7 @@ function module_code(library_namespace) {
29963000
= wiki_API.to_namespace(parameters.name,
29973001
'Template', options);
29983002
}
3003+
29993004
} else {
30003005
// {{Wikipedia:T}}嵌入[[Wikipedia:T]]
30013006
parameters.page_title = parameters.name;

application/net/wiki/template_functions/general_functions.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ function module_code(library_namespace) {
137137

138138
expand_template_Colored_link.incomplete = 'Only for get_all_anchors() @ zh.moegirl [[ARGONAVIS from BanG Dream! 翻唱曲列表]]';
139139

140+
// --------------------------------------------------------------------------------------------
141+
// 可能被用來配合網頁錨點 anchors 使用的模板。
142+
143+
function expand_template_Fake_heading(options) {
144+
var parameters = this.parameters;
145+
return '<div class="fake-heading ' + 'h'
146+
// #default = h2
147+
+ (parameters.level || parameters.sub || 2) + '">'
148+
+ (parameters[1] || parameters.Section || parameters.分段)
149+
+ '</div>';
150+
}
151+
140152
// --------------------------------------------------------------------------------------------
141153
// 一些會產生網頁錨點 anchors 的模板或模組。
142154
// Templates or modules that generate web anchors
@@ -605,6 +617,12 @@ function module_code(library_namespace) {
605617
}
606618
},
607619

620+
// 可能被用來配合網頁錨點 anchors 使用的模板。
621+
'Fake heading' : {
622+
properties : {
623+
expand : expand_template_Fake_heading
624+
}
625+
},
608626
// 一些會產生網頁錨點 anchors 的模板或模組。
609627
// Templates or modules that generate web anchors
610628
Anchor : {

application/net/wiki/template_functions/zhwiki.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ function module_code(library_namespace) {
135135

136136
// --------------------------------------------------------------------------------------------
137137

138+
function expand_template_Bookmark(options) {
139+
var parameters = this.parameters;
140+
return (parameters[1] ? '<span id="' + parameters[1] + '"></span>' : '')
141+
+ (parameters[2] || parameters[1]);
142+
}
143+
144+
// --------------------------------------------------------------------------------------------
145+
138146
// [[w:zh:Template:Al]]
139147
function expand_template_Al(options) {
140148
var token = this;
@@ -161,6 +169,15 @@ function module_code(library_namespace) {
161169

162170
// --------------------------------------------------------------------------------------------
163171

172+
function parse_template_Bookmark(options) {
173+
var token = this;
174+
return token.page_title_list.map(function(title) {
175+
return wiki_API.title_link_of(title);
176+
}).join('、');
177+
}
178+
179+
// --------------------------------------------------------------------------------------------
180+
164181
function parse_template_不存檔(token, index, parent, options) {
165182
token.message_expire_date = Infinity;
166183
}
@@ -434,6 +451,11 @@ function module_code(library_namespace) {
434451
}
435452
},
436453
Al : parse_template_Al,
454+
Bookmark : {
455+
properties : {
456+
expand : expand_template_Bookmark
457+
}
458+
},
437459

438460
// {{Do not archive}}
439461
// wiki/routine/20210429.Auto-archiver.js: avoid being archived

0 commit comments

Comments
 (0)