forked from moinejf/abc2svg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdline.js
115 lines (104 loc) · 2.54 KB
/
cmdline.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
// abc2svg - cmdline.js - command line
//
// 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/>.
// global variables
var errtxt = ''
// -- replace the exotic end of lines by standard ones
function set_eoln(file) {
var i = file.indexOf('\r')
if (i < 0)
return undefined // standard
if (file[i + 1] == '\n')
return file.replace(/\r\n/g, '\n') // M$
return file.replace(/\r/g, '\n') // Mac
}
// -- abc2svg init argument
var user = {
read_file: function(fn) { // include a file (%%abc-include)
var file = readFile(fn),
file2 = set_eoln(file)
return file2 || file
},
errmsg: function(msg, l, c) { // get the errors
if (typeof printErr == 'function')
printErr(msg)
else
errtxt += msg + '\n'
}
}
var abc = new Abc(user) // (global for 'toxxx.js')
function do_file(fn) {
var file = user.read_file(fn)
if (!file) {
j = fn.lastIndexOf("/")
if (j < 0)
j = 0;
i = fn.indexOf(".", j)
if (i < 0) {
fn += ".abc";
file = user.read_file(fn)
}
}
if (!file)
abort(new Error("Cannot read file '" + fn + "'"))
// if (typeof(utf_convert) == "function")
// file = utf_convert(file)
// if some Postscript definition, load the interpreter
if (file.indexOf('%%beginps') >= 0 && typeof Psvg != "function") {
loadRelativeToScript('psvg-1.js');
abc.ps_def(abc)
}
try {
abc.tosvg(fn, file)
}
catch (e) {
abort(e)
}
}
function abc_cmd(cmd, args) {
var arg, parm, fn;
abc_init()
while (1) {
arg = args.shift()
if (!arg)
break
if (arg[0] == "-") {
if (arg[1] == "-") {
parm = args.shift();
abc.tosvg(cmd, arg.replace('--', 'I:') +
" " + parm + "\n")
}
} else {
if (fn) {
do_file(fn);
abc.tosvg('cmd', '%%select\n')
}
fn = arg
}
}
if (fn)
do_file(fn);
abc_end()
}
// nodejs
if (typeof module == 'object' && typeof exports == 'object') {
exports.abc = abc;
exports.errtxt = errtxt;
exports.user = user;
exports.abc_cmd = abc_cmd
}