-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileRenderer.js
127 lines (102 loc) · 2.98 KB
/
fileRenderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require('fs');
const R = require('ramda');
const cheerio = require('cheerio');
const highlight = require('highlight.js');
const constants = require('./constants.js')
const chooseLanguage = require('./languageChooser');
class FileRenderer {
constructor(filename) {
this.textNodes = [];
this.fileName = filename;
this.file = fs.readFileSync(filename, 'utf8');
let language = chooseLanguage(filename);
console.log(this.fileName, language)
var highlighted;
if (language) {
highlighted = highlight.highlight(language, this.file);
}
else {
highlighted = highlight.highlightAuto(this.file);
}
let html = highlighted.value
let $ = cheerio.load(html);
this.recurseNodes($(':root'), undefined, $);
// put \n's in their own nodes by themselves
this.textNodes = R.flatten(this.textNodes.map(n => {
let splitText = this._newlineSplitter(n.text);
return splitText.map(t => {
return R.merge(n, { text: t });
})
}));
}
_newlineSplitter (text) {
let splitted = text.split(/\n/);
if (splitted.length === 1) {
return splitted;
}
return R.intersperse("\n", splitted).filter(t => {
return t.length !== 0;
});
}
_replaceTabs (text) {
return text.replace(/\t/g, ' ');
}
recurseNodes (el, currentClass, $) {
currentClass = currentClass || 'hljs';
var thisElem = $(el);
if (thisElem[0].tagName === 'span') {
let className = thisElem.attr('class');
if (className) {
currentClass = className;
}
}
if (thisElem[0].nodeType === constants.TEXT_NODE) {
this.textNodes.push({
"class": currentClass,
"text": this._replaceTabs(thisElem.text())
});
}
let children = thisElem.contents();
for (let index = 0; index < children.length; index++) {
this.recurseNodes(children[index], currentClass, $);
}
}
_renderTitle(doc) {
let padding = 6;
doc.font(constants.FONT.HEADER).fontSize(constants.FONT.HEADER_SIZE);
let height = doc.heightOfString(this.fileName) + padding*2;
doc.moveDown();
let y = doc.y - padding;
doc.rect(0, y, constants.PAPER.A4.WIDTH, height)
.fillColor('#eee')
.fill();
doc.fillColor('#333')
.text(this.fileName)
.moveDown()
.fontSize()
}
render(doc, theme) {
this._renderTitle(doc);
doc.fontSize(constants.FONT.BODY_SIZE);
this.textNodes.forEach((node) => {
let format = theme.match(node.class, 'hljs') || {};
let opts = {
continued: node.text !== "\n",
};
if (format.color) {
doc.fillColor(format.color);
}
// if (format['font-style'] === 'italic') {
// doc.font('fonts/SourceCodePro-Italic.ttf');
// }
if (format['font-weight'] === 'bold') {
doc.font(constants.FONT.BODY_BOLD);
}
else {
doc.font(constants.FONT.BODY_REGULAR)
}
doc.text(node.text, opts);
})
}
}
module.exports = FileRenderer;