diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-11-19 11:54:43 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-11-19 11:54:43 +0000 |
| commit | 68d8c8140fb6ef1118fa2815243817f8e4f55b59 (patch) | |
| tree | d4e4be8d3b124d98f2d204ed5f97585a8729ca94 /lib/styles | |
| parent | 5f84e7887b262b96fd6fd47ce6bb1cd96c196f69 (diff) | |
Move html-paths and document-matchers to styles module
Diffstat (limited to 'lib/styles')
| -rw-r--r-- | lib/styles/document-matchers.js | 40 | ||||
| -rw-r--r-- | lib/styles/html-paths.js | 75 |
2 files changed, 115 insertions, 0 deletions
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 []; + } +}; |
