Browse Source

Move abbreviations from core to a block chain

pull/24/head
Alex Kocharin 10 years ago
parent
commit
3a3904ad79
  1. 1
      lib/parser_block.js
  2. 1
      lib/parser_core.js
  3. 48
      lib/rules_block/abbr.js
  4. 75
      lib/rules_core/abbr.js
  5. 12
      test/fixtures/markdown-it/abbr.txt

1
lib/parser_block.js

@ -15,6 +15,7 @@ var _rules = [
[ 'blockquote', require('./rules_block/blockquote'), [ 'paragraph', 'blockquote', 'list' ] ],
[ 'hr', require('./rules_block/hr'), [ 'paragraph', 'blockquote', 'list' ] ],
[ 'list', require('./rules_block/list'), [ 'paragraph', 'blockquote' ] ],
[ 'abbr', require('./rules_block/abbr'), [ 'paragraph' ] ],
[ 'footnote', require('./rules_block/footnote'), [ 'paragraph' ] ],
[ 'heading', require('./rules_block/heading'), [ 'paragraph', 'blockquote' ] ],
[ 'lheading', require('./rules_block/lheading') ],

1
lib/parser_core.js

@ -12,7 +12,6 @@ var Ruler = require('./ruler');
var _rules = [
[ 'block', require('./rules_core/block') ],
[ 'abbr', require('./rules_core/abbr') ],
[ 'references', require('./rules_core/references') ],
[ 'inline', require('./rules_core/inline') ],
[ 'footnote_tail', require('./rules_core/footnote_tail') ],

48
lib/rules_block/abbr.js

@ -0,0 +1,48 @@
// Parse abbreviation definitions, i.e. `*[abbr]: description`
//
'use strict';
module.exports = function parseAbbr(state, startLine, endLine, silent) {
var label, title, ch, labelStart, labelEnd,
pos = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
if (pos + 2 >= max) { return false; }
if (state.src.charCodeAt(pos++) !== 0x2A/* * */) { return false; }
if (state.src.charCodeAt(pos++) !== 0x5B/* [ */) { return false; }
labelStart = pos;
for (; pos < max; pos++) {
ch = state.src.charCodeAt(pos);
if (ch === 0x5B /* [ */) {
return false;
} else if (ch === 0x5D /* ] */) {
labelEnd = pos;
break;
} else if (ch === 0x5C /* \ */) {
pos++;
}
}
if (labelEnd < 0 || state.src.charCodeAt(labelEnd + 1) !== 0x3A/* : */) {
return false;
}
if (silent) { return true; }
label = state.src.slice(labelStart, labelEnd).replace(/\\(.)/g, '$1');
title = state.src.slice(labelEnd + 2, max).trim();
if (title.length === 0) { return false; }
if (!state.env.abbreviations) { state.env.abbreviations = {}; }
// prepend ':' to avoid conflict with Object.prototype members
if (typeof state.env.abbreviations[':' + label] === 'undefined') {
state.env.abbreviations[':' + label] = title;
}
state.line = startLine + 1;
return true;
};

75
lib/rules_core/abbr.js

@ -1,75 +0,0 @@
// Parse abbreviation definitions, i.e. `*[abbr]: description`
//
'use strict';
function parseAbbr(str, parserInline, options, env) {
var pos, label, title, ch,
max = str.length,
labelEnd = -1;
if (str.charCodeAt(0) !== 0x2A/* * */) { return -1; }
if (str.charCodeAt(1) !== 0x5B/* [ */) { return -1; }
if (str.indexOf(']:') === -1) { return -1; }
for (pos = 2; pos < max; pos++) {
ch = str.charCodeAt(pos);
if (ch === 0x5B /* [ */) {
return -1;
} else if (ch === 0x5D /* ] */) {
labelEnd = pos;
break;
} else if (ch === 0x5C /* \ */) {
pos++;
}
}
if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; }
// abbr title is always one line, so looking for ending "\n" here
for (pos = labelEnd + 2; pos < max; pos++) {
if (str.charCodeAt(pos) === 0x0A) { break; }
}
label = str.slice(2, labelEnd).replace(/\\(.)/g, '$1');
title = str.slice(labelEnd + 2, pos).trim();
if (title.length === 0) { return -1; }
if (!env.abbreviations) { env.abbreviations = {}; }
// prepend ':' to avoid conflict with Object.prototype members
if (typeof env.abbreviations[':' + label] === 'undefined') {
env.abbreviations[':' + label] = title;
}
return pos;
}
module.exports = function abbr(state) {
var tokens = state.tokens, i, l, content, pos;
if (state.inlineMode) {
return;
}
// Parse inlines
for (i = 1, l = tokens.length - 1; i < l; i++) {
if (tokens[i - 1].type === 'paragraph_open' &&
tokens[i].type === 'inline' &&
tokens[i + 1].type === 'paragraph_close') {
content = tokens[i].content;
while (content.length) {
pos = parseAbbr(content, state.inline, state.options, state.env);
if (pos < 0) { break; }
content = content.slice(pos).trim();
}
tokens[i].content = content;
if (!content.length) {
tokens[i - 1].tight = true;
tokens[i + 1].tight = true;
}
}
}
};

12
test/fixtures/markdown-it/abbr.txt

@ -11,18 +11,6 @@ is maintained by the W3C.
is maintained by the <abbr title="World Wide Web Consortium">W3C</abbr>.</p>
.
They can be multiline (see pandoc implementation). Not sure about newlines, but we should at least skip those definitions:
.
*[
foo
bar
]: desc
foo
.
<p>foo</p>
.
They can contain arbitrary markup (see pandoc implementation):
.

Loading…
Cancel
Save