Skip to content

Commit

Permalink
refactor abstract interface; random cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand committed Aug 18, 2024
1 parent 858720d commit 42211c9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
5 changes: 4 additions & 1 deletion src/odr/html_service.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <odr/html_service.hpp>

#include <odr/internal/abstract/html_service.hpp>
#include <odr/internal/html/html_writer.hpp>

#include <iostream>

Expand All @@ -26,7 +27,9 @@ std::string HtmlFragment::name() const { return m_impl->name(); }
void HtmlFragment::write_html_fragment(
std::ostream &os, const odr::HtmlConfig &config,
const odr::HtmlResourceLocator &resourceLocator) const {
m_impl->write_html_fragment(os, config, resourceLocator);
internal::html::HtmlWriter out(os, config);

m_impl->write_html_fragment(out, config, resourceLocator);
}

} // namespace odr
6 changes: 5 additions & 1 deletion src/odr/internal/abstract/html_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include <iosfwd>
#include <memory>

namespace odr::internal::html {
class HtmlWriter;
}

namespace odr::internal::abstract {
class File;
class HtmlFragment;
Expand All @@ -25,7 +29,7 @@ class HtmlFragment {
[[nodiscard]] virtual std::string name() const = 0;

virtual void
write_html_fragment(std::ostream &os, const HtmlConfig &config,
write_html_fragment(html::HtmlWriter &out, const HtmlConfig &config,
const HtmlResourceLocator &resourceLocator) const = 0;
};

Expand Down
16 changes: 4 additions & 12 deletions src/odr/internal/html/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ class TextHtmlFragment final : public abstract::HtmlFragment {
[[nodiscard]] std::string name() const final { return "document"; }

void
write_html_fragment(std::ostream &os, const HtmlConfig &config,
write_html_fragment(HtmlWriter &out, const HtmlConfig &config,
const HtmlResourceLocator &resourceLocator) const final {
HtmlWriter out(os, config);

auto root = m_document.root_element();
auto element = root.text_root();

Expand Down Expand Up @@ -71,10 +69,8 @@ class SlideHtmlFragment final : public abstract::HtmlFragment {
[[nodiscard]] std::string name() const final { return m_slide.name(); }

void
write_html_fragment(std::ostream &os, const HtmlConfig &config,
write_html_fragment(HtmlWriter &out, const HtmlConfig &config,
const HtmlResourceLocator &resourceLocator) const final {
HtmlWriter out(os, config);

internal::html::translate_slide(m_slide, out, config);
}

Expand All @@ -91,10 +87,8 @@ class SheetHtmlFragment final : public abstract::HtmlFragment {
[[nodiscard]] std::string name() const final { return m_sheet.name(); }

void
write_html_fragment(std::ostream &os, const HtmlConfig &config,
write_html_fragment(HtmlWriter &out, const HtmlConfig &config,
const HtmlResourceLocator &resourceLocator) const final {
HtmlWriter out(os, config);

translate_sheet(m_sheet, out, config);
}

Expand All @@ -111,10 +105,8 @@ class PageHtmlFragment final : public abstract::HtmlFragment {
[[nodiscard]] std::string name() const final { return m_page.name(); }

void
write_html_fragment(std::ostream &os, const HtmlConfig &config,
write_html_fragment(HtmlWriter &out, const HtmlConfig &config,
const HtmlResourceLocator &resourceLocator) const final {
HtmlWriter out(os, config);

internal::html::translate_page(m_page, out, config);
}

Expand Down
25 changes: 12 additions & 13 deletions src/odr/internal/html/html_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ HtmlElementOptions::set_extra(std::optional<HtmlWritable> _extra) {
return *this;
}

HtmlWriter::HtmlWriter(std::ostream &out, bool format, std::uint8_t indent)
: m_out{out}, m_format{format}, m_indent(indent, ' ') {}
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_current_indent{current_indent} {}

HtmlWriter::HtmlWriter(std::ostream &out, const HtmlConfig &config)
: HtmlWriter{out, config.format_html, config.html_indent} {}
Expand Down Expand Up @@ -158,7 +160,7 @@ void HtmlWriter::write_header_title(const std::string &title) {
void HtmlWriter::write_header_viewport(const std::string &viewport) {
write_new_line();

m_out << "<meta name=\"viewport\" content=\"";
m_out << R"(<meta name="viewport" content=")";
m_out << viewport;
m_out << "\"/>";
}
Expand All @@ -182,7 +184,7 @@ void HtmlWriter::write_header_charset(const std::string &charset) {
void HtmlWriter::write_header_style(const std::string &href) {
write_new_line();

m_out << "<link rel=\"stylesheet\" href=\"";
m_out << R"(<link rel="stylesheet" href=")";
m_out << href;
m_out << "\"/>";
}
Expand All @@ -204,7 +206,7 @@ void HtmlWriter::write_header_style_end() {
void HtmlWriter::write_script(const std::string &src) {
write_new_line();

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

void HtmlWriter::write_script_begin() {
Expand All @@ -221,7 +223,7 @@ void HtmlWriter::write_script_end() {
m_out << "</script>";
}

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

Expand All @@ -238,7 +240,7 @@ void HtmlWriter::write_body_end() {
}

void HtmlWriter::write_element_begin(const std::string &name,
HtmlElementOptions options) {
const HtmlElementOptions &options) {
write_new_line();
if (options.close_type == HtmlCloseType::standard) {
++m_current_indent;
Expand Down Expand Up @@ -270,12 +272,9 @@ void HtmlWriter::write_element_end(const std::string &name) {
}

bool HtmlWriter::is_inline_mode() const {
for (const auto &element : m_stack) {
if (element.inline_element) {
return true;
}
}
return false;
return std::ranges::all_of(m_stack, [](const StackElement &element) {
return element.inline_element;
});
}

void HtmlWriter::write_new_line() {
Expand Down
9 changes: 5 additions & 4 deletions src/odr/internal/html/html_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ struct HtmlElementOptions {

class HtmlWriter {
public:
HtmlWriter(std::ostream &out, bool format, std::uint8_t indent);
HtmlWriter(std::ostream &out, bool format, std::uint8_t indent,
std::uint32_t current_indent = 0);
HtmlWriter(std::ostream &out, const HtmlConfig &config);

void write_begin();
Expand All @@ -68,14 +69,14 @@ class HtmlWriter {
void write_script_begin();
void write_script_end();

void write_body_begin(HtmlElementOptions options = {});
void write_body_begin(const HtmlElementOptions &options = {});
void write_body_end();

void write_element_begin(const std::string &name,
HtmlElementOptions options = {});
const HtmlElementOptions &options = {});
void write_element_end(const std::string &name);

bool is_inline_mode() const;
[[nodiscard]] bool is_inline_mode() const;
void write_new_line();
void write_raw(const HtmlWritable &writable, bool new_line = true);

Expand Down

0 comments on commit 42211c9

Please sign in to comment.