1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
//== HTMLRewrite.cpp - Translate source code into prettified HTML --*- C++ -*-//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the HTMLRewriter clas, which is used to translate the
// text of a source file into prettified HTML.
//
//===----------------------------------------------------------------------===//
#include "clang/Rewrite/Rewriter.h"
#include "clang/Rewrite/HTMLRewrite.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/Support/MemoryBuffer.h"
#include <sstream>
using namespace clang;
void html::EscapeText(Rewriter& R, unsigned FileID, bool EscapeSpaces) {
const llvm::MemoryBuffer *Buf = R.getSourceMgr().getBuffer(FileID);
const char* C = Buf->getBufferStart();
const char* FileEnd = Buf->getBufferEnd();
assert (C <= FileEnd);
for (unsigned FilePos = 0; C != FileEnd ; ++C, ++FilePos) {
SourceLocation Loc = SourceLocation::getFileLoc(FileID, FilePos);
switch (*C) {
default: break;
case ' ':
if (EscapeSpaces) R.ReplaceText(Loc, 1, " ", 5);
break;
case '<': R.ReplaceText(Loc, 1, "<", 4); break;
case '>': R.ReplaceText(Loc, 1, ">", 4); break;
case '&': R.ReplaceText(Loc, 1, "&", 5); break;
}
}
}
void html::InsertTag(Rewriter& R, html::Tags tag,
SourceLocation B, SourceLocation E,
bool NewlineOpen, bool NewlineClose, bool OutermostTag) {
const char* TagStr = 0;
switch (tag) {
default: break;
case PRE: TagStr = "pre"; break;
case HEAD: TagStr = "head"; break;
case BODY: TagStr = "body"; break;
}
assert (TagStr && "Tag not supported.");
{ // Generate the opening tag.
std::ostringstream os;
os << '<' << TagStr << '>';
if (NewlineOpen) os << '\n';
const char* s = os.str().c_str();
unsigned n = os.str().size();
if (OutermostTag)
R.InsertTextBefore(B, s, n);
else
R.InsertTextAfter(B, s, n);
}
{ // Generate the closing tag.
std::ostringstream os;
os << "</" << TagStr << '>';
if (NewlineClose) os << '\n';
const char* s = os.str().c_str();
unsigned n = os.str().size();
if (OutermostTag)
R.InsertTextAfter(E, s, n);
else
R.InsertTextBefore(E, s, n);
}
}
|