-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprinters.js
More file actions
95 lines (85 loc) · 2.2 KB
/
Copy pathprinters.js
File metadata and controls
95 lines (85 loc) · 2.2 KB
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
const archy = require('archy');
const wrapHref = require('./wrapHref');
const wrapHtml = require('./wrapHtml');
/**
* Base printer class. Cannot be implemented directly.
*/
class AbstractPrinter {
constructor() {
if (this.constructor === AbstractPrinter) {
throw new Error('AbstractPrinter is abstract and cannot be constructed directly');
}
}
_dirTreeToArchyTree(node) {
if (!node.children) {
return this.printNode(node);
}
return {
label: this.printNode(node),
nodes: node.children.map(childNode => this._dirTreeToArchyTree(childNode)),
};
}
print(node) {
return archy(this._dirTreeToArchyTree(node))
}
printNode(_node) {
throw new Error('Printer::printNode is not implemented');
}
}
/**
* Prints the tree as simply as possible
*
* Example:
* ```
* ├─┬ A
* │ └── c.html
* ├── a.html
* ├── a.txt
* └── b.html
* ```
*/
class PlainTextPrinter extends AbstractPrinter {
printNode(node) {
return node.name;
}
}
/**
* Prints the tree as an HTML document, with the tree printed
* inside a <pre> element.
*
* Example (simplified):
* ```
* <!doctype html>
* <html>
* <body>
* <pre>
* <a href=\\"./\\">1</a>
* ├─┬ <a href=\\"./A\\">A</a>
* │ └── <a href=\\"./A/c.html\\">c.html</a>
* ├── <a href=\\"./a.html\\">a.html</a>
* ├── <a href=\\"./a.txt\\">a.txt</a>
* └── <a href=\\"./b.html\\">b.html</a>
* </pre>
* </body>
* </html>
* ```
*/
class HtmlPrinter extends AbstractPrinter {
constructor(cwd, linkFolders) {
super();
this.cwd = cwd;
this.linkFolders = linkFolders;
}
print(node) {
const outTree = super.print(node);
return wrapHtml(outTree, node.name);
}
printNode(node) {
if (node.children) {
// any node that has children is a "folder"
return this.linkFolders ? wrapHref(node, this.cwd) : node.name;
}
return wrapHref(node, this.cwd);
}
}
module.exports = { AbstractPrinter, PlainTextPrinter, HtmlPrinter };