Vitaly Puzrin
10 years ago
9 changed files with 1 additions and 249 deletions
@ -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; |
|
||||
}; |
|
@ -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); |
|
||||
} |
|
||||
} |
|
||||
}; |
|
@ -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> |
|
||||
. |
|
Loading…
Reference in new issue