Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 1 | //== 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 Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #include <sstream> |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 24 | void html::EscapeText(Rewriter& R, unsigned FileID, |
| 25 | bool EscapeSpaces, bool ReplaceTabs) { |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 26 | |
| 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 | |
| 33 | for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) { |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 35 | switch (*C) { |
| 36 | default: break; |
| 37 | |
| 38 | case ' ': |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 39 | if (EscapeSpaces) { |
| 40 | SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos); |
| 41 | R.ReplaceText(Loc, 1, " ", 6); |
| 42 | } |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 43 | break; |
| 44 | |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 45 | case '\t': { |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 46 | if (!ReplaceTabs) |
| 47 | break; |
| 48 | |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 49 | SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos); |
Ted Kremenek | 4ce32cb | 2008-04-08 22:28:15 +0000 | [diff] [blame] | 50 | |
| 51 | if (EscapeSpaces) |
| 52 | R.ReplaceText(Loc, 1, " ", 6*4); |
| 53 | else |
| 54 | R.ReplaceText(Loc, 1, " ", 4); |
| 55 | |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 56 | break; |
| 57 | } |
| 58 | |
| 59 | case '<': { |
| 60 | SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos); |
| 61 | R.ReplaceText(Loc, 1, "<", 4); |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | case '>': { |
| 66 | SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos); |
| 67 | R.ReplaceText(Loc, 1, ">", 4); |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | case '&': { |
| 72 | SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos); |
| 73 | R.ReplaceText(Loc, 1, "&", 5); |
| 74 | break; |
| 75 | } |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 80 | std::string html::EscapeText(const std::string& s, bool EscapeSpaces, |
| 81 | bool ReplaceTabs) { |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 82 | |
| 83 | unsigned len = s.size(); |
| 84 | std::ostringstream os; |
| 85 | |
| 86 | for (unsigned i = 0 ; i < len; ++i) { |
| 87 | |
| 88 | char c = s[i]; |
| 89 | |
| 90 | switch (c) { |
| 91 | default: |
| 92 | os << c; break; |
| 93 | |
| 94 | case ' ': |
Ted Kremenek | 8fb0016 | 2008-03-31 23:14:05 +0000 | [diff] [blame] | 95 | if (EscapeSpaces) os << " "; |
Ted Kremenek | 487f827 | 2008-03-27 17:28:58 +0000 | [diff] [blame] | 96 | else os << ' '; |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 97 | break; |
| 98 | |
Ted Kremenek | fa5be36 | 2008-04-08 22:37:58 +0000 | [diff] [blame] | 99 | case '\t': |
| 100 | if (ReplaceTabs) |
| 101 | for (unsigned i = 0; i < 4; ++i) os << " "; |
| 102 | else os << c; |
| 103 | |
| 104 | break; |
| 105 | |
Ted Kremenek | 053ef59 | 2008-03-27 17:15:29 +0000 | [diff] [blame] | 106 | case '<': os << "<"; break; |
| 107 | case '>': os << ">"; break; |
| 108 | case '&': os << "&"; break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return os.str(); |
| 113 | } |
| 114 | |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 115 | static void AddLineNumber(Rewriter& R, unsigned LineNo, |
| 116 | SourceLocation B, SourceLocation E) { |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 117 | llvm::SmallString<100> Str; |
| 118 | Str += "<tr><td class=\"num\" id=\"LN"; |
| 119 | Str.append_uint(LineNo); |
| 120 | Str += "\">"; |
| 121 | Str.append_uint(LineNo); |
| 122 | Str += "</td><td class=\"line\">"; |
| 123 | |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 124 | if (B == E) { // Handle empty lines. |
Chris Lattner | 57df3b9 | 2008-04-16 04:11:35 +0000 | [diff] [blame] | 125 | Str += " </td></tr>"; |
| 126 | R.InsertTextBefore(B, &Str[0], Str.size()); |
| 127 | } else { |
| 128 | R.InsertTextBefore(B, &Str[0], Str.size()); |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 129 | R.InsertCStrBefore(E, "</td></tr>"); |
| 130 | } |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void html::AddLineNumbers(Rewriter& R, unsigned FileID) { |
| 134 | |
| 135 | const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID); |
| 136 | const char* FileBeg = Buf->getBufferStart(); |
| 137 | const char* FileEnd = Buf->getBufferEnd(); |
| 138 | const char* C = FileBeg; |
| 139 | |
| 140 | assert (C <= FileEnd); |
| 141 | |
| 142 | unsigned LineNo = 0; |
| 143 | unsigned FilePos = 0; |
| 144 | |
| 145 | while (C != FileEnd) { |
| 146 | |
| 147 | ++LineNo; |
| 148 | unsigned LineStartPos = FilePos; |
| 149 | unsigned LineEndPos = FileEnd - FileBeg; |
| 150 | |
| 151 | assert (FilePos <= LineEndPos); |
| 152 | assert (C < FileEnd); |
| 153 | |
| 154 | // Scan until the newline (or end-of-file). |
| 155 | |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 156 | while (C != FileEnd) { |
| 157 | char c = *C; |
| 158 | ++C; |
| 159 | |
| 160 | if (c == '\n') { |
| 161 | LineEndPos = FilePos++; |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 162 | break; |
| 163 | } |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 164 | |
| 165 | ++FilePos; |
| 166 | } |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 167 | |
| 168 | AddLineNumber(R, LineNo, |
| 169 | SourceLocation::getFileLoc(FileID, LineStartPos), |
Ted Kremenek | 49cd635 | 2008-04-03 07:12:29 +0000 | [diff] [blame] | 170 | SourceLocation::getFileLoc(FileID, LineEndPos)); |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Add one big div tag that surrounds all of the code. |
| 174 | |
| 175 | R.InsertCStrBefore(SourceLocation::getFileLoc(FileID, 0), |
Ted Kremenek | e8e019e | 2008-03-19 21:59:05 +0000 | [diff] [blame] | 176 | "<table class=\"code\">\n"); |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 177 | |
| 178 | R.InsertCStrAfter(SourceLocation::getFileLoc(FileID, FileEnd - FileBeg), |
Ted Kremenek | e8e019e | 2008-03-19 21:59:05 +0000 | [diff] [blame] | 179 | "</table>"); |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 180 | } |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 181 | |
| 182 | void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID) { |
| 183 | |
| 184 | const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID); |
| 185 | const char* FileStart = Buf->getBufferStart(); |
| 186 | const char* FileEnd = Buf->getBufferEnd(); |
| 187 | |
| 188 | SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0); |
| 189 | SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart); |
| 190 | |
| 191 | // Generate header |
| 192 | |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 193 | R.InsertCStrBefore(StartLoc, |
| 194 | "<html>\n<head>\n" |
| 195 | "<style type=\"text/css\">\n" |
| 196 | " body { color:#000000; background-color:#ffffff }\n" |
| 197 | " body { font-family:Helvetica, sans-serif; font-size:10pt }\n" |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 198 | " h1 { font-size:14pt }\n" |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 199 | " .code { border-spacing:0px; width:100%; }\n" |
| 200 | " .code { font-family: \"Andale Mono\", monospace; font-size:10pt }\n" |
| 201 | " .code { line-height: 1.2em }\n" |
| 202 | " .num { width:2.5em; padding-right:2ex; background-color:#eeeeee }\n" |
| 203 | " .num { text-align:right; font-size: smaller }\n" |
| 204 | " .num { color:#444444 }\n" |
| 205 | " .line { padding-left: 1ex; border-left: 3px solid #ccc }\n" |
| 206 | " .line { white-space: pre }\n" |
| 207 | " .msg { background-color:#fff8b4; color:#000000 }\n" |
| 208 | " .msg { -webkit-box-shadow:1px 1px 7px #000 }\n" |
| 209 | " .msg { -webkit-border-radius:5px }\n" |
| 210 | " .msg { font-family:Helvetica, sans-serif; font-size: smaller }\n" |
| 211 | " .msg { font-weight: bold }\n" |
| 212 | " .msg { float:left }\n" |
| 213 | " .msg { padding:0.5em 1ex 0.5em 1ex }\n" |
| 214 | " .msg { margin-top:10px; margin-bottom:10px }\n" |
| 215 | " .mrange { background-color:#dfddf3 }\n" |
| 216 | " .mrange { border-bottom:1px solid #6F9DBE }\n" |
| 217 | " .PathIndex { font-weight: bold }\n" |
Ted Kremenek | 4b0f813 | 2008-04-15 21:25:08 +0000 | [diff] [blame] | 218 | " table.simpletable {\n" |
| 219 | " padding: 5px;\n" |
| 220 | " font-size:12pt;\n" |
| 221 | " margin:20px;\n" |
| 222 | " border-collapse: collapse; border-spacing: 0px;\n" |
| 223 | " }\n" |
| 224 | " td.rowname {\n" |
| 225 | " text-align:right; font-weight:bold; color:#444444;\n" |
| 226 | " padding-right:2ex; }\n" |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 227 | "</style>\n</head>\n<body>"); |
| 228 | |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 229 | // Generate footer |
| 230 | |
Ted Kremenek | 70bcba6 | 2008-04-09 15:40:40 +0000 | [diff] [blame] | 231 | R.InsertCStrAfter(EndLoc, "</body></html>\n"); |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 232 | } |