From 2ec58f7fa223f250843eaa5f21d6a938617cc612 Mon Sep 17 00:00:00 2001 From: Stephan Hradek Date: Sun, 1 Dec 2013 21:21:15 +0100 Subject: [PATCH] added quote rules --- .../modules/parsers/wikiparser/rules/quote.js | 98 +++++++++++++++++++ .../parsers/wikiparser/rules/quoteblock.js | 96 ++++++++++++++++++ 2 files changed, 194 insertions(+) create mode 100644 core/modules/parsers/wikiparser/rules/quote.js create mode 100644 core/modules/parsers/wikiparser/rules/quoteblock.js diff --git a/core/modules/parsers/wikiparser/rules/quote.js b/core/modules/parsers/wikiparser/rules/quote.js new file mode 100644 index 000000000..393259a78 --- /dev/null +++ b/core/modules/parsers/wikiparser/rules/quote.js @@ -0,0 +1,98 @@ +/*\ +title: $:/core/modules/parsers/wikiparser/rules/quote.js +type: application/javascript +module-type: wikirule + +Wiki text block rule for quotes. For example: + +``` +> This is aquote +> It has two paragraphs + +> This is another quite +>> This is a quoted quote +> And another paragraph +``` + +A CSS class can be applied to a quote item as follows: + +``` +> First quoted paragraph +>.active Second prargraph has the class `active` +> Third quoted paragraph +``` + +\*/ +(function(){ + +/*jslint node: true, browser: true */ +/*global $tw: false */ +"use strict"; + +exports.name = "quote"; +exports.types = {block: true}; + +exports.init = function(parser) { + this.parser = parser; + // Regexp to match + this.matchRegExp = /(>+)/mg; +}; + +var quoteInfo = {quoteTag: "blockquote", paraTag: "p", attributes: { class: { type: "string", value:"tw-quote" }}}; + +/* +Parse the most recent match +*/ +exports.parse = function() { + // Array of parse tree nodes for the previous row of the quote + var quoteStack = []; + // Cycle through the items in the quote + while(true) { + // Match the list marker + var reMatch = /(>+)/mg; + reMatch.lastIndex = this.parser.pos; + var match = reMatch.exec(this.parser.source); + if(!match || match.index !== this.parser.pos) { + break; + } + // Move past the quote marker + this.parser.pos = match.index + match[0].length; + // Walk through the quote markers for the current row + for(var t=0; t match[0].length) { + quoteStack.splice(match[0].length,quoteStack.length - match[0].length); + } + // Process the body of the list item into the last list item + var lastQuoteChildren = quoteStack[quoteStack.length-1].children, + lastQuoteItem = lastQuoteChildren[lastQuoteChildren.length-1], + classes = this.parser.parseClasses(); + this.parser.skipWhitespace({treatNewlinesAsNonWhitespace: true}); + var tree = this.parser.parseInlineRun(/(\r?\n)/mg); + lastQuoteItem.children.push.apply(lastQuoteItem.children,tree); + if(classes.length > 0) { + $tw.utils.addClassToParseTreeNode(lastQuoteItem,classes.join(" ")); + } + // Consume any whitespace following the list item + this.parser.skipWhitespace(); + } + // Return the root element of the list + return [quoteStack[0]]; +}; + +})(); diff --git a/core/modules/parsers/wikiparser/rules/quoteblock.js b/core/modules/parsers/wikiparser/rules/quoteblock.js new file mode 100644 index 000000000..b3a0c8ae6 --- /dev/null +++ b/core/modules/parsers/wikiparser/rules/quoteblock.js @@ -0,0 +1,96 @@ +/*\ +title: $:/core/modules/parsers/wikiparser/rules/quoteblock.js +type: application/javascript +module-type: wikirule + +Wiki text rule for quote blocks. For example: + +``` + <<<.optionalClass(es) optional cited from + a quote + <<< + + <<<.optionalClass(es) + a quote + <<< optional cited from +``` + +Quotes can be quoted by putting more 0 ) { + tree.unshift({ + type: "element", + tag: "cite", + children: cite + }); + } + + // Move past the 0 ) { + tree.push({ + type: "element", + tag: "cite", + children: cite + }); + } + + // Return the blockquote element + return [{ + type: "element", + tag: "blockquote", + attributes: { + class: { type: "string", value: classes.join(" ") }, + }, + children: tree + }]; +}; + +})();