Skip to content

Commit e68bd5c

Browse files
committed
+function parse_odd_external_link(all, URL, delimiter, parameters)
1 parent 813d146 commit e68bd5c

File tree

2 files changed

+111
-18
lines changed

2 files changed

+111
-18
lines changed

_test suite/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3614,6 +3614,15 @@ function test_wiki() {
36143614
assert(['https://tools.wmflabs.org/guc/index.php?uselang={{uselang}}&user={{urlencode:{{{1|{{PAGENAME}}}}}}}', parsed[0].toString()], 'wiki.parse: external link #16-2');
36153615
assert([' ', parsed[1]], 'wiki.parse: external link #16-3');
36163616
assert(['全域貢獻', parsed[2]], 'wiki.parse: external link #16-4');
3617+
wikitext = '[<!-- --><!-- -->ht<!-- -->tp:/<!-- -->/github.com github]'; parsed = CeL.wiki.parse(wikitext);
3618+
assert([wikitext, parsed.toString()], 'wiki.parse: external link #17');
3619+
assert(['external_link', parsed.type], 'wiki.parse: external link #17-1');
3620+
assert(['<!-- --><!-- -->ht<!-- -->tp:/<!-- -->/github.com', parsed[0].toString()], 'wiki.parse: external link #17-2');
3621+
// @[[Template:User toolbox]]
3622+
wikitext = '[{{fullurl:Special:Log|user={{urlencode:{{{1|{{PAGENAME}}}}}}}}} 日志]'; parsed = CeL.wiki.parse(wikitext);
3623+
assert([wikitext, parsed.toString()], 'wiki.parse: external link #17');
3624+
assert(['external_link', parsed.type], 'wiki.parse: external link #17-1');
3625+
assert(['{{fullurl:Special:Log|user={{urlencode:{{{1|{{PAGENAME}}}}}}}}}', parsed[0].toString()], 'wiki.parse: external link #17-2');
36173626

36183627
wikitext = 't<!--='; parsed = CeL.wiki.parse(wikitext);
36193628
assert([wikitext, parsed.toString()]);

application/net/wiki/parser/wikitext.js

Lines changed: 102 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ function module_code(library_namespace) {
385385
PATTERN_URL_WITH_PROTOCOL_GLOBAL.source
386386
// 允許 [//example.com/]
387387
.replace('):)', '):|\\/\\/)').replace(/^\([^()]+\)/, /\[/.source)
388-
+ /(?:([^\S\r\n]+)([^\]]*))?\]/.source, 'ig'),
388+
+ /(?:([^\S\n]+)([^\]]*))?\]/.source, 'ig'),
389389

390390
// 若包含 br|hr| 會導致 "aa<br>\nbb</br>\ncc" 解析錯誤!
391391
/** {String}以"|"分開之 wiki tag name。 [[Help:Wiki markup]], HTML tags. 不包含 <a>! */
@@ -841,9 +841,9 @@ function module_code(library_namespace) {
841841
// https://www.mediawiki.org/wiki/Help:Magic_words
842842
// https://zh.wikipedia.org/w/api.php?action=query&meta=siteinfo&siprop=functionhooks&utf8&format=json
843843
('DISPLAYTITLE|DEFAULTSORT|DEFAULTSORTKEY|DEFAULTCATEGORYSORT|デフォルトソート'
844-
+ '|ns|nse|lc|lcfirst|uc|ucfirst' + '|padleft|padright|bidi'
845-
+ '|formatnum' + '|urlencode|anchorencode'
846-
+ '|localurl|fullurl|filepath'
844+
+ '|NOEXTERNALLANGLINKS' + '|ns|nse|lc|lcfirst|uc|ucfirst'
845+
+ '|padleft|padright|bidi' + '|formatnum'
846+
+ '|urlencode|anchorencode' + '|localurl|fullurl|filepath'
847847

848848
// https://www.mediawiki.org/wiki/Help:Magic_words#Transclusion_modifiers
849849
// https://en.wikipedia.org/wiki/Help:Transclusion#Transclusion_modifiers
@@ -876,7 +876,7 @@ function module_code(library_namespace) {
876876

877877
// 狀態開關: [[mw:Help:Magic words#Behavior switches]]
878878
var PATTERN_BEHAVIOR_SWITCH = /__([A-Z]+(?:_[A-Z]+)*)__/g;
879-
PATTERN_BEHAVIOR_SWITCH = /__(NOTOC|FORCETOC|TOC|NOEDITSECTION|NEWSECTIONLINK|NONEWSECTIONLINK|NOGALLERY|HIDDENCAT|NOCONTENTCONVERT|NOCC|NOTITLECONVERT|NOTC|INDEX|NOINDEX|STATICREDIRECT|NOGLOBAL)__/g;
879+
PATTERN_BEHAVIOR_SWITCH = /__(NOTOC|FORCETOC|TOC|ARCHIVEDTALK|NOTALK|NOEDITSECTION|NEWSECTIONLINK|NONEWSECTIONLINK|NOGALLERY|HIDDENCAT|EXPECTUNUSEDCATEGORY|EXPECTUNUSEDTEMPLATE|NOCONTENTCONVERT|NOCC|NOTITLECONVERT|NOTC|INDEX|NOINDEX|STATICREDIRECT|DISAMBIG|EXPECTED_UNCONNECTED_PAGE|NOGLOBAL)__/g;
880880

881881
// [[w:en:Wikipedia:Extended image syntax]]
882882
// [[mw:Help:Images]]
@@ -1827,6 +1827,93 @@ function module_code(library_namespace) {
18271827
return include_mark + (queue.length - 1) + end_mark;
18281828
}
18291829

1830+
function parse_odd_external_link(all, URL, delimiter, parameters) {
1831+
URL = parse_wikitext(URL, options, queue);
1832+
1833+
if (!Array.isArray(URL)) {
1834+
// 一般正規的外部連結應該在上一個 parse_external_link 已處理過。
1835+
return all;
1836+
}
1837+
1838+
if (URL.type !== 'plain') {
1839+
URL = [ URL ];
1840+
}
1841+
1842+
var prefix_to_detect = [];
1843+
var index = 0;
1844+
for (; index < URL.length; index++) {
1845+
var token = URL[index];
1846+
if (typeof token === 'string') {
1847+
prefix_to_detect.push(token);
1848+
continue;
1849+
}
1850+
1851+
if (token.type === 'comment') {
1852+
continue;
1853+
}
1854+
1855+
if (prefix_to_detect.length === 0) {
1856+
if (token.type === 'magic_word_function'
1857+
//
1858+
&& (token.name in {
1859+
// [{{fullurl:Special:...|...}} ...]
1860+
FULLURL : true,
1861+
CANONICALURL : true,
1862+
FILEPATH : true
1863+
})) {
1864+
break;
1865+
}
1866+
1867+
if (false && token.type === 'transclusion'
1868+
// [{{Bare URL template}} ...]
1869+
&& (token.name in {
1870+
// [[w:en:Template:NRWbahnarchivURL]]
1871+
NRWbahnarchivURL : true
1872+
})) {
1873+
break;
1874+
}
1875+
1876+
return all;
1877+
}
1878+
1879+
break;
1880+
}
1881+
1882+
if (prefix_to_detect.length > 0) {
1883+
prefix_to_detect = '[' + prefix_to_detect.join('')
1884+
+ URL.slice(index).join('') + (delimiter || '')
1885+
+ (parameters || '') + ']';
1886+
// console.trace(prefix_to_detect);
1887+
prefix_to_detect = parse_wikitext(prefix_to_detect, Object
1888+
.assign(Object.clone(options), {
1889+
no_odd_external_link : true
1890+
}), queue);
1891+
if (prefix_to_detect.type !== 'external_link')
1892+
return all;
1893+
} else {
1894+
prefix_to_detect = null;
1895+
}
1896+
1897+
if (!Array.isArray(URL) || URL.length > 1 || URL.type
1898+
&& URL.type !== 'plain') {
1899+
URL = [ URL ];
1900+
}
1901+
if (delimiter || parameters) {
1902+
URL.push(delimiter || '');
1903+
if (parameters) {
1904+
parameters = parse_wikitext(parameters, options, queue);
1905+
URL.push(parameters);
1906+
}
1907+
}
1908+
1909+
if (prefix_to_detect)
1910+
URL.url = prefix_to_detect[0];
1911+
1912+
_set_wiki_type(URL, 'external_link');
1913+
queue.push(URL);
1914+
return include_mark + (queue.length - 1) + end_mark;
1915+
}
1916+
18301917
// --------------------------------------------------------------------
18311918

18321919
function is_invalid_page_name(page_name) {
@@ -4805,23 +4892,20 @@ function module_code(library_namespace) {
48054892
// external link
48064893
// [http://... ...]
48074894

4808-
/**
4809-
* <code>
4810-
TODO:
4811-
@[[Template:User toolbox]]
4812-
[{{fullurl:Special:Log|user={{urlencode:{{{1|{{PAGENAME}}}}}}}}} 日志]
4813-
4814-
[{{URL template}} ...]
4895+
// 不可跨行。
4896+
wikitext = wikitext.replace_till_stable(
4897+
PATTERN_external_link_global, parse_external_link);
48154898

4816-
[<!-- --><!-- -->ht<!-- -->tp://... ...]
4817-
</code>
4818-
*
4899+
/**
48194900
* @see https://github.com/5j9/wikitextparser/blob/master/tests/wikitext/test_external_links.py
4901+
* @see [[Category:External link templates]]
48204902
*/
48214903

4822-
// 不可跨行。
4823-
wikitext = wikitext.replace_till_stable(
4824-
PATTERN_external_link_global, parse_external_link);
4904+
if (!options.no_odd_external_link) {
4905+
wikitext = wikitext.replace_till_stable(
4906+
/\[([^\[\]\s\n]+)(?:([^\S\n]+)([^\]]*?))?\]/gi,
4907+
parse_odd_external_link);
4908+
}
48254909

48264910
// ----------------------------------------------------
48274911
// plain url

0 commit comments

Comments
 (0)