blob: 9ed5953c6037d1a112cff7bbd258a128127a335a [file] [log] [blame]
Ted Kremenek6a340832008-03-18 21:19:49 +00001//== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the HTMLRewriter clas, which is used to translate the
11// text of a source file into prettified HTML.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Rewrite/Rewriter.h"
16#include "clang/Rewrite/HTMLRewrite.h"
17#include "clang/Basic/SourceManager.h"
Chris Lattner57df3b92008-04-16 04:11:35 +000018#include "llvm/ADT/SmallString.h"
Ted Kremenek6a340832008-03-18 21:19:49 +000019#include "llvm/Support/MemoryBuffer.h"
20#include <sstream>
21
22using namespace clang;
23
Ted Kremenekfa5be362008-04-08 22:37:58 +000024void html::EscapeText(Rewriter& R, unsigned FileID,
25 bool EscapeSpaces, bool ReplaceTabs) {
Ted Kremenek6a340832008-03-18 21:19:49 +000026
27 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
28 const char* C = Buf->getBufferStart();
29 const char* FileEnd = Buf->getBufferEnd();
30
31 assert (C <= FileEnd);
32
Chris Lattner73527142008-04-16 04:33:23 +000033 RewriteBuffer &RB = R.getEditBuffer(FileID);
34
Ted Kremenek6a340832008-03-18 21:19:49 +000035 for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
Ted Kremenek49cd6352008-04-03 07:12:29 +000036
Ted Kremenek6a340832008-03-18 21:19:49 +000037 switch (*C) {
Chris Lattner73527142008-04-16 04:33:23 +000038 default: break;
39
40 case ' ':
41 if (EscapeSpaces)
42 RB.ReplaceText(FilePos, 1, "&nbsp;", 6);
43 break;
Ted Kremenek6a340832008-03-18 21:19:49 +000044
Chris Lattner73527142008-04-16 04:33:23 +000045 case '\t':
46 if (!ReplaceTabs)
Ted Kremenek49cd6352008-04-03 07:12:29 +000047 break;
Chris Lattner73527142008-04-16 04:33:23 +000048 if (EscapeSpaces)
49 RB.ReplaceText(FilePos, 1, "&nbsp;&nbsp;&nbsp;&nbsp;", 6*4);
50 else
51 RB.ReplaceText(FilePos, 1, " ", 4);
52 break;
53
54 case '<':
55 RB.ReplaceText(FilePos, 1, "&lt;", 4);
56 break;
57
58 case '>':
59 RB.ReplaceText(FilePos, 1, "&gt;", 4);
60 break;
61
62 case '&':
63 RB.ReplaceText(FilePos, 1, "&amp;", 5);
64 break;
Ted Kremenek6a340832008-03-18 21:19:49 +000065 }
66 }
67}
68
Ted Kremenekfa5be362008-04-08 22:37:58 +000069std::string html::EscapeText(const std::string& s, bool EscapeSpaces,
70 bool ReplaceTabs) {
Ted Kremenek053ef592008-03-27 17:15:29 +000071
72 unsigned len = s.size();
73 std::ostringstream os;
74
75 for (unsigned i = 0 ; i < len; ++i) {
76
77 char c = s[i];
Ted Kremenek053ef592008-03-27 17:15:29 +000078 switch (c) {
Chris Lattner8570f0b2008-04-16 04:37:29 +000079 default:
80 os << c; break;
81
82 case ' ':
83 if (EscapeSpaces) os << "&nbsp;";
84 else os << ' ';
85 break;
86
87 case '\t':
88 if (ReplaceTabs)
89 for (unsigned i = 0; i < 4; ++i)
90 os << "&nbsp;";
91 else
92 os << c;
93
Ted Kremenek053ef592008-03-27 17:15:29 +000094 break;
Chris Lattner8570f0b2008-04-16 04:37:29 +000095
96 case '<': os << "&lt;"; break;
97 case '>': os << "&gt;"; break;
98 case '&': os << "&amp;"; break;
Ted Kremenek053ef592008-03-27 17:15:29 +000099 }
100 }
101
102 return os.str();
103}
104
Chris Lattner8570f0b2008-04-16 04:37:29 +0000105static void AddLineNumber(RewriteBuffer &RB, unsigned LineNo,
106 unsigned B, unsigned E) {
Chris Lattner57df3b92008-04-16 04:11:35 +0000107 llvm::SmallString<100> Str;
108 Str += "<tr><td class=\"num\" id=\"LN";
109 Str.append_uint(LineNo);
110 Str += "\">";
111 Str.append_uint(LineNo);
112 Str += "</td><td class=\"line\">";
113
Ted Kremenek49cd6352008-04-03 07:12:29 +0000114 if (B == E) { // Handle empty lines.
Chris Lattner57df3b92008-04-16 04:11:35 +0000115 Str += " </td></tr>";
Chris Lattner8570f0b2008-04-16 04:37:29 +0000116 RB.InsertTextBefore(B, &Str[0], Str.size());
Chris Lattner57df3b92008-04-16 04:11:35 +0000117 } else {
Chris Lattner8570f0b2008-04-16 04:37:29 +0000118 RB.InsertTextBefore(B, &Str[0], Str.size());
119 RB.InsertTextBefore(E, "</td></tr>", strlen("</td></tr>"));
Ted Kremenek49cd6352008-04-03 07:12:29 +0000120 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000121}
122
123void html::AddLineNumbers(Rewriter& R, unsigned FileID) {
124
125 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
126 const char* FileBeg = Buf->getBufferStart();
127 const char* FileEnd = Buf->getBufferEnd();
128 const char* C = FileBeg;
Chris Lattner8570f0b2008-04-16 04:37:29 +0000129 RewriteBuffer &RB = R.getEditBuffer(FileID);
Ted Kremenekb485cd12008-03-18 23:08:51 +0000130
131 assert (C <= FileEnd);
132
133 unsigned LineNo = 0;
134 unsigned FilePos = 0;
135
136 while (C != FileEnd) {
137
138 ++LineNo;
139 unsigned LineStartPos = FilePos;
140 unsigned LineEndPos = FileEnd - FileBeg;
141
142 assert (FilePos <= LineEndPos);
143 assert (C < FileEnd);
144
145 // Scan until the newline (or end-of-file).
146
Ted Kremenek49cd6352008-04-03 07:12:29 +0000147 while (C != FileEnd) {
148 char c = *C;
149 ++C;
150
151 if (c == '\n') {
152 LineEndPos = FilePos++;
Ted Kremenekb485cd12008-03-18 23:08:51 +0000153 break;
154 }
Ted Kremenek49cd6352008-04-03 07:12:29 +0000155
156 ++FilePos;
157 }
Ted Kremenekb485cd12008-03-18 23:08:51 +0000158
Chris Lattner8570f0b2008-04-16 04:37:29 +0000159 AddLineNumber(RB, LineNo, LineStartPos, LineEndPos);
Ted Kremenekd6c13602008-03-19 05:07:26 +0000160 }
161
Chris Lattner8570f0b2008-04-16 04:37:29 +0000162 // Add one big table tag that surrounds all of the code.
163 RB.InsertTextBefore(0, "<table class=\"code\">\n",
164 strlen("<table class=\"code\">\n"));
Ted Kremenekd6c13602008-03-19 05:07:26 +0000165
Chris Lattner8570f0b2008-04-16 04:37:29 +0000166 RB.InsertTextAfter(FileEnd - FileBeg, "</table>", strlen("</table>"));
Ted Kremenekb485cd12008-03-18 23:08:51 +0000167}
Ted Kremenekad0a2032008-03-19 06:14:37 +0000168
169void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID) {
170
171 const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
172 const char* FileStart = Buf->getBufferStart();
173 const char* FileEnd = Buf->getBufferEnd();
174
175 SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0);
176 SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart);
177
178 // Generate header
179
Ted Kremenek70bcba62008-04-09 15:40:40 +0000180 R.InsertCStrBefore(StartLoc,
181 "<html>\n<head>\n"
182 "<style type=\"text/css\">\n"
183 " body { color:#000000; background-color:#ffffff }\n"
184 " body { font-family:Helvetica, sans-serif; font-size:10pt }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000185 " h1 { font-size:14pt }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000186 " .code { border-spacing:0px; width:100%; }\n"
187 " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n"
188 " .code { line-height: 1.2em }\n"
189 " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n"
190 " .num { text-align:right; font-size: smaller }\n"
191 " .num { color:#444444 }\n"
192 " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n"
193 " .line { white-space: pre }\n"
194 " .msg { background-color:#fff8b4; color:#000000 }\n"
195 " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n"
196 " .msg { -webkit-border-radius:5px }\n"
197 " .msg { font-family:Helvetica, sans-serif; font-size: smaller }\n"
198 " .msg { font-weight: bold }\n"
199 " .msg { float:left }\n"
200 " .msg { padding:0.5em 1ex 0.5em 1ex }\n"
201 " .msg { margin-top:10px; margin-bottom:10px }\n"
202 " .mrange { background-color:#dfddf3 }\n"
203 " .mrange { border-bottom:1px solid #6F9DBE }\n"
204 " .PathIndex { font-weight: bold }\n"
Ted Kremenek4b0f8132008-04-15 21:25:08 +0000205 " table.simpletable {\n"
206 " padding: 5px;\n"
207 " font-size:12pt;\n"
208 " margin:20px;\n"
209 " border-collapse: collapse; border-spacing: 0px;\n"
210 " }\n"
211 " td.rowname {\n"
212 " text-align:right; font-weight:bold; color:#444444;\n"
213 " padding-right:2ex; }\n"
Ted Kremenek70bcba62008-04-09 15:40:40 +0000214 "</style>\n</head>\n<body>");
215
Ted Kremenekad0a2032008-03-19 06:14:37 +0000216 // Generate footer
217
Ted Kremenek70bcba62008-04-09 15:40:40 +0000218 R.InsertCStrAfter(EndLoc, "</body></html>\n");
Ted Kremenekad0a2032008-03-19 06:14:37 +0000219}