rtf-parser
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "rtf-parser", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "This is a general RTF parser. It takes a text stream and produces a document object representing the parsed document. In and of itself, this isn't super useful but it's the building block for other tools to convert RTF into other formats.", | ||
@@ -12,6 +12,15 @@ "main": "index.js", | ||
"standard": "^8.6.0", | ||
"tap": "^9.0.3" | ||
"tap": "^9.0.3", | ||
"weallbehave": "*", | ||
"weallcontribute": "*", | ||
"standard-version": "*" | ||
}, | ||
"scripts": { | ||
"test": "standard && tap test/" | ||
"test": "standard && tap test/", | ||
"prerelease": "npm t", | ||
"release": "standard-version -s", | ||
"postrelease": "npm publish && git push --follow-tags", | ||
"pretest": "standard", | ||
"update-coc": "weallbehave -o . && git add CODE_OF_CONDUCT.md && git commit -m 'docs(coc): updated CODE_OF_CONDUCT.md'", | ||
"update-contrib": "weallcontribute -o . && git add CONTRIBUTING.md && git commit -m 'docs(contributing): updated CONTRIBUTING.md'" | ||
}, | ||
@@ -18,0 +27,0 @@ "keywords": [ |
@@ -42,3 +42,3 @@ # rtf-parser | ||
437, 737, 775, 850, 852, 853, 855, 857, 858, 860, 861, 863, 865, 866, 869, | ||
1125, 1250, 1251, 1252, 1253, 1254, 1257 | ||
932, 1125, 1250, 1251, 1252, 1253, 1254, 1257 | ||
* Unicode characters. | ||
@@ -45,0 +45,0 @@ * Non-unicode representations of: non-breaking spaces, soft hyphens and non-breaking hyphens. |
@@ -13,3 +13,3 @@ 'use strict' | ||
437, 737, 775, 850, 852, 853, 855, 857, 858, 860, 861, 863, 865, 866, | ||
869, 1125, 1250, 1251, 1252, 1253, 1254, 1257 ] | ||
869, 932, 1125, 1250, 1251, 1252, 1253, 1254, 1257 ] | ||
const codeToCP = { | ||
@@ -43,2 +43,3 @@ 0: 'ASCII', | ||
this.once('prefinish', () => this.finisher()) | ||
this.hexStore = [] | ||
} | ||
@@ -55,3 +56,3 @@ _write (cmd, encoding, done) { | ||
finisher () { | ||
while (this.groupStack.length) this.cmd$groupEnd () | ||
while (this.groupStack.length) this.cmd$groupEnd() | ||
const initialStyle = this.doc.content[0].style | ||
@@ -69,4 +70,15 @@ for (let prop of Object.keys(this.doc.style)) { | ||
} | ||
flushHexStore () { | ||
if (this.hexStore.length > 0) { | ||
let hexstr = this.hexStore.map(cmd => cmd.value).join('') | ||
this.group.addContent(new RTFSpan({ | ||
value: iconv.decode( | ||
Buffer.from(hexstr, 'hex'), this.group.get('charset')) | ||
})) | ||
this.hexStore.splice(0) | ||
} | ||
} | ||
cmd$groupStart () { | ||
this.flushHexStore() | ||
if (this.group) this.groupStack.push(this.group) | ||
@@ -76,8 +88,11 @@ this.group = new RTFGroup(this.group || this.doc) | ||
cmd$ignorable () { | ||
this.flushHexStore() | ||
this.group.ignorable = true | ||
} | ||
cmd$endParagraph () { | ||
this.flushHexStore() | ||
this.group.addContent(new RTFParagraph()) | ||
} | ||
cmd$groupEnd () { | ||
this.flushHexStore() | ||
const endingGroup = this.group | ||
@@ -98,5 +113,7 @@ this.group = this.groupStack.pop() | ||
cmd$text (cmd) { | ||
this.flushHexStore() | ||
this.group.addContent(new RTFSpan(cmd)) | ||
} | ||
cmd$controlWord (cmd) { | ||
this.flushHexStore() | ||
if (!this.group.type) this.group.type = cmd.value | ||
@@ -111,6 +128,3 @@ const method = 'ctrl$' + cmd.value.replace(/-(.)/g, (_, char) => char.toUpperCase()) | ||
cmd$hexchar (cmd) { | ||
this.group.addContent(new RTFSpan({ | ||
value: iconv.decode( | ||
Buffer.from(cmd.value, 'hex'), this.group.get('charset')) | ||
})) | ||
this.hexStore.push(cmd) | ||
} | ||
@@ -165,3 +179,5 @@ | ||
var charBuf = Buffer.alloc ? Buffer.alloc(2) : new Buffer(2) | ||
charBuf.writeUInt16LE(num, 0) | ||
// RTF, for reasons, represents unicode characters as signed integers | ||
// thus managing to match literally no one. | ||
charBuf.writeInt16LE(num, 0) | ||
this.group.addContent(new RTFSpan({value: iconv.decode(charBuf, 'ucs2')})) | ||
@@ -231,2 +247,4 @@ } | ||
this.group.currentFont = this.group.table[num] = new Font() | ||
} else if (this.group.parent instanceof FontTable) { | ||
this.group.parent.currentFont = this.group.parent.table[num] = new Font() | ||
} else { | ||
@@ -237,58 +255,58 @@ this.group.style.font = num | ||
ctrl$fnil () { | ||
if (this.group instanceof FontTable) { | ||
this.group.currentFont.family = 'nil' | ||
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) { | ||
this.group.get('currentFont').family = 'nil' | ||
} | ||
} | ||
ctrl$froman () { | ||
if (this.group instanceof FontTable) { | ||
this.group.currentFont.family = 'roman' | ||
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) { | ||
this.group.get('currentFont').family = 'roman' | ||
} | ||
} | ||
ctrl$fswiss () { | ||
if (this.group instanceof FontTable) { | ||
this.group.currentFont.family = 'swiss' | ||
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) { | ||
this.group.get('currentFont').family = 'swiss' | ||
} | ||
} | ||
ctrl$fmodern () { | ||
if (this.group instanceof FontTable) { | ||
this.group.currentFont.family = 'modern' | ||
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) { | ||
this.group.get('currentFont').family = 'modern' | ||
} | ||
} | ||
ctrl$fscript () { | ||
if (this.group instanceof FontTable) { | ||
this.group.currentFont.family = 'script' | ||
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) { | ||
this.group.get('currentFont').family = 'script' | ||
} | ||
} | ||
ctrl$fdecor () { | ||
if (this.group instanceof FontTable) { | ||
this.group.currentFont.family = 'decor' | ||
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) { | ||
this.group.get('currentFont').family = 'decor' | ||
} | ||
} | ||
ctrl$ftech () { | ||
if (this.group instanceof FontTable) { | ||
this.group.currentFont.family = 'tech' | ||
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) { | ||
this.group.get('currentFont').family = 'tech' | ||
} | ||
} | ||
ctrl$fbidi () { | ||
if (this.group instanceof FontTable) { | ||
this.group.currentFont.family = 'bidi' | ||
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) { | ||
this.group.get('currentFont').family = 'bidi' | ||
} | ||
} | ||
ctrl$fcharset (code) { | ||
if (this.group instanceof FontTable) { | ||
let charset = null | ||
if (code === 1) { | ||
charset = this.group.get('charset') | ||
} else { | ||
charset = codeToCP[code] | ||
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) { | ||
let charset = null | ||
if (code === 1) { | ||
charset = this.group.get('charset') | ||
} else { | ||
charset = codeToCP[code] | ||
} | ||
if (charset == null) { | ||
return this.emit('error', new Error('Unsupported charset code #' + code)) | ||
} | ||
this.group.get('currentFont').charset = charset | ||
} | ||
if (charset == null) { | ||
return this.emit('error', new Error('Unsupported charset code #' + code)) | ||
} | ||
this.group.currentFont.charset = charset | ||
} | ||
} | ||
ctrl$fprq (pitch) { | ||
if (this.group instanceof FontTable) { | ||
this.group.currentFont.pitch = pitch | ||
if (this.group instanceof FontTable || this.group.parent instanceof FontTable) { | ||
this.group.get('currentFont').pitch = pitch | ||
} | ||
@@ -359,3 +377,3 @@ } | ||
addContent (text) { | ||
this.currentFont.name = text.value.replace(/;\s*$/, '') | ||
this.currentFont.name += text.value.replace(/;\s*$/, '') | ||
} | ||
@@ -368,3 +386,3 @@ } | ||
this.charset = null | ||
this.name = null | ||
this.name = '' | ||
this.pitch = 0 | ||
@@ -371,0 +389,0 @@ } |
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
25571
11
711
5