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
|
//== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the HTMLRewriter clas, which is used to translate the
// text of a source file into prettified HTML.
//
//===----------------------------------------------------------------------===//
#include "clang/Rewrite/Rewriter.h"
#include "clang/Rewrite/HTMLRewrite.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/Support/MemoryBuffer.h"
#include <sstream>
using namespace clang;
void html::EscapeText(Rewriter& R, unsigned FileID,
bool EscapeSpaces, bool ReplaceTabs) {
const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
const char* C = Buf->getBufferStart();
const char* FileEnd = Buf->getBufferEnd();
assert (C <= FileEnd);
for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
switch (*C) {
default: break;
case ' ':
if (EscapeSpaces) {
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
R.ReplaceText(Loc, 1, " ", 6);
}
break;
case '\t': {
if (!ReplaceTabs)
break;
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
if (EscapeSpaces)
R.ReplaceText(Loc, 1, " ", 6*4);
else
R.ReplaceText(Loc, 1, " ", 4);
break;
}
case '<': {
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
R.ReplaceText(Loc, 1, "<", 4);
break;
}
case '>': {
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
R.ReplaceText(Loc, 1, ">", 4);
break;
}
case '&': {
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
R.ReplaceText(Loc, 1, "&", 5);
break;
}
}
}
}
std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
bool ReplaceTabs) {
unsigned len = s.size();
std::ostringstream os;
for (unsigned i = 0 ; i < len; ++i) {
char c = s[i];
switch (c) {
default:
os << c; break;
case ' ':
if (EscapeSpaces) os << " ";
else os << ' ';
break;
case '\t':
if (ReplaceTabs)
for (unsigned i = 0; i < 4; ++i) os << " ";
else os << c;
break;
case '<': os << "<"; break;
case '>': os << ">"; break;
case '&': os << "&"; break;
}
}
return os.str();
}
static void AddLineNumber(Rewriter& R, unsigned LineNo,
SourceLocation B, SourceLocation E) {
std::ostringstream os;
os << "<tr><td class=\"num\" id=\"LN" << LineNo << "\">"
<< LineNo << "</td><td class=\"line\">";
if (B == E) { // Handle empty lines.
os << " </td></tr>";
R.InsertStrBefore(B, os.str());
}
else {
R.InsertStrBefore(B, os.str());
R.InsertCStrBefore(E, "</td></tr>");
}
}
void html::AddLineNumbers(Rewriter& R, unsigned FileID) {
const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
const char* FileBeg = Buf->getBufferStart();
const char* FileEnd = Buf->getBufferEnd();
const char* C = FileBeg;
assert (C <= FileEnd);
unsigned LineNo = 0;
unsigned FilePos = 0;
while (C != FileEnd) {
++LineNo;
unsigned LineStartPos = FilePos;
unsigned LineEndPos = FileEnd - FileBeg;
assert (FilePos <= LineEndPos);
assert (C < FileEnd);
// Scan until the newline (or end-of-file).
while (C != FileEnd) {
char c = *C;
++C;
if (c == '\n') {
LineEndPos = FilePos++;
break;
}
++FilePos;
}
AddLineNumber(R, LineNo,
SourceLocation::getFileLoc(FileID, LineStartPos),
SourceLocation::getFileLoc(FileID, LineEndPos));
}
// Add one big div tag that surrounds all of the code.
R.InsertCStrBefore(SourceLocation::getFileLoc(FileID, 0),
"<table class=\"code\">\n");
R.InsertCStrAfter(SourceLocation::getFileLoc(FileID, FileEnd - FileBeg),
"</table>");
}
void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID) {
const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
const char* FileStart = Buf->getBufferStart();
const char* FileEnd = Buf->getBufferEnd();
SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0);
SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart);
// Generate header
R.InsertCStrBefore(StartLoc,
"<html>\n<head>\n"
"<style type=\"text/css\">\n"
" body { color:#000000; background-color:#ffffff }\n"
" body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
" h1 { font-size:14pt }\n"
" .code { border-spacing:0px; width:100%; }\n"
" .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
" .code { line-height: 1.2em }\n"
" .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
" .num { text-align:right; font-size: smaller }\n"
" .num { color:#444444 }\n"
" .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
" .line { white-space: pre }\n"
" .msg { background-color:#fff8b4; color:#000000 }\n"
" .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
" .msg { -webkit-border-radius:5px }\n"
" .msg { font-family:Helvetica, sans-serif; font-size: smaller }\n"
" .msg { font-weight: bold }\n"
" .msg { float:left }\n"
" .msg { padding:0.5em 1ex 0.5em 1ex }\n"
" .msg { margin-top:10px; margin-bottom:10px }\n"
" .mrange { background-color:#dfddf3 }\n"
" .mrange { border-bottom:1px solid #6F9DBE }\n"
" .PathIndex { font-weight: bold }\n"
" table.simpletable {\n"
" padding: 5px;\n"
" font-size:12pt;\n"
" margin:20px;\n"
" border-collapse: collapse; border-spacing: 0px;\n"
" }\n"
" td.rowname {\n"
" text-align:right; font-weight:bold; color:#444444;\n"
" padding-right:2ex; }\n"
"</style>\n</head>\n<body>");
// Generate footer
R.InsertCStrAfter(EndLoc, "</body></html>\n");
}
|