Skip to content

Commit

Permalink
pull more code in
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Sep 29, 2024
1 parent 91423fd commit ab954e1
Show file tree
Hide file tree
Showing 4 changed files with 405 additions and 202 deletions.
15 changes: 7 additions & 8 deletions src/odr/internal/html/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void front(const Document &document, HtmlWriter &out, const HtmlConfig &config,
out.write_body_begin(HtmlElementOptions().set_class(body_clazz));
}

void back(const Document &document, internal::html::HtmlWriter &out,
void back(const Document &document, html::HtmlWriter &out,
const HtmlConfig &config,
const HtmlResourceLocator &resourceLocator) {
(void)document;
Expand All @@ -104,8 +104,8 @@ void back(const Document &document, internal::html::HtmlWriter &out,
std::string fill_path_variables(const std::string &path,
std::optional<std::uint32_t> index = {}) {
std::string result = path;
internal::util::string::replace_all(result, "{index}",
index ? std::to_string(*index) : "");
util::string::replace_all(result, "{index}",
index ? std::to_string(*index) : "");
return result;
}

Expand Down Expand Up @@ -219,7 +219,7 @@ class SlideHtmlFragment final : public HtmlFragmentBase {
void
write_html_fragment(HtmlWriter &out, const HtmlConfig &config,
const HtmlResourceLocator &resourceLocator) const final {
internal::html::translate_slide(m_slide, out, config, resourceLocator);
html::translate_slide(m_slide, out, config, resourceLocator);
}

private:
Expand Down Expand Up @@ -253,7 +253,7 @@ class PageHtmlFragment final : public HtmlFragmentBase {
void
write_html_fragment(HtmlWriter &out, const HtmlConfig &config,
const HtmlResourceLocator &resourceLocator) const final {
internal::html::translate_page(m_page, out, config, resourceLocator);
html::translate_page(m_page, out, config, resourceLocator);
}

private:
Expand Down Expand Up @@ -304,12 +304,11 @@ Html html::translate_document(const odr::Document &document,
std::uint32_t i = 0;
for (const auto &fragment : service.fragments()) {
std::string filled_path = get_output_path(document, i, output_path, config);
std::ofstream ostream(filled_path);
std::ofstream ostream(filled_path, std::ios::out);
if (!ostream.is_open()) {
throw FileWriteError();
}
internal::html::HtmlWriter out(ostream, config.format_html,
config.html_indent);
html::HtmlWriter out(ostream, config.format_html, config.html_indent);

fragment.write_html_document(out.out(), config, resourceLocator);

Expand Down
74 changes: 37 additions & 37 deletions src/odr/internal/html/html_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,123 +121,123 @@ HtmlElementOptions::set_extra(std::optional<HtmlWritable> _extra) {

HtmlWriter::HtmlWriter(std::ostream &out, bool format, std::uint8_t indent,
std::uint32_t current_indent)
: m_out{out}, m_format{format}, m_indent(indent, ' '),
: m_out{&out}, m_format{format}, m_indent(indent, ' '),
m_current_indent{current_indent} {}

HtmlWriter::HtmlWriter(std::ostream &out, const HtmlConfig &config)
: HtmlWriter{out, config.format_html, config.html_indent} {}

void HtmlWriter::write_begin() {
m_out << "<!DOCTYPE html>\n";
m_out << "<html>";
out() << "<!DOCTYPE html>\n";
out() << "<html>";
}

void HtmlWriter::write_end() {
write_new_line();

m_out << "</html>";
out() << "</html>";
}

void HtmlWriter::write_header_begin() {
write_new_line();
++m_current_indent;

m_out << "<head>";
out() << "<head>";
}

void HtmlWriter::write_header_end() {
--m_current_indent;
write_new_line();

m_out << "</head>";
out() << "</head>";
}

void HtmlWriter::write_header_title(const std::string &title) {
write_new_line();

m_out << "<title>" << title << "</title>";
out() << "<title>" << title << "</title>";
}

void HtmlWriter::write_header_viewport(const std::string &viewport) {
write_new_line();

m_out << R"(<meta name="viewport" content=")";
m_out << viewport;
m_out << "\"/>";
out() << R"(<meta name="viewport" content=")";
out() << viewport;
out() << "\"/>";
}

void HtmlWriter::write_header_target(const std::string &target) {
write_new_line();

m_out << "<base target=\"";
m_out << target;
m_out << "\"/>";
out() << "<base target=\"";
out() << target;
out() << "\"/>";
}

void HtmlWriter::write_header_charset(const std::string &charset) {
write_new_line();

m_out << "<meta charset=\"";
m_out << charset;
m_out << "\"/>";
out() << "<meta charset=\"";
out() << charset;
out() << "\"/>";
}

void HtmlWriter::write_header_style(const std::string &href) {
write_new_line();

m_out << R"(<link rel="stylesheet" href=")";
m_out << href;
m_out << "\"/>";
out() << R"(<link rel="stylesheet" href=")";
out() << href;
out() << "\"/>";
}

void HtmlWriter::write_header_style_begin() {
write_new_line();
++m_current_indent;

m_out << "<style>";
out() << "<style>";
}

void HtmlWriter::write_header_style_end() {
--m_current_indent;
write_new_line();

m_out << "</style>";
out() << "</style>";
}

void HtmlWriter::write_script(const std::string &src) {
write_new_line();

m_out << R"(<script type="text/javascript" src=")" << src << "\"></script>";
out() << R"(<script type="text/javascript" src=")" << src << "\"></script>";
}

void HtmlWriter::write_script_begin() {
write_new_line();
++m_current_indent;

m_out << "<script>";
out() << "<script>";
}

void HtmlWriter::write_script_end() {
--m_current_indent;
write_new_line();

m_out << "</script>";
out() << "</script>";
}

void HtmlWriter::write_body_begin(const HtmlElementOptions &options) {
write_new_line();
++m_current_indent;

m_out << "<body";
write_element_options(m_out, options);
m_out << ">";
out() << "<body";
write_element_options(out(), options);
out() << ">";
}

void HtmlWriter::write_body_end() {
--m_current_indent;
write_new_line();

m_out << "</body>";
out() << "</body>";
}

void HtmlWriter::write_element_begin(const std::string &name,
Expand All @@ -248,12 +248,12 @@ void HtmlWriter::write_element_begin(const std::string &name,
m_stack.push_back({name, options.inline_element});
}

m_out << "<" << name;
write_element_options(m_out, options);
out() << "<" << name;
write_element_options(out(), options);
if (options.close_type == HtmlCloseType::trailing) {
m_out << "/>";
out() << "/>";
} else {
m_out << ">";
out() << ">";
}
}

Expand All @@ -269,7 +269,7 @@ void HtmlWriter::write_element_end(const std::string &name) {
}
m_stack.pop_back();

m_out << "</" << name << ">";
out() << "</" << name << ">";
}

bool HtmlWriter::is_inline_mode() const {
Expand All @@ -286,9 +286,9 @@ void HtmlWriter::write_new_line() {
return;
}

m_out << '\n';
out() << '\n';
for (std::uint32_t i = 0; i < m_current_indent; ++i) {
m_out << m_indent;
out() << m_indent;
}
}

Expand All @@ -297,9 +297,9 @@ void HtmlWriter::write_raw(const HtmlWritable &writable, bool new_line) {
write_new_line();
}

write_writable(m_out, writable);
write_writable(out(), writable);
}

std::ostream &HtmlWriter::out() { return m_out; }
std::ostream &HtmlWriter::out() { return *m_out; }

} // namespace odr::internal::html
2 changes: 1 addition & 1 deletion src/odr/internal/html/html_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class HtmlWriter {
bool inline_element{false};
};

std::ostream &m_out;
std::ostream *m_out{nullptr};
bool m_format{false};
std::string m_indent;
std::uint32_t m_current_indent{0};
Expand Down
Loading

0 comments on commit ab954e1

Please sign in to comment.