Skip to content

Commit 45ff696

Browse files
committed
Rewrite to let all interfaces pass through options
1 parent cda0e86 commit 45ff696

File tree

2 files changed

+56
-47
lines changed

2 files changed

+56
-47
lines changed

index.js

+20-36
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,39 @@
22
const parse = require('rtf-parser')
33
const rtfToHTML = require('./rtf-to-html.js')
44

5-
const htmlOptions = {
6-
template: function (doc, defaults, content) {
7-
return `<!DOCTYPE html>
8-
<html>
9-
<head>
10-
<meta charset="UTF-8">
11-
<style>
12-
body {
13-
margin-left: ${doc.marginLeft / 20}pt;
14-
margin-right: ${doc.marginRight / 20}pt;
15-
margin-top: ${doc.marginTop / 20}pt;
16-
margin-bottom: ${doc.marginBottom / 20}pt;
17-
font-size: ${defaults.fontSize / 2}pt;
18-
text-indent: ${defaults.firstLineIndent / 20}pt;
19-
}
20-
</style>
21-
</head>
22-
<body>
23-
${content.replace(/\n/, '\n ')}
24-
</body>
25-
</html>
26-
`
27-
},
28-
paragraphTag: 'p',
29-
lineBreaks: '\n\n'
30-
}
31-
325
module.exports = asStream
336
module.exports.fromStream = fromStream
347
module.exports.fromString = fromString
35-
module.exports.htmlOptions = htmlOptions
368

37-
function asStream (cb) {
38-
return parse(htmlifyresult(cb))
9+
function asStream (opts, cb) {
10+
if (arguments.length === 1) {
11+
cb = opts
12+
opts = null
13+
}
14+
return parse(htmlifyresult(opts, cb))
3915
}
4016

41-
function fromStream (stream, cb) {
42-
return parse.stream(stream, htmlifyresult(cb))
17+
function fromStream (stream, opts, cb) {
18+
if (arguments.length === 2) {
19+
cb = opts
20+
opts = null
21+
}
22+
return parse.stream(stream, htmlifyresult(opts, cb))
4323
}
4424

45-
function fromString (string, cb) {
46-
return parse.string(string, htmlifyresult(cb))
25+
function fromString (string, opts, cb) {
26+
if (arguments.length === 2) {
27+
cb = opts
28+
opts = null
29+
}
30+
return parse.string(string, htmlifyresult(opts, cb))
4731
}
4832

49-
function htmlifyresult (cb) {
33+
function htmlifyresult (opts, cb) {
5034
return (err, doc) => {
5135
if (err) return cb(err)
5236
try {
53-
return cb(null, rtfToHTML(doc, htmlOptions))
37+
return cb(null, rtfToHTML(doc, opts))
5438
} catch (ex) {
5539
return cb(ex)
5640
}

rtf-to-html.js

+36-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
'use strict'
22
module.exports = rtfToHTML
33

4+
function outputTemplate (doc, defaults, content) {
5+
return `<!DOCTYPE html>
6+
<html>
7+
<head>
8+
<meta charset="UTF-8">
9+
<style>
10+
body {
11+
margin-left: ${doc.marginLeft / 20}pt;
12+
margin-right: ${doc.marginRight / 20}pt;
13+
margin-top: ${doc.marginTop / 20}pt;
14+
margin-bottom: ${doc.marginBottom / 20}pt;
15+
font-size: ${defaults.fontSize / 2}pt;
16+
text-indent: ${defaults.firstLineIndent / 20}pt;
17+
}
18+
</style>
19+
</head>
20+
<body>
21+
${content.replace(/\n/, '\n ')}
22+
</body>
23+
</html>
24+
`
25+
}
26+
427
function rtfToHTML (doc, options) {
5-
const defaults = {
6-
// font: doc.style.font || {name: 'Times', family: 'roman'},
28+
const defaults = Object.assign({
29+
font: doc.style.font || {name: 'Times', family: 'roman'},
730
fontSize: doc.style.fontSize || 24,
831
bold: false,
932
italic: false,
@@ -16,10 +39,12 @@ function rtfToHTML (doc, options) {
1639
align: 'left',
1740
valign: 'normal',
1841

19-
options: options
20-
}
21-
const content = doc.content.map(para => renderPara(para, defaults)).filter(html => html != null).join(options.lineBreaks)
22-
return options.template(doc, defaults, content)
42+
paraBreaks: '\n\n',
43+
paraTag: 'p',
44+
template: outputTemplate
45+
}, options || {})
46+
const content = doc.content.map(para => renderPara(para, defaults)).filter(html => html != null).join(defaults.paraBreaks)
47+
return defaults.template(doc, defaults, content)
2348
}
2449

2550
function font (ft) {
@@ -63,9 +88,9 @@ function CSS (chunk, defaults) {
6388
if (chunk.style.fontSize != null && chunk.style.fontSize !== defaults.fontSize) {
6489
css += `font-size: ${chunk.style.fontSize / 2}pt;`
6590
}
66-
// if (chunk.style.font != null && chunk.style.font.name !== defaults.font.name) {
67-
// css += font(chunk.style.font)
68-
// }
91+
if (!defaults.disableFonts && chunk.style.font != null && chunk.style.font.name !== defaults.font.name) {
92+
css += font(chunk.style.font)
93+
}
6994
return css
7095
}
7196

@@ -108,8 +133,8 @@ function renderPara (para, defaults) {
108133
for (let item of Object.keys(para.style)) {
109134
if (para.style[item] != null) pdefaults[item] = para.style[item]
110135
}
111-
let paragraphTag = defaults.options.paragraphTag
112-
return `<${paragraphTag}${style ? ' style="' + style + '"' : ''}>${tags.open}${para.content.map(span => renderSpan(span, pdefaults)).join('')}${tags.close}</${paragraphTag}>`
136+
const paraTag = defaults.paraTag
137+
return `<${paraTag}${style ? ' style="' + style + '"' : ''}>${tags.open}${para.content.map(span => renderSpan(span, pdefaults)).join('')}${tags.close}</${paraTag}>`
113138
}
114139

115140
function renderSpan (span, defaults) {

0 commit comments

Comments
 (0)