1 | /**************************************************************************** |
2 | * Copyright (C) 2012-2016 Woboq GmbH |
3 | * Olivier Goffart <contact at woboq.com> |
4 | * https://woboq.com/codebrowser.html |
5 | * |
6 | * This file is part of the Woboq Code Browser. |
7 | * |
8 | * Commercial License Usage: |
9 | * Licensees holding valid commercial licenses provided by Woboq may use |
10 | * this file in accordance with the terms contained in a written agreement |
11 | * between the licensee and Woboq. |
12 | * For further information see https://woboq.com/codebrowser.html |
13 | * |
14 | * Alternatively, this work may be used under a Creative Commons |
15 | * Attribution-NonCommercial-ShareAlike 3.0 (CC-BY-NC-SA 3.0) License. |
16 | * http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US |
17 | * This license does not allow you to use the code browser to assist the |
18 | * development of your commercial software. If you intent to do so, consider |
19 | * purchasing a commercial licence. |
20 | ****************************************************************************/ |
21 | |
22 | #pragma once |
23 | |
24 | #include <llvm/ADT/SmallString.h> |
25 | #include <map> |
26 | #include <set> |
27 | #include <string> |
28 | #include <unordered_set> |
29 | #include <vector> |
30 | |
31 | namespace llvm { |
32 | class raw_ostream; |
33 | } |
34 | |
35 | |
36 | /* This class generate the HTML out of a file with the said tags. |
37 | */ |
38 | class Generator |
39 | { |
40 | |
41 | struct Tag |
42 | { |
43 | std::string name; |
44 | std::string attributes; |
45 | int pos; |
46 | int len; |
47 | std::string innerHtml; |
48 | bool operator<(const Tag &other) const |
49 | { |
50 | // This is the order of the opening tag. Order first by position, then by length |
51 | // (in the reverse order) with the exception of length of 0 which always goes first. |
52 | // Ordered first by position, and then by lenth (reverse order) |
53 | return (pos != other.pos) ? pos < other.pos |
54 | : len == 0 || (other.len != 0 && len > other.len); |
55 | } |
56 | bool operator==(const Tag &other) const |
57 | { |
58 | return std::tie(args: pos, args: len, args: name, args: attributes) |
59 | == std::tie(args: other.pos, args: other.len, args: other.name, args: other.attributes); |
60 | } |
61 | void open(llvm::raw_ostream &myfile) const; |
62 | void close(llvm::raw_ostream &myfile) const; |
63 | }; |
64 | |
65 | std::multiset<Tag> tags; |
66 | |
67 | std::map<std::string, std::string> projects; |
68 | |
69 | public: |
70 | void addTag(std::string name, std::string attributes, int pos, int len, |
71 | std::string innerHtml = {}) |
72 | { |
73 | if (len < 0) { |
74 | return; |
75 | } |
76 | Tag t = { .name: std::move(name), .attributes: std::move(attributes), .pos: pos, .len: len, .innerHtml: std::move(innerHtml) }; |
77 | auto it = tags.find(x: t); |
78 | if (it != tags.end() && *it == t) |
79 | return; // Hapens in macro for example |
80 | tags.insert(x: std::move(t)); |
81 | } |
82 | void addProject(std::string a, std::string b) |
83 | { |
84 | projects.insert(x: { std::move(a), std::move(b) }); |
85 | } |
86 | |
87 | void generate(llvm::StringRef outputPrefix, std::string dataPath, const std::string &filename, |
88 | const char *begin, const char *end, llvm::StringRef , |
89 | llvm::StringRef warningMessage, |
90 | const std::set<std::string> &interestingDefitions); |
91 | |
92 | static llvm::StringRef escapeAttr(llvm::StringRef, llvm::SmallVectorImpl<char> &buffer); |
93 | |
94 | /** |
95 | * This function takes a reference name that was already processed by `escapeAttr` and |
96 | * replaces additional chars which are not allowed in filenames on all platforms. |
97 | * |
98 | * @param s already escaped ref |
99 | * @param buffer |
100 | * @return |
101 | */ |
102 | static llvm::StringRef escapeAttrForFilename(llvm::StringRef s, |
103 | llvm::SmallVectorImpl<char> &buffer); |
104 | static void escapeAttr(llvm::raw_ostream &os, llvm::StringRef s); |
105 | |
106 | struct EscapeAttr |
107 | { |
108 | llvm::StringRef value; |
109 | }; |
110 | }; |
111 | |
112 | inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, Generator::EscapeAttr s) |
113 | { |
114 | Generator::escapeAttr(os, s: s.value); |
115 | return os; |
116 | } |
117 | |