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