1
1
'use strict'
2
2
module . exports = rtfToHTML
3
3
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
+
4
27
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' } ,
7
30
fontSize : doc . style . fontSize || 24 ,
8
31
bold : false ,
9
32
italic : false ,
@@ -16,10 +39,12 @@ function rtfToHTML (doc, options) {
16
39
align : 'left' ,
17
40
valign : 'normal' ,
18
41
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 )
23
48
}
24
49
25
50
function font ( ft ) {
@@ -63,9 +88,9 @@ function CSS (chunk, defaults) {
63
88
if ( chunk . style . fontSize != null && chunk . style . fontSize !== defaults . fontSize ) {
64
89
css += `font-size: ${ chunk . style . fontSize / 2 } pt;`
65
90
}
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
+ }
69
94
return css
70
95
}
71
96
@@ -108,8 +133,8 @@ function renderPara (para, defaults) {
108
133
for ( let item of Object . keys ( para . style ) ) {
109
134
if ( para . style [ item ] != null ) pdefaults [ item ] = para . style [ item ]
110
135
}
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 } >`
113
138
}
114
139
115
140
function renderSpan ( span , defaults ) {
0 commit comments