using namespace clang;
void html::EscapeText(Rewriter& R, unsigned FileID, bool EscapeSpaces) {
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) {
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
switch (*C) {
default: break;
case ' ':
if (EscapeSpaces) R.ReplaceText(Loc, 1, " ", 6);
break;
case '\t': R.ReplaceText(Loc, 1, " ", 6*4); break;
case '<': R.ReplaceText(Loc, 1, "<", 4); break;
case '>': R.ReplaceText(Loc, 1, ">", 4); break;
case '&': R.ReplaceText(Loc, 1, "&", 5); break;
}
}
}
std::string html::EscapeText(const std::string& s, bool EscapeSpaces) {
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': for (unsigned i = 0; i < 4; ++i) os << " "; break;
case '<': os << "<"; break;
case '>': os << ">"; break;
case '&': os << "&"; break;
}
}
return os.str();
}
static void AddLineNumber(Rewriter& R, unsigned LineNo,
SourceLocation B, SourceLocation E) {
// Put the closing first.
R.InsertCStrBefore(E, "");
if (B == E) // Handle empty lines.
R.InsertCStrBefore(B, " | ");
else {
R.InsertCStrBefore(E, "");
R.InsertCStrBefore(B, "");
}
// Insert a div tag for the line number.
std::ostringstream os;
os << " | " << LineNo << " | ";
R.InsertStrBefore(B, os.str());
// Now prepend the .
R.InsertCStrBefore(B, "
");
}
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).
for ( ; C != FileEnd ; ++C, ++FilePos)
if (*C == '\n') {
LineEndPos = FilePos;
break;
}
AddLineNumber(R, LineNo,
SourceLocation::getFileLoc(FileID, LineStartPos),
SourceLocation::getFileLoc(FileID, LineEndPos));
if (C != FileEnd) {
++C;
++FilePos;
}
}
// Add one big div tag that surrounds all of the code.
R.InsertCStrBefore(SourceLocation::getFileLoc(FileID, 0),
"\n");
R.InsertCStrAfter(SourceLocation::getFileLoc(FileID, FileEnd - FileBeg),
"
");
}
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
{
std::ostringstream os;
os << "\n\n"
<< "\n\n";
R.InsertStrBefore(StartLoc, os.str());
}
// Generate footer
{
std::ostringstream os;
os << "\n";
R.InsertStrAfter(EndLoc, os.str());
}
}