-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (76 loc) · 2.83 KB
/
index.js
File metadata and controls
88 lines (76 loc) · 2.83 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
var Q = require('q');
var vizjs = require('viz.js');
// var ASSET_PATH = 'assets/images/graphviz/';
function processBlock(blk) {
var deferred = Q.defer();
var book = this;
var code = blk.body;
var config = book.config.get('pluginsConfig.graphviz', {});
if (blk.kwargs['config']) {
config = blk.kwargs['config'];
}
var format = "svg";
if (config && config.format)
format = config.format;
var engine = "dot";
if (config && config.engine)
engine = config.engine;
var result = vizjs(code, { format: format, engine: engine})
deferred.resolve(result);
return deferred.promise;
}
module.exports = {
blocks: {
graphviz: {
process: processBlock
}
},
hooks: {
// For all the hooks, this represent the current generator
// [init", "finish", "finish:before", "page", "page:before"] are working.
// page:* are marked as deprecated because it's better if plugins start using blocks instead.
// But page and page:before will probably stay at the end (useful in some cases).
// This is called before the book is generated
"init": function() {
if (!Object.keys(this.book.config.get('pluginsConfig.graphviz', {})).length) {
this.book.config.set('pluginsConfig.graphviz', {
format: 'svg'
});
}
},
// This is called after the book generation
"finish": function() {
// Done
},
// Before the end of book generation
"finish:before": function() {
// Nothing to do
},
// The following hooks are called for each page of the book
// and can be used to change page content (html, data or markdown)
// Before parsing documents
"page:before": function(page) {
// Get all code texts
umls = page.content.match(/^```dot((.*[\r\n]+)+?)?```$/igm);
// Begin replace
if (umls instanceof Array) {
for (var i = 0, len = umls.length; i < len; i++) {
page.content = page.content.replace(
umls[i],
umls[i].replace(/^```dot/, '{% graphviz %}').replace(/```$/, '{% endgraphviz %}'));
}
}
// Get all code texts
umls = page.content.match(/^```graphviz((.*[\r\n]+)+?)?```$/igm);
// Begin replace
if (umls instanceof Array) {
for (var i = 0, len = umls.length; i < len; i++) {
page.content = page.content.replace(
umls[i],
umls[i].replace(/^```graphviz/, '{% graphviz %}').replace(/```$/, '{% endgraphviz %}'));
}
}
return page;
}
}
};