easy-template-x
Advanced tools
Comparing version 2.0.0 to 2.1.0
# Change Log | ||
## [2.1.0 - 2021-07-29](https://github.com/alonrbar/easy-template-x/tree/v2.1.0) | ||
### Added | ||
- Add skipEmptyTag option ([#45](https://github.com/alonrbar/easy-template-x/issues/45)). | ||
## [2.0.0 - 2021-03-19](https://github.com/alonrbar/easy-template-x/tree/v2.0.0) | ||
@@ -4,0 +10,0 @@ |
@@ -8,9 +8,13 @@ import { TemplatePlugin } from '../plugins'; | ||
import { TemplateContext } from './templateContext'; | ||
export interface TemplateCompilerOptions { | ||
defaultContentType: string; | ||
containerContentType: string; | ||
skipEmptyTags?: boolean; | ||
} | ||
export declare class TemplateCompiler { | ||
private readonly delimiterSearcher; | ||
private readonly tagParser; | ||
private readonly defaultContentType; | ||
private readonly containerContentType; | ||
private readonly options; | ||
private readonly pluginsLookup; | ||
constructor(delimiterSearcher: DelimiterSearcher, tagParser: TagParser, plugins: TemplatePlugin[], defaultContentType: string, containerContentType: string); | ||
constructor(delimiterSearcher: DelimiterSearcher, tagParser: TagParser, plugins: TemplatePlugin[], options: TemplateCompilerOptions); | ||
compile(node: XmlNode, data: ScopeData, context: TemplateContext): Promise<void>; | ||
@@ -20,3 +24,4 @@ parseTags(node: XmlNode): Tag[]; | ||
private detectContentType; | ||
private simpleTagReplacements; | ||
private findCloseTagIndex; | ||
} |
@@ -7,2 +7,3 @@ import { ScopeDataResolver } from './compilation'; | ||
plugins?: TemplatePlugin[]; | ||
skipEmptyTags?: boolean; | ||
defaultContentType?: string; | ||
@@ -9,0 +10,0 @@ containerContentType?: string; |
@@ -8,2 +8,3 @@ export * from './array'; | ||
export * from './sha1'; | ||
export * from './txt'; | ||
export * from './types'; |
{ | ||
"name": "easy-template-x", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Generate docx documents from templates, in Node or in the browser.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
import { UnclosedTagError, UnknownContentTypeError, UnopenedTagError } from '../errors'; | ||
import { PluginContent, TemplatePlugin } from '../plugins'; | ||
import { IMap } from '../types'; | ||
import { isPromiseLike, toDictionary } from '../utils'; | ||
import { isPromiseLike, stringValue, toDictionary } from '../utils'; | ||
import { XmlNode } from '../xml'; | ||
@@ -12,2 +12,8 @@ import { DelimiterSearcher } from './delimiterSearcher'; | ||
export interface TemplateCompilerOptions { | ||
defaultContentType: string; | ||
containerContentType: string; | ||
skipEmptyTags?: boolean; | ||
} | ||
/** | ||
@@ -31,4 +37,3 @@ * The TemplateCompiler works roughly the same way as a source code compiler. | ||
plugins: TemplatePlugin[], | ||
private readonly defaultContentType: string, | ||
private readonly containerContentType: string | ||
private readonly options: TemplateCompilerOptions | ||
) { | ||
@@ -74,9 +79,4 @@ this.pluginsLookup = toDictionary(plugins, p => p.contentType); | ||
if (tag.disposition === TagDisposition.SelfClosed) { | ||
await this.simpleTagReplacements(plugin, tag, data, context); | ||
// replace simple tag | ||
const job = plugin.simpleTagReplacements(tag, data, context); | ||
if (isPromiseLike(job)) { | ||
await job; | ||
} | ||
} else if (tag.disposition === TagDisposition.Open) { | ||
@@ -109,8 +109,19 @@ | ||
if (tag.disposition === TagDisposition.Open || tag.disposition === TagDisposition.Close) | ||
return this.containerContentType; | ||
return this.options.containerContentType; | ||
// implicit - text | ||
return this.defaultContentType; | ||
return this.options.defaultContentType; | ||
} | ||
private async simpleTagReplacements(plugin: TemplatePlugin, tag: Tag, data: ScopeData, context: TemplateContext): Promise<void> { | ||
if (this.options.skipEmptyTags && stringValue(data.getScopeData()) === '') { | ||
return; | ||
} | ||
const job = plugin.simpleTagReplacements(tag, data, context); | ||
if (isPromiseLike(job)) { | ||
await job; | ||
} | ||
} | ||
private findCloseTagIndex(fromIndex: number, openTag: Tag, tags: Tag[]): number { | ||
@@ -117,0 +128,0 @@ let openTags = 0; |
import { ScopeData, Tag } from '../../compilation'; | ||
import { DocxParser } from '../../office'; | ||
import { stringValue } from '../../utils'; | ||
import { XmlNode, XmlTextNode } from '../../xml'; | ||
@@ -18,4 +19,3 @@ import { TemplatePlugin } from '../templatePlugin'; | ||
const value = data.getScopeData(); | ||
const stringValue = (value === null || value === undefined) ? '' : value.toString(); | ||
const lines = stringValue.split('\n'); | ||
const lines = stringValue(value).split('\n'); | ||
@@ -22,0 +22,0 @@ if (lines.length < 2) { |
@@ -46,4 +46,7 @@ import { DelimiterSearcher, ScopeData, Tag, TagParser, TemplateCompiler, TemplateContext } from './compilation'; | ||
this.options.plugins, | ||
this.options.defaultContentType, | ||
this.options.containerContentType | ||
{ | ||
skipEmptyTags: this.options.skipEmptyTags, | ||
defaultContentType: this.options.defaultContentType, | ||
containerContentType: this.options.containerContentType | ||
} | ||
); | ||
@@ -50,0 +53,0 @@ |
@@ -10,2 +10,11 @@ import { ScopeDataResolver } from './compilation'; | ||
/** | ||
* Determines the behavior in case of an empty input data. If set to true | ||
* the tag will be left untouched, if set to false the tag will be replaced | ||
* by an empty string. | ||
* | ||
* Default: false | ||
*/ | ||
public skipEmptyTags?= false; | ||
public defaultContentType?= TEXT_CONTENT_TYPE; | ||
@@ -12,0 +21,0 @@ |
@@ -8,2 +8,3 @@ export * from './array'; | ||
export * from './sha1'; | ||
export * from './txt'; | ||
export * from './types'; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
390335
163
9250