2008-03-19 05:19:49 +08:00
|
|
|
//== 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;
|
|
|
|
|
2008-04-09 06:37:58 +08:00
|
|
|
void html::EscapeText(Rewriter& R, unsigned FileID,
|
|
|
|
bool EscapeSpaces, bool ReplaceTabs) {
|
2008-03-19 05:19:49 +08:00
|
|
|
|
|
|
|
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) {
|
2008-04-03 15:12:29 +08:00
|
|
|
|
2008-03-19 05:19:49 +08:00
|
|
|
switch (*C) {
|
|
|
|
default: break;
|
|
|
|
|
|
|
|
case ' ':
|
2008-04-03 15:12:29 +08:00
|
|
|
if (EscapeSpaces) {
|
|
|
|
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
|
|
|
|
R.ReplaceText(Loc, 1, " ", 6);
|
|
|
|
}
|
2008-03-19 05:19:49 +08:00
|
|
|
break;
|
|
|
|
|
2008-04-03 15:12:29 +08:00
|
|
|
case '\t': {
|
2008-04-09 06:37:58 +08:00
|
|
|
if (!ReplaceTabs)
|
|
|
|
break;
|
|
|
|
|
2008-04-03 15:12:29 +08:00
|
|
|
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
|
2008-04-09 06:28:15 +08:00
|
|
|
|
|
|
|
if (EscapeSpaces)
|
|
|
|
R.ReplaceText(Loc, 1, " ", 6*4);
|
|
|
|
else
|
|
|
|
R.ReplaceText(Loc, 1, " ", 4);
|
|
|
|
|
2008-04-03 15:12:29 +08:00
|
|
|
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;
|
|
|
|
}
|
2008-03-19 05:19:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-09 06:37:58 +08:00
|
|
|
std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
|
|
|
|
bool ReplaceTabs) {
|
2008-03-28 01:15:29 +08:00
|
|
|
|
|
|
|
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 ' ':
|
2008-04-01 07:14:05 +08:00
|
|
|
if (EscapeSpaces) os << " ";
|
2008-03-28 01:28:58 +08:00
|
|
|
else os << ' ';
|
2008-03-28 01:15:29 +08:00
|
|
|
break;
|
|
|
|
|
2008-04-09 06:37:58 +08:00
|
|
|
case '\t':
|
|
|
|
if (ReplaceTabs)
|
|
|
|
for (unsigned i = 0; i < 4; ++i) os << " ";
|
|
|
|
else os << c;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2008-03-28 01:15:29 +08:00
|
|
|
case '<': os << "<"; break;
|
|
|
|
case '>': os << ">"; break;
|
|
|
|
case '&': os << "&"; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2008-03-19 07:08:51 +08:00
|
|
|
static void AddLineNumber(Rewriter& R, unsigned LineNo,
|
|
|
|
SourceLocation B, SourceLocation E) {
|
2008-03-19 09:30:02 +08:00
|
|
|
|
2008-03-19 07:08:51 +08:00
|
|
|
std::ostringstream os;
|
2008-04-08 07:53:30 +08:00
|
|
|
os << "<tr><td class=\"num\" id=\"LN" << LineNo << "\">"
|
|
|
|
<< LineNo << "</td><td class=\"line\">";
|
2008-03-19 15:53:42 +08:00
|
|
|
|
2008-04-03 15:12:29 +08:00
|
|
|
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>");
|
|
|
|
}
|
2008-03-19 07:08:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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).
|
|
|
|
|
2008-04-03 15:12:29 +08:00
|
|
|
while (C != FileEnd) {
|
|
|
|
char c = *C;
|
|
|
|
++C;
|
|
|
|
|
|
|
|
if (c == '\n') {
|
|
|
|
LineEndPos = FilePos++;
|
2008-03-19 07:08:51 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-04-03 15:12:29 +08:00
|
|
|
|
|
|
|
++FilePos;
|
|
|
|
}
|
2008-03-19 07:08:51 +08:00
|
|
|
|
|
|
|
AddLineNumber(R, LineNo,
|
|
|
|
SourceLocation::getFileLoc(FileID, LineStartPos),
|
2008-04-03 15:12:29 +08:00
|
|
|
SourceLocation::getFileLoc(FileID, LineEndPos));
|
2008-03-19 13:07:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add one big div tag that surrounds all of the code.
|
|
|
|
|
|
|
|
R.InsertCStrBefore(SourceLocation::getFileLoc(FileID, 0),
|
2008-03-20 05:59:05 +08:00
|
|
|
"<table class=\"code\">\n");
|
2008-03-19 13:07:26 +08:00
|
|
|
|
|
|
|
R.InsertCStrAfter(SourceLocation::getFileLoc(FileID, FileEnd - FileBeg),
|
2008-03-20 05:59:05 +08:00
|
|
|
"</table>");
|
2008-03-19 07:08:51 +08:00
|
|
|
}
|
2008-03-19 14:14:37 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2008-04-09 23:40:40 +08:00
|
|
|
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"
|
2008-04-16 05:25:08 +08:00
|
|
|
" h1 { font-size:14pt }\n"
|
2008-04-09 23:40:40 +08:00
|
|
|
" .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"
|
2008-04-16 05:25:08 +08:00
|
|
|
" 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"
|
2008-04-09 23:40:40 +08:00
|
|
|
"</style>\n</head>\n<body>");
|
|
|
|
|
2008-03-19 14:14:37 +08:00
|
|
|
// Generate footer
|
|
|
|
|
2008-04-09 23:40:40 +08:00
|
|
|
R.InsertCStrAfter(EndLoc, "</body></html>\n");
|
2008-03-19 14:14:37 +08:00
|
|
|
}
|