Ted Kremenek | 24612ae | 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 | f08ee32 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 49 | static void AddLineNumber(Rewriter& R, unsigned LineNo, |
| 50 | SourceLocation B, SourceLocation E) { |
Ted Kremenek | 90588af | 2008-03-19 01:30:02 +0000 | [diff] [blame^] | 51 | |
| 52 | // Surround the line with a span tag. |
Ted Kremenek | f08ee32 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | 90588af | 2008-03-19 01:30:02 +0000 | [diff] [blame^] | 54 | R.InsertTextBefore(E, "</span>", 7); |
| 55 | R.InsertTextBefore(B, "<span style=lines>", 18); |
| 56 | |
| 57 | // Insert a span tag for the line number. |
Ted Kremenek | f08ee32 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 58 | |
| 59 | std::ostringstream os; |
Ted Kremenek | 90588af | 2008-03-19 01:30:02 +0000 | [diff] [blame^] | 60 | os << "<span style=nums>" << LineNo << "</span>"; |
| 61 | R.InsertTextBefore(B, os.str().c_str(), os.str().size()); |
Ted Kremenek | f08ee32 | 2008-03-18 23:08:51 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void html::AddLineNumbers(Rewriter& R, unsigned FileID) { |
| 65 | |
| 66 | const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID); |
| 67 | const char* FileBeg = Buf->getBufferStart(); |
| 68 | const char* FileEnd = Buf->getBufferEnd(); |
| 69 | const char* C = FileBeg; |
| 70 | |
| 71 | assert (C <= FileEnd); |
| 72 | |
| 73 | unsigned LineNo = 0; |
| 74 | unsigned FilePos = 0; |
| 75 | |
| 76 | while (C != FileEnd) { |
| 77 | |
| 78 | ++LineNo; |
| 79 | unsigned LineStartPos = FilePos; |
| 80 | unsigned LineEndPos = FileEnd - FileBeg; |
| 81 | |
| 82 | assert (FilePos <= LineEndPos); |
| 83 | assert (C < FileEnd); |
| 84 | |
| 85 | // Scan until the newline (or end-of-file). |
| 86 | |
| 87 | for ( ; C != FileEnd ; ++C, ++FilePos) |
| 88 | if (*C == '\n') { |
| 89 | LineEndPos = FilePos; |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | AddLineNumber(R, LineNo, |
| 94 | SourceLocation::getFileLoc(FileID, LineStartPos), |
| 95 | SourceLocation::getFileLoc(FileID, LineEndPos)); |
| 96 | |
| 97 | if (C != FileEnd) { |
| 98 | ++C; |
| 99 | ++FilePos; |
| 100 | } |
| 101 | } |
| 102 | } |