|
| 1 | +'use strict' |
| 2 | +module.exports = rtfToHTML |
| 3 | + |
| 4 | +function rtfToHTML (doc) { |
| 5 | + const defaults = { |
| 6 | + font: doc.content[0].style.font || {name: 'Times', family: 'roman'}, |
| 7 | + fontSize: doc.content[0].style.fontSize || 24, |
| 8 | + bold: false, |
| 9 | + italic: false, |
| 10 | + underline: false, |
| 11 | + strikethrough: false, |
| 12 | + foreground: {red: 0, blue: 0, green: 0}, |
| 13 | + background: {red: 255, blue: 255, green: 255}, |
| 14 | + firstLineIndent: doc.content[0].style.firstLineIndent || 0, |
| 15 | + indent: 0, |
| 16 | + align: 'left', |
| 17 | + valign: 'normal' |
| 18 | + } |
| 19 | + const content = doc.content.map(para => renderPara(para, defaults)).filter(html => html != null).join('\n\n') |
| 20 | + return `<!DOCTYPE html> |
| 21 | +<html> |
| 22 | + <head> |
| 23 | + <meta charset="UTF-8"> |
| 24 | + <style> |
| 25 | + body { |
| 26 | + margin-left: ${doc.marginLeft / 20}pt; |
| 27 | + margin-right: ${doc.marginRight / 20}pt; |
| 28 | + margin-top: ${doc.marginTop / 20}pt; |
| 29 | + margin-bottom: ${doc.marginBottom /20}pt; |
| 30 | + font-family: ${defaults.font.name.replace(/-\w+$/,'')}, ${genericFontMap[defaults.font.family]}; |
| 31 | + text-indent: ${defaults.firstLineIndent / 20}pt; |
| 32 | + } |
| 33 | + </style> |
| 34 | + </head> |
| 35 | + <body> |
| 36 | +${content} |
| 37 | + </body> |
| 38 | +</html> |
| 39 | +` |
| 40 | +} |
| 41 | + |
| 42 | +const genericFontMap = { |
| 43 | + roman: 'serif', |
| 44 | + swiss: 'sans-serif', |
| 45 | + script: 'cursive', |
| 46 | + decor: 'fantasy', |
| 47 | + modern: 'sans-serif', |
| 48 | + tech: 'monospace', |
| 49 | + bidi: 'serif' |
| 50 | +} |
| 51 | + |
| 52 | +function colorEq (aa, bb) { |
| 53 | + if (!aa && !bb) return true |
| 54 | + if (!bb) return false |
| 55 | + return aa.red === bb.red && aa.blue === bb.blue && aa.green === bb.green |
| 56 | +} |
| 57 | + |
| 58 | +function CSS (chunk, defaults) { |
| 59 | + let css = '' |
| 60 | + if (chunk.style.foreground != null && !colorEq(chunk.style.foreground, defaults.foreground)) { |
| 61 | + css += `color: rgb(${chunk.style.foreground.red}, ${chunk.style.foreground.green}, ${chunk.style.foreground.blue});` |
| 62 | + } |
| 63 | + if (chunk.style.background != null && !colorEq(chunk.style.background, defaults.background)) { |
| 64 | + css += `background-color: rgb(${chunk.style.background.red}, ${chunk.style.background.green}, ${chunk.style.background.blue});` |
| 65 | + } |
| 66 | + if (chunk.style.firstLineIndent != null && chunk.style.firstLineIndent > 0 && chunk.style.firstLineIndent !== defaults.firstLineIndent) { |
| 67 | + css += `text-indent: ${chunk.style.firstLineIndent / 20}pt;` |
| 68 | + } |
| 69 | + if (chunk.style.indent != null && chunk.style.indent !== defaults.indent) { |
| 70 | + css += `padding-left: ${chunk.style.indent / 20}pt;` |
| 71 | + } |
| 72 | + if (chunk.style.align != null && chunk.style.align !== defaults.align) { |
| 73 | + css += `text-align: ${chunk.style.align};` |
| 74 | + } |
| 75 | + if (chunk.style.fontSize != null && chunk.style.fontSize !== defaults.fontSize) { |
| 76 | + css += `font-size: ${chunk.style.fontSize / 2}pt;` |
| 77 | + } |
| 78 | + if (chunk.style.font != null && chunk.style.font.name !== defaults.font.name) { |
| 79 | + css += `font-family: ${chunk.style.font.name.replace(/-\w+$/,'')}, ${genericFontMap[chunk.style.font.family]};` |
| 80 | + } |
| 81 | + return css |
| 82 | +} |
| 83 | + |
| 84 | +function styleTags (chunk, defaults) { |
| 85 | + let open = '' |
| 86 | + let close = '' |
| 87 | + if (chunk.style.italic != null && chunk.style.italic !== defaults.italic) { |
| 88 | + open += '<em>' |
| 89 | + close = '</em>' + close |
| 90 | + } |
| 91 | + if (chunk.style.bold != null && chunk.style.bold !== defaults.bold) { |
| 92 | + open += '<strong>' |
| 93 | + close = '</strong>' + close |
| 94 | + } |
| 95 | + if (chunk.style.strikethrough != null && chunk.style.strikethrough !== defaults.strikethrough) { |
| 96 | + open += '<s>' |
| 97 | + close = '</s>' + close |
| 98 | + } |
| 99 | + if (chunk.style.underline != null && chunk.style.underline !== defaults.underline) { |
| 100 | + open += '<u>' |
| 101 | + close = '</u>' + close |
| 102 | + } |
| 103 | + if (chunk.style.valign != null && chunk.style.valign !== defaults.valign) { |
| 104 | + if (chunk.style.valign === 'super') { |
| 105 | + open += '<sup>' |
| 106 | + close = '</sup>' + close |
| 107 | + } else if (chunk.style.valign === 'sub') { |
| 108 | + open += '<sup>' |
| 109 | + close = '</sup>' + close |
| 110 | + } |
| 111 | + } |
| 112 | + return {open, close} |
| 113 | +} |
| 114 | + |
| 115 | +function renderPara (para, defaults) { |
| 116 | + if (para.content.length === 0) return |
| 117 | + const style = CSS(para, defaults) |
| 118 | + const tags = styleTags(para, defaults) |
| 119 | + return `<p${style ? ' style="' + style + '"' : ''}>${tags.open}${para.content.map(span => renderSpan(span, Object.assign({}, defaults, para.style))).join('')}${tags.close}</p>` |
| 120 | +} |
| 121 | + |
| 122 | +function renderSpan (span, defaults) { |
| 123 | + const style = CSS(span, defaults) |
| 124 | + const tags = styleTags(span, defaults) |
| 125 | + const value = `${tags.open}${span.value}${tags.close}` |
| 126 | + if (style) { |
| 127 | + return `<span style="${style}">${value}</span>` |
| 128 | + } else { |
| 129 | + return value |
| 130 | + } |
| 131 | +} |
0 commit comments