Browse Source

Moved link parse functions to helpers

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
35063086ba
  1. 5
      lib/helpers/normalize_reference.js
  2. 75
      lib/helpers/parse_link_destination.js
  3. 53
      lib/helpers/parse_link_label.js
  4. 41
      lib/helpers/parse_link_title.js
  5. 173
      lib/links.js
  6. 67
      lib/parser_ref.js
  7. 3
      lib/rules_core/abbr.js
  8. 66
      lib/rules_core/references.js
  9. 8
      lib/rules_inline/links.js

5
lib/helpers/normalize_reference.js

@ -0,0 +1,5 @@
'use strict';
module.exports = function normalizeReference(str) {
return str.trim().replace(/\s+/g, ' ').toLowerCase();
};

75
lib/helpers/parse_link_destination.js

@ -0,0 +1,75 @@
// Parse link destination
//
// on success it returns a string and updates state.pos;
// on failure it returns null
//
'use strict';
var unescapeMd = require('../common/utils').unescapeMd;
module.exports = function parseLinkDestination(state, pos) {
var code, level,
start = pos,
max = state.posMax;
if (state.src.charCodeAt(pos) === 0x3C /* < */) {
pos++;
while (pos < max) {
code = state.src.charCodeAt(pos);
if (code === 0x0A /* \n */) { return false; }
if (code === 0x3E /* > */) {
state.pos = pos + 1;
state.linkContent = unescapeMd(state.src.slice(start + 1, pos));
return true;
}
if (code === 0x5C /* \ */ && pos + 1 < max) {
pos += 2;
continue;
}
pos++;
}
// no closing '>'
return false;
}
// this should be ... } else { ... branch
level = 0;
while (pos < max) {
code = state.src.charCodeAt(pos);
if (code === 0x20) { break; }
// ascii control characters
if (code < 0x20 || code === 0x7F) { break; }
if (code === 0x5C /* \ */ && pos + 1 < max) {
pos += 2;
continue;
}
if (code === 0x28 /* ( */) {
level++;
if (level > 1) { break; }
}
if (code === 0x29 /* ) */) {
level--;
if (level < 0) { break; }
}
pos++;
}
if (start === pos) { return false; }
state.linkContent = unescapeMd(state.src.slice(start, pos));
if (!state.parser.validateLink(state.linkContent)) { return false; }
state.pos = pos;
return true;
};

53
lib/helpers/parse_link_label.js

@ -0,0 +1,53 @@
// Parse link label
//
// this function assumes that first character ("[") already matches;
// returns the end of the label
//
'use strict';
module.exports = function parseLinkLabel(state, start) {
var level, found, marker,
labelEnd = -1,
max = state.posMax,
oldPos = state.pos,
oldFlag = state.isInLabel;
if (state.isInLabel) { return -1; }
if (state.labelUnmatchedScopes) {
state.labelUnmatchedScopes--;
return -1;
}
state.pos = start + 1;
state.isInLabel = true;
level = 1;
while (state.pos < max) {
marker = state.src.charCodeAt(state.pos);
if (marker === 0x5B /* [ */) {
level++;
} else if (marker === 0x5D /* ] */) {
level--;
if (level === 0) {
found = true;
break;
}
}
state.parser.skipToken(state);
}
if (found) {
labelEnd = state.pos;
state.labelUnmatchedScopes = 0;
} else {
state.labelUnmatchedScopes = level - 1;
}
// restore old state
state.pos = oldPos;
state.isInLabel = oldFlag;
return labelEnd;
};

41
lib/helpers/parse_link_title.js

@ -0,0 +1,41 @@
// Parse link title
//
// on success it returns a string and updates state.pos;
// on failure it returns null
//
'use strict';
var unescapeMd = require('../common/utils').unescapeMd;
module.exports = function parseLinkTitle(state, pos) {
var code,
start = pos,
max = state.posMax,
marker = state.src.charCodeAt(pos);
if (marker !== 0x22 /* " */ && marker !== 0x27 /* ' */ && marker !== 0x28 /* ( */) { return false; }
pos++;
// if opening marker is "(", switch it to closing marker ")"
if (marker === 0x28) { marker = 0x29; }
while (pos < max) {
code = state.src.charCodeAt(pos);
if (code === marker) {
state.pos = pos + 1;
state.linkContent = unescapeMd(state.src.slice(start + 1, pos));
return true;
}
if (code === 0x5C /* \ */ && pos + 1 < max) {
pos += 2;
continue;
}
pos++;
}
return false;
};

173
lib/links.js

@ -1,173 +0,0 @@
'use strict';
var unescapeMd = require('./common/utils').unescapeMd;
//
// Parse link label
//
// this function assumes that first character ("[") already matches;
// returns the end of the label
function parseLinkLabel(state, start) {
var level, found, marker,
labelEnd = -1,
max = state.posMax,
oldPos = state.pos,
oldFlag = state.isInLabel;
if (state.isInLabel) { return -1; }
if (state.labelUnmatchedScopes) {
state.labelUnmatchedScopes--;
return -1;
}
state.pos = start + 1;
state.isInLabel = true;
level = 1;
while (state.pos < max) {
marker = state.src.charCodeAt(state.pos);
if (marker === 0x5B /* [ */) {
level++;
} else if (marker === 0x5D /* ] */) {
level--;
if (level === 0) {
found = true;
break;
}
}
state.parser.skipToken(state);
}
if (found) {
labelEnd = state.pos;
state.labelUnmatchedScopes = 0;
} else {
state.labelUnmatchedScopes = level - 1;
}
// restore old state
state.pos = oldPos;
state.isInLabel = oldFlag;
return labelEnd;
}
//
// Parse link destination
//
// on success it returns a string and updates state.pos;
// on failure it returns null
function parseLinkDestination(state, pos) {
var code, level,
start = pos,
max = state.posMax;
if (state.src.charCodeAt(pos) === 0x3C /* < */) {
pos++;
while (pos < max) {
code = state.src.charCodeAt(pos);
if (code === 0x0A /* \n */) { return false; }
if (code === 0x3E /* > */) {
state.pos = pos + 1;
state.linkContent = unescapeMd(state.src.slice(start + 1, pos));
return true;
}
if (code === 0x5C /* \ */ && pos + 1 < max) {
pos += 2;
continue;
}
pos++;
}
// no closing '>'
return false;
}
// this should be ... } else { ... branch
level = 0;
while (pos < max) {
code = state.src.charCodeAt(pos);
if (code === 0x20) { break; }
// ascii control characters
if (code < 0x20 || code === 0x7F) { break; }
if (code === 0x5C /* \ */ && pos + 1 < max) {
pos += 2;
continue;
}
if (code === 0x28 /* ( */) {
level++;
if (level > 1) { break; }
}
if (code === 0x29 /* ) */) {
level--;
if (level < 0) { break; }
}
pos++;
}
if (start === pos) { return false; }
state.linkContent = unescapeMd(state.src.slice(start, pos));
if (!state.parser.validateLink(state.linkContent)) { return false; }
state.pos = pos;
return true;
}
//
// Parse link title
//
// on success it returns a string and updates state.pos;
// on failure it returns null
function parseLinkTitle(state, pos) {
var code,
start = pos,
max = state.posMax,
marker = state.src.charCodeAt(pos);
if (marker !== 0x22 /* " */ && marker !== 0x27 /* ' */ && marker !== 0x28 /* ( */) { return false; }
pos++;
// if opening marker is "(", switch it to closing marker ")"
if (marker === 0x28) { marker = 0x29; }
while (pos < max) {
code = state.src.charCodeAt(pos);
if (code === marker) {
state.pos = pos + 1;
state.linkContent = unescapeMd(state.src.slice(start + 1, pos));
return true;
}
if (code === 0x5C /* \ */ && pos + 1 < max) {
pos += 2;
continue;
}
pos++;
}
return false;
}
function normalizeReference(str) {
return str.trim().replace(/\s+/g, ' ').toLowerCase();
}
module.exports.parseLinkLabel = parseLinkLabel;
module.exports.parseLinkDestination = parseLinkDestination;
module.exports.parseLinkTitle = parseLinkTitle;
module.exports.normalizeReference = normalizeReference;

67
lib/parser_ref.js

@ -1,67 +0,0 @@
'use strict';
var StateInline = require('./rules_inline/state_inline');
var parseLinkLabel = require('./links').parseLinkLabel;
var parseLinkDestination = require('./links').parseLinkDestination;
var parseLinkTitle = require('./links').parseLinkTitle;
var normalizeReference = require('./links').normalizeReference;
// Parse link reference definition.
//
module.exports = function parse_reference(str, parser, options, env) {
var state, labelEnd, pos, max, code, start, href, title, label;
if (str.charCodeAt(0) !== 0x5B/* [ */) { return -1; }
if (str.indexOf(']:') === -1) { return -1; }
state = new StateInline(str, parser, options, env);
labelEnd = parseLinkLabel(state, 0);
if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; }
max = state.posMax;
// [label]: destination 'title'
// ^^^ skip optional whitespace here
for (pos = labelEnd + 2; pos < max; pos++) {
code = state.src.charCodeAt(pos);
if (code !== 0x20 && code !== 0x0A) { break; }
}
// [label]: destination 'title'
// ^^^^^^^^^^^ parse this
if (!parseLinkDestination(state, pos)) { return -1; }
href = state.linkContent;
pos = state.pos;
// [label]: destination 'title'
// ^^^ skipping those spaces
start = pos;
for (pos = pos + 1; pos < max; pos++) {
code = state.src.charCodeAt(pos);
if (code !== 0x20 && code !== 0x0A) { break; }
}
// [label]: destination 'title'
// ^^^^^^^ parse this
if (pos < max && start !== pos && parseLinkTitle(state, pos)) {
title = state.linkContent;
pos = state.pos;
} else {
title = '';
pos = start;
}
// ensure that the end of the line is empty
while (pos < max && state.src.charCodeAt(pos) === 0x20/* space */) { pos++; }
if (pos < max && state.src.charCodeAt(pos) !== 0x0A) { return -1; }
label = normalizeReference(str.slice(1, labelEnd));
env.references[label] = env.references[label] || { title: title, href: href };
return pos;
};

3
lib/rules_core/abbr.js

@ -3,8 +3,9 @@
'use strict'; 'use strict';
var StateInline = require('../rules_inline/state_inline'); var StateInline = require('../rules_inline/state_inline');
var parseLinkLabel = require('../links').parseLinkLabel; var parseLinkLabel = require('../helpers/parse_link_label');
function parseAbbr(str, parser, options, env) { function parseAbbr(str, parser, options, env) {

66
lib/rules_core/references.js

@ -1,6 +1,68 @@
'use strict'; 'use strict';
var parseRef = require('../parser_ref');
var StateInline = require('../rules_inline/state_inline');
var parseLinkLabel = require('../helpers/parse_link_label');
var parseLinkDestination = require('../helpers/parse_link_destination');
var parseLinkTitle = require('../helpers/parse_link_title');
var normalizeReference = require('../helpers/normalize_reference');
function parseReference(str, parser, options, env) {
var state, labelEnd, pos, max, code, start, href, title, label;
if (str.charCodeAt(0) !== 0x5B/* [ */) { return -1; }
if (str.indexOf(']:') === -1) { return -1; }
state = new StateInline(str, parser, options, env);
labelEnd = parseLinkLabel(state, 0);
if (labelEnd < 0 || str.charCodeAt(labelEnd + 1) !== 0x3A/* : */) { return -1; }
max = state.posMax;
// [label]: destination 'title'
// ^^^ skip optional whitespace here
for (pos = labelEnd + 2; pos < max; pos++) {
code = state.src.charCodeAt(pos);
if (code !== 0x20 && code !== 0x0A) { break; }
}
// [label]: destination 'title'
// ^^^^^^^^^^^ parse this
if (!parseLinkDestination(state, pos)) { return -1; }
href = state.linkContent;
pos = state.pos;
// [label]: destination 'title'
// ^^^ skipping those spaces
start = pos;
for (pos = pos + 1; pos < max; pos++) {
code = state.src.charCodeAt(pos);
if (code !== 0x20 && code !== 0x0A) { break; }
}
// [label]: destination 'title'
// ^^^^^^^ parse this
if (pos < max && start !== pos && parseLinkTitle(state, pos)) {
title = state.linkContent;
pos = state.pos;
} else {
title = '';
pos = start;
}
// ensure that the end of the line is empty
while (pos < max && state.src.charCodeAt(pos) === 0x20/* space */) { pos++; }
if (pos < max && state.src.charCodeAt(pos) !== 0x0A) { return -1; }
label = normalizeReference(str.slice(1, labelEnd));
env.references[label] = env.references[label] || { title: title, href: href };
return pos;
}
module.exports = function references(state) { module.exports = function references(state) {
var tokens = state.tokens, i, l, content, pos; var tokens = state.tokens, i, l, content, pos;
@ -15,7 +77,7 @@ module.exports = function references(state) {
content = tokens[i].content; content = tokens[i].content;
while (content.length) { while (content.length) {
pos = parseRef(content, state.inline, state.options, state.env); pos = parseReference(content, state.inline, state.options, state.env);
if (pos < 0) { break; } if (pos < 0) { break; }
content = content.slice(pos).trim(); content = content.slice(pos).trim();
} }

8
lib/rules_inline/links.js

@ -2,10 +2,10 @@
'use strict'; 'use strict';
var parseLinkLabel = require('../links').parseLinkLabel; var parseLinkLabel = require('../helpers/parse_link_label');
var parseLinkDestination = require('../links').parseLinkDestination; var parseLinkDestination = require('../helpers/parse_link_destination');
var parseLinkTitle = require('../links').parseLinkTitle; var parseLinkTitle = require('../helpers/parse_link_title');
var normalizeReference = require('../links').normalizeReference; var normalizeReference = require('../helpers/normalize_reference');
module.exports = function links(state, silent) { module.exports = function links(state, silent) {

Loading…
Cancel
Save