SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
format_html.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <iostream>
13
16#include <seqan3/version.hpp>
17
18namespace seqan3::detail
19{
20
33class format_html : public format_help_base<format_html>
34{
36 using base_type = format_help_base<format_html>;
37
39 friend base_type;
40
41public:
45 format_html() = default;
46 format_html(format_html const & pf) = default;
47 format_html & operator=(format_html const & pf) = default;
48 format_html(format_html &&) = default;
49 format_html & operator=(format_html &&) = default;
50 ~format_html() = default;
51
53 format_html(std::vector<std::string> const & names, bool const advanced = false) : base_type{names, advanced} {};
55
56private:
58 void maybe_close_list()
59 {
60 if (is_dl)
61 {
62 std::cout << "</dl>\n";
63 is_dl = false;
64 }
65 }
66
68 void maybe_close_paragraph()
69 {
70 if (is_p)
71 {
72 std::cout << "</p>\n";
73 is_p = false;
74 }
75 }
76
78 void print_header()
79 {
80 // Print HTML boilerplate header.
81 std::cout << "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" "
82 << "http://www.w3.org/TR/html4/strict.dtd\">\n"
83 << "<html lang=\"en\">\n"
84 << "<head>\n"
85 << "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\n"
86 << "<title>" << escape_special_xml_chars(meta.app_name) << " &mdash; "
87 << escape_special_xml_chars(meta.short_description) << "</title>\n"
88 << "</head>\n"
89 << "<body>\n";
90
91 std::cout << "<h1>" << to_html(meta.app_name) << "</h1>\n"
92 << "<div>" << to_html(meta.short_description) << "</div>\n";
93 }
94
98 void print_section(std::string const & title)
99 {
100 // SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
101 maybe_close_list();
102 maybe_close_paragraph();
103 std::cout << "<h2>" << to_html(title) << "</h2>\n";
104 }
105
109 void print_subsection(std::string const & title)
110 {
111 // SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
112 maybe_close_list();
113 maybe_close_paragraph();
114 std::cout << "<h3>" << to_html(title) << "</h3>\n";
115 }
116
122 void print_line(std::string const & text, bool line_is_paragraph)
123 {
124 // SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
125 maybe_close_list();
126 if (!is_p) // open parapgraph
127 {
128 std::cout << "<p>\n";
129 is_p = true;
130 }
131 std::cout << to_html(text) << "\n";
132 if (line_is_paragraph)
133 maybe_close_paragraph();
134 else
135 std::cout << "<br>\n";
136 }
137
147 void print_list_item(std::string const & term, std::string const & desc)
148 {
149 // SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
150 maybe_close_paragraph();
151
152 if (!is_dl)
153 {
154 std::cout << "<dl>\n";
155 is_dl = true;
156 }
157 std::cout << "<dt>" << to_html(term) << "</dt>\n"
158 << "<dd>" << to_html(desc) << "</dd>\n";
159 }
160
162 void print_footer()
163 {
164 maybe_close_paragraph();
165
166 // Print HTML boilerplate footer.
167 std::cout << "</body></html>";
168 }
169
174 std::string to_html(std::string const & input)
175 {
176 std::string buffer = escape_special_xml_chars(input);
177 std::string result;
178 std::vector<std::string> open_tags; // acts as a stack of html tags
179
180 for (auto it = input.begin(); it != input.end(); ++it)
181 {
182 if (*it == '\\')
183 {
184 // Handle escape sequence, we interpret only "\-", "\fI", and "\fB".
185 ++it;
186 assert(!(it == input.end()));
187 if (*it == '-')
188 {
189 result.push_back(*it);
190 }
191 else if (*it == 'f')
192 {
193 ++it;
194 assert(!(it == input.end()));
195 if (*it == 'I')
196 {
197 open_tags.push_back("em");
198 result.append("<em>");
199 }
200 else if (*it == 'B')
201 {
202 open_tags.push_back("strong");
203 result.append("<strong>");
204 }
205 else if (*it == 'P')
206 {
207 assert(!open_tags.empty());
208 result.append("</");
209 result.append(open_tags.back());
210 result.append(">");
211 open_tags.pop_back();
212 }
213 else
214 {
215 result.append("\\f");
216 result.push_back(*it);
217 }
218 }
219 else
220 {
221 result.push_back('\\');
222 result.push_back(*it);
223 }
224 }
225 else
226 {
227 result.push_back(*it);
228 }
229 }
230
231 return result;
232 }
233
238 std::string in_bold(std::string const & str)
239 {
240 return "<strong>" + str + "</strong>";
241 }
242
244 bool is_dl{false};
246 bool is_p{false};
247};
248
249} // namespace seqan3::detail
T append(T... args)
T back(T... args)
T begin(T... args)
T empty(T... args)
T end(T... args)
Provides the format_base struct containing all helper functions that are needed in all formats.
@ advanced
Definition auxiliary.hpp:254
T pop_back(T... args)
T push_back(T... args)
Checks if program is run interactively and retrieves dimensions of terminal (Transferred from seqan2)...
Provides SeqAn version macros and global variables.
Hide me