add comments and improve variable names
This commit is contained in:
parent
479eb64a76
commit
90d14d9ffc
|
@ -386,6 +386,7 @@ Discourse.Dialect = {
|
||||||
var pos = args.start.lastIndex - match[0].length,
|
var pos = args.start.lastIndex - match[0].length,
|
||||||
leading = block.slice(0, pos),
|
leading = block.slice(0, pos),
|
||||||
trailing = match[2] ? match[2].replace(/^\n*/, "") : "";
|
trailing = match[2] ? match[2].replace(/^\n*/, "") : "";
|
||||||
|
// just give up if there's no stop tag in this or any next block
|
||||||
if (block.indexOf(args.stop, pos + args.stop.length) === -1 && lastChance()) { return; }
|
if (block.indexOf(args.stop, pos + args.stop.length) === -1 && lastChance()) { return; }
|
||||||
if (leading.length > 0) { result.push(['p'].concat(this.processInline(leading))); }
|
if (leading.length > 0) { result.push(['p'].concat(this.processInline(leading))); }
|
||||||
if (trailing.length > 0) {
|
if (trailing.length > 0) {
|
||||||
|
@ -393,47 +394,58 @@ Discourse.Dialect = {
|
||||||
block.lineNumber + countLines(leading) + (match[2] ? match[2].length : 0) - trailing.length));
|
block.lineNumber + countLines(leading) + (match[2] ? match[2].length : 0) - trailing.length));
|
||||||
}
|
}
|
||||||
|
|
||||||
// find matching stop tag in blocks.
|
// go through the available blocks to find the matching stop tag.
|
||||||
var contentBlocks = [], nesting = 0, endPos, ep, offset, startPos, sp, m, b;
|
var contentBlocks = [], nesting = 0, actualEndPos = -1, currentBlock;
|
||||||
blockloop:
|
blockloop:
|
||||||
while (b = next.shift()) {
|
while (currentBlock = next.shift()) {
|
||||||
|
// collect all the start and stop tags in the current block
|
||||||
args.start.lastIndex = 0;
|
args.start.lastIndex = 0;
|
||||||
startPos = []; sp = 0;
|
var startPos = [], m;
|
||||||
while (m = (args.start).exec(b)) {
|
while (m = (args.start).exec(currentBlock)) {
|
||||||
startPos.push(args.start.lastIndex - m[0].length);
|
startPos.push(args.start.lastIndex - m[0].length);
|
||||||
args.start.lastIndex = args.start.lastIndex - (m[2] ? m[2].length : 0);
|
args.start.lastIndex = args.start.lastIndex - (m[2] ? m[2].length : 0);
|
||||||
}
|
}
|
||||||
endPos = []; ep = 0; offset = 0;
|
var endPos = [], offset = 0;
|
||||||
while ((pos = b.indexOf(args.stop, offset)) !== -1) {
|
while ((pos = currentBlock.indexOf(args.stop, offset)) !== -1) {
|
||||||
endPos.push(pos);
|
endPos.push(pos);
|
||||||
offset += (pos + args.stop.length);
|
offset += (pos + args.stop.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// go through the available end tags:
|
||||||
|
var ep = 0, sp = 0; // array indices
|
||||||
while (ep < endPos.length) {
|
while (ep < endPos.length) {
|
||||||
if (sp < startPos.length && startPos[sp] < endPos[ep]) {
|
if (sp < startPos.length && startPos[sp] < endPos[ep]) {
|
||||||
|
// there's an end tag, but there's also another start tag first. we need to go deeper.
|
||||||
sp++; nesting++;
|
sp++; nesting++;
|
||||||
} else if (nesting > 0) {
|
} else if (nesting > 0) {
|
||||||
|
// found an end tag, but we must go up a level first.
|
||||||
ep++; nesting--;
|
ep++; nesting--;
|
||||||
} else {
|
} else {
|
||||||
|
// found an end tag and we're at the top: done!
|
||||||
|
actualEndPos = endPos[ep];
|
||||||
break blockloop;
|
break blockloop;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastChance()) {
|
if (lastChance()) {
|
||||||
ep = endPos.length - 1;
|
// when lastChance() becomes true the first time, currentBlock contains the last
|
||||||
|
// end tag available in the input blocks but it's not on the right nesting level
|
||||||
|
// or we would have terminated the loop already. the only thing we can do is to
|
||||||
|
// treat the last available end tag as tho it were matched with our start tag
|
||||||
|
// and let the emitter figure out how to render the garbage inside.
|
||||||
|
actualEndPos = endPos[endPos.length - 1];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// any left-over start tags still increase the nesting level
|
||||||
nesting += startPos.length - sp;
|
nesting += startPos.length - sp;
|
||||||
contentBlocks.push(b);
|
contentBlocks.push(currentBlock);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ep < endPos.length) {
|
var before = currentBlock.slice(0, actualEndPos).replace(/\n*$/, ""),
|
||||||
var before = b.slice(0, endPos[ep]).replace(/\n*$/, ""),
|
after = currentBlock.slice(actualEndPos + args.stop.length).replace(/^\n*/, "");
|
||||||
after = b.slice(endPos[ep] + args.stop.length).replace(/^\n*/, "");
|
if (before.length > 0) contentBlocks.push(MD.mk_block(before, "", currentBlock.lineNumber));
|
||||||
if (before.length > 0) contentBlocks.push(MD.mk_block(before, "", b.lineNumber));
|
if (after.length > 0) next.unshift(MD.mk_block(after, "", currentBlock.lineNumber + countLines(before)));
|
||||||
if (after.length > 0) next.unshift(MD.mk_block(after, "", b.lineNumber + countLines(before)));
|
|
||||||
}
|
|
||||||
|
|
||||||
var emitterResult = args.emitter.call(this, contentBlocks, match, dialect.options);
|
var emitterResult = args.emitter.call(this, contentBlocks, match, dialect.options);
|
||||||
if (emitterResult) { result.push(emitterResult); }
|
if (emitterResult) { result.push(emitterResult); }
|
||||||
|
|
Loading…
Reference in New Issue