-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.cc
90 lines (70 loc) · 2.4 KB
/
html.cc
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
/* HTML-related functions for TagReport. */
#include "config.h"
#include "tagreport.h"
#include "templates.h"
#include "html.h"
#include <iostream>
#include <cerrno>
#include <cassert>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
/* Character escaping for HTML */
static const char replacechars [] = { '&', '<', '>', '\"' };
static const char* replacehtml [] = { "&", "<", ">", """ };
void write_html (const char* file, vector<struct Song*>* songs, char* target_dir)
{
ofstream out;
unsigned int i;
out.open (file, ios::out | ios::trunc);
if (!out.is_open())
{
cerr << "Failed to open file " << file << ": " << strerror(errno) << endl;
if (!force)
exit(1);
}
/* Write out canned headers to the target file */
out << HTMLdtd << "<html>\n<head>\n<title>" << endl;
OUTPUT_HEADER(out, songs, target_dir, template_title);
out << "</title>" << endl;
/* For CSS and JavaScript and such... */
OUTPUT_HEADER_IF_SET(out, songs, target_dir, template_head_body);
out << "</head>" << endl;
OUTPUT_HEADER(out, songs, target_dir, template_body_tag);
OUTPUT_HEADER_IF_SET(out, songs, target_dir, template_header);
/* Some statistics ... */
OUTPUT_HEADER(out, songs, target_dir, template_stats);
/* Use literally. */
OUTPUT_HEADER_IF_SET(out, songs, target_dir, template_prebody);
/* Loop through the vector and HTML-output its contents. */
assert (template_body.is_set());
for (i = 0; i < songs->size(); i++)
OUTPUT_BODY(out, (*songs)[i], i + 1);
/* Footer output. Treated as a header for processing purposes. */
OUTPUT_HEADER(out, songs, target_dir, template_footer);
out << "</body>\n</html>" << endl;
/* Flush the file and close it */
out.close();
}
/* Escapes illegal HTML characters into their long counterparts. */
void htmlify (string & in)
{
unsigned int c, i;
/* replace each found character in replacechars by respective
* entry in replacehtml */
for (c = 0; c < in.length(); c++)
{
for (i = 0; i < ARRAY_SIZE(replacechars); i++)
{
if (in[c] == replacechars[i])
in.replace (c, 1, replacehtml[i]);
}
}
/* replace spaces with two , it's the slightest bit overkill
* i.e. htmlify(" ") should become " ", but it becomes
* " " */
while ((c = in.find(" ")) != string::npos)
in.replace(c, 2, " ");
}