Sharg 1.1.2-rc.1
The argument parser for bio-c++ tools.
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
13
14namespace sharg::detail
15{
16
29class format_html : public format_help_base<format_html>
30{
33
35 friend base_type;
36
37public:
41 format_html() = default;
42 format_html(format_html const & pf) = default;
43 format_html & operator=(format_html const & pf) = default;
44 format_html(format_html &&) = default;
46 ~format_html() = default;
47
50 update_notifications const version_updates,
51 bool const advanced = false) :
52 base_type{names, version_updates, advanced} {};
54
55private:
58 {
59 if (is_dl)
60 {
61 std::cout << "</dl>\n";
62 is_dl = false;
63 }
64 }
65
68 {
69 if (is_p)
70 {
71 std::cout << "</p>\n";
72 is_p = false;
73 }
74 }
75
78 {
79 // Print HTML boilerplate header.
80 std::cout << "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" "
81 << "http://www.w3.org/TR/html4/strict.dtd\">\n"
82 << "<html lang=\"en\">\n"
83 << "<head>\n"
84 << "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\n"
85 << "<title>" << escape_special_xml_chars(meta.app_name) << " &mdash; "
87 << "</head>\n"
88 << "<body>\n";
89
90 std::cout << "<h1>" << to_html(meta.app_name) << "</h1>\n"
91 << "<div>" << to_html(meta.short_description) << "</div>\n";
92 }
93
97 void print_section(std::string const & title)
98 {
99 // SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
102 std::cout << "<h2>" << to_html(title) << "</h2>\n";
103 }
104
108 void print_subsection(std::string const & title)
109 {
110 // SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
113 std::cout << "<h3>" << to_html(title) << "</h3>\n";
114 }
115
121 void print_line(std::string const & text, bool line_is_paragraph)
122 {
123 // SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
125 if (!is_p) // open parapgraph
126 {
127 std::cout << "<p>\n";
128 is_p = true;
129 }
130 std::cout << to_html(text) << "\n";
131 if (line_is_paragraph)
133 else
134 std::cout << "<br>\n";
135 }
136
146 void print_list_item(std::string const & term, std::string const & desc)
147 {
148 // SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
150
151 if (!is_dl)
152 {
153 std::cout << "<dl>\n";
154 is_dl = true;
155 }
156 std::cout << "<dt>" << to_html(term) << "</dt>\n"
157 << "<dd>" << to_html(desc) << "</dd>\n";
158 }
159
162 {
164
165 // Print HTML boilerplate footer.
166 std::cout << "</body></html>";
167 }
168
174 {
176 std::string result;
177 std::vector<std::string> open_tags; // acts as a stack of html tags
178
179 for (auto it = input.begin(); it != input.end(); ++it)
180 {
181 if (*it == '\\')
182 {
183 // Handle escape sequence, we interpret only "\-", "\fI", and "\fB".
184 ++it;
185 assert(!(it == input.end()));
186 if (*it == '-')
187 {
188 result.push_back(*it);
189 }
190 else if (*it == 'f')
191 {
192 ++it;
193 assert(!(it == input.end()));
194 if (*it == 'I')
195 {
196 open_tags.push_back("em");
197 result.append("<em>");
198 }
199 else if (*it == 'B')
200 {
201 open_tags.push_back("strong");
202 result.append("<strong>");
203 }
204 else if (*it == 'P')
205 {
206 assert(!open_tags.empty());
207 result.append("</");
208 result.append(open_tags.back());
209 result.append(">");
210 open_tags.pop_back();
211 }
212 else
213 {
214 result.append("\\f");
215 result.push_back(*it);
216 }
217 }
218 else
219 {
220 result.push_back('\\');
221 result.push_back(*it);
222 }
223 }
224 else
225 {
226 result.push_back(*it);
227 }
228 }
229
230 return result;
231 }
232
238 {
239 return "<strong>" + str + "</strong>";
240 }
241
243 bool is_dl{false};
245 bool is_p{false};
246};
247
248} // namespace sharg::detail
T append(T... args)
T back(T... args)
T begin(T... args)
static std::string escape_special_xml_chars(std::string const &original)
Escapes certain characters for correct output.
Definition format_base.hpp:137
The format that contains all helper functions needed in all formats for printing the interface descri...
Definition format_base.hpp:248
parser_meta_data meta
Stores all meta information about the application.
Definition format_base.hpp:508
The format that prints the help page as html to std::cout.
Definition format_html.hpp:30
void print_header()
Prints a help page header in HTML format to std::cout.
Definition format_html.hpp:77
void print_line(std::string const &text, bool line_is_paragraph)
Prints a text in HTML format to std::cout.
Definition format_html.hpp:121
void print_list_item(std::string const &term, std::string const &desc)
Prints a help page list_item in HTML format to std::cout.
Definition format_html.hpp:146
friend base_type
Befriend the base class to give access to the private member functions.
Definition format_html.hpp:35
void print_section(std::string const &title)
Prints a section title in HTML format to std::cout.
Definition format_html.hpp:97
std::string in_bold(std::string const &str)
Format string as in_bold.
Definition format_html.hpp:237
format_html(std::vector< std::string > const &names, update_notifications const version_updates, bool const advanced=false)
Defaulted.
Definition format_html.hpp:49
std::string to_html(std::string const &input)
Converts console output formatting to the HTML equivalent.
Definition format_html.hpp:173
bool is_dl
Current state is either inside a html <dl> tag (true) or not (false).
Definition format_html.hpp:243
format_html(format_html &&)=default
Defaulted.
void maybe_close_list()
Closes HTML list tag (dl) if needed.
Definition format_html.hpp:57
bool is_p
Current state is either inside a html <p> tag (true) or not (false).
Definition format_html.hpp:245
void print_subsection(std::string const &title)
Prints a subsection title in HTML format to std::cout.
Definition format_html.hpp:108
format_html(format_html const &pf)=default
Defaulted.
void print_footer()
Prints a help page footer in HTML format to std::cout.
Definition format_html.hpp:161
format_html()=default
Defaulted.
format_html & operator=(format_html &&)=default
Defaulted.
void maybe_close_paragraph()
Closes HTML paragraph tag (p) if needed.
Definition format_html.hpp:67
format_html & operator=(format_html const &pf)=default
Defaulted.
~format_html()=default
Defaulted.
T empty(T... args)
T end(T... args)
Provides the format_base struct containing all helper functions that are needed in all formats.
update_notifications
Indicates whether application allows automatic update notifications by the sharg::parser.
Definition auxiliary.hpp:26
T pop_back(T... args)
T push_back(T... args)
std::string short_description
A short description of the application (e.g. "A tool for mapping reads to the genome").
Definition auxiliary.hpp:57
std::string app_name
The application name that will be displayed on the help page.
Definition auxiliary.hpp:51
Hide me