From 68d8c8140fb6ef1118fa2815243817f8e4f55b59 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 19 Nov 2016 11:54:43 +0000 Subject: Move html-paths and document-matchers to styles module --- lib/document-matchers.js | 40 ---------------------- lib/document-to-html.js | 2 +- lib/html-paths.js | 75 ----------------------------------------- lib/html/ast.js | 2 +- lib/style-reader.js | 4 +-- lib/styles/document-matchers.js | 40 ++++++++++++++++++++++ lib/styles/html-paths.js | 75 +++++++++++++++++++++++++++++++++++++++++ lib/underline.js | 2 +- 8 files changed, 120 insertions(+), 120 deletions(-) delete mode 100644 lib/document-matchers.js delete mode 100644 lib/html-paths.js create mode 100644 lib/styles/document-matchers.js create mode 100644 lib/styles/html-paths.js (limited to 'lib') diff --git a/lib/document-matchers.js b/lib/document-matchers.js deleted file mode 100644 index bdc213c..0000000 --- a/lib/document-matchers.js +++ /dev/null @@ -1,40 +0,0 @@ -exports.paragraph = paragraph; -exports.run = run; -exports.bold = new Matcher("bold"); -exports.italic = new Matcher("italic"); -exports.underline = new Matcher("underline"); -exports.strikethrough = new Matcher("strikethrough"); -exports.commentReference = new Matcher("commentReference"); - - -function paragraph(options) { - return new Matcher("paragraph", options); -} - -function run(options) { - return new Matcher("run", options); -} - -function Matcher(elementType, options) { - options = options || {}; - this._elementType = elementType; - this._styleId = options.styleId; - this._styleName = options.styleName; - if (options.list) { - this._listIndex = options.list.levelIndex; - this._listIsOrdered = options.list.isOrdered; - } -} - -Matcher.prototype.matches = function(element) { - return element.type === this._elementType && - (this._styleId === undefined || element.styleId === this._styleId) && - (this._styleName === undefined || (element.styleName && element.styleName.toUpperCase() === this._styleName.toUpperCase())) && - (this._listIndex === undefined || isList(element, this._listIndex, this._listIsOrdered)); -}; - -function isList(element, levelIndex, isOrdered) { - return element.numbering && - element.numbering.level == levelIndex && - element.numbering.isOrdered == isOrdered; -} diff --git a/lib/document-to-html.js b/lib/document-to-html.js index 6094803..61797a1 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -2,7 +2,7 @@ var _ = require("underscore"); var promises = require("./promises"); var documents = require("./documents"); -var htmlPaths = require("./html-paths"); +var htmlPaths = require("./styles/html-paths"); var results = require("./results"); var images = require("./images"); var Html = require("./html"); diff --git a/lib/html-paths.js b/lib/html-paths.js deleted file mode 100644 index 8906329..0000000 --- a/lib/html-paths.js +++ /dev/null @@ -1,75 +0,0 @@ -var _ = require("underscore"); - -var html = require("./html"); - -exports.topLevelElement = topLevelElement; -exports.elements = elements; -exports.element = element; - -function topLevelElement(tagName, attributes) { - return elements([element(tagName, attributes, {fresh: true})]); -} - -function elements(elementStyles) { - return new HtmlPath(elementStyles.map(function(elementStyle) { - if (_.isString(elementStyle)) { - return element(elementStyle); - } else { - return elementStyle; - } - })); -} - -function HtmlPath(elements) { - this._elements = elements; -} - -HtmlPath.prototype.wrap = function wrap(children) { - var result = children(); - for (var index = this._elements.length - 1; index >= 0; index--) { - result = this._elements[index].wrapNodes(result); - } - return result; -}; - -function element(tagName, attributes, options) { - options = options || {}; - return new Element(tagName, attributes, options); -} - -function Element(tagName, attributes, options) { - var tagNames = {}; - if (_.isArray(tagName)) { - tagName.forEach(function(tagName) { - tagNames[tagName] = true; - }); - tagName = tagName[0]; - } else { - tagNames[tagName] = true; - } - - this.tagName = tagName; - this.tagNames = tagNames; - this.attributes = attributes || {}; - this.fresh = options.fresh; - this.separator = options.separator; -} - -Element.prototype.matchesElement = function(element) { - return this.tagNames[element.tagName] && _.isEqual(this.attributes || {}, element.attributes || {}); -}; - -Element.prototype.wrap = function wrap(generateNodes) { - return this.wrapNodes(generateNodes()); -}; - -Element.prototype.wrapNodes = function wrapNodes(nodes) { - return [html.elementWithTag(this, nodes)]; -}; - -exports.empty = elements([]); -exports.ignore = { - wrap: function() { - return []; - } -}; diff --git a/lib/html/ast.js b/lib/html/ast.js index db9e690..0dd8120 100644 --- a/lib/html/ast.js +++ b/lib/html/ast.js @@ -1,4 +1,4 @@ -var htmlPaths = require("../html-paths"); +var htmlPaths = require("../styles/html-paths"); function nonFreshElement(tagName, attributes, children) { diff --git a/lib/style-reader.js b/lib/style-reader.js index ba453bd..2889554 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -1,8 +1,8 @@ var _ = require("underscore"); var lop = require("lop"); -var documentMatchers = require("./document-matchers"); -var htmlPaths = require("./html-paths"); +var documentMatchers = require("./styles/document-matchers"); +var htmlPaths = require("./styles/html-paths"); var tokenise = require("./styles/parser/tokeniser").tokenise; var results = require("./results"); diff --git a/lib/styles/document-matchers.js b/lib/styles/document-matchers.js new file mode 100644 index 0000000..bdc213c --- /dev/null +++ b/lib/styles/document-matchers.js @@ -0,0 +1,40 @@ +exports.paragraph = paragraph; +exports.run = run; +exports.bold = new Matcher("bold"); +exports.italic = new Matcher("italic"); +exports.underline = new Matcher("underline"); +exports.strikethrough = new Matcher("strikethrough"); +exports.commentReference = new Matcher("commentReference"); + + +function paragraph(options) { + return new Matcher("paragraph", options); +} + +function run(options) { + return new Matcher("run", options); +} + +function Matcher(elementType, options) { + options = options || {}; + this._elementType = elementType; + this._styleId = options.styleId; + this._styleName = options.styleName; + if (options.list) { + this._listIndex = options.list.levelIndex; + this._listIsOrdered = options.list.isOrdered; + } +} + +Matcher.prototype.matches = function(element) { + return element.type === this._elementType && + (this._styleId === undefined || element.styleId === this._styleId) && + (this._styleName === undefined || (element.styleName && element.styleName.toUpperCase() === this._styleName.toUpperCase())) && + (this._listIndex === undefined || isList(element, this._listIndex, this._listIsOrdered)); +}; + +function isList(element, levelIndex, isOrdered) { + return element.numbering && + element.numbering.level == levelIndex && + element.numbering.isOrdered == isOrdered; +} diff --git a/lib/styles/html-paths.js b/lib/styles/html-paths.js new file mode 100644 index 0000000..27bb777 --- /dev/null +++ b/lib/styles/html-paths.js @@ -0,0 +1,75 @@ +var _ = require("underscore"); + +var html = require("../html"); + +exports.topLevelElement = topLevelElement; +exports.elements = elements; +exports.element = element; + +function topLevelElement(tagName, attributes) { + return elements([element(tagName, attributes, {fresh: true})]); +} + +function elements(elementStyles) { + return new HtmlPath(elementStyles.map(function(elementStyle) { + if (_.isString(elementStyle)) { + return element(elementStyle); + } else { + return elementStyle; + } + })); +} + +function HtmlPath(elements) { + this._elements = elements; +} + +HtmlPath.prototype.wrap = function wrap(children) { + var result = children(); + for (var index = this._elements.length - 1; index >= 0; index--) { + result = this._elements[index].wrapNodes(result); + } + return result; +}; + +function element(tagName, attributes, options) { + options = options || {}; + return new Element(tagName, attributes, options); +} + +function Element(tagName, attributes, options) { + var tagNames = {}; + if (_.isArray(tagName)) { + tagName.forEach(function(tagName) { + tagNames[tagName] = true; + }); + tagName = tagName[0]; + } else { + tagNames[tagName] = true; + } + + this.tagName = tagName; + this.tagNames = tagNames; + this.attributes = attributes || {}; + this.fresh = options.fresh; + this.separator = options.separator; +} + +Element.prototype.matchesElement = function(element) { + return this.tagNames[element.tagName] && _.isEqual(this.attributes || {}, element.attributes || {}); +}; + +Element.prototype.wrap = function wrap(generateNodes) { + return this.wrapNodes(generateNodes()); +}; + +Element.prototype.wrapNodes = function wrapNodes(nodes) { + return [html.elementWithTag(this, nodes)]; +}; + +exports.empty = elements([]); +exports.ignore = { + wrap: function() { + return []; + } +}; diff --git a/lib/underline.js b/lib/underline.js index fd5dc0f..3c59d0d 100644 --- a/lib/underline.js +++ b/lib/underline.js @@ -1,4 +1,4 @@ -var htmlPaths = require("./html-paths"); +var htmlPaths = require("./styles/html-paths"); var Html = require("./html"); -- cgit v1.2.3