@depup/mammoth 1.12.0-depup.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/.eslintrc.json +75 -0
  2. package/.github/ISSUE_TEMPLATE.md +12 -0
  3. package/.github/pull_request_template.md +3 -0
  4. package/.github/workflows/tests.yml +26 -0
  5. package/LICENSE +22 -0
  6. package/NEWS +530 -0
  7. package/README.md +37 -0
  8. package/bin/mammoth +38 -0
  9. package/browser/docx/files.js +14 -0
  10. package/browser/unzip.js +12 -0
  11. package/changes.json +34 -0
  12. package/lib/document-to-html.js +469 -0
  13. package/lib/documents.js +254 -0
  14. package/lib/docx/body-reader.js +798 -0
  15. package/lib/docx/comments-reader.js +31 -0
  16. package/lib/docx/content-types-reader.js +58 -0
  17. package/lib/docx/document-xml-reader.js +30 -0
  18. package/lib/docx/docx-reader.js +226 -0
  19. package/lib/docx/files.js +80 -0
  20. package/lib/docx/notes-reader.js +28 -0
  21. package/lib/docx/numbering-xml.js +111 -0
  22. package/lib/docx/office-xml-reader.js +73 -0
  23. package/lib/docx/relationships-reader.js +43 -0
  24. package/lib/docx/style-map.js +75 -0
  25. package/lib/docx/styles-reader.js +90 -0
  26. package/lib/docx/uris.js +21 -0
  27. package/lib/html/ast.js +51 -0
  28. package/lib/html/index.js +41 -0
  29. package/lib/html/simplify.js +88 -0
  30. package/lib/images.js +31 -0
  31. package/lib/index.d.ts +86 -0
  32. package/lib/index.js +111 -0
  33. package/lib/main.js +63 -0
  34. package/lib/options-reader.js +107 -0
  35. package/lib/promises.js +42 -0
  36. package/lib/raw-text.js +14 -0
  37. package/lib/results.js +72 -0
  38. package/lib/style-reader.js +365 -0
  39. package/lib/styles/document-matchers.js +100 -0
  40. package/lib/styles/html-paths.js +75 -0
  41. package/lib/styles/parser/tokeniser.js +30 -0
  42. package/lib/transforms.js +62 -0
  43. package/lib/underline.js +11 -0
  44. package/lib/unzip.js +20 -0
  45. package/lib/writers/html-writer.js +159 -0
  46. package/lib/writers/index.js +14 -0
  47. package/lib/writers/markdown-writer.js +163 -0
  48. package/lib/xml/index.js +8 -0
  49. package/lib/xml/nodes.js +70 -0
  50. package/lib/xml/reader.js +69 -0
  51. package/lib/xml/writer.js +61 -0
  52. package/lib/xml/xmldom.js +23 -0
  53. package/lib/zipfile.js +72 -0
  54. package/mammoth.browser.js +22096 -0
  55. package/mammoth.browser.min.js +20 -0
  56. package/package.json +106 -0
  57. package/test/.eslintrc.json +7 -0
  58. package/test/document-to-html.tests.js +892 -0
  59. package/test/docx/body-reader.tests.js +2037 -0
  60. package/test/docx/comments-reader.tests.js +53 -0
  61. package/test/docx/content-types-reader.tests.js +45 -0
  62. package/test/docx/document-matchers.js +42 -0
  63. package/test/docx/document-xml-reader.tests.js +41 -0
  64. package/test/docx/docx-reader.tests.js +179 -0
  65. package/test/docx/files.tests.js +107 -0
  66. package/test/docx/notes-reader.tests.js +36 -0
  67. package/test/docx/numbering-xml.tests.js +183 -0
  68. package/test/docx/office-xml-reader.tests.js +41 -0
  69. package/test/docx/relationships-reader.tests.js +65 -0
  70. package/test/docx/style-map.tests.js +113 -0
  71. package/test/docx/styles-reader.tests.js +143 -0
  72. package/test/docx/testing.js +12 -0
  73. package/test/docx/uris.tests.js +22 -0
  74. package/test/html/simplify.tests.js +141 -0
  75. package/test/html/write.tests.js +42 -0
  76. package/test/images.tests.js +112 -0
  77. package/test/main.tests.js +89 -0
  78. package/test/mammoth.tests.js +523 -0
  79. package/test/mocha.opts +1 -0
  80. package/test/options-reader.tests.js +63 -0
  81. package/test/raw-text.tests.js +61 -0
  82. package/test/results.tests.js +15 -0
  83. package/test/style-reader.tests.js +291 -0
  84. package/test/styles/document-matchers.tests.js +91 -0
  85. package/test/styles/html-paths.tests.js +20 -0
  86. package/test/styles/parser/tokeniser.tests.js +104 -0
  87. package/test/test-data/comments.docx +0 -0
  88. package/test/test-data/embedded-style-map.docx +0 -0
  89. package/test/test-data/empty.docx +0 -0
  90. package/test/test-data/empty.zip +0 -0
  91. package/test/test-data/endnotes.docx +0 -0
  92. package/test/test-data/external-picture.docx +0 -0
  93. package/test/test-data/footnote-hyperlink.docx +0 -0
  94. package/test/test-data/footnotes.docx +0 -0
  95. package/test/test-data/hello.zip +0 -0
  96. package/test/test-data/hyperlinks/word/_rels/document.xml.rels +10 -0
  97. package/test/test-data/hyperlinks/word/document.xml +18 -0
  98. package/test/test-data/simple/word/document.xml +18 -0
  99. package/test/test-data/simple-list.docx +0 -0
  100. package/test/test-data/single-paragraph.docx +0 -0
  101. package/test/test-data/strict-format.docx +0 -0
  102. package/test/test-data/strikethrough.docx +0 -0
  103. package/test/test-data/tables.docx +0 -0
  104. package/test/test-data/text-box.docx +0 -0
  105. package/test/test-data/tiny-picture-target-base-relative.docx +0 -0
  106. package/test/test-data/tiny-picture.docx +0 -0
  107. package/test/test-data/tiny-picture.png +0 -0
  108. package/test/test-data/underline.docx +0 -0
  109. package/test/test-data/utf8-bom.docx +0 -0
  110. package/test/test.js +11 -0
  111. package/test/testing.js +55 -0
  112. package/test/transforms.tests.js +125 -0
  113. package/test/unzip.tests.js +38 -0
  114. package/test/writers/html-writer.tests.js +133 -0
  115. package/test/writers/markdown-writer.tests.js +304 -0
  116. package/test/xml/reader.tests.js +85 -0
  117. package/test/xml/writer.tests.js +81 -0
  118. package/test/zipfile.tests.js +61 -0
@@ -0,0 +1,75 @@
1
+ var _ = require("underscore");
2
+
3
+ var promises = require("../promises");
4
+ var xml = require("../xml");
5
+
6
+ exports.writeStyleMap = writeStyleMap;
7
+ exports.readStyleMap = readStyleMap;
8
+
9
+
10
+ var schema = "http://schemas.zwobble.org/mammoth/style-map";
11
+ var styleMapPath = "mammoth/style-map";
12
+ var styleMapAbsolutePath = "/" + styleMapPath;
13
+
14
+ function writeStyleMap(docxFile, styleMap) {
15
+ docxFile.write(styleMapPath, styleMap);
16
+ return updateRelationships(docxFile).then(function() {
17
+ return updateContentTypes(docxFile);
18
+ });
19
+ }
20
+
21
+ function updateRelationships(docxFile) {
22
+ var path = "word/_rels/document.xml.rels";
23
+ var relationshipsUri = "http://schemas.openxmlformats.org/package/2006/relationships";
24
+ var relationshipElementName = "{" + relationshipsUri + "}Relationship";
25
+ return docxFile.read(path, "utf8")
26
+ .then(xml.readString)
27
+ .then(function(relationshipsContainer) {
28
+ var relationships = relationshipsContainer.children;
29
+ addOrUpdateElement(relationships, relationshipElementName, "Id", {
30
+ "Id": "rMammothStyleMap",
31
+ "Type": schema,
32
+ "Target": styleMapAbsolutePath
33
+ });
34
+
35
+ var namespaces = {"": relationshipsUri};
36
+ return docxFile.write(path, xml.writeString(relationshipsContainer, namespaces));
37
+ });
38
+ }
39
+
40
+ function updateContentTypes(docxFile) {
41
+ var path = "[Content_Types].xml";
42
+ var contentTypesUri = "http://schemas.openxmlformats.org/package/2006/content-types";
43
+ var overrideName = "{" + contentTypesUri + "}Override";
44
+ return docxFile.read(path, "utf8")
45
+ .then(xml.readString)
46
+ .then(function(typesElement) {
47
+ var children = typesElement.children;
48
+ addOrUpdateElement(children, overrideName, "PartName", {
49
+ "PartName": styleMapAbsolutePath,
50
+ "ContentType": "text/prs.mammoth.style-map"
51
+ });
52
+ var namespaces = {"": contentTypesUri};
53
+ return docxFile.write(path, xml.writeString(typesElement, namespaces));
54
+ });
55
+ }
56
+
57
+ function addOrUpdateElement(elements, name, identifyingAttribute, attributes) {
58
+ var existingElement = _.find(elements, function(element) {
59
+ return element.name === name &&
60
+ element.attributes[identifyingAttribute] === attributes[identifyingAttribute];
61
+ });
62
+ if (existingElement) {
63
+ existingElement.attributes = attributes;
64
+ } else {
65
+ elements.push(xml.element(name, attributes));
66
+ }
67
+ }
68
+
69
+ function readStyleMap(docxFile) {
70
+ if (docxFile.exists(styleMapPath)) {
71
+ return docxFile.read(styleMapPath, "utf8");
72
+ } else {
73
+ return promises.resolve(null);
74
+ }
75
+ }
@@ -0,0 +1,90 @@
1
+ exports.readStylesXml = readStylesXml;
2
+ exports.Styles = Styles;
3
+ exports.defaultStyles = new Styles({}, {});
4
+
5
+ function Styles(paragraphStyles, characterStyles, tableStyles, numberingStyles) {
6
+ return {
7
+ findParagraphStyleById: function(styleId) {
8
+ return paragraphStyles[styleId];
9
+ },
10
+ findCharacterStyleById: function(styleId) {
11
+ return characterStyles[styleId];
12
+ },
13
+ findTableStyleById: function(styleId) {
14
+ return tableStyles[styleId];
15
+ },
16
+ findNumberingStyleById: function(styleId) {
17
+ return numberingStyles[styleId];
18
+ }
19
+ };
20
+ }
21
+
22
+ Styles.EMPTY = new Styles({}, {}, {}, {});
23
+
24
+ function readStylesXml(root) {
25
+ var paragraphStyles = {};
26
+ var characterStyles = {};
27
+ var tableStyles = {};
28
+ var numberingStyles = {};
29
+
30
+ var styles = {
31
+ "paragraph": paragraphStyles,
32
+ "character": characterStyles,
33
+ "table": tableStyles,
34
+ "numbering": numberingStyles
35
+ };
36
+
37
+ root.getElementsByTagName("w:style").forEach(function(styleElement) {
38
+ var style = readStyleElement(styleElement);
39
+ var styleSet = styles[style.type];
40
+
41
+ // Per 17.7.4.17 style (Style Definition) of ECMA-376 4th edition Part 1:
42
+ //
43
+ // > If multiple style definitions each declare the same value for their
44
+ // > styleId, then the first such instance shall keep its current
45
+ // > identifier with all other instances being reassigned in any manner
46
+ // > desired.
47
+ //
48
+ // For the purpose of conversion, there's no point holding onto styles
49
+ // with reassigned style IDs, so we ignore such style definitions.
50
+
51
+ if (styleSet && styleSet[style.styleId] === undefined) {
52
+ styleSet[style.styleId] = style;
53
+ }
54
+ });
55
+
56
+ return new Styles(paragraphStyles, characterStyles, tableStyles, numberingStyles);
57
+ }
58
+
59
+ function readStyleElement(styleElement) {
60
+ var type = styleElement.attributes["w:type"];
61
+
62
+ if (type === "numbering") {
63
+ return readNumberingStyleElement(type, styleElement);
64
+ } else {
65
+ var styleId = readStyleId(styleElement);
66
+ var name = styleName(styleElement);
67
+ return {type: type, styleId: styleId, name: name};
68
+ }
69
+ }
70
+
71
+ function styleName(styleElement) {
72
+ var nameElement = styleElement.first("w:name");
73
+ return nameElement ? nameElement.attributes["w:val"] : null;
74
+ }
75
+
76
+ function readNumberingStyleElement(type, styleElement) {
77
+ var styleId = readStyleId(styleElement);
78
+
79
+ var numId = styleElement
80
+ .firstOrEmpty("w:pPr")
81
+ .firstOrEmpty("w:numPr")
82
+ .firstOrEmpty("w:numId")
83
+ .attributes["w:val"];
84
+
85
+ return {type: type, numId: numId, styleId: styleId};
86
+ }
87
+
88
+ function readStyleId(styleElement) {
89
+ return styleElement.attributes["w:styleId"];
90
+ }
@@ -0,0 +1,21 @@
1
+ exports.uriToZipEntryName = uriToZipEntryName;
2
+ exports.replaceFragment = replaceFragment;
3
+
4
+ function uriToZipEntryName(base, uri) {
5
+ if (uri.charAt(0) === "/") {
6
+ return uri.substr(1);
7
+ } else {
8
+ // In general, we should check first and second for trailing and leading slashes,
9
+ // but in our specific case this seems to be sufficient
10
+ return base + "/" + uri;
11
+ }
12
+ }
13
+
14
+
15
+ function replaceFragment(uri, fragment) {
16
+ var hashIndex = uri.indexOf("#");
17
+ if (hashIndex !== -1) {
18
+ uri = uri.substring(0, hashIndex);
19
+ }
20
+ return uri + "#" + fragment;
21
+ }
@@ -0,0 +1,51 @@
1
+ var htmlPaths = require("../styles/html-paths");
2
+
3
+
4
+ function nonFreshElement(tagName, attributes, children) {
5
+ return elementWithTag(
6
+ htmlPaths.element(tagName, attributes, {fresh: false}),
7
+ children);
8
+ }
9
+
10
+ function freshElement(tagName, attributes, children) {
11
+ var tag = htmlPaths.element(tagName, attributes, {fresh: true});
12
+ return elementWithTag(tag, children);
13
+ }
14
+
15
+ function elementWithTag(tag, children) {
16
+ return {
17
+ type: "element",
18
+ tag: tag,
19
+ children: children || []
20
+ };
21
+ }
22
+
23
+ function text(value) {
24
+ return {
25
+ type: "text",
26
+ value: value
27
+ };
28
+ }
29
+
30
+ var forceWrite = {
31
+ type: "forceWrite"
32
+ };
33
+
34
+ exports.freshElement = freshElement;
35
+ exports.nonFreshElement = nonFreshElement;
36
+ exports.elementWithTag = elementWithTag;
37
+ exports.text = text;
38
+ exports.forceWrite = forceWrite;
39
+
40
+ var voidTagNames = {
41
+ "br": true,
42
+ "hr": true,
43
+ "img": true,
44
+ "input": true
45
+ };
46
+
47
+ function isVoidElement(node) {
48
+ return (node.children.length === 0) && voidTagNames[node.tag.tagName];
49
+ }
50
+
51
+ exports.isVoidElement = isVoidElement;
@@ -0,0 +1,41 @@
1
+ var ast = require("./ast");
2
+
3
+ exports.freshElement = ast.freshElement;
4
+ exports.nonFreshElement = ast.nonFreshElement;
5
+ exports.elementWithTag = ast.elementWithTag;
6
+ exports.text = ast.text;
7
+ exports.forceWrite = ast.forceWrite;
8
+
9
+ exports.simplify = require("./simplify");
10
+
11
+ function write(writer, nodes) {
12
+ nodes.forEach(function(node) {
13
+ writeNode(writer, node);
14
+ });
15
+ }
16
+
17
+ function writeNode(writer, node) {
18
+ toStrings[node.type](writer, node);
19
+ }
20
+
21
+ var toStrings = {
22
+ element: generateElementString,
23
+ text: generateTextString,
24
+ forceWrite: function() { }
25
+ };
26
+
27
+ function generateElementString(writer, node) {
28
+ if (ast.isVoidElement(node)) {
29
+ writer.selfClosing(node.tag.tagName, node.tag.attributes);
30
+ } else {
31
+ writer.open(node.tag.tagName, node.tag.attributes);
32
+ write(writer, node.children);
33
+ writer.close(node.tag.tagName);
34
+ }
35
+ }
36
+
37
+ function generateTextString(writer, node) {
38
+ writer.text(node.value);
39
+ }
40
+
41
+ exports.write = write;
@@ -0,0 +1,88 @@
1
+ var _ = require("underscore");
2
+
3
+ var ast = require("./ast");
4
+
5
+ function simplify(nodes) {
6
+ return collapse(removeEmpty(nodes));
7
+ }
8
+
9
+ function collapse(nodes) {
10
+ var children = [];
11
+
12
+ nodes.map(collapseNode).forEach(function(child) {
13
+ appendChild(children, child);
14
+ });
15
+ return children;
16
+ }
17
+
18
+ function collapseNode(node) {
19
+ return collapsers[node.type](node);
20
+ }
21
+
22
+ var collapsers = {
23
+ element: collapseElement,
24
+ text: identity,
25
+ forceWrite: identity
26
+ };
27
+
28
+ function collapseElement(node) {
29
+ return ast.elementWithTag(node.tag, collapse(node.children));
30
+ }
31
+
32
+ function identity(value) {
33
+ return value;
34
+ }
35
+
36
+ function appendChild(children, child) {
37
+ var lastChild = children[children.length - 1];
38
+ if (child.type === "element" && !child.tag.fresh && lastChild && lastChild.type === "element" && child.tag.matchesElement(lastChild.tag)) {
39
+ if (child.tag.separator) {
40
+ appendChild(lastChild.children, ast.text(child.tag.separator));
41
+ }
42
+ child.children.forEach(function(grandChild) {
43
+ // Mutation is fine since simplifying elements create a copy of the children.
44
+ appendChild(lastChild.children, grandChild);
45
+ });
46
+ } else {
47
+ children.push(child);
48
+ }
49
+ }
50
+
51
+ function removeEmpty(nodes) {
52
+ return flatMap(nodes, function(node) {
53
+ return emptiers[node.type](node);
54
+ });
55
+ }
56
+
57
+ function flatMap(values, func) {
58
+ return _.flatten(_.map(values, func), true);
59
+ }
60
+
61
+ var emptiers = {
62
+ element: elementEmptier,
63
+ text: textEmptier,
64
+ forceWrite: neverEmpty
65
+ };
66
+
67
+ function neverEmpty(node) {
68
+ return [node];
69
+ }
70
+
71
+ function elementEmptier(element) {
72
+ var children = removeEmpty(element.children);
73
+ if (children.length === 0 && !ast.isVoidElement(element)) {
74
+ return [];
75
+ } else {
76
+ return [ast.elementWithTag(element.tag, children)];
77
+ }
78
+ }
79
+
80
+ function textEmptier(node) {
81
+ if (node.value.length === 0) {
82
+ return [];
83
+ } else {
84
+ return [node];
85
+ }
86
+ }
87
+
88
+ module.exports = simplify;
package/lib/images.js ADDED
@@ -0,0 +1,31 @@
1
+ var _ = require("underscore");
2
+
3
+ var promises = require("./promises");
4
+ var Html = require("./html");
5
+
6
+ exports.imgElement = imgElement;
7
+
8
+ function imgElement(func) {
9
+ return function(element, messages) {
10
+ return promises.when(func(element)).then(function(result) {
11
+ var attributes = {};
12
+ if (element.altText) {
13
+ attributes.alt = element.altText;
14
+ }
15
+ _.extend(attributes, result);
16
+
17
+ return [Html.freshElement("img", attributes)];
18
+ });
19
+ };
20
+ }
21
+
22
+ // Undocumented, but retained for backwards-compatibility with 0.3.x
23
+ exports.inline = exports.imgElement;
24
+
25
+ exports.dataUri = imgElement(function(element) {
26
+ return element.readAsBase64String().then(function(imageBuffer) {
27
+ return {
28
+ src: "data:" + element.contentType + ";base64," + imageBuffer
29
+ };
30
+ });
31
+ });
package/lib/index.d.ts ADDED
@@ -0,0 +1,86 @@
1
+ interface Mammoth {
2
+ convertToHtml: (input: Input, options?: Options) => Promise<Result>;
3
+ extractRawText: (input: Input) => Promise<Result>;
4
+ embedStyleMap: (input: Input, styleMap: string) => Promise<{
5
+ toArrayBuffer: () => ArrayBuffer,
6
+ toBuffer: () => Buffer,
7
+ }>;
8
+ images: Images;
9
+ }
10
+
11
+ type Input = NodeJsInput | BrowserInput;
12
+
13
+ type NodeJsInput = PathInput | BufferInput;
14
+
15
+ interface PathInput {
16
+ path: string;
17
+ }
18
+
19
+ interface BufferInput {
20
+ buffer: Buffer;
21
+ }
22
+
23
+ type BrowserInput = ArrayBufferInput;
24
+
25
+ interface ArrayBufferInput {
26
+ arrayBuffer: ArrayBuffer;
27
+ }
28
+
29
+ interface Options {
30
+ styleMap?: string | Array<string>;
31
+ includeEmbeddedStyleMap?: boolean;
32
+ includeDefaultStyleMap?: boolean;
33
+ convertImage?: ImageConverter;
34
+ ignoreEmptyParagraphs?: boolean;
35
+ idPrefix?: string;
36
+ externalFileAccess?: boolean;
37
+ transformDocument?: (element: any) => any;
38
+ }
39
+
40
+ interface ImageConverter {
41
+ __mammothBrand: "ImageConverter";
42
+ }
43
+
44
+ interface Image {
45
+ contentType: string;
46
+ readAsArrayBuffer: () => Promise<ArrayBuffer>;
47
+ readAsBase64String: () => Promise<string>;
48
+ readAsBuffer: () => Promise<Buffer>;
49
+ read: ImageRead;
50
+ }
51
+
52
+ interface ImageRead {
53
+ (): Promise<Buffer>;
54
+ (encoding: string): Promise<string>;
55
+ }
56
+
57
+ interface ImageAttributes {
58
+ src: string;
59
+ }
60
+
61
+ interface Images {
62
+ dataUri: ImageConverter;
63
+ imgElement: (f: (image: Image) => Promise<ImageAttributes>) => ImageConverter;
64
+ }
65
+
66
+ interface Result {
67
+ value: string;
68
+ messages: Array<Message>;
69
+ }
70
+
71
+ type Message = Warning | Error;
72
+
73
+ interface Warning {
74
+ type: "warning";
75
+ message: string;
76
+ }
77
+
78
+ interface Error {
79
+ type: "error";
80
+ message: string;
81
+ error: unknown;
82
+ }
83
+
84
+ declare const mammoth: Mammoth;
85
+
86
+ export = mammoth;
package/lib/index.js ADDED
@@ -0,0 +1,111 @@
1
+ var _ = require("underscore");
2
+
3
+ var docxReader = require("./docx/docx-reader");
4
+ var docxStyleMap = require("./docx/style-map");
5
+ var DocumentConverter = require("./document-to-html").DocumentConverter;
6
+ var convertElementToRawText = require("./raw-text").convertElementToRawText;
7
+ var readStyle = require("./style-reader").readStyle;
8
+ var readOptions = require("./options-reader").readOptions;
9
+ var unzip = require("./unzip");
10
+ var Result = require("./results").Result;
11
+
12
+ exports.convertToHtml = convertToHtml;
13
+ exports.convertToMarkdown = convertToMarkdown;
14
+ exports.convert = convert;
15
+ exports.extractRawText = extractRawText;
16
+ exports.images = require("./images");
17
+ exports.transforms = require("./transforms");
18
+ exports.underline = require("./underline");
19
+ exports.embedStyleMap = embedStyleMap;
20
+ exports.readEmbeddedStyleMap = readEmbeddedStyleMap;
21
+
22
+ function convertToHtml(input, options) {
23
+ return convert(input, options);
24
+ }
25
+
26
+ function convertToMarkdown(input, options) {
27
+ var markdownOptions = Object.create(options || {});
28
+ markdownOptions.outputFormat = "markdown";
29
+ return convert(input, markdownOptions);
30
+ }
31
+
32
+ function convert(input, options) {
33
+ options = readOptions(options);
34
+
35
+ return unzip.openZip(input)
36
+ .tap(function(docxFile) {
37
+ return docxStyleMap.readStyleMap(docxFile).then(function(styleMap) {
38
+ options.embeddedStyleMap = styleMap;
39
+ });
40
+ })
41
+ .then(function(docxFile) {
42
+ return docxReader.read(docxFile, input, options)
43
+ .then(function(documentResult) {
44
+ return documentResult.map(options.transformDocument);
45
+ })
46
+ .then(function(documentResult) {
47
+ return convertDocumentToHtml(documentResult, options);
48
+ });
49
+ });
50
+ }
51
+
52
+ function readEmbeddedStyleMap(input) {
53
+ return unzip.openZip(input)
54
+ .then(docxStyleMap.readStyleMap);
55
+ }
56
+
57
+ function convertDocumentToHtml(documentResult, options) {
58
+ var styleMapResult = parseStyleMap(options.readStyleMap());
59
+ var parsedOptions = _.extend({}, options, {
60
+ styleMap: styleMapResult.value
61
+ });
62
+ var documentConverter = new DocumentConverter(parsedOptions);
63
+
64
+ return documentResult.flatMapThen(function(document) {
65
+ return styleMapResult.flatMapThen(function(styleMap) {
66
+ return documentConverter.convertToHtml(document);
67
+ });
68
+ });
69
+ }
70
+
71
+ function parseStyleMap(styleMap) {
72
+ return Result.combine((styleMap || []).map(readStyle))
73
+ .map(function(styleMap) {
74
+ return styleMap.filter(function(styleMapping) {
75
+ return !!styleMapping;
76
+ });
77
+ });
78
+ }
79
+
80
+
81
+ function extractRawText(input) {
82
+ return unzip.openZip(input)
83
+ .then(docxReader.read)
84
+ .then(function(documentResult) {
85
+ return documentResult.map(convertElementToRawText);
86
+ });
87
+ }
88
+
89
+ function embedStyleMap(input, styleMap) {
90
+ return unzip.openZip(input)
91
+ .tap(function(docxFile) {
92
+ return docxStyleMap.writeStyleMap(docxFile, styleMap);
93
+ })
94
+ .then(function(docxFile) {
95
+ return docxFile.toArrayBuffer();
96
+ })
97
+ .then(function(arrayBuffer) {
98
+ return {
99
+ toArrayBuffer: function() {
100
+ return arrayBuffer;
101
+ },
102
+ toBuffer: function() {
103
+ return Buffer.from(arrayBuffer);
104
+ }
105
+ };
106
+ });
107
+ }
108
+
109
+ exports.styleMapping = function() {
110
+ throw new Error('Use a raw string instead of mammoth.styleMapping e.g. "p[style-name=\'Title\'] => h1" instead of mammoth.styleMapping("p[style-name=\'Title\'] => h1")');
111
+ };
package/lib/main.js ADDED
@@ -0,0 +1,63 @@
1
+ /* global process */
2
+
3
+ var fs = require("fs");
4
+ var path = require("path");
5
+
6
+ var mammoth = require("./");
7
+ var promises = require("./promises");
8
+ var images = require("./images");
9
+
10
+ function main(argv) {
11
+ var docxPath = argv["docx-path"];
12
+ var outputPath = argv["output-path"];
13
+ var outputDir = argv.output_dir;
14
+ var outputFormat = argv.output_format;
15
+ var styleMapPath = argv.style_map;
16
+
17
+ readStyleMap(styleMapPath).then(function(styleMap) {
18
+ var options = {
19
+ styleMap: styleMap,
20
+ outputFormat: outputFormat
21
+ };
22
+
23
+ if (outputDir) {
24
+ var basename = path.basename(docxPath, ".docx");
25
+ outputPath = path.join(outputDir, basename + ".html");
26
+ var imageIndex = 0;
27
+ options.convertImage = images.imgElement(function(element) {
28
+ imageIndex++;
29
+ var extension = element.contentType.split("/")[1];
30
+ var filename = imageIndex + "." + extension;
31
+
32
+ return element.read().then(function(imageBuffer) {
33
+ var imagePath = path.join(outputDir, filename);
34
+ return promises.nfcall(fs.writeFile, imagePath, imageBuffer);
35
+ }).then(function() {
36
+ return {src: filename};
37
+ });
38
+ });
39
+ }
40
+
41
+ return mammoth.convert({path: docxPath}, options)
42
+ .then(function(result) {
43
+ result.messages.forEach(function(message) {
44
+ process.stderr.write(message.message);
45
+ process.stderr.write("\n");
46
+ });
47
+
48
+ var outputStream = outputPath ? fs.createWriteStream(outputPath) : process.stdout;
49
+
50
+ outputStream.write(result.value);
51
+ });
52
+ }).done();
53
+ }
54
+
55
+ function readStyleMap(styleMapPath) {
56
+ if (styleMapPath) {
57
+ return promises.nfcall(fs.readFile, styleMapPath, "utf8");
58
+ } else {
59
+ return promises.resolve(null);
60
+ }
61
+ }
62
+
63
+ module.exports = main;