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 | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 49 | static void AddLineNumber(Rewriter& R, unsigned LineNo, |
| 50 | SourceLocation B, SourceLocation E) { |
Ted Kremenek | f830997 | 2008-03-19 01:30:02 +0000 | [diff] [blame] | 51 | |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 52 | // Surround the line text with a div tag. |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame^] | 53 | |
| 54 | R.InsertCStrBefore(E, "</div>"); |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 56 | if (B == E) // Handle empty lines. |
| 57 | R.InsertCStrBefore(B, "<div class=\"lines\"> </div>"); |
| 58 | else { |
| 59 | R.InsertCStrBefore(E, "</div>"); |
| 60 | R.InsertCStrBefore(B, "<div class=\"lines\">"); |
| 61 | } |
Ted Kremenek | f830997 | 2008-03-19 01:30:02 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 63 | // Insert a div tag for the line number. |
| 64 | |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 65 | std::ostringstream os; |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 66 | os << "<div class=\"nums\">" << LineNo << "</div>"; |
| 67 | |
| 68 | R.InsertStrBefore(B, os.str()); |
| 69 | |
| 70 | // Now surround the whole line with another div tag. |
| 71 | |
| 72 | R.InsertCStrBefore(B, "<div class=\"codeline\">"); |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame^] | 73 | |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void html::AddLineNumbers(Rewriter& R, unsigned FileID) { |
| 77 | |
| 78 | const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID); |
| 79 | const char* FileBeg = Buf->getBufferStart(); |
| 80 | const char* FileEnd = Buf->getBufferEnd(); |
| 81 | const char* C = FileBeg; |
| 82 | |
| 83 | assert (C <= FileEnd); |
| 84 | |
| 85 | unsigned LineNo = 0; |
| 86 | unsigned FilePos = 0; |
| 87 | |
| 88 | while (C != FileEnd) { |
| 89 | |
| 90 | ++LineNo; |
| 91 | unsigned LineStartPos = FilePos; |
| 92 | unsigned LineEndPos = FileEnd - FileBeg; |
| 93 | |
| 94 | assert (FilePos <= LineEndPos); |
| 95 | assert (C < FileEnd); |
| 96 | |
| 97 | // Scan until the newline (or end-of-file). |
| 98 | |
| 99 | for ( ; C != FileEnd ; ++C, ++FilePos) |
| 100 | if (*C == '\n') { |
| 101 | LineEndPos = FilePos; |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | AddLineNumber(R, LineNo, |
| 106 | SourceLocation::getFileLoc(FileID, LineStartPos), |
| 107 | SourceLocation::getFileLoc(FileID, LineEndPos)); |
| 108 | |
| 109 | if (C != FileEnd) { |
| 110 | ++C; |
| 111 | ++FilePos; |
| 112 | } |
Ted Kremenek | d6c1360 | 2008-03-19 05:07:26 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Add one big div tag that surrounds all of the code. |
| 116 | |
| 117 | R.InsertCStrBefore(SourceLocation::getFileLoc(FileID, 0), |
| 118 | "<div id=\"codeblock\">"); |
| 119 | |
| 120 | R.InsertCStrAfter(SourceLocation::getFileLoc(FileID, FileEnd - FileBeg), |
| 121 | "</div>"); |
Ted Kremenek | b485cd1 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 122 | } |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 123 | |
| 124 | void html::AddHeaderFooterInternalBuiltinCSS(Rewriter& R, unsigned FileID) { |
| 125 | |
| 126 | const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID); |
| 127 | const char* FileStart = Buf->getBufferStart(); |
| 128 | const char* FileEnd = Buf->getBufferEnd(); |
| 129 | |
| 130 | SourceLocation StartLoc = SourceLocation::getFileLoc(FileID, 0); |
| 131 | SourceLocation EndLoc = SourceLocation::getFileLoc(FileID, FileEnd-FileStart); |
| 132 | |
| 133 | // Generate header |
| 134 | |
| 135 | { |
| 136 | std::ostringstream os; |
| 137 | |
| 138 | os << "<html>\n<head>\n" |
| 139 | << " <style type=\"text/css\">\n" |
| 140 | << " .codeblock { width:100% }\n" |
| 141 | << " .codeline { font-family: \"Andale Mono\", fixed; font-size:10pt }\n" |
| 142 | << " .codeline { height:1.5em; line-height:1.5em }\n" |
| 143 | << " .nums, .lines { float:left; height:100% }\n" |
| 144 | << " .nums { background-color: #eeeeee }\n" |
| 145 | << " .nums { font-size:smaller }\n" |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame^] | 146 | << " .nums { width:2.5em; padding-right:2ex; text-align:right }\n" |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 147 | << " .lines { padding-left: 1ex; border-left: 3px solid #ccc }\n" |
| 148 | << " .lines { white-space: pre }\n" |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame^] | 149 | << " .msg { background-color:#fcff4c; float:left }\n" |
| 150 | << " .msg { font-family:Helvetica, sans-serif; font-size: smaller }\n" |
| 151 | << " .msg { padding:5px; margin-top:10px; margin-bottom:10px }\n" |
Ted Kremenek | ad0a203 | 2008-03-19 06:14:37 +0000 | [diff] [blame] | 152 | << " </style>\n" |
| 153 | << "</head>\n" |
| 154 | << "<body>"; |
| 155 | |
| 156 | R.InsertStrBefore(StartLoc, os.str()); |
| 157 | } |
| 158 | |
| 159 | // Generate footer |
| 160 | |
| 161 | { |
| 162 | std::ostringstream os; |
| 163 | |
| 164 | os << "</body></html>\n"; |
| 165 | R.InsertStrAfter(EndLoc, os.str()); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | |