forked from moinejf/abc2svg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoxhtml.js
160 lines (146 loc) · 4.06 KB
/
toxhtml.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// abc2svg - toxhtml.js - SVG generation
//
// Copyright (C) 2014-2017 Jean-Francois Moine
//
// This file is part of abc2svg.
//
// abc2svg is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// abc2svg is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
// replace <>& by XML character references
function clean_txt(txt) {
return txt.replace(/<|>|&.*?;|&/g, function(c) {
switch (c) {
case '<': return "<"
case '>': return ">"
}
if (c == '&')
return "&"
return c
})
}
function abort(e) {
abc.blk_out();
abc.blk_flush();
if (errtxt)
print("<pre>" + clean_txt(errtxt) + "</pre>");
print("<pre>" + e.message + "\n*** Abort ***\n" + e.stack + "</pre>");
print("</body> </html>");
quit()
}
// entry point from cmdline
function abc_init() {
function get_date() {
var now = new Date()
return now.toUTCString()
} // get_date()
// output a header or footer
function gen_hf(type, str) {
var a, i, page,
lcr = ["left", "center", "right"];
a = abc.header_footer(str)
for (i = 0; i < 3; i++) {
if (!a[i])
continue
str = a[i]
if (str.indexOf('\n') >= 0)
str = str.replace('\n', '<br/>')
if (str.indexOf('\x0c') >= 0) {
str = str.replace('\x0c', '');
page = " page"
} else {
page = ''
}
print('<div class="' + type + ' ' + lcr[i] + page + '">' +
str + '</div>')
}
}
user.page_format = true;
// output the xhtml header
user.img_out = function(str) {
var header = abc.get_fmt("header"),
footer = abc.get_fmt("footer"),
topmargin = abc.get_fmt("topmargin"),
media_s = ' @media print {\n\
body {margin:0; padding:0; border:0}\n\
div.newpage {page-break-before: always}\n\
div.nobrk {page-break-inside: avoid}\n\
}',
media_f =' @media screen {\n\
div.header {display: none}\n\
div.footer {display: none}\n\
}\n\
@media print {\n\
body {margin:0; padding:0; border:0;\n\
counter-reset: page;\n\
counter-increment: page; }\n\
div.newpage {page-break-before: always}\n\
div.nobrk {page-break-inside: avoid}\n\
div.header {\n\
position: fixed;\n\
top: 0pt;\n\
width: 100%;\n\
' + abc.style_font(abc.get_fmt("headerfont")) + '\n\
}\n\
div.footer {\n\
position: fixed;\n\
bottom: 0pt;\n\
width: 100%;\n\
' + abc.style_font(abc.get_fmt("footerfont")) + '\n\
}\n\
div.page:after {\n\
counter-increment: page;\n\
content: counter(page);\n\
}\n\
div.left {text-align: left}\n\
div.center {text-align: center}\n\
div.right {text-align: right}\n\
}';
if (!topmargin)
topmargin = "1cm";
print('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"\n\
"http://www.w3.org/TR/xhtml1/DTD/xhtml1.dtd">\n\
<html xmlns="http://www.w3.org/1999/xhtml">\n\
<head>\n\
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\n\
<meta name="generator" content="abc2svg-' + abc2svg.version + '"/>\n\
<!-- CreationDate: ' + get_date() + '-->\n\
<style type="text/css">\n\
@page {margin-top: ' + topmargin + '}\n\
text, tspan {white-space:pre}\n\
svg {display:block}\n' +
((header || footer) ? media_f : media_s) + '\n\
</style>\n\
<title>abc2svg document</title>\n\
</head>\n\
<body>')
if (header)
gen_hf("header", header)
if (footer)
gen_hf("footer", footer);
// output the first generated string
print(str);
// change the output function
user.img_out = function(str) { print(str) }
}
}
function abc_end() {
if (errtxt)
print("<pre>" + clean_txt(errtxt) + "</pre>")
print("</body>\n</html>")
}
// nodejs
if (typeof module == 'object' && typeof exports == 'object') {
exports.abort = abort;
exports.abc_init = abc_init;
exports.abc_end = abc_end;
}