Debian Codegraph @

lib/ChordPro/res/abc/abc2svg/cmdline.js

raw symbols SCIP
1 // abc2svg - cmdline.js - command line
2 //
3 // Copyright (C) 2014-2023 Jean-Francois Moine
4 //
5 // This file is part of abc2svg.
6 //
7 // abc2svg is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // abc2svg is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
19
20 // user definitions
21 var user = {
22 read_file: function(fn) { // read a file (main or included)
23 var i,
24 p = fn,
25 file = abc2svg.readFile(p)
26
27 if (!file && fn[0] != '/') {
28 for (i = 0; i < abc2svg.path.length; i++) {
29 p = abc2svg.path[i] + '/' + fn
30 file = abc2svg.readFile(p)
31 if (file)
32 break
33 }
34 }
35
36 if (!file)
37 return file
38
39 // memorize the file path
40 i = p.lastIndexOf('/')
41 if (i > 0) {
42 p = p.slice(0, i)
43 if (abc2svg.path.indexOf(p) < 0)
44 abc2svg.path.unshift(p)
45 }
46
47 // convert the file content into a Unix string
48 i = file.indexOf('\r')
49 if (i >= 0) {
50 if (file[i + 1] == '\n')
51 file = file.replace(/\r\n/g, '\n') // M$
52 else
53 file = file.replace(/\r/g, '\n') // Mac
54 }
55
56 // load the required modules (synchronous)
57 abc2svg.modules.load(file)
58
59 return file
60 },
61 errtxt: '',
62 errmsg: // print or store the error messages
63 typeof abc2svg.printErr == 'function'
64 ? function(msg, l, c) { abc2svg.printErr(msg) }
65 : function(msg, l, c) { user.errtxt += msg + '\n' }
66 } // user
67
68 var abc // (global for 'toxxx.js')
69
70 if (!abc2svg.path)
71 abc2svg.path = [] // path to ABC files - from env ABCPATH
72
73 // treat a file
74 function do_file(fn) {
75 var file = user.read_file(fn)
76
77 if (!file) {
78 if (fn != "default.abc")
79 user.errmsg("Cannot read file '" + fn + "'")
80 return
81 }
82 // if (typeof(utf_convert) == "function")
83 // file = utf_convert(file)
84
85 // generate
86 try {
87 abc.tosvg(fn, file)
88 } catch (e) {
89 abc2svg.abort(e)
90 }
91 } // do_file()
92
93 function abc_cmd(cmd, args, interp_name) {
94 var arg, parm, fn;
95
96 abc2svg.abort = function(e) {
97 abc2svg.printErr('javascript error: ' + e.message +
98 '\nStack:\n' + e.stack)
99 if (abc) {
100 abc.get_parse().state = 0 // force block flush
101 abc.blk_flush()
102 abc2svg.abc_end()
103 }
104 abc2svg.quit()
105 } // abort()
106
107 // put the last options before the last file
108 function arg_reorder(a) {
109 var f,
110 i = a.length - 2
111
112 while (i > 2 && a[i].slice(0, 2) == '--')
113 i -= 2
114 f = a[--i]
115 a.splice(i, 1)
116 a.push(f)
117 } // arg_reorder()
118
119 // if the first argument is a javascript file, load it
120 if (args[0] && args[0].slice(-3) == '.js') {
121 abc2svg.loadjs(args[0])
122 args.shift()
123 }
124
125 if (!args[0]) {
126 abc2svg.printErr('ABC translator with ' + interp_name +
127 ' and abc2svg-' + abc2svg.version + ' ' +
128 abc2svg.vdate +
129 '\nUsage:\n ' + cmd +
130 ' [script.js] [options] ABC_file [[options] ABC_file]* [options]\n\
131 Arguments:\n\
132 script.js generation script to load - default: tohtml.js (HTML+SVG)\n\
133 options ABC options (the last options are moved before the last file)\n\
134 ABC_file ABC file')
135 abc2svg.quit()
136 }
137
138 // the default output is HTML+SVG
139 if (typeof abc2svg.abc_init != 'function')
140 abc2svg.loadjs("tohtml.js")
141
142 // initialize the backend
143 abc = new abc2svg.Abc(user)
144 if (typeof global == "object" && !global.abc)
145 global.abc = abc
146 abc2svg.abc_init(args)
147
148 // load 'default.abc'
149 try {
150 do_file("default.abc")
151 } catch (e) {
152 }
153
154 // put the last options before the last ABC file
155 if (args.length > 2 && args[args.length - 2].slice(0, 2) == '--')
156 arg_reorder(args)
157
158 while (1) {
159 arg = args.shift()
160 if (!arg)
161 break
162 if (arg[0] == "-" && arg[1] == "-") {
163 parm = arg.replace('--', 'I:') + " " +
164 args.shift() + "\n"
165 abc2svg.modules.load(parm)
166 abc.tosvg(cmd, parm)
167 } else {
168 do_file(arg)
169 abc.tosvg('cmd', '%%select\n')
170 }
171 }
172 abc2svg.abc_end()
173 }
174
175 // nodejs
176 if (typeof module == 'object' && typeof exports == 'object') {
177 exports.user = user;
178 exports.abc_cmd = abc_cmd
179 }