From df050e44817819ec890145d7e9f647b58bd6310c Mon Sep 17 00:00:00 2001 From: Gabriel Wicke Date: Fri, 13 Apr 2012 11:00:51 +0200 Subject: [PATCH] Convert external link syntax stops to stack Eat unbalanced external link parts within template parameters. This does not produce the same output as the PHP parser (try echo '{{YouTube}}' | node parse.js), but preserves a level of sanity. Need to check how common this is for external links. If it is rare enough, moving the ']' after the parser function manually would fix the rendering for the YouTube case. Change-Id: I597d808efff36baa22191e7946a0061cc31120e8 --- modules/parser/mediawiki.tokenizer.peg.js | 4 ++-- modules/parser/pegTokenizer.pegjs.txt | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/modules/parser/mediawiki.tokenizer.peg.js b/modules/parser/mediawiki.tokenizer.peg.js index 42fb27742c..40fc194681 100644 --- a/modules/parser/mediawiki.tokenizer.peg.js +++ b/modules/parser/mediawiki.tokenizer.peg.js @@ -139,7 +139,7 @@ PegTokenizer.prototype.inline_breaks = function (input, pos, stops ) { return counters.template && input[pos + 1] === "}" || null; case ":": return counters.colon && - ! counters.extlink && + ! stops.onStack( 'extlink' ) && ! counters.linkdesc || null; case "\r": return stops.onStack( 'table' ) && @@ -151,7 +151,7 @@ PegTokenizer.prototype.inline_breaks = function (input, pos, stops ) { input[pos + 1] === '|' || null; case "]": - return counters.extlink || + return stops.onStack( 'extlink' ) || ( counters.linkdesc && input[pos + 1] === ']' ) || null; case "<": diff --git a/modules/parser/pegTokenizer.pegjs.txt b/modules/parser/pegTokenizer.pegjs.txt index ad3d727a31..6e5404229d 100644 --- a/modules/parser/pegTokenizer.pegjs.txt +++ b/modules/parser/pegTokenizer.pegjs.txt @@ -667,7 +667,7 @@ behavior_switch **************************************************************/ autolink - = ! { return stops.onCount('extlink') } + = ! { return stops.onStack('extlink') } (urllink / autoref / isbn) urllink @@ -676,16 +676,16 @@ urllink } extlink - = ! { return stops.onCount('extlink') } // extlink cannot be nested + = ! { return stops.onStack('extlink') } // extlink cannot be nested ( "[" - & { return stops.inc('extlink'); } + & { return stops.push('extlink', true); } //target:urllink target:extlink_preprocessor_text text:(( space / [\u00A0\u1680\u180E\u2000-\u200A\u202F\u205F\u3000] )* t:inlineline { return t } )? "]" { - stops.dec('extlink'); + stops.pop('extlink'); if ( text === '' ) { // XXX: Link numbering should be implemented in post-processor. text = [ "[" + linkCount + "]" ]; @@ -699,7 +699,7 @@ extlink ] ) ]; } - / "[" & { stops.dec('extlink'); return false; } + / "[" & { return stops.pop('extlink'); } ) autoref @@ -939,10 +939,12 @@ template_param_text input.substr( pos +1, 9) ); */ // re-enable tables within template parameters stops.push('table', false ); + stops.push('extlink', false); return stops.inc('template') } il:nested_block+ { stops.pop('table'); + stops.pop('extlink'); stops.dec('template'); //console.warn( 'tpt match: ' + pp (il) + " stops: " + pp(stops)); var r = flatten( il ), @@ -962,7 +964,7 @@ template_param_text return r; } - / & { stops.pop('table'); return stops.dec('template'); } + / & { stops.pop('table'); stops.pop('extlink'); return stops.dec('template'); } // TODO: handle link prefixes as in al[[Razi]]