aboutsummaryrefslogtreecommitdiff
path: root/js/lib/inlines.js
blob: 5fde099690681efb8d9574b29491c0d6d54e3679 (plain)
  1. var fromCodePoint = require('./from-code-point.js');
  2. var entityToChar = require('./html5-entities.js').entityToChar;
  3. // Constants for character codes:
  4. var C_NEWLINE = 10;
  5. var C_SPACE = 32;
  6. var C_ASTERISK = 42;
  7. var C_UNDERSCORE = 95;
  8. var C_BACKTICK = 96;
  9. var C_OPEN_BRACKET = 91;
  10. var C_CLOSE_BRACKET = 93;
  11. var C_LESSTHAN = 60;
  12. var C_GREATERTHAN = 62;
  13. var C_BANG = 33;
  14. var C_BACKSLASH = 92;
  15. var C_AMPERSAND = 38;
  16. var C_OPEN_PAREN = 40;
  17. var C_COLON = 58;
  18. // Some regexps used in inline parser:
  19. var ESCAPABLE = '[!"#$%&\'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]';
  20. var ESCAPED_CHAR = '\\\\' + ESCAPABLE;
  21. var IN_DOUBLE_QUOTES = '"(' + ESCAPED_CHAR + '|[^"\\x00])*"';
  22. var IN_SINGLE_QUOTES = '\'(' + ESCAPED_CHAR + '|[^\'\\x00])*\'';
  23. var IN_PARENS = '\\((' + ESCAPED_CHAR + '|[^)\\x00])*\\)';
  24. var REG_CHAR = '[^\\\\()\\x00-\\x20]';
  25. var IN_PARENS_NOSP = '\\((' + REG_CHAR + '|' + ESCAPED_CHAR + ')*\\)';
  26. var TAGNAME = '[A-Za-z][A-Za-z0-9]*';
  27. var ATTRIBUTENAME = '[a-zA-Z_:][a-zA-Z0-9:._-]*';
  28. var UNQUOTEDVALUE = "[^\"'=<>`\\x00-\\x20]+";
  29. var SINGLEQUOTEDVALUE = "'[^']*'";
  30. var DOUBLEQUOTEDVALUE = '"[^"]*"';
  31. var ATTRIBUTEVALUE = "(?:" + UNQUOTEDVALUE + "|" + SINGLEQUOTEDVALUE + "|" + DOUBLEQUOTEDVALUE + ")";
  32. var ATTRIBUTEVALUESPEC = "(?:" + "\\s*=" + "\\s*" + ATTRIBUTEVALUE + ")";
  33. var ATTRIBUTE = "(?:" + "\\s+" + ATTRIBUTENAME + ATTRIBUTEVALUESPEC + "?)";
  34. var OPENTAG = "<" + TAGNAME + ATTRIBUTE + "*" + "\\s*/?>";
  35. var CLOSETAG = "</" + TAGNAME + "\\s*[>]";
  36. var HTMLCOMMENT = "<!--([^-]+|[-][^-]+)*-->";
  37. var PROCESSINGINSTRUCTION = "[<][?].*?[?][>]";
  38. var DECLARATION = "<![A-Z]+" + "\\s+[^>]*>";
  39. var CDATA = "<!\\[CDATA\\[([^\\]]+|\\][^\\]]|\\]\\][^>])*\\]\\]>";
  40. var HTMLTAG = "(?:" + OPENTAG + "|" + CLOSETAG + "|" + HTMLCOMMENT + "|" +
  41. PROCESSINGINSTRUCTION + "|" + DECLARATION + "|" + CDATA + ")";
  42. var ENTITY = "&(?:#x[a-f0-9]{1,8}|#[0-9]{1,8}|[a-z][a-z0-9]{1,31});";
  43. var reHtmlTag = new RegExp('^' + HTMLTAG, 'i');
  44. var reLinkTitle = new RegExp(
  45. '^(?:"(' + ESCAPED_CHAR + '|[^"\\x00])*"' +
  46. '|' +
  47. '\'(' + ESCAPED_CHAR + '|[^\'\\x00])*\'' +
  48. '|' +
  49. '\\((' + ESCAPED_CHAR + '|[^)\\x00])*\\))');
  50. var reLinkDestinationBraces = new RegExp(
  51. '^(?:[<](?:[^<>\\n\\\\\\x00]' + '|' + ESCAPED_CHAR + '|' + '\\\\)*[>])');
  52. var reLinkDestination = new RegExp(
  53. '^(?:' + REG_CHAR + '+|' + ESCAPED_CHAR + '|' + IN_PARENS_NOSP + ')*');
  54. var reEscapable = new RegExp(ESCAPABLE);
  55. var reAllEscapedChar = new RegExp('\\\\(' + ESCAPABLE + ')', 'g');
  56. var reEscapedChar = new RegExp('^\\\\(' + ESCAPABLE + ')');
  57. var reEntityHere = new RegExp('^' + ENTITY, 'i');
  58. var reEntity = new RegExp(ENTITY, 'gi');
  59. // Matches a character with a special meaning in markdown,
  60. // or a string of non-special characters. Note: we match
  61. // clumps of _ or * or `, because they need to be handled in groups.
  62. var reMain = /^(?:[_*`\n]+|[\[\]\\!<&*_]|(?: *[^\n `\[\]\\!<&*_]+)+|[ \n]+)/m;
  63. // Replace entities and backslash escapes with literal characters.
  64. var unescapeString = function(s) {
  65. return s.replace(reAllEscapedChar, '$1')
  66. .replace(reEntity, entityToChar);
  67. };
  68. // Normalize reference label: collapse internal whitespace
  69. // to single space, remove leading/trailing whitespace, case fold.
  70. var normalizeReference = function(s) {
  71. return s.trim()
  72. .replace(/\s+/,' ')
  73. .toUpperCase();
  74. };
  75. // INLINE PARSER
  76. // These are methods of an InlineParser object, defined below.
  77. // An InlineParser keeps track of a subject (a string to be
  78. // parsed) and a position in that subject.
  79. // If re matches at current position in the subject, advance
  80. // position in subject and return the match; otherwise return null.
  81. var match = function(re) {
  82. var match = re.exec(this.subject.slice(this.pos));
  83. if (match) {
  84. this.pos += match.index + match[0].length;
  85. return match[0];
  86. } else {
  87. return null;
  88. }
  89. };
  90. // Returns the code for the character at the current subject position, or -1
  91. // there are no more characters.
  92. var peek = function() {
  93. if (this.pos < this.subject.length) {
  94. return this.subject.charCodeAt(this.pos);
  95. } else {
  96. return -1;
  97. }
  98. };
  99. // Parse zero or more space characters, including at most one newline
  100. var spnl = function() {
  101. this.match(/^ *(?:\n *)?/);
  102. return 1;
  103. };
  104. // All of the parsers below try to match something at the current position
  105. // in the subject. If they succeed in matching anything, they
  106. // return the inline matched, advancing the subject.
  107. // Attempt to parse backticks, returning either a backtick code span or a
  108. // literal sequence of backticks.
  109. var parseBackticks = function(inlines) {
  110. var startpos = this.pos;
  111. var ticks = this.match(/^`+/);
  112. if (!ticks) {
  113. return 0;
  114. }
  115. var afterOpenTicks = this.pos;
  116. var foundCode = false;
  117. var match;
  118. while (!foundCode && (match = this.match(/`+/m))) {
  119. if (match == ticks) {
  120. inlines.push({ t: 'Code', c: this.subject.slice(afterOpenTicks,
  121. this.pos - ticks.length)
  122. .replace(/[ \n]+/g,' ')
  123. .trim() });
  124. return true;
  125. }
  126. }
  127. // If we got here, we didn't match a closing backtick sequence.
  128. this.pos = afterOpenTicks;
  129. inlines.push({ t: 'Str', c: ticks });
  130. return true;
  131. };
  132. // Parse a backslash-escaped special character, adding either the escaped
  133. // character, a hard line break (if the backslash is followed by a newline),
  134. // or a literal backslash to the 'inlines' list.
  135. var parseBackslash = function(inlines) {
  136. var subj = this.subject,
  137. pos = this.pos;
  138. if (subj.charCodeAt(pos) === C_BACKSLASH) {
  139. if (subj.charAt(pos + 1) === '\n') {
  140. this.pos = this.pos + 2;
  141. inlines.push({ t: 'Hardbreak' });
  142. } else if (reEscapable.test(subj.charAt(pos + 1))) {
  143. this.pos = this.pos + 2;
  144. inlines.push({ t: 'Str', c: subj.charAt(pos + 1) });
  145. } else {
  146. this.pos++;
  147. inlines.push({t: 'Str', c: '\\'});
  148. }
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. };
  154. // Attempt to parse an autolink (URL or email in pointy brackets).
  155. var parseAutolink = function(inlines) {
  156. var m;
  157. var dest;
  158. if ((m = this.match(/^<([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)>/))) { // email autolink
  159. dest = m.slice(1,-1);
  160. inlines.push(
  161. {t: 'Link',
  162. label: [{ t: 'Str', c: dest }],
  163. destination: 'mailto:' + encodeURI(unescape(dest)) });
  164. return true;
  165. } else if ((m = this.match(/^<(?:coap|doi|javascript|aaa|aaas|about|acap|cap|cid|crid|data|dav|dict|dns|file|ftp|geo|go|gopher|h323|http|https|iax|icap|im|imap|info|ipp|iris|iris.beep|iris.xpc|iris.xpcs|iris.lwz|ldap|mailto|mid|msrp|msrps|mtqp|mupdate|news|nfs|ni|nih|nntp|opaquelocktoken|pop|pres|rtsp|service|session|shttp|sieve|sip|sips|sms|snmp|soap.beep|soap.beeps|tag|tel|telnet|tftp|thismessage|tn3270|tip|tv|urn|vemmi|ws|wss|xcon|xcon-userid|xmlrpc.beep|xmlrpc.beeps|xmpp|z39.50r|z39.50s|adiumxtra|afp|afs|aim|apt|attachment|aw|beshare|bitcoin|bolo|callto|chrome|chrome-extension|com-eventbrite-attendee|content|cvs|dlna-playsingle|dlna-playcontainer|dtn|dvb|ed2k|facetime|feed|finger|fish|gg|git|gizmoproject|gtalk|hcp|icon|ipn|irc|irc6|ircs|itms|jar|jms|keyparc|lastfm|ldaps|magnet|maps|market|message|mms|ms-help|msnim|mumble|mvn|notes|oid|palm|paparazzi|platform|proxy|psyc|query|res|resource|rmi|rsync|rtmp|secondlife|sftp|sgn|skype|smb|soldat|spotify|ssh|steam|svn|teamspeak|things|udp|unreal|ut2004|ventrilo|view-source|webcal|wtai|wyciwyg|xfire|xri|ymsgr):[^<>\x00-\x20]*>/i))) {
  166. dest = m.slice(1,-1);
  167. inlines.push({
  168. t: 'Link',
  169. label: [{ t: 'Str', c: dest }],
  170. destination: encodeURI(unescape(dest)) });
  171. return true;
  172. } else {
  173. return false;
  174. }
  175. };
  176. // Attempt to parse a raw HTML tag.
  177. var parseHtmlTag = function(inlines) {
  178. var m = this.match(reHtmlTag);
  179. if (m) {
  180. inlines.push({ t: 'Html', c: m });
  181. return true;
  182. } else {
  183. return false;
  184. }
  185. };
  186. // Scan a sequence of characters with code cc, and return information about
  187. // the number of delimiters and whether they are positioned such that
  188. // they can open and/or close emphasis or strong emphasis. A utility
  189. // function for strong/emph parsing.
  190. var scanDelims = function(cc) {
  191. var numdelims = 0;
  192. var first_close_delims = 0;
  193. var char_before, char_after, cc_after;
  194. var startpos = this.pos;
  195. char_before = this.pos === 0 ? '\n' :
  196. this.subject.charAt(this.pos - 1);
  197. while (this.peek() === cc) {
  198. numdelims++;
  199. this.pos++;
  200. }
  201. cc_after = this.peek();
  202. if (cc_after === -1) {
  203. char_after = '\n';
  204. } else {
  205. char_after = fromCodePoint(cc_after);
  206. }
  207. var can_open = numdelims > 0 && numdelims <= 3 && !(/\s/.test(char_after));
  208. var can_close = numdelims > 0 && numdelims <= 3 && !(/\s/.test(char_before));
  209. if (cc === C_UNDERSCORE) {
  210. can_open = can_open && !((/[a-z0-9]/i).test(char_before));
  211. can_close = can_close && !((/[a-z0-9]/i).test(char_after));
  212. }
  213. this.pos = startpos;
  214. return { numdelims: numdelims,
  215. can_open: can_open,
  216. can_close: can_close };
  217. };
  218. var Emph = function(ils) {
  219. return {t: 'Emph', c: ils};
  220. };
  221. var Strong = function(ils) {
  222. return {t: 'Strong', c: ils};
  223. };
  224. var Str = function(s) {
  225. return {t: 'Str', c: s};
  226. };
  227. // Attempt to parse emphasis or strong emphasis.
  228. var parseEmphasis = function(cc,inlines) {
  229. var startpos = this.pos;
  230. var res = this.scanDelims(cc);
  231. var numdelims = res.numdelims;
  232. if (numdelims === 0) {
  233. this.pos = startpos;
  234. return false;
  235. }
  236. if (res.can_close) {
  237. // Walk the stack and find a matching opener, if possible
  238. var opener = this.emphasis_openers;
  239. while (opener) {
  240. if (opener.cc === cc) { // we have a match!
  241. if (opener.numdelims <= numdelims) { // all openers used
  242. this.pos += opener.numdelims;
  243. var X;
  244. switch (opener.numdelims) {
  245. case 3:
  246. X = function(x) { return Strong([Emph(x)]); };
  247. break;
  248. case 2:
  249. X = Strong;
  250. break;
  251. case 1:
  252. default:
  253. X = Emph;
  254. break;
  255. }
  256. inlines[opener.pos] = X(inlines.slice(opener.pos + 1));
  257. inlines.splice(opener.pos + 1, inlines.length - (opener.pos + 1));
  258. // Remove entries after this, to prevent overlapping nesting:
  259. this.emphasis_openers = opener.previous;
  260. return true;
  261. } else if (opener.numdelims > numdelims) { // only some openers used
  262. this.pos += numdelims;
  263. opener.numdelims -= numdelims;
  264. inlines[opener.pos].c =
  265. inlines[opener.pos].c.slice(0, opener.numdelims);
  266. var X = numdelims === 2 ? Strong : Emph;
  267. inlines[opener.pos + 1] = X(inlines.slice(opener.pos + 1));
  268. inlines.splice(opener.pos + 2, inlines.length - (opener.pos + 2));
  269. // Remove entries after this, to prevent overlapping nesting:
  270. this.emphasis_openers = opener;
  271. return true;
  272. }
  273. }
  274. opener = opener.previous;
  275. }
  276. }
  277. // If we're here, we didn't match a closer.
  278. this.pos += numdelims;
  279. inlines.push(Str(this.subject.slice(startpos, startpos + numdelims)));
  280. if (res.can_open) {
  281. // Add entry to stack for this opener
  282. this.emphasis_openers = { cc: cc,
  283. numdelims: numdelims,
  284. pos: inlines.length - 1,
  285. previous: this.emphasis_openers };
  286. }
  287. return true;
  288. };
  289. // Attempt to parse link title (sans quotes), returning the string
  290. // or null if no match.
  291. var parseLinkTitle = function() {
  292. var title = this.match(reLinkTitle);
  293. if (title) {
  294. // chop off quotes from title and unescape:
  295. return unescapeString(title.substr(1, title.length - 2));
  296. } else {
  297. return null;
  298. }
  299. };
  300. // Attempt to parse link destination, returning the string or
  301. // null if no match.
  302. var parseLinkDestination = function() {
  303. var res = this.match(reLinkDestinationBraces);
  304. if (res) { // chop off surrounding <..>:
  305. return encodeURI(unescape(unescapeString(res.substr(1, res.length - 2))));
  306. } else {
  307. res = this.match(reLinkDestination);
  308. if (res !== null) {
  309. return encodeURI(unescape(unescapeString(res)));
  310. } else {
  311. return null;
  312. }
  313. }
  314. };
  315. // Attempt to parse a link label, returning number of characters parsed.
  316. var parseLinkLabel = function() {
  317. if (this.peek() != C_OPEN_BRACKET) {
  318. return 0;
  319. }
  320. var startpos = this.pos;
  321. var nest_level = 0;
  322. if (this.label_nest_level > 0) {
  323. // If we've already checked to the end of this subject
  324. // for a label, even with a different starting [, we
  325. // know we won't find one here and we can just return.
  326. // This avoids lots of backtracking.
  327. // Note: nest level 1 would be: [foo [bar]
  328. // nest level 2 would be: [foo [bar [baz]
  329. this.label_nest_level--;
  330. return 0;
  331. }
  332. this.pos++; // advance past [
  333. var c;
  334. while ((c = this.peek()) && c != -1 && (c != C_CLOSE_BRACKET || nest_level > 0)) {
  335. switch (c) {
  336. case C_BACKTICK:
  337. this.parseBackticks([]);
  338. break;
  339. case C_LESSTHAN:
  340. if (!(this.parseAutolink([]) || this.parseHtmlTag([]))) {
  341. this.pos++;
  342. }
  343. break;
  344. case C_OPEN_BRACKET: // nested []
  345. nest_level++;
  346. this.pos++;
  347. break;
  348. case C_CLOSE_BRACKET: // nested []
  349. nest_level--;
  350. this.pos++;
  351. break;
  352. case C_BACKSLASH:
  353. this.parseBackslash([]);
  354. break;
  355. default:
  356. this.parseString([]);
  357. }
  358. }
  359. if (c === C_CLOSE_BRACKET) {
  360. this.label_nest_level = 0;
  361. this.pos++; // advance past ]
  362. return this.pos - startpos;
  363. } else {
  364. if (c === -1) {
  365. this.label_nest_level = nest_level;
  366. }
  367. this.pos = startpos;
  368. return 0;
  369. }
  370. };
  371. // Parse raw link label, including surrounding [], and return
  372. // inline contents. (Note: this is not a method of InlineParser.)
  373. var parseRawLabel = function(s) {
  374. // note: parse without a refmap; we don't want links to resolve
  375. // in nested brackets!
  376. return new InlineParser().parse(s.substr(1, s.length - 2), {});
  377. };
  378. // Attempt to parse a link. If successful, return the link.
  379. var parseLink = function(inlines) {
  380. var startpos = this.pos;
  381. var reflabel;
  382. var n;
  383. var dest;
  384. var title;
  385. n = this.parseLinkLabel();
  386. if (n === 0) {
  387. return false;
  388. }
  389. var afterlabel = this.pos;
  390. var rawlabel = this.subject.substr(startpos, n);
  391. // if we got this far, we've parsed a label.
  392. // Try to parse an explicit link: [label](url "title")
  393. if (this.peek() == C_OPEN_PAREN) {
  394. this.pos++;
  395. if (this.spnl() &&
  396. ((dest = this.parseLinkDestination()) !== null) &&
  397. this.spnl() &&
  398. // make sure there's a space before the title:
  399. (/^\s/.test(this.subject.charAt(this.pos - 1)) &&
  400. (title = this.parseLinkTitle() || '') || true) &&
  401. this.spnl() &&
  402. this.match(/^\)/)) {
  403. inlines.push({ t: 'Link',
  404. destination: dest,
  405. title: title,
  406. label: parseRawLabel(rawlabel) });
  407. return true;
  408. } else {
  409. this.pos = startpos;
  410. return false;
  411. }
  412. }
  413. // If we're here, it wasn't an explicit link. Try to parse a reference link.
  414. // first, see if there's another label
  415. var savepos = this.pos;
  416. this.spnl();
  417. var beforelabel = this.pos;
  418. n = this.parseLinkLabel();
  419. if (n == 2) {
  420. // empty second label
  421. reflabel = rawlabel;
  422. } else if (n > 0) {
  423. reflabel = this.subject.slice(beforelabel, beforelabel + n);
  424. } else {
  425. this.pos = savepos;
  426. reflabel = rawlabel;
  427. }
  428. // lookup rawlabel in refmap
  429. var link = this.refmap[normalizeReference(reflabel)];
  430. if (link) {
  431. inlines.push({t: 'Link',
  432. destination: link.destination,
  433. title: link.title,
  434. label: parseRawLabel(rawlabel) });
  435. return true;
  436. } else {
  437. this.pos = startpos;
  438. return false;
  439. }
  440. // Nothing worked, rewind:
  441. this.pos = startpos;
  442. return false;
  443. };
  444. // Attempt to parse an entity, return Entity object if successful.
  445. var parseEntity = function(inlines) {
  446. var m;
  447. if ((m = this.match(reEntityHere))) {
  448. inlines.push({ t: 'Str', c: entityToChar(m) });
  449. return true;
  450. } else {
  451. return false;
  452. }
  453. };
  454. // Parse a run of ordinary characters, or a single character with
  455. // a special meaning in markdown, as a plain string, adding to inlines.
  456. var parseString = function(inlines) {
  457. var m;
  458. if ((m = this.match(reMain))) {
  459. inlines.push({ t: 'Str', c: m });
  460. return true;
  461. } else {
  462. return false;
  463. }
  464. };
  465. // Parse a newline. If it was preceded by two spaces, return a hard
  466. // line break; otherwise a soft line break.
  467. var parseNewline = function(inlines) {
  468. var m = this.match(/^ *\n/);
  469. if (m) {
  470. if (m.length > 2) {
  471. inlines.push({ t: 'Hardbreak' });
  472. } else if (m.length > 0) {
  473. inlines.push({ t: 'Softbreak' });
  474. }
  475. return true;
  476. }
  477. return false;
  478. };
  479. // Attempt to parse an image. If the opening '!' is not followed
  480. // by a link, return a literal '!'.
  481. var parseImage = function(inlines) {
  482. if (this.match(/^!/)) {
  483. var link = this.parseLink(inlines);
  484. if (link) {
  485. inlines[inlines.length - 1].t = 'Image';
  486. return true;
  487. } else {
  488. inlines.push({ t: 'Str', c: '!' });
  489. return true;
  490. }
  491. } else {
  492. return false;
  493. }
  494. };
  495. // Attempt to parse a link reference, modifying refmap.
  496. var parseReference = function(s, refmap) {
  497. this.subject = s;
  498. this.pos = 0;
  499. this.label_nest_level = 0;
  500. var rawlabel;
  501. var dest;
  502. var title;
  503. var matchChars;
  504. var startpos = this.pos;
  505. var match;
  506. // label:
  507. matchChars = this.parseLinkLabel();
  508. if (matchChars === 0) {
  509. return 0;
  510. } else {
  511. rawlabel = this.subject.substr(0, matchChars);
  512. }
  513. // colon:
  514. if (this.peek() === C_COLON) {
  515. this.pos++;
  516. } else {
  517. this.pos = startpos;
  518. return 0;
  519. }
  520. // link url
  521. this.spnl();
  522. dest = this.parseLinkDestination();
  523. if (dest === null || dest.length === 0) {
  524. this.pos = startpos;
  525. return 0;
  526. }
  527. var beforetitle = this.pos;
  528. this.spnl();
  529. title = this.parseLinkTitle();
  530. if (title === null) {
  531. title = '';
  532. // rewind before spaces
  533. this.pos = beforetitle;
  534. }
  535. // make sure we're at line end:
  536. if (this.match(/^ *(?:\n|$)/) === null) {
  537. this.pos = startpos;
  538. return 0;
  539. }
  540. var normlabel = normalizeReference(rawlabel);
  541. if (!refmap[normlabel]) {
  542. refmap[normlabel] = { destination: dest, title: title };
  543. }
  544. return this.pos - startpos;
  545. };
  546. // Parse the next inline element in subject, advancing subject position.
  547. // On success, add the result to the inlines list, and return true.
  548. // On failure, return false.
  549. var parseInline = function(inlines) {
  550. var startpos = this.pos;
  551. var origlen = inlines.length;
  552. var c = this.peek();
  553. if (c === -1) {
  554. return false;
  555. }
  556. var res;
  557. switch(c) {
  558. case C_NEWLINE:
  559. case C_SPACE:
  560. res = this.parseNewline(inlines);
  561. break;
  562. case C_BACKSLASH:
  563. res = this.parseBackslash(inlines);
  564. break;
  565. case C_BACKTICK:
  566. res = this.parseBackticks(inlines);
  567. break;
  568. case C_ASTERISK:
  569. case C_UNDERSCORE:
  570. res = this.parseEmphasis(c, inlines);
  571. break;
  572. case C_OPEN_BRACKET:
  573. res = this.parseLink(inlines);
  574. break;
  575. case C_BANG:
  576. res = this.parseImage(inlines);
  577. break;
  578. case C_LESSTHAN:
  579. res = this.parseAutolink(inlines) || this.parseHtmlTag(inlines);
  580. break;
  581. case C_AMPERSAND:
  582. res = this.parseEntity(inlines);
  583. break;
  584. default:
  585. res = this.parseString(inlines);
  586. break;
  587. }
  588. if (!res) {
  589. this.pos += 1;
  590. inlines.push({t: 'Str', c: fromCodePoint(c)});
  591. }
  592. return true;
  593. };
  594. // Parse s as a list of inlines, using refmap to resolve references.
  595. var parseInlines = function(s, refmap) {
  596. this.subject = s;
  597. this.pos = 0;
  598. this.refmap = refmap || {};
  599. this.emphasis_openers = null;
  600. var inlines = [];
  601. while (this.parseInline(inlines)) {
  602. }
  603. return inlines;
  604. };
  605. // The InlineParser object.
  606. function InlineParser(){
  607. return {
  608. subject: '',
  609. label_nest_level: 0, // used by parseLinkLabel method
  610. emphasis_openers: null, // used by parseEmphasis method
  611. pos: 0,
  612. refmap: {},
  613. match: match,
  614. peek: peek,
  615. spnl: spnl,
  616. unescapeString: unescapeString,
  617. parseBackticks: parseBackticks,
  618. parseBackslash: parseBackslash,
  619. parseAutolink: parseAutolink,
  620. parseHtmlTag: parseHtmlTag,
  621. scanDelims: scanDelims,
  622. parseEmphasis: parseEmphasis,
  623. parseLinkTitle: parseLinkTitle,
  624. parseLinkDestination: parseLinkDestination,
  625. parseLinkLabel: parseLinkLabel,
  626. parseLink: parseLink,
  627. parseEntity: parseEntity,
  628. parseString: parseString,
  629. parseNewline: parseNewline,
  630. parseImage: parseImage,
  631. parseReference: parseReference,
  632. parseInline: parseInline,
  633. parse: parseInlines
  634. };
  635. }
  636. module.exports = InlineParser;