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