Vitaly Puzrin
10 years ago
9 changed files with 245 additions and 248 deletions
@ -0,0 +1,5 @@ |
|||
'use strict'; |
|||
|
|||
module.exports = function normalizeReference(str) { |
|||
return str.trim().replace(/\s+/g, ' ').toLowerCase(); |
|||
}; |
@ -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; |
|||
}; |
@ -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; |
|||
}; |
@ -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; |
|||
}; |
@ -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; |
@ -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; |
|||
}; |
Loading…
Reference in new issue