From 69abf509c99105c6bc7e4b8cb74a4f7e7c9c9b18 Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 13 Mar 2017 00:50:29 +0000 Subject: [PATCH 01/15] removed tab stops and placed it in the Wiki --- README.md | 52 ---------------------------------------------------- 1 file changed, 52 deletions(-) diff --git a/README.md b/README.md index 45c119a654..9b7bec4c7e 100644 --- a/README.md +++ b/README.md @@ -265,58 +265,6 @@ This will produce: * Bullet points * Are awesome -## Tab Stops -If you do not know why tab stops are useful, then I recommend you do a bit of research. It enables side by side text which is nicely laid out without the need for tables, or constantly pressing space bar. - -**Note**: At the moment, the unit of measurement for a tab stop is counter intuitive for a human. It is using OpenXMLs own measuring system. For example, 2268 roughly translates to 3cm. Therefore in the future, I may consider changing it to percentages or even cm. - -![Word 2013 Tabs](http://www.teachucomp.com/wp-content/uploads/blog-4-22-2015-UsingTabStopsInWord-1024x577.png "Word 2013 Tab Stops") - -Simply call the relevant methods on the paragraph listed below. Then just add a `tab()` method call to a text object. Adding multiple `tabStops` will mean you would have to chain `tab()` until the desired `tabStop` is selected. Example is shown below. - -### Left Tab Stop -```js -paragraph.leftTabStop(2268); -``` -2268 is the distance from the left side. - -### Center Tab Stop -```js -paragraph.centerTabStp(2268); -``` -2268 is the distance from the left side. - -### Right Tab Stop -```js -paragraph.rightTabStop(2268); -``` -2268 is the distance from the left side. - -### Max Right Tab Stop -```js -paragraph.maxRightTabStop(); -``` -This will create a tab stop on the very edge of the right hand side. Handy for right aligning and left aligning text on the same line. - -### Example -```js -var paragraph = new docx.Paragraph().maxRightTabStop(); -var leftText = new docx.TextRun("Hey everyone").bold(); -var rightText = new docx.TextRun("11th November 2015").tab(); -paragraph.addText(leftText); -paragraph.addText(rightText); -``` -The example above will create a left aligned text, and a right aligned text on the same line. The laymans approach to this problem would be to either use text boxes or tables. YUK! - -```js -var paragraph = new docx.Paragraph(); -paragraph.maxRightTabStop(); -paragraph.leftTabStop(1000); -var text = new docx.TextRun("Second tab stop here I come!").tab().tab(); -paragraph.addText(text); -``` -The above shows the use of two tab stops, and how to select/use it. - # Exporting Check the Wiki for exporting guide From e6ed3a55ffb6c68c887ba45daba95d22d70a55c5 Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 13 Mar 2017 00:51:01 +0000 Subject: [PATCH 02/15] updated contents --- README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/README.md b/README.md index 9b7bec4c7e..c24059730f 100644 --- a/README.md +++ b/README.md @@ -34,12 +34,6 @@ - [Break](#) - [Chaining](#) - [Bullet Points](#) - - [Tab Stops](#) - - [Left Tab Stop](#) - - [Center Tab Stop](#) - - [Right Tab Stop](#) - - [Max Right Tab Stop](#) - - [Example](#) - [Exporting](#) - [Express](#) - [Standalone .docx file](#) From 8c58ec2ba47efa5928d18089603ee2e847c264d9 Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 13 Mar 2017 00:54:05 +0000 Subject: [PATCH 03/15] moved bullet points to Wiki --- README.md | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index c24059730f..75a5919603 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,6 @@ - [Underline](#) - [Break](#) - [Chaining](#) - - [Bullet Points](#) - [Exporting](#) - [Express](#) - [Standalone .docx file](#) @@ -53,6 +52,9 @@ $ npm run demo will run the demo app in the `demo` folder, which creates a file called "My Document.docx" in the root of the project +# Guide + +Please refer to [the Wiki](https://github.com/dolanmiu/docx/wiki) for details on how to use this library, examples and much more! # Usage @@ -243,22 +245,6 @@ What if you want to create a paragraph which is ***bold*** and ***italic***? paragraph.bold().italic(); ``` -## Bullet Points -To make a bullet point, simply make a paragraph into a bullet point: -```js -var text = new docx.TextRun("Bullet points"); -var paragraph = new docx.Paragraph(text).bullet(); - -var text2 = new docx.TextRun("Are awesome"); -var paragraph2 = new docx.Paragraph(text2).bullet(); - -doc.addParagraph(paragraph); -doc.addParagraph(paragraph2); -``` -This will produce: -* Bullet points -* Are awesome - # Exporting Check the Wiki for exporting guide From efa4f1933a81952755e95814857b91e762b76cad Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 13 Mar 2017 00:57:57 +0000 Subject: [PATCH 04/15] moved Text section to Wiki --- README.md | 74 ------------------------------------------------------- 1 file changed, 74 deletions(-) diff --git a/README.md b/README.md index 75a5919603..e758b0ac90 100644 --- a/README.md +++ b/README.md @@ -26,13 +26,6 @@ - [Text Alignment](#) - [Example](#) - [Thematic Break (Page Break)](#) - - [Text](#) - - [Typographical Emphasis](#) - - [Bold](#) - - [Italics](#) - - [Underline](#) - - [Break](#) - - [Chaining](#) - [Exporting](#) - [Express](#) - [Standalone .docx file](#) @@ -178,73 +171,6 @@ var paragraph = new docx.Paragraph("Amazing Heading").heading1().pageBreak(); ``` The above example will create a heading and start a new page immediately afterwards. -## Text -Paragraphs need `text run` objects. To create text: -```js -var text = new docx.TextRun("My awesome text here for my university dissertation"); -paragraph.addText(text); -``` -Text objects have methods inside which changes the way the text is displayed. - -### Typographical Emphasis -More info [here](https://english.stackexchange.com/questions/97081/what-is-the-typography-term-which-refers-to-the-usage-of-bold-italics-and-unde) -#### Bold -```js -text.bold(); -``` - -#### Italics -```js -text.italic(); -``` - -#### Underline -```js -text.underline(); -``` - -#### Strike through -```js -text.strike(); -``` - -#### Double strike through -```js -text.doubleStrike(); -``` - -#### Superscript -```js -text.superScript(); -``` - -#### Subscript -```js -text.subScript(); -``` - -#### All Capitals -```js -text.allCaps(); -``` - -#### Small Capitals -```js -text.smallCaps(); -``` - -### Break -Sometimes you would want to put text underneath another line of text but inside the same paragraph. -```js -text.break(); -``` - -### Chaining -What if you want to create a paragraph which is ***bold*** and ***italic***? -```js -paragraph.bold().italic(); -``` - # Exporting Check the Wiki for exporting guide From 2662bcc60be42f6902efc30240573eccbe232dde Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 13 Mar 2017 01:03:57 +0000 Subject: [PATCH 05/15] removed Paragraph from readme, and put it in the Wiki --- README.md | 82 ------------------------------------------------------- 1 file changed, 82 deletions(-) diff --git a/README.md b/README.md index e758b0ac90..f378f348c8 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,6 @@ - [Install](#) - [Usage](#) - [Create simple Word Document](#) - - [Create Paragraph](#) - - [Styles](#) - - [Heading1 - Heading5](#) - - [Title](#) - - [Text Alignment](#) - - [Example](#) - - [Thematic Break (Page Break)](#) - [Exporting](#) - [Express](#) - [Standalone .docx file](#) @@ -96,81 +89,6 @@ revision You can mix and match whatever properties you want, or provide no properties. -## Create Paragraph -Every text block in OpenXML is organised in paragraphs. You can add more text to the paragraph by doing this: -```js -var paragraph = new docx.Paragraph(), -``` -```js -var text = new docx.TextRun('Lorem Ipsum Foo Bar'); -var paragraph = new docx.Paragraph(); -paragraph.addText(text); -``` -```js -var paragraph = new docx.Paragraph("Short hand notation for adding text."); -``` - -After you create the paragraph, you must add the paragraph into the `document`: -```js -doc.addParagraph(paragraph); -``` - -### Styles -Styles is a very important part of the look of a word document. At the moment, only headings and title is supported, but son the rest will be supported along with custom styles! - -![Word 2013 Styles menu](http://content.gcflearnfree.org/topics/233/style_apply_choose.png "Word 2013 Styles menu") - -#### Heading1 - Heading5 -```js -paragraph.heading1(); -paragraph.heading2(); -paragraph.heading3(); -paragraph.heading4(); -paragraph.heading5(); -``` - -#### Title -```js -paragraph.title(); -``` - -### Text Alignment -To change the text alignment of a paragraph, for center, left, right or justified: -```js -paragraph.center(); -``` -```js -paragraph.left(); -``` -```js -paragraph.right(); -``` -```js -paragraph.justified(); -``` - -#### Example -```js -paragraph.heading1().center(); -``` -The above will create a `heading 1` which is `centered`. - -### Thematic Break -To add a break in the page, simply add `.thematicBreak()` on a paragraph: - -```js -var paragraph = new docx.Paragraph("Amazing Heading").heading1().thematicBreak(); -``` -The above example will create a heading with a page break directly under it. - -### Page Break -To move to a new page (insert a page break), simply add `.pageBreak()` on a paragraph: - -```js -var paragraph = new docx.Paragraph("Amazing Heading").heading1().pageBreak(); -``` -The above example will create a heading and start a new page immediately afterwards. - # Exporting Check the Wiki for exporting guide From bf2e5201c8d3d85064e02bc2a03871f3c0b517f0 Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 13 Mar 2017 01:11:11 +0000 Subject: [PATCH 06/15] made usage much more clear --- README.md | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f378f348c8..2833b422ee 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ will run the demo app in the `demo` folder, which creates a file called "My Docu Please refer to [the Wiki](https://github.com/dolanmiu/docx/wiki) for details on how to use this library, examples and much more! -# Usage +# Simple Usage ```js // Used to create docx files @@ -51,19 +51,24 @@ var docx = require('docx'); // Create document var doc = new docx.Document(); +// Add some content in the document +var paragraph = new docx.Paragraph("Some cool text here."); +// Add more text into the paragraph if you wish +paragraph.addText(new docx.TextRun('Lorem Ipsum Foo Bar')); +doc.addParagraph(paragraph); + // Used to export the file into a .docx file +var exporter = new docx.LocalPacker(doc); + +// Or use the express packer to make the file downloadable. // res is express' Response object var exporter = new docx.ExpressPacker(doc, res); -var exporter = new docx.LocalPacker(doc); -``` -## Create simple Word Document -```js -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph(); -var text = new docx.TextRun('Hello World'); -paragraph.addText(text); -doc.addParagraph(paragraph); + +exporter.pack('My First Document'); + +// done! A file called 'My First Document.docx' +// will be in your file system if you used LocalPacker +// Or it will start downloading if you are using Express ``` ### Document properties From ae41e22df513a7888b3754eb3a4f76fe17c2b532 Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 13 Mar 2017 01:19:39 +0000 Subject: [PATCH 07/15] moved Documents section to Wiki. Cleaned up Wiki Added felipeochoa mention removed TOC --- README.md | 42 ++++-------------------------------------- 1 file changed, 4 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 2833b422ee..f3eaa5929d 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@

- Generate .docx files with JS/TS very easily + Generate .docx files with JS/TS very easily, written in TS.

===== @@ -11,19 +11,9 @@ [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Known Vulnerabilities][snky-image]][snky-url] # docx -> A tool to create Word Documents (.docx) with JS or TS, written in TS. [![NPM](https://nodei.co/npm/docx.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/docx/) -# Table of Contents -- [Install](#) -- [Usage](#) - - [Create simple Word Document](#) -- [Exporting](#) - - [Express](#) - - [Standalone .docx file](#) -- [Examples](#) - # Install ```sh @@ -71,39 +61,15 @@ exporter.pack('My First Document'); // Or it will start downloading if you are using Express ``` -### Document properties -You can add properties to the Word document by specifying options, for example: -```js -var doc = new docx.Document({ - creator: 'Dolan Miu', - description: 'My extremely interesting document', - title: 'My Document' -}); -``` - -#### Full list of options: -``` -creator -description -title -subject -keywords -lastModifiedBy -revision -``` - -You can mix and match whatever properties you want, or provide no properties. - -# Exporting -Check the Wiki for exporting guide - # Examples -Check the Wiki for examples +Check [the Wiki](https://github.com/dolanmiu/docx/wiki/Examples) for examples. ===== Made with 💖 +Huge thanks to [@felipeochoa](https://github.com/felipeochoa) for awesome contributions to this project + [npm-image]: https://badge.fury.io/js/docx.svg [npm-url]: https://npmjs.org/package/docx [travis-image]: https://travis-ci.org/dolanmiu/docx.svg?branch=master From ba80440faff6f5abc8cdc9765b9351681fca022e Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 13 Mar 2017 01:21:26 +0000 Subject: [PATCH 08/15] formatting and correct heading values --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f3eaa5929d..31ad40e1eb 100644 --- a/README.md +++ b/README.md @@ -10,17 +10,17 @@ [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Known Vulnerabilities][snky-image]][snky-url] -# docx - [![NPM](https://nodei.co/npm/docx.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/docx/) -# Install +# docx + +## Install ```sh $ npm install --save docx ``` -# Demo +## Demo ```sh $ npm run demo @@ -28,11 +28,11 @@ $ npm run demo will run the demo app in the `demo` folder, which creates a file called "My Document.docx" in the root of the project -# Guide +## Guide Please refer to [the Wiki](https://github.com/dolanmiu/docx/wiki) for details on how to use this library, examples and much more! -# Simple Usage +## Simple Usage ```js // Used to create docx files @@ -61,7 +61,7 @@ exporter.pack('My First Document'); // Or it will start downloading if you are using Express ``` -# Examples +## Examples Check [the Wiki](https://github.com/dolanmiu/docx/wiki/Examples) for examples. ===== From 457d074a599893a446ac91d90cf6d7ce6cf09b7e Mon Sep 17 00:00:00 2001 From: felipe Date: Mon, 13 Mar 2017 12:23:18 +0100 Subject: [PATCH 09/15] added paragraph formatting methods to numbering Level --- ts/numbering/level.ts | 48 ++++++++++++++++ ts/tests/numberingTest.ts | 117 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) diff --git a/ts/numbering/level.ts b/ts/numbering/level.ts index 02f0896f21..bc59625212 100644 --- a/ts/numbering/level.ts +++ b/ts/numbering/level.ts @@ -1,3 +1,4 @@ +import * as paragraph from "../docx/paragraph/formatting"; import { ParagraphProperties } from "../docx/paragraph/properties"; import { RunProperties } from "../docx/run/properties"; import { Attributes, XmlAttributeComponent, XmlComponent } from "../docx/xml-components"; @@ -86,4 +87,51 @@ export class Level extends XmlComponent { this.runProperties.push(property); return this; } + + // --------------------- Paragraph formatting ------------------------ // + + public center(): Level { + this.addParagraphProperty(new paragraph.Alignment("center")); + return this; + } + + public left(): Level { + this.addParagraphProperty(new paragraph.Alignment("left")); + return this; + } + + public right(): Level { + this.addParagraphProperty(new paragraph.Alignment("right")); + return this; + } + + public justified(): Level { + this.addParagraphProperty(new paragraph.Alignment("both")); + return this; + } + + public thematicBreak(): Level { + this.addParagraphProperty(new paragraph.ThematicBreak()); + return this; + } + + public maxRightTabStop(): Level { + this.addParagraphProperty(new paragraph.MaxRightTabStop()); + return this; + } + + public leftTabStop(position: number): Level { + this.addParagraphProperty(new paragraph.LeftTabStop(position)); + return this; + } + + public indent(left: number, hanging?: number): Level { + this.addParagraphProperty(new paragraph.Indent(left, hanging)); + return this; + } + + public spacing(params: paragraph.ISpacingProperties): Level { + this.addParagraphProperty(new paragraph.Spacing(params)); + return this; + }; } diff --git a/ts/tests/numberingTest.ts b/ts/tests/numberingTest.ts index 00bd6afb10..210cbe8a43 100644 --- a/ts/tests/numberingTest.ts +++ b/ts/tests/numberingTest.ts @@ -105,5 +105,122 @@ describe("AbstractNumbering", () => { expect(tree["w:lvl"]).to.include({ "w:numFmt": [{ _attr: { "w:val": "lowerLetter" } }] }); expect(tree["w:lvl"]).to.include({ "w:lvlText": [{ _attr: { "w:val": "%1)" } }] }); }); + + describe("formatting methods: paragraph properties", () => { + it("#indent", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerLetter", "%0.") + .indent(720); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:pPr": [{"w:ind": [{_attr: {"w:left": 720}}]}], + }); + }); + + it("#spacing", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerLetter", "%0.") + .spacing({before: 50, after: 150}); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:pPr": [ + {"w:spacing": [{_attr: {"w:before": 50, "w:after": 150}}]}, + ], + }); + }); + + it("#center", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerLetter", "%0.") + .center(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:pPr": [ + {"w:jc": [{_attr: {"w:val": "center"}}]}, + ], + }); + }); + + it("#left", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.", "left") + .left(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:pPr": [ + {"w:jc": [{_attr: {"w:val": "left"}}]}, + ], + }); + }); + + it("#right", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .right(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:pPr": [ + {"w:jc": [{_attr: {"w:val": "right"}}]}, + ], + }); + }); + + it("#justified", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .justified(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:pPr": [ + {"w:jc": [{_attr: {"w:val": "both"}}]}, + ], + }); + }); + + it("#thematicBreak", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .thematicBreak(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:pPr": [ + {"w:pBdr": [{"w:bottom": [{_attr: { + "w:color": "auto", + "w:space": "1", + "w:val": "single", + "w:sz": "6", + }}]}]}, + ], + }); + }); + + it("#leftTabStop", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .leftTabStop(1200); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:pPr": [ + {"w:tabs": [ + {"w:tab": [{_attr: {"w:val": "left", "w:pos": 1200}}]}, + ]}, + ], + }); + }); + + it("#maxRightTabStop", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .maxRightTabStop(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:pPr": [ + {"w:tabs": [ + {"w:tab": [{_attr: {"w:val": "right", "w:pos": 9026}}]}, + ]}, + ], + }); + }); + }); }); }); From 762fc0ca9db084281b4a8b1f878d98c5c7da2c9b Mon Sep 17 00:00:00 2001 From: felipe Date: Mon, 13 Mar 2017 12:30:36 +0100 Subject: [PATCH 10/15] added run formatting methods to levels --- ts/numbering/level.ts | 63 ++++++++++++++ ts/tests/numberingTest.ts | 170 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 233 insertions(+) diff --git a/ts/numbering/level.ts b/ts/numbering/level.ts index bc59625212..4aabee238a 100644 --- a/ts/numbering/level.ts +++ b/ts/numbering/level.ts @@ -1,5 +1,6 @@ import * as paragraph from "../docx/paragraph/formatting"; import { ParagraphProperties } from "../docx/paragraph/properties"; +import * as formatting from "../docx/run/formatting"; import { RunProperties } from "../docx/run/properties"; import { Attributes, XmlAttributeComponent, XmlComponent } from "../docx/xml-components"; @@ -88,6 +89,68 @@ export class Level extends XmlComponent { return this; } + // ---------- Run formatting ---------------------- // + + public size(twips: number): Level { + this.addRunProperty(new formatting.Size(twips)); + return this; + } + + public bold(): Level { + this.addRunProperty(new formatting.Bold()); + return this; + } + + public italics(): Level { + this.addRunProperty(new formatting.Italics()); + return this; + } + + public smallCaps(): Level { + this.addRunProperty(new formatting.SmallCaps()); + return this; + } + + public allCaps(): Level { + this.addRunProperty(new formatting.Caps()); + return this; + } + + public strike(): Level { + this.addRunProperty(new formatting.Strike()); + return this; + } + + public doubleStrike(): Level { + this.addRunProperty(new formatting.DoubleStrike()); + return this; + } + + public subScript(): Level { + this.addRunProperty(new formatting.SubScript()); + return this; + } + + public superScript(): Level { + this.addRunProperty(new formatting.SuperScript()); + return this; + } + + public underline(underlineType?: string, color?: string): Level { + this.addRunProperty(new formatting.Underline(underlineType, color)); + return this; + } + + public color(color: string): Level { + this.addRunProperty(new formatting.Color(color)); + return this; + } + + public font(fontName: string): Level { + this.addRunProperty(new formatting.RunFonts(fontName)); + return this; + } + // --------------------- Paragraph formatting ------------------------ // public center(): Level { diff --git a/ts/tests/numberingTest.ts b/ts/tests/numberingTest.ts index 210cbe8a43..59a71e720e 100644 --- a/ts/tests/numberingTest.ts +++ b/ts/tests/numberingTest.ts @@ -222,5 +222,175 @@ describe("AbstractNumbering", () => { }); }); }); + + describe("formatting methods: run properties", () => { + it("#size", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .size(24); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:sz": [{_attr: {"w:val": 24}}]}, + ], + }); + }); + + it("#smallCaps", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .smallCaps(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:smallCaps": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + it("#allCaps", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .allCaps(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:caps": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + it("#strike", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .strike(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:strike": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + it("#doubleStrike", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .doubleStrike(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:dstrike": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + it("#subScript", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .subScript(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:vertAlign": [{_attr: {"w:val": "subscript"}}]}, + ], + }); + }); + + it("#superScript", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .superScript(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:vertAlign": [{_attr: {"w:val": "superscript"}}]}, + ], + }); + }); + + it("#font", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .font("Times"); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [{"w:rFonts": [{_attr: {"w:ascii": "Times", "w:hAnsi": "Times"}}]}], + }); + }); + + it("#bold", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .bold(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:b": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + it("#italics", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .italics(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:i": [{_attr: {"w:val": true}}]}, + ], + }); + }); + + describe("#underline", () => { + it("should set underline to 'single' if no arguments are given", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .underline(); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:u": [{_attr: {"w:val": "single"}}]}, + ], + }); + }); + + it("should set the style if given", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .underline("double"); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:u": [{_attr: {"w:val": "double"}}]}, + ], + }); + }); + + it("should set the style and color if given", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .underline("double", "005599"); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:u": [{_attr: {"w:val": "double", "w:color": "005599"}}]}, + ], + }); + }); + }); + + it("#color", () => { + const abstractNumbering = new AbstractNumbering(1); + const level = abstractNumbering.createLevel(0, "lowerRoman", "%0.") + .color("123456"); + const tree = new Formatter().format(level); + expect(tree["w:lvl"]).to.include({ + "w:rPr": [ + {"w:color": [{_attr: {"w:val": "123456"}}]}, + ], + }); + }); + }); }); }); From 245f0ae2eaa950e489310fe33ef1d7fd281e691b Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 13 Mar 2017 12:30:22 +0000 Subject: [PATCH 11/15] added gitter integration --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 31ad40e1eb..dc5fb85aea 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ===== -[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Known Vulnerabilities][snky-image]][snky-url] +[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Known Vulnerabilities][snky-image]][snky-url] [![Chat on Gitter][gitter-image]][gitter-url] [![NPM](https://nodei.co/npm/docx.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/docx/) @@ -78,3 +78,5 @@ Huge thanks to [@felipeochoa](https://github.com/felipeochoa) for awesome contri [daviddm-url]: https://david-dm.org/dolanmiu/docx [snky-image]: https://snyk.io/test/github/dolanmiu/docx/badge.svg [snky-url]: https://snyk.io/test/github/dolanmiu/docx +[gitter-image]: https://badges.gitter.im/dolanmiu/docx.svg +[gitter-url]: https://gitter.im/docx-lib/Lobby From d7a70ffe03f62d7b5dfd0fd7c2c067c03f33ddab Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 13 Mar 2017 16:14:03 +0000 Subject: [PATCH 12/15] use gemnasium rather than david-dm --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dc5fb85aea..31104a59ec 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ===== -[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Known Vulnerabilities][snky-image]][snky-url] [![Chat on Gitter][gitter-image]][gitter-url] +[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][gemnasium-image]][gemnasium-url] [![Known Vulnerabilities][snky-image]][snky-url] [![Chat on Gitter][gitter-image]][gitter-url] [![NPM](https://nodei.co/npm/docx.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/docx/) @@ -80,3 +80,6 @@ Huge thanks to [@felipeochoa](https://github.com/felipeochoa) for awesome contri [snky-url]: https://snyk.io/test/github/dolanmiu/docx [gitter-image]: https://badges.gitter.im/dolanmiu/docx.svg [gitter-url]: https://gitter.im/docx-lib/Lobby +[gemnasium-image]: https://gemnasium.com/badges/github.com/dolanmiu/docx.svg +[gemnasium-url]: https://gemnasium.com/github.com/dolanmiu/docx + From 0c128ae4a59186f18127b22ee2078a42359cdb28 Mon Sep 17 00:00:00 2001 From: felipe Date: Mon, 13 Mar 2017 20:35:27 +0100 Subject: [PATCH 13/15] add template to npm bundle --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 2cee1629ad..c8751c321d 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ }, "files": [ "ts", - "build" + "build", + "template" ], "repository": { "type": "git", From c7b7b585bef0f43475b505c459e84495df4127dd Mon Sep 17 00:00:00 2001 From: felipe Date: Mon, 13 Mar 2017 20:35:42 +0100 Subject: [PATCH 14/15] fix path to template when installed via npm I'm not sure why, buy app-root-path was not picking up the correct path for this module when installed via NPM. Since we're already using relative paths for imports, I don't think we gain much from using it anyway, so I've changed to using path.resolve instead --- package.json | 2 -- ts/export/packer/packer.ts | 8 +++++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index c8751c321d..1408a7fdde 100644 --- a/package.json +++ b/package.json @@ -29,10 +29,8 @@ "clippy" ], "dependencies": { - "@types/app-root-path": "^1.2.4", "@types/archiver": "^0.15.37", "@types/express": "^4.0.35", - "app-root-path": "^2.0.1", "archiver": "^1.3.0", "xml": "^1.0.1" }, diff --git a/ts/export/packer/packer.ts b/ts/export/packer/packer.ts index 3da1291911..167d178f8a 100644 --- a/ts/export/packer/packer.ts +++ b/ts/export/packer/packer.ts @@ -1,5 +1,5 @@ -import * as appRoot from "app-root-path"; import * as archiver from "archiver"; +import * as path from "path"; import * as xml from "xml"; import { Document } from "../../docx"; import { Numbering } from "../../numbering"; @@ -8,6 +8,8 @@ import { Styles } from "../../styles"; import { DefaultStylesFactory } from "../../styles/factory"; import { Formatter } from "../formatter"; +const templatePath = path.resolve(__dirname, "../../../template"); + export abstract class Packer { protected archive: any; protected document: Document; @@ -53,12 +55,12 @@ export abstract class Packer { this.archive.pipe(output); this.archive.glob("**", { expand: true, - cwd: appRoot.path + "/template", + cwd: templatePath, }); this.archive.glob("**/.rels", { expand: true, - cwd: appRoot.path + "/template", + cwd: templatePath, }); const xmlDocument = xml(this.formatter.format(this.document)); From 486777b1080646b9a1a8095a07c6347ae2eda2e2 Mon Sep 17 00:00:00 2001 From: Dolan Date: Mon, 13 Mar 2017 23:35:24 +0000 Subject: [PATCH 15/15] named this addRun because I realised Text can be a Run, but also a Picture can be a run --- ts/docx/paragraph/index.ts | 5 +++-- ts/tests/export/formatterTest.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ts/docx/paragraph/index.ts b/ts/docx/paragraph/index.ts index a85c014764..4ae172eb1b 100644 --- a/ts/docx/paragraph/index.ts +++ b/ts/docx/paragraph/index.ts @@ -1,4 +1,5 @@ import { Num } from "../../numbering/num"; +import { Run } from "../run"; import { TextRun } from "../run/text-run"; import { XmlComponent } from "../xml-components"; @@ -24,14 +25,14 @@ export class Paragraph extends XmlComponent { } } - public addText(run: TextRun): Paragraph { + public addRun(run: Run): Paragraph { this.root.push(run); return this; } public createTextRun(text: string): TextRun { const run = new TextRun(text); - this.addText(run); + this.addRun(run); return run; } diff --git a/ts/tests/export/formatterTest.ts b/ts/tests/export/formatterTest.ts index 12222c583f..1e69be4e18 100644 --- a/ts/tests/export/formatterTest.ts +++ b/ts/tests/export/formatterTest.ts @@ -29,7 +29,7 @@ describe("Formatter", () => { it("should format simple paragraph with bold text", () => { const paragraph = new docx.Paragraph(); - paragraph.addText(new docx.TextRun("test").bold()); + paragraph.addRun(new docx.TextRun("test").bold()); const newJson = formatter.format(paragraph); assert.isDefined(newJson["w:p"][1]["w:r"][0]["w:rPr"][0]["w:b"][0]._attr["w:val"]); });