From b5768bced10c6dafe8bc621c6ef01aa0bd9d975c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 7 Jul 2016 20:25:41 +0100 Subject: Store children in thunks when generating HTML nodes --- lib/html-paths.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'lib/html-paths.js') diff --git a/lib/html-paths.js b/lib/html-paths.js index 04bc655..4021887 100644 --- a/lib/html-paths.js +++ b/lib/html-paths.js @@ -1,5 +1,7 @@ var _ = require("underscore"); +var html = require("./html"); + exports.topLevelElement = topLevelElement; exports.elements = elements; exports.element = element; @@ -9,15 +11,27 @@ function topLevelElement(tagName, attributes) { } function elements(elementStyles) { - return elementStyles.map(function(elementStyle) { + 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); @@ -44,4 +58,12 @@ 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([]); -- cgit v1.2.3 From baf9808eae3441de70a00cd043e10e0ebf06b7c0 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 7 Jul 2016 20:44:20 +0100 Subject: Add HTML path for ignoring elements --- lib/html-paths.js | 5 +++++ lib/style-reader.js | 28 +++++++++++++++++----------- test/mammoth.tests.js | 6 +++--- test/style-reader.tests.js | 4 ++++ 4 files changed, 29 insertions(+), 14 deletions(-) (limited to 'lib/html-paths.js') diff --git a/lib/html-paths.js b/lib/html-paths.js index 4021887..5a00cd8 100644 --- a/lib/html-paths.js +++ b/lib/html-paths.js @@ -67,3 +67,8 @@ Element.prototype.wrapNodes = function wrapNodes(nodes) { }; exports.empty = elements([]); +exports.ignore = { + wrap: function() { + return []; + } +}; diff --git a/lib/style-reader.js b/lib/style-reader.js index ab9a171..fe60930 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -177,16 +177,21 @@ function htmlPathRule() { return htmlPaths.element(tagName, attributes, options); }); - return lop.rules.then( - lop.rules.zeroOrMoreWithSeparator( - styleElementRule, - lop.rules.sequence( - whitespaceRule, - lop.rules.tokenOfType("gt"), - whitespaceRule - ) - ), - htmlPaths.elements + return lop.rules.firstOf("html path", + lop.rules.then(lop.rules.tokenOfType("bang"), function() { + return htmlPaths.ignore; + }), + lop.rules.then( + lop.rules.zeroOrMoreWithSeparator( + styleElementRule, + lop.rules.sequence( + whitespaceRule, + lop.rules.tokenOfType("gt"), + whitespaceRule + ) + ), + htmlPaths.elements + ) ); } @@ -234,7 +239,8 @@ function tokenise(string) { {name: "close-square-bracket", regex: /\]/}, {name: "string", regex: /'([^']*)'/}, {name: "integer", regex: /([0-9]+)/}, - {name: "choice", regex: /\|/} + {name: "choice", regex: /\|/}, + {name: "bang", regex: /(!)/} ]); return tokeniser.tokenise(string); } diff --git a/test/mammoth.tests.js b/test/mammoth.tests.js index 1a82014..12bee96 100644 --- a/test/mammoth.tests.js +++ b/test/mammoth.tests.js @@ -114,12 +114,12 @@ describe('mammoth', function() { test('warning if style mapping is not understood', function() { var docxPath = path.join(__dirname, "test-data/single-paragraph.docx"); var options = { - styleMap: "!!!!\np => h1" + styleMap: "????\np => h1" }; return mammoth.convertToHtml({path: docxPath}, options).then(function(result) { assert.equal("

Walking on imported air

", result.value); - var warning = "Did not understand this style mapping, so ignored it: !!!!\n" + - 'Error was at character number 1: Expected element type but got unrecognisedCharacter "!"'; + var warning = "Did not understand this style mapping, so ignored it: ????\n" + + 'Error was at character number 1: Expected element type but got unrecognisedCharacter "?"'; assert.deepEqual(result.messages, [results.warning(warning)]); }); }); diff --git a/test/style-reader.tests.js b/test/style-reader.tests.js index 7265e8f..46a9fa1 100644 --- a/test/style-reader.tests.js +++ b/test/style-reader.tests.js @@ -54,6 +54,10 @@ describe('styleReader.readHtmlPath', function() { assertHtmlPath("p:fresh", expected); }); + test('reads ignore element', function() { + assertHtmlPath("!", htmlPaths.ignore); + }); + function assertHtmlPath(input, expected) { assert.deepEqual(readHtmlPath(input), results.success(expected)); } -- cgit v1.2.3