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