diff options
-rw-r--r-- | js/lib/blocks.js | 58 | ||||
-rw-r--r-- | js/lib/inlines.js | 32 |
2 files changed, 67 insertions, 23 deletions
diff --git a/js/lib/blocks.js b/js/lib/blocks.js index cc2263a..1e20d29 100644 --- a/js/lib/blocks.js +++ b/js/lib/blocks.js @@ -1,4 +1,4 @@ -Node = require('./node'); +var Node = require('./node'); var C_GREATERTHAN = 62; var C_SPACE = 32; @@ -9,11 +9,13 @@ var unescapeString = new InlineParser().unescapeString; // Returns true if string contains only space characters. var isBlank = function(s) { + "use strict"; return /^\s*$/.test(s); }; // Convert tabs to spaces on each line using a 4-space tab stop. var detabLine = function(text) { + "use strict"; if (text.indexOf('\t') === -1) { return text; } else { @@ -29,6 +31,7 @@ var detabLine = function(text) { // Attempt to match a regex in string s at offset offset. // Return index of match or -1. var matchAt = function(re, s, offset) { + "use strict"; var res = s.slice(offset).match(re); if (res) { return offset + res.index; @@ -50,18 +53,20 @@ var reHrule = /^(?:(?:\* *){3,}|(?:_ *){3,}|(?:- *){3,}) *$/; // These are methods of a DocParser object, defined below. var makeBlock = function(tag, start_line, start_column) { - var b = new Node(tag); - b.pos.start = [start_line, start_column]; - b.pos.end = []; // assigned in finalization step - b.open = true; - b.last_line_blank = false; - b.string_content = ""; - b.strings = []; - return b; + "use strict"; + var b = new Node(tag); + b.pos.start = [start_line, start_column]; + b.pos.end = []; // assigned in finalization step + b.open = true; + b.last_line_blank = false; + b.string_content = ""; + b.strings = []; + return b; }; // Returns true if parent block can contain child block. var canContain = function(parent_type, child_type) { + "use strict"; return ( parent_type === 'Document' || parent_type === 'BlockQuote' || parent_type === 'ListItem' || @@ -70,6 +75,7 @@ var canContain = function(parent_type, child_type) { // Returns true if block type can accept lines of text. var acceptsLines = function(block_type) { + "use strict"; return ( block_type === 'Paragraph' || block_type === 'IndentedCode' || block_type === 'FencedCode' ); @@ -78,15 +84,16 @@ var acceptsLines = function(block_type) { // Returns true if block ends with a blank line, descending if needed // into lists and sublists. var endsWithBlankLine = function(block) { + "use strict"; while (block) { - if (block.last_line_blank) { - return true; - } - if (block.t === 'List' || block.t === 'ListItem') { - block = block.lastChild; - } else { - break; - } + if (block.last_line_blank) { + return true; + } + if (block.t === 'List' || block.t === 'ListItem') { + block = block.lastChild; + } else { + break; + } } return false; }; @@ -96,6 +103,7 @@ var endsWithBlankLine = function(block) { // all the lists. (This is used to implement the "two blank lines // break of of all lists" feature.) var breakOutOfLists = function(block, line_number) { + "use strict"; var b = block; var last_list = null; do { @@ -118,6 +126,7 @@ var breakOutOfLists = function(block, line_number) { // Add a line to the block at the tip. We assume the tip // can accept lines -- that check should be done before calling this. var addLine = function(ln, offset) { + "use strict"; var s = ln.slice(offset); if (!(this.tip.open)) { throw { msg: "Attempted to add line (" + ln + ") to closed container." }; @@ -129,6 +138,7 @@ var addLine = function(ln, offset) { // accept children, close and finalize it and try its parent, // and so on til we find a block that can accept children. var addChild = function(tag, line_number, offset) { + "use strict"; while (!canContain(this.tip.t, tag)) { this.finalize(this.tip, line_number); } @@ -143,6 +153,7 @@ var addChild = function(tag, line_number, offset) { // Parse a list marker and return data on the marker (type, // start, delimiter, bullet character, padding) or null. var parseListMarker = function(ln, offset) { + "use strict"; var rest = ln.slice(offset); var match; var spaces_after_marker; @@ -178,6 +189,7 @@ var parseListMarker = function(ln, offset) { // with the same delimiter and bullet character. This is used // in agglomerating list items into lists. var listsMatch = function(list_data, item_data) { + "use strict"; return (list_data.type === item_data.type && list_data.delimiter === item_data.delimiter && list_data.bullet_char === item_data.bullet_char); @@ -187,6 +199,7 @@ var listsMatch = function(list_data, item_data) { // We parse markdown text by calling this on each line of input, // then finalizing the document. var incorporateLine = function(ln, line_number) { + "use strict"; var all_matched = true; var first_nonspace; @@ -528,6 +541,7 @@ var incorporateLine = function(ln, line_number) { // of paragraphs for reference definitions. Reset the tip to the // parent of the closed block. var finalize = function(block, line_number) { + "use strict"; var pos; // don't do anything if the block is already closed if (!block.open) { @@ -612,18 +626,21 @@ var finalize = function(block, line_number) { // Walk through a block & children recursively, parsing string content // into inline content where appropriate. Returns new object. var processInlines = function(block) { + "use strict"; var node, event; var walker = block.walker(); - while (event = walker.next()) { + while ((event = walker.next())) { node = event.node; - if (!event.entering && (node.t == 'Paragraph' || node.t == 'Header')) { - this.inlineParser.parse(node, this.refmap); + if (!event.entering && (node.t === 'Paragraph' || + node.t === 'Header')) { + this.inlineParser.parse(node, this.refmap); } } }; // The main parsing function. Returns a parsed document AST. var parse = function(input) { + "use strict"; this.doc = makeBlock('Document', 1, 1); this.tip = this.doc; this.refmap = {}; @@ -642,6 +659,7 @@ var parse = function(input) { // The DocParser object. function DocParser(){ + "use strict"; return { doc: makeBlock('Document', 1, 1), tip: this.doc, diff --git a/js/lib/inlines.js b/js/lib/inlines.js index 23b2b1e..5cec93b 100644 --- a/js/lib/inlines.js +++ b/js/lib/inlines.js @@ -1,3 +1,5 @@ +var Node = require('./node'); + var fromCodePoint = require('./from-code-point.js'); var entityToChar = require('./html5-entities.js').entityToChar; @@ -73,6 +75,7 @@ var reMain = /^(?:[_*`\n]+|[\[\]\\!<&*_]|(?: *[^\n `\[\]\\!<&*_]+)+|[ \n]+)/m; // Replace entities and backslash escapes with literal characters. var unescapeString = function(s) { + "use strict"; return s.replace(reAllEscapedChar, '$1') .replace(reEntity, entityToChar); }; @@ -80,16 +83,18 @@ var unescapeString = function(s) { // Normalize reference label: collapse internal whitespace // to single space, remove leading/trailing whitespace, case fold. var normalizeReference = function(s) { + "use strict"; return s.trim() .replace(/\s+/, ' ') .toUpperCase(); }; var text = function(s) { + "use strict"; var node = new Node('Text'); node.c = s; return node; -} +}; // INLINE PARSER @@ -100,6 +105,7 @@ var text = function(s) { // If re matches at current position in the subject, advance // position in subject and return the match; otherwise return null. var match = function(re) { + "use strict"; var m = re.exec(this.subject.slice(this.pos)); if (m) { this.pos += m.index + m[0].length; @@ -112,6 +118,7 @@ var match = function(re) { // Returns the code for the character at the current subject position, or -1 // there are no more characters. var peek = function() { + "use strict"; if (this.pos < this.subject.length) { return this.subject.charCodeAt(this.pos); } else { @@ -121,6 +128,7 @@ var peek = function() { // Parse zero or more space characters, including at most one newline var spnl = function() { + "use strict"; this.match(/^ *(?:\n *)?/); return 1; }; @@ -132,6 +140,7 @@ var spnl = function() { // Attempt to parse backticks, adding either a backtick code span or a // literal sequence of backticks. var parseBackticks = function(block) { + "use strict"; var ticks = this.match(/^`+/); if (!ticks) { return 0; @@ -161,6 +170,7 @@ var parseBackticks = function(block) { // character, a hard line break (if the backslash is followed by a newline), // or a literal backslash to the block's children. var parseBackslash = function(block) { + "use strict"; var subj = this.subject, pos = this.pos; var node; @@ -184,6 +194,7 @@ var parseBackslash = function(block) { // Attempt to parse an autolink (URL or email in pointy brackets). var parseAutolink = function(block) { + "use strict"; var m; var dest; var node; @@ -208,6 +219,7 @@ var parseAutolink = function(block) { // Attempt to parse a raw HTML tag. var parseHtmlTag = function(block) { + "use strict"; var m = this.match(reHtmlTag); var node; if (m) { @@ -225,6 +237,7 @@ var parseHtmlTag = function(block) { // they can open and/or close emphasis or strong emphasis. A utility // function for strong/emph parsing. var scanDelims = function(cc) { + "use strict"; var numdelims = 0; var char_before, char_after, cc_after; var startpos = this.pos; @@ -264,6 +277,7 @@ var scanDelims = function(cc) { // Attempt to parse emphasis or strong emphasis. var parseEmphasis = function(cc, block) { + "use strict"; var res = this.scanDelims(cc); var numdelims = res.numdelims; @@ -295,6 +309,7 @@ var parseEmphasis = function(cc, block) { }; var removeDelimiter = function(delim) { + "use strict"; if (delim.previous !== null) { delim.previous.next = delim.next; } @@ -312,7 +327,6 @@ var processEmphasis = function(block, stack_bottom) { var opener_inl, closer_inl; var nextstack, tempstack; var use_delims; - var contents; var tmp, next; // find first closer above stack_bottom: @@ -402,6 +416,7 @@ var processEmphasis = function(block, stack_bottom) { // Attempt to parse link title (sans quotes), returning the string // or null if no match. var parseLinkTitle = function() { + "use strict"; var title = this.match(reLinkTitle); if (title) { // chop off quotes from title and unescape: @@ -414,6 +429,7 @@ var parseLinkTitle = function() { // Attempt to parse link destination, returning the string or // null if no match. var parseLinkDestination = function() { + "use strict"; var res = this.match(reLinkDestinationBraces); if (res) { // chop off surrounding <..>: return encodeURI(unescape(unescapeString(res.substr(1, res.length - 2)))); @@ -429,12 +445,14 @@ var parseLinkDestination = function() { // Attempt to parse a link label, returning number of characters parsed. var parseLinkLabel = function() { + "use strict"; var m = this.match(/^\[(?:[^\\\[\]]|\\[\[\]]){0,1000}\]/); return m === null ? 0 : m.length; }; // Add open bracket to delimiter stack and add a text node to block's children. var parseOpenBracket = function(block) { + "use strict"; var startpos = this.pos; this.pos += 1; @@ -463,6 +481,7 @@ var parseOpenBracket = function(block) { // IF next character is [, and ! delimiter to delimiter stack and // add a text node to block's children. Otherwise just add a text node. var parseBang = function(block) { + "use strict"; var startpos = this.pos; this.pos += 1; @@ -496,12 +515,13 @@ var parseBang = function(block) { // to block's children. If there is a matching delimiter, // remove it from the delimiter stack. var parseCloseBracket = function(block) { + "use strict"; + var startpos; var is_image; var dest; var title; var matched = false; - var i; var reflabel; var opener; @@ -622,6 +642,8 @@ var parseCloseBracket = function(block) { // Attempt to parse an entity, return Entity object if successful. var parseEntity = function(block) { + "use strict"; + var m; if ((m = this.match(reEntityHere))) { block.appendChild(text(entityToChar(m))); @@ -634,6 +656,7 @@ var parseEntity = function(block) { // Parse a run of ordinary characters, or a single character with // a special meaning in markdown, as a plain string. var parseString = function(block) { + "use strict"; var m; if ((m = this.match(reMain))) { block.appendChild(text(m)); @@ -646,6 +669,7 @@ var parseString = function(block) { // Parse a newline. If it was preceded by two spaces, return a hard // line break; otherwise a soft line break. var parseNewline = function(block) { + "use strict"; var m = this.match(/^ *\n/); if (m) { var node = new Node(m.length > 2 ? 'Hardbreak' : 'Softbreak'); @@ -658,6 +682,7 @@ var parseNewline = function(block) { // Attempt to parse a link reference, modifying refmap. var parseReference = function(s, refmap) { + "use strict"; this.subject = s; this.pos = 0; var rawlabel; @@ -771,6 +796,7 @@ var parseInline = function(block) { // Parse string_content in block into inline children, // using refmap to resolve references. var parseInlines = function(block, refmap) { + "use strict"; this.subject = block.string_content.trim(); this.pos = 0; this.refmap = refmap || {}; |