From 61639c72759c3bafa086227c734af1d598f766f8 Mon Sep 17 00:00:00 2001 From: Kostas Dizas <254960+kostasdizas@users.noreply.github.com> Date: Tue, 11 Sep 2018 14:06:18 +0100 Subject: [PATCH] Added write_rich_text method to xlsx_producer This eliminates some duplicate code as reported in #165 --- source/detail/serialization/xlsx_producer.cpp | 203 +++++++----------- source/detail/serialization/xlsx_producer.hpp | 10 +- 2 files changed, 78 insertions(+), 135 deletions(-) diff --git a/source/detail/serialization/xlsx_producer.cpp b/source/detail/serialization/xlsx_producer.cpp index f8172c16..96431110 100644 --- a/source/detail/serialization/xlsx_producer.cpp +++ b/source/detail/serialization/xlsx_producer.cpp @@ -813,6 +813,73 @@ void xlsx_producer::write_pivot_table(const relationship & /*rel*/) write_end_element(constants::ns("spreadsheetml"), "pivotTableDefinition"); } +void xlsx_producer::write_rich_text(const std::string &ns, const xlnt::rich_text &text) +{ + if (text.runs().size() == 1 && !text.runs().at(0).second.is_set()) + { + write_start_element(ns, "t"); + write_characters(text.plain_text(), text.runs().front().preserve_space); + write_end_element(ns, "t"); + return; + } + + for (const auto &run : text.runs()) + { + write_start_element(ns, "r"); + + if (run.second.is_set()) + { + write_start_element(ns, "rPr"); + + if (run.second.get().bold()) + { + write_start_element(ns, "b"); + write_end_element(ns, "b"); + } + + if (run.second.get().has_size()) + { + write_start_element(ns, "sz"); + write_attribute("val", run.second.get().size()); + write_end_element(ns, "sz"); + } + + if (run.second.get().has_color()) + { + write_start_element(ns, "color"); + write_color(run.second.get().color()); + write_end_element(ns, "color"); + } + + if (run.second.get().has_name()) + { + write_start_element(ns, "rFont"); + write_attribute("val", run.second.get().name()); + write_end_element(ns, "rFont"); + } + + if (run.second.get().has_family()) + { + write_start_element(ns, "family"); + write_attribute("val", run.second.get().family()); + write_end_element(ns, "family"); + } + + if (run.second.get().has_scheme()) + { + write_start_element(ns, "scheme"); + write_attribute("val", run.second.get().scheme()); + write_end_element(ns, "scheme"); + } + + write_end_element(ns, "rPr"); + } + + write_element(ns, "t", run.first, run.preserve_space); + write_end_element(ns, "r"); + } +} + void xlsx_producer::write_shared_string_table(const relationship & /*rel*/) { static const auto &xmlns = constants::ns("spreadsheetml"); @@ -854,79 +921,8 @@ void xlsx_producer::write_shared_string_table(const relationship & /*rel*/) for (const auto &string : source_.shared_strings_by_id()) { - if (string.second.runs().size() == 1 && !string.second.runs().at(0).second.is_set()) - { - write_start_element(xmlns, "si"); - write_start_element(xmlns, "t"); - - write_characters(string.second.plain_text(), string.second.runs().front().preserve_space); - - write_end_element(xmlns, "t"); - write_end_element(xmlns, "si"); - - continue; - } - write_start_element(xmlns, "si"); - - for (const auto &run : string.second.runs()) - { - write_start_element(xmlns, "r"); - - if (run.second.is_set()) - { - write_start_element(xmlns, "rPr"); - - if (run.second.get().bold()) - { - write_start_element(xmlns, "b"); - write_end_element(xmlns, "b"); - } - - if (run.second.get().has_size()) - { - write_start_element(xmlns, "sz"); - write_attribute("val", run.second.get().size()); - write_end_element(xmlns, "sz"); - } - - if (run.second.get().has_color()) - { - write_start_element(xmlns, "color"); - write_color(run.second.get().color()); - write_end_element(xmlns, "color"); - } - - if (run.second.get().has_name()) - { - write_start_element(xmlns, "rFont"); - write_attribute("val", run.second.get().name()); - write_end_element(xmlns, "rFont"); - } - - if (run.second.get().has_family()) - { - write_start_element(xmlns, "family"); - write_attribute("val", run.second.get().family()); - write_end_element(xmlns, "family"); - } - - if (run.second.get().has_scheme()) - { - write_start_element(xmlns, "scheme"); - write_attribute("val", run.second.get().scheme()); - write_end_element(xmlns, "scheme"); - } - - write_end_element(xmlns, "rPr"); - } - - write_start_element(xmlns, "t"); - write_characters(run.first, run.preserve_space); - write_end_element(xmlns, "t"); - write_end_element(xmlns, "r"); - } - + write_rich_text(xmlns, string.second); write_end_element(xmlns, "si"); } @@ -2603,8 +2599,7 @@ void xlsx_producer::write_worksheet(const relationship &rel) case cell::type::inline_string: write_start_element(xmlns, "is"); - // TODO: make a write_rich_text method and use that here - write_element(xmlns, "t", cell.value()); + write_rich_text(xmlns, cell.value()); write_end_element(xmlns, "is"); break; @@ -3067,65 +3062,11 @@ void xlsx_producer::write_comments(const relationship & /*rel*/, worksheet ws, c write_attribute("ref", cell_ref.to_string()); auto author_id = authors.at(cell_comment.author()); write_attribute("authorId", author_id); + write_start_element(xmlns, "text"); - - for (const auto &run : cell_comment.text().runs()) - { - write_start_element(xmlns, "r"); - - if (run.second.is_set()) - { - write_start_element(xmlns, "rPr"); - - if (run.second.get().bold()) - { - write_start_element(xmlns, "b"); - write_end_element(xmlns, "b"); - } - - if (run.second.get().has_size()) - { - write_start_element(xmlns, "sz"); - write_attribute("val", run.second.get().size()); - write_end_element(xmlns, "sz"); - } - - if (run.second.get().has_color()) - { - write_start_element(xmlns, "color"); - write_color(run.second.get().color()); - write_end_element(xmlns, "color"); - } - - if (run.second.get().has_name()) - { - write_start_element(xmlns, "rFont"); - write_attribute("val", run.second.get().name()); - write_end_element(xmlns, "rFont"); - } - - if (run.second.get().has_family()) - { - write_start_element(xmlns, "family"); - write_attribute("val", run.second.get().family()); - write_end_element(xmlns, "family"); - } - - if (run.second.get().has_scheme()) - { - write_start_element(xmlns, "scheme"); - write_attribute("val", run.second.get().scheme()); - write_end_element(xmlns, "scheme"); - } - - write_end_element(xmlns, "rPr"); - } - - write_element(xmlns, "t", run.first); - write_end_element(xmlns, "r"); - } - + write_rich_text(xmlns, cell_comment.text()); write_end_element(xmlns, "text"); + write_end_element(xmlns, "comment"); } diff --git a/source/detail/serialization/xlsx_producer.hpp b/source/detail/serialization/xlsx_producer.hpp index 96050942..d6dc564f 100644 --- a/source/detail/serialization/xlsx_producer.hpp +++ b/source/detail/serialization/xlsx_producer.hpp @@ -45,6 +45,7 @@ class fill; class font; class path; class relationship; +class rich_text; class streaming_workbook_writer; class variant; class workbook; @@ -138,7 +139,7 @@ private: /// we're trying to match. /// std::string write_bool(bool boolean) const; - + void write_relationships(const std::vector &relationships, const path &part); void write_color(const xlnt::color &color); void write_border(const xlnt::border &b); @@ -146,12 +147,13 @@ private: void write_font(const xlnt::font &f); void write_table_styles(); void write_colors(const std::vector &colors); + void write_rich_text(const std::string &ns, const xlnt::rich_text &text); template - void write_element(const std::string &ns, const std::string &name, T value) + void write_element(const std::string &ns, const std::string &name, T value, bool preserve_whitespace = false) { write_start_element(ns, name); - write_characters(value); + write_characters(value, preserve_whitespace); write_end_element(ns, name); } @@ -192,7 +194,7 @@ private: /// A reference to the workbook which is the object of read/write operations. /// const workbook &source_; - + std::unique_ptr archive_; std::unique_ptr current_part_serializer_; std::unique_ptr current_part_streambuf_;