Browse Source

Removed abbr rules (move to plugin)

pull/25/head
Vitaly Puzrin 10 years ago
parent
commit
c0ccaa79ce
  1. 1
      lib/parser_block.js
  2. 1
      lib/parser_core.js
  3. 3
      lib/presets/commonmark.js
  4. 1
      lib/presets/default.js
  5. 8
      lib/renderer.js
  6. 48
      lib/rules_block/abbr.js
  7. 91
      lib/rules_core/abbr2.js
  8. 81
      test/fixtures/markdown-it/abbr.txt
  9. 16
      test/fixtures/markdown-it/proto.txt

1
lib/parser_block.js

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

1
lib/parser_core.js

@ -14,7 +14,6 @@ var _rules = [
[ 'block', require('./rules_core/block') ],
[ 'inline', require('./rules_core/inline') ],
[ 'footnote_tail', require('./rules_core/footnote_tail') ],
[ 'abbr2', require('./rules_core/abbr2') ],
[ 'replacements', require('./rules_core/replacements') ],
[ 'smartquotes', require('./rules_core/smartquotes') ],
[ 'linkify', require('./rules_core/linkify') ]

3
lib/presets/commonmark.js

@ -33,8 +33,7 @@ module.exports = {
core: {
rules: [
'block',
'inline',
'abbr2'
'inline'
]
},

1
lib/presets/default.js

@ -37,7 +37,6 @@ module.exports = {
'replacements',
'linkify',
'smartquotes',
'abbr2',
'footnote_tail'
]
},

8
lib/renderer.js

@ -231,14 +231,6 @@ rules.htmltag = function (tokens, idx /*, options, env */) {
};
rules.abbr_open = function (tokens, idx /*, options, env */) {
return '<abbr title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '">';
};
rules.abbr_close = function (/* tokens, idx, options, env */) {
return '</abbr>';
};
rules.footnote_ref = function (tokens, idx) {
var n = Number(tokens[idx].id + 1).toString();
var id = 'fnref' + n;

48
lib/rules_block/abbr.js

@ -1,48 +0,0 @@
// 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;
};

91
lib/rules_core/abbr2.js

@ -1,91 +0,0 @@
// Enclose abbreviations in <abbr> tags
//
'use strict';
var arrayReplaceAt = require('../common/utils').arrayReplaceAt;
var PUNCT_CHARS = ' \n()[]\'".,!?-';
// from Google closure library
// http://closure-library.googlecode.com/git-history/docs/local_closure_goog_string_string.js.source.html#line1021
function regEscape(s) {
return s.replace(/([-()\[\]{}+?*.$\^|,:#<!\\])/g, '\\$1');
}
module.exports = function abbr2(state) {
var i, j, l, tokens, token, text, nodes, pos, level, reg, m, regText,
blockTokens = state.tokens;
if (!state.env.abbreviations) { return; }
if (!state.env.abbrRegExp) {
regText = '(^|[' + PUNCT_CHARS.split('').map(regEscape).join('') + '])'
+ '(' + Object.keys(state.env.abbreviations).map(function (x) {
return x.substr(1);
}).sort(function (a, b) {
return b.length - a.length;
}).map(regEscape).join('|') + ')'
+ '($|[' + PUNCT_CHARS.split('').map(regEscape).join('') + '])';
state.env.abbrRegExp = new RegExp(regText, 'g');
}
reg = state.env.abbrRegExp;
for (j = 0, l = blockTokens.length; j < l; j++) {
if (blockTokens[j].type !== 'inline') { continue; }
tokens = blockTokens[j].children;
// We scan from the end, to keep position when new tags added.
for (i = tokens.length - 1; i >= 0; i--) {
token = tokens[i];
if (token.type !== 'text') { continue; }
pos = 0;
text = token.content;
reg.lastIndex = 0;
level = token.level;
nodes = [];
while ((m = reg.exec(text))) {
if (reg.lastIndex > pos) {
nodes.push({
type: 'text',
content: text.slice(pos, m.index + m[1].length),
level: level
});
}
nodes.push({
type: 'abbr_open',
title: state.env.abbreviations[':' + m[2]],
level: level++
});
nodes.push({
type: 'text',
content: m[2],
level: level
});
nodes.push({
type: 'abbr_close',
level: --level
});
pos = reg.lastIndex - m[3].length;
}
if (!nodes.length) { continue; }
if (pos < text.length) {
nodes.push({
type: 'text',
content: text.slice(pos),
level: level
});
}
// replace current node
blockTokens[j].children = tokens = arrayReplaceAt(tokens, i, nodes);
}
}
};

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

@ -1,81 +0,0 @@
An example from php markdown readme:
.
*[HTML]: Hyper Text Markup Language
*[W3C]: World Wide Web Consortium
The HTML specification
is maintained by the W3C.
.
<p>The <abbr title="Hyper Text Markup Language">HTML</abbr> specification
is maintained by the <abbr title="World Wide Web Consortium">W3C</abbr>.</p>
.
They can contain arbitrary markup (see pandoc implementation):
.
*[`\]:`]: foo
\`]:\`
.
<p><abbr title="foo">`]:`</abbr></p>
.
No empty abbreviations:
.
*[foo]:
foo
.
<p>*[foo]:
foo</p>
.
Intersecting abbreviations (first should match):
.
*[Bar Foo]: 123
*[Foo Bar]: 456
Foo Bar Foo
Bar Foo Bar
.
<p><abbr title="456">Foo Bar</abbr> Foo</p>
<p><abbr title="123">Bar Foo</abbr> Bar</p>
.
Don't bother with nested abbreviations (yet?):
.
*[JS]: javascript
*[HTTP]: hyper text blah blah
*[JS HTTP]: is awesome
JS HTTP is a collection of low-level javascript HTTP-related modules
.
<p><abbr title="is awesome">JS HTTP</abbr> is a collection of low-level javascript <abbr title="hyper text blah blah">HTTP</abbr>-related modules</p>
.
Mixing up abbreviations and references:
.
*[foo]: 123
[bar]: 456
*[baz]: 789
[quux]: 012
and a paragraph continuation
foo [bar] baz [quux]
.
<p>and a paragraph continuation</p>
<p><abbr title="123">foo</abbr> <a href="456">bar</a> <abbr title="789">baz</abbr> <a href="012">quux</a></p>
.
Don't match the middle of the string:
.
*[foo]: blah
*[bar]: blah
foobar
.
<p>foobar</p>
.

16
test/fixtures/markdown-it/proto.txt

@ -22,14 +22,6 @@
</section>
.
.
*[__proto__]: blah
__proto__ \_\_proto\_\_
.
<p><strong>proto</strong> <abbr title="blah">__proto__</abbr></p>
.
.
[hasOwnProperty]
@ -52,11 +44,3 @@ __proto__ \_\_proto\_\_
</ol>
</section>
.
.
*[hasOwnProperty]: blah
hasOwnProperty
.
<p><abbr title="blah">hasOwnProperty</abbr></p>
.

Loading…
Cancel
Save