Loading...
Searching...
No Matches
format_html.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <iostream>
#include <seqan3/argument_parser/detail/format_base.hpp>
#include <seqan3/argument_parser/detail/terminal.hpp>
#include <seqan3/version.hpp>
namespace seqan3::detail
{
class format_html : public format_help_base<format_html>
{
using base_type = format_help_base<format_html>;
friend base_type;
public:
format_html() = default;
format_html(format_html const & pf) = default;
format_html & operator=(format_html const & pf) = default;
format_html(format_html &&) = default;
format_html & operator=(format_html &&) = default;
~format_html() = default;
format_html(std::vector<std::string> const & names, bool const advanced = false) : base_type{names, advanced} {};
private:
void maybe_close_list()
{
if (is_dl)
{
std::cout << "</dl>\n";
is_dl = false;
}
}
void maybe_close_paragraph()
{
if (is_p)
{
std::cout << "</p>\n";
is_p = false;
}
}
void print_header()
{
// Print HTML boilerplate header.
std::cout << "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" "
<< "http://www.w3.org/TR/html4/strict.dtd\">\n"
<< "<html lang=\"en\">\n"
<< "<head>\n"
<< "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\n"
<< "<title>" << escape_special_xml_chars(meta.app_name) << " &mdash; "
<< escape_special_xml_chars(meta.short_description) << "</title>\n"
<< "</head>\n"
<< "<body>\n";
std::cout << "<h1>" << to_html(meta.app_name) << "</h1>\n"
<< "<div>" << to_html(meta.short_description) << "</div>\n";
}
void print_section(std::string const & title)
{
// SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
maybe_close_list();
maybe_close_paragraph();
std::cout << "<h2>" << to_html(title) << "</h2>\n";
}
void print_subsection(std::string const & title)
{
// SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
maybe_close_list();
maybe_close_paragraph();
std::cout << "<h3>" << to_html(title) << "</h3>\n";
}
void print_line(std::string const & text, bool line_is_paragraph)
{
// SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
maybe_close_list();
if (!is_p) // open parapgraph
{
std::cout << "<p>\n";
is_p = true;
}
std::cout << to_html(text) << "\n";
if (line_is_paragraph)
maybe_close_paragraph();
else
std::cout << "<br>\n";
}
void print_list_item(std::string const & term, std::string const & desc)
{
// SEQAN_ASSERT_NOT_MSG(isDl && isP, "Current <dl> and <p> are mutually exclusive.");
maybe_close_paragraph();
if (!is_dl)
{
std::cout << "<dl>\n";
is_dl = true;
}
std::cout << "<dt>" << to_html(term) << "</dt>\n"
<< "<dd>" << to_html(desc) << "</dd>\n";
}
void print_footer()
{
maybe_close_paragraph();
// Print HTML boilerplate footer.
std::cout << "</body></html>";
}
std::string to_html(std::string const & input)
{
std::string buffer = escape_special_xml_chars(input);
std::string result;
std::vector<std::string> open_tags; // acts as a stack of html tags
for (auto it = input.begin(); it != input.end(); ++it)
{
if (*it == '\\')
{
// Handle escape sequence, we interpret only "\-", "\fI", and "\fB".
++it;
assert(!(it == input.end()));
if (*it == '-')
{
result.push_back(*it);
}
else if (*it == 'f')
{
++it;
assert(!(it == input.end()));
if (*it == 'I')
{
open_tags.push_back("em");
result.append("<em>");
}
else if (*it == 'B')
{
open_tags.push_back("strong");
result.append("<strong>");
}
else if (*it == 'P')
{
assert(!open_tags.empty());
result.append("</");
result.append(open_tags.back());
result.append(">");
open_tags.pop_back();
}
else
{
result.append("\\f");
result.push_back(*it);
}
}
else
{
result.push_back('\\');
result.push_back(*it);
}
}
else
{
result.push_back(*it);
}
}
return result;
}
std::string in_bold(std::string const & str)
{
return "<strong>" + str + "</strong>";
}
bool is_dl{false};
bool is_p{false};
};
} // namespace seqan3::detail
std::string::append
T append(T... args)
std::vector::back
T back(T... args)
std::string::begin
T begin(T... args)
std::vector::empty
T empty(T... args)
std::string::end
T end(T... args)
format_base.hpp
Provides the format_base struct containing all helper functions that are needed in all formats.
seqan3::advanced
@ advanced
Definition auxiliary.hpp:256
iostream
std::vector::pop_back
T pop_back(T... args)
std::string::push_back
T push_back(T... args)
terminal.hpp
Checks if program is run interactively and retrieves dimensions of terminal (Transferred from seqan2)...
version.hpp
Provides SeqAn version macros and global variables.
Hide me