-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgi.cpp
More file actions
288 lines (273 loc) · 10.6 KB
/
Copy pathcgi.cpp
File metadata and controls
288 lines (273 loc) · 10.6 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (C) 2013 Mario Lang <mlang@delysid.org>
//
// Distributed under the terms of the GNU General Public License version 3.
// (see accompanying file LICENSE.txt or copy at
// http://www.gnu.org/licenses/gpl-3.0-standalone.html)
#include <bmc/ttb/ttb.h>
#include "config.hpp"
#include <fstream>
#include <boost/spirit/include/qi_parse.hpp>
#include <boost/locale/encoding_utf.hpp>
#include "bmc/braille/parsing/grammar/score.hpp"
#include "bmc/braille/semantic_analysis.hpp"
#include <boost/program_options.hpp>
#include "bmc/lilypond.hpp"
#include <cgicc/Cgicc.h>
#include <cgicc/CgiUtils.h>
#include <cgicc/HTMLClasses.h>
#include <cgicc/HTTPContentHeader.h>
#include <cgicc/XHTMLDoctype.h>
// Output the has of an arbitrary string in hex.
std::string hash(std::string const &string)
{
std::stringstream stream;
stream << std::hex << std::hash<std::string>()(string);
return stream.str();
}
cgicc::option table_option(std::string const &id, cgicc::Cgicc const &cgi) {
cgicc::option o(id);
o.set("value", id);
if (cgi.getElement("table") != cgi.getElements().end() and
cgi.getElement("table")->getValue() == id)
o.set("selected", "selected");
return o;
}
std::ostream &table_select(std::ostream &os, cgicc::Cgicc const &cgi) {
os << cgicc::select().set("name", "table").set("id", "table")
<< table_option("brf", cgi)
<< table_option("ar", cgi)
<< table_option("as", cgi)
<< table_option("awa", cgi)
<< table_option("bg", cgi)
<< table_option("bh", cgi)
<< table_option("bn", cgi)
<< table_option("bo", cgi)
<< table_option("bra", cgi)
<< table_option("cs", cgi)
<< table_option("cy", cgi)
<< table_option("da", cgi)
<< table_option("da-1252", cgi)
<< table_option("da-lt", cgi)
<< table_option("de", cgi)
<< table_option("dra", cgi)
<< table_option("el", cgi)
<< table_option("en", cgi)
<< table_option("en_CA", cgi)
<< table_option("en_GB", cgi)
<< table_option("en-nabcc", cgi)
<< table_option("en_US", cgi)
<< table_option("eo", cgi)
<< table_option("es", cgi)
<< table_option("et", cgi)
<< table_option("fi", cgi)
<< table_option("fr", cgi)
<< table_option("fr-2007", cgi)
<< table_option("fr_CA", cgi)
<< table_option("fr-cbifs", cgi)
<< table_option("fr_FR", cgi)
<< table_option("fr-vs", cgi)
<< table_option("ga", cgi)
<< table_option("gd", cgi)
<< table_option("gon", cgi)
<< table_option("gu", cgi)
<< table_option("he", cgi)
<< table_option("hi", cgi)
<< table_option("hr", cgi)
<< table_option("hu", cgi)
<< table_option("hy", cgi)
<< table_option("is", cgi)
<< table_option("it", cgi)
<< table_option("kha", cgi)
<< table_option("kn", cgi)
<< table_option("kok", cgi)
<< table_option("kru", cgi)
<< table_option("lt", cgi)
<< table_option("lv", cgi)
<< table_option("mg", cgi)
<< table_option("mi", cgi)
<< table_option("ml", cgi)
<< table_option("mni", cgi)
<< table_option("mr", cgi)
<< table_option("mt", cgi)
<< table_option("mun", cgi)
<< table_option("mwr", cgi)
<< table_option("ne", cgi)
<< table_option("new", cgi)
<< table_option("nl", cgi)
<< table_option("nl_BE", cgi)
<< table_option("nl_NL", cgi)
<< table_option("no", cgi)
<< table_option("no-generic", cgi)
<< table_option("no-oub", cgi)
<< table_option("nwc", cgi)
<< table_option("or", cgi)
<< table_option("pa", cgi)
<< table_option("pi", cgi)
<< table_option("pl", cgi)
<< table_option("pt", cgi)
<< table_option("ro", cgi)
<< table_option("ru", cgi)
<< table_option("sa", cgi)
<< table_option("sat", cgi)
<< table_option("sd", cgi)
<< table_option("sk", cgi)
<< table_option("sv", cgi)
<< table_option("sv-1989", cgi)
<< table_option("sv-1996", cgi)
<< table_option("sw", cgi)
<< table_option("ta", cgi)
<< table_option("te", cgi)
<< table_option("tr", cgi)
<< table_option("vi", cgi)
<< cgicc::select();
return os;
}
cgicc::option instrument_option(std::string const &instrument, cgicc::Cgicc const &cgi) {
cgicc::option o(instrument);
o.set("value", instrument);
if (cgi.getElement("instrument") != cgi.getElements().end() and
cgi.getElement("instrument")->getValue() == instrument)
o.set("selected", "selected");
return o;
}
std::ostream &instrument_select(std::ostream &os, cgicc::Cgicc const &cgi) {
os << cgicc::select().set("name", "instrument").set("id", "instrument");
for (auto instrument: music::lilypond::instruments) {
os << instrument_option(instrument, cgi);
}
os << cgicc::select();
return os;
}
int
main(int argc, char const *argv[])
{
std::locale::global(std::locale(""));
cgicc::Cgicc cgi;
if (cgi.getElement("table") != cgi.getElements().end()) {
replaceTextTable(TABLES_DIRECTORY, cgi.getElement("table")->getValue().c_str());
}
cgicc::textarea music_input(cgi("music"));
music_input.set("id", "music");
music_input.set("cols", "32");
music_input.set("rows", "10");
music_input.set("name", "music");
cgicc::const_form_iterator braille(cgi.getElement("music"));
std::string prefix;
if (braille != cgi.getElements().end()) {
std::wstring source(boost::locale::conv::utf_to_utf<wchar_t>(braille->getValue()));
typedef std::wstring::const_iterator iterator_type;
iterator_type const end = source.end();
iterator_type iter = source.begin();
typedef music::braille::error_handler<iterator_type> error_handler_type;
error_handler_type error_handler(iter, end);
typedef music::braille::score_grammar<iterator_type> parser_type;
parser_type parser(error_handler);
boost::spirit::traits::attribute_of<parser_type>::type score;
bool const success = parse(iter, end, parser, score);
if (success and iter == end) {
music::braille::compiler<error_handler_type> compile(error_handler);
if (compile(score)) {
prefix = hash(braille->getValue());
std::string dir("/tmp/bmc.cgi/");
std::ofstream bmc(dir + prefix + ".bmc");
bmc << braille->getValue();
bmc.close();
std::ofstream ly(dir + prefix + ".ly");
music::lilypond::generator generate(ly, true, true, false);
generate.remove_tagline();
if (not cgi("instrument").empty())
generate.instrument(cgi("instrument"));
generate(score);
ly.close();
std::string cmd("lilypond --png -dpaper-size='\"a5\"' -o " + dir + prefix + " " + dir + prefix + ".ly" + " >/tmp/bmc.cgi/log 2>&1");
if (system(cmd.c_str()) != 0) {
prefix = "";
}
}
}
}
if (not prefix.empty() and
cgi.getElement("type") != cgi.getElements().end()) {
if (cgi.getElement("type")->getValue() == "play") {
std::ifstream midi_file("/tmp/bmc.cgi/" + prefix + ".midi");
if (midi_file.good()) {
std::cout << "Content-type: audio/midi" << std::endl << std::endl;
std::cout << midi_file.rdbuf();
exit(EXIT_SUCCESS);
}
}
}
if (not cgi("hash").empty()) {
if (cgi.getElement("type")->getValue() == "png") {
std::ifstream png_file("/tmp/bmc.cgi/" + cgi("hash") + ".png");
if (png_file.good()) {
std::cout << "Content-type: image/png" << std::endl << std::endl;
std::cout << png_file.rdbuf();
exit(EXIT_SUCCESS);
}
} else if (cgi.getElement("type")->getValue() == "pdf") {
std::string cmd("lilypond --pdf -o /tmp/bmc.cgi/" + cgi("hash") + " /tmp/bmc.cgi/" + cgi("hash") + ".ly");
if (system(cmd.c_str()) == 0) {
std::ifstream pdf_file("/tmp/bmc.cgi/" + cgi("hash") + ".pdf");
if (pdf_file.good()) {
std::cout << "Content-type: application/pdf" << std::endl << std::endl;
std::cout << pdf_file.rdbuf();
exit(EXIT_SUCCESS);
}
}
}
}
std::cout << "Content-type: text/html; charset=utf-8"
<< std::endl
<< std::endl
<< cgicc::XHTMLDoctype(cgicc::XHTMLDoctype::eStrict)
<< cgicc::html()
<< cgicc::head()
<< cgicc::title("Braille Music Compiler")
<< cgicc::head()
<< cgicc::body();
std::cout << cgicc::h1("Automatic transcription of braille music code to print and sound") << std::endl;
if (cgi("music").empty()) {
std::cout << cgicc::p() << "See this "
<< cgicc::a("braille music code tutorial")
.set("href", "https://bmc.branchable.com/tutorial/")
<< " for details and inspiration."
<< cgicc::p() << std::endl;
std::cout << cgicc::p("To allow for several parts in a single score, music always needs to end with a final bar sign.") << std::endl;
}
if (not cgi("music").empty() and prefix.empty()) {
std::cout << cgicc::p("Unable to translate braille music, try again.").set("class", "error") << std::endl;
std::cout << cgicc::p() << "The "
<< cgicc::a("tutorial").set("href", "https://bmc.branchable.com/tutorial/")
<< " contains working examples which can be used as a starting point. Just invoke the Edit link below each example to load it back in here." << cgicc::p() << std::endl;
}
std::cout << cgicc::form();
std::cout << cgicc::label("Select braille table: ").set("for", "table");
table_select(std::cout, cgi);
std::cout << cgicc::div()
<< cgicc::label("Enter braille music: ").set("for", "music");
std::cout << music_input << cgicc::div();
std::cout << cgicc::input().set("type", "submit").set("value", "Transcribe to print");
if (not prefix.empty()) {
instrument_select(std::cout, cgi);
std::cout << cgicc::input().set("type", "submit").set("name", "type").set("value", "play");
}
std::cout << cgicc::form();
if (not prefix.empty()) {
std::string alt;
std::ifstream ly("/tmp/bmc.cgi/" + prefix + ".ly");
if (ly.good()) {
std::istreambuf_iterator<char> ly_begin(ly.rdbuf()), ly_end;
alt = std::string(ly_begin, ly_end);
std::string::size_type i = 0;
while ((i = alt.find("\"", i)) != std::string::npos) {
alt.replace(i, 1, """);
}
}
std::cout << cgicc::img().set("src", cgi.getEnvironment().getScriptName() + "?hash=" + prefix + "&type=png").set("alt", alt);
std::cout << cgicc::div() << cgicc::a("Download PDF").set("href", cgi.getEnvironment().getScriptName() + "?hash=" + prefix + "&type=pdf") << cgicc::div() << std::endl;
}
std::cout << cgicc::body()
<< cgicc::html();
return EXIT_SUCCESS;
}