Skip to content

Commit

Permalink
fix export to html of lists with rich text tags (#2286)
Browse files Browse the repository at this point in the history
  • Loading branch information
giuspen committed Jun 1, 2023
1 parent 7a7dbe0 commit 3f952ab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 34 deletions.
8 changes: 4 additions & 4 deletions src/ct/ct_export2html.cc
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,10 @@ void CtExport2Html::_html_get_from_treestore_node(CtTreeIter node_iter,
CtListInfo* pListInfoFrom,
const CtListInfo* pListInfoTo)
{
int ret_forward_start{0};
if (*pListInfoFrom == *pListInfoTo) {
return ret_forward_start;
return 0;
}
//spdlog::debug("list_info t={} s={} l={} c={} n={}", static_cast<int>(pListInfoTo->type),
// pListInfoTo->startoffs, pListInfoTo->level, pListInfoTo->count_nl, pListInfoTo->num_seq);
int ret_forward_start{0};
auto f_increase_level_ol = [&](){
curr_html_text += (CtConst::TAG_OL_START + CtConst::TAG_LI_START);
nested_list_types.push_back(pListInfoTo->type);
Expand Down Expand Up @@ -701,6 +699,8 @@ void CtExport2Html::_html_get_from_treestore_node(CtTreeIter node_iter,
CtCurrAttributesMap& curr_attributes,
CtListInfo* pCurrListInfo)
{
//spdlog::debug("'{}' t={} s={} l={} c={} n={}", start_iter.get_text(curr_iter), static_cast<int>(pCurrListInfo->type),
// pCurrListInfo->startoffs, pCurrListInfo->level, pCurrListInfo->count_nl, pCurrListInfo->num_seq);
Glib::ustring list_html_tags;
const int forward_start = _html_process_list_info_change(list_html_tags, nested_list_types, &curr_list_info, pCurrListInfo);
//spdlog::debug("fw={} +'{}'", forward_start, list_html_tags.raw());
Expand Down
29 changes: 5 additions & 24 deletions src/ct/ct_misc_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ void CtTextIterUtil::generic_process_slot(const CtConfig* const pCtConfig,
const int start_offset,
const int end_offset,
const Glib::RefPtr<Gtk::TextBuffer>& rTextBuffer,
SerializeFunc serialize_func,
SerializeFunc f_serialize_func,
const bool list_info/*= false*/)
{
CtCurrAttributesMap curr_attributes;
Expand All @@ -422,8 +422,7 @@ void CtTextIterUtil::generic_process_slot(const CtConfig* const pCtConfig,
}
}

CtListInfo curr_list_info, prev_list_info;
bool list_info_changed{false};
CtListInfo curr_list_info;
bool last_was_newline{false};
if (not curr_end_iter.backward_char()) {
last_was_newline = true;
Expand All @@ -433,46 +432,28 @@ void CtTextIterUtil::generic_process_slot(const CtConfig* const pCtConfig,
curr_end_iter.forward_char();
}

bool first_list_info_change{true};
while (curr_end_iter.forward_char()) {
if (curr_end_iter.compare(real_end_iter) >= 0) {
break;
}

if (list_info and last_was_newline) {
curr_list_info = CtList{pCtConfig, rTextBuffer}.get_paragraph_list_info(curr_end_iter);
if (curr_list_info != prev_list_info) {
list_info_changed = true;
if (first_list_info_change) {
first_list_info_change = false;
if ((curr_end_iter.get_offset() - curr_start_iter.get_offset()) > 1) {
// this means that we are not starting with a list item, there is non-list preceding
curr_end_iter.backward_char();
(void)CtTextIterUtil::rich_text_attributes_update(curr_end_iter, curr_attributes, delta_attributes);
serialize_func(curr_start_iter, curr_end_iter, curr_attributes, &prev_list_info);
for (auto& currDelta : delta_attributes) curr_attributes[currDelta.first] = currDelta.second;
curr_start_iter = curr_end_iter;
curr_end_iter.forward_char();
}
}
prev_list_info = curr_list_info;
}
}

last_was_newline = '\n' == curr_end_iter.get_char();

if (CtTextIterUtil::rich_text_attributes_update(curr_end_iter, curr_attributes, delta_attributes) or
(list_info and last_was_newline and list_info_changed))
(list_info and last_was_newline))
{
serialize_func(curr_start_iter, curr_end_iter, curr_attributes, &curr_list_info);
f_serialize_func(curr_start_iter, curr_end_iter, curr_attributes, &curr_list_info);
for (auto& currDelta : delta_attributes) curr_attributes[currDelta.first] = currDelta.second;
curr_start_iter = curr_end_iter;
if (list_info_changed) list_info_changed = false;
}
}

if (curr_start_iter.compare(real_end_iter) < 0) {
serialize_func(curr_start_iter, real_end_iter, curr_attributes, &curr_list_info);
f_serialize_func(curr_start_iter, real_end_iter, curr_attributes, &curr_list_info);
}
}

Expand Down
21 changes: 15 additions & 6 deletions tests/tests_lists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,23 @@ const Glib::ustring bufferContent_1{
const Glib::ustring bufferContent_2{
"ciao" _NL // 0
_NL // 5
"- primo elemento" _NL // 6
"- secondo elemento" _NL}; // 23
"- primo elemento con tag" _NL // 6
"- secondo elemento" _NL}; // 31

TEST(ListsGroup, CtListInfo_2)
{
Glib::init();
auto pBuffer = Gsv::Buffer::create();
auto rTextTagTable = Gtk::TextTagTable::create();
auto pBuffer = Gsv::Buffer::create(rTextTagTable);
pBuffer->set_text(bufferContent_2);
const std::string tagName{CtConst::TAG_WEIGHT + CtConst::CHAR_USCORE + CtConst::TAG_PROP_VAL_HEAVY};
auto rTextTag = Gtk::TextTag::create(tagName);
rTextTag->property_weight() = Pango::Weight::WEIGHT_HEAVY;
rTextTagTable->add(rTextTag);
pBuffer->apply_tag_by_name(tagName,
pBuffer->get_iter_at_offset(23),
pBuffer->get_iter_at_offset(26));

CtList ct_list{&ct_config, pBuffer};

CtListInfo curr_list_info = ct_list.get_paragraph_list_info(pBuffer->get_iter_at_offset(0));
Expand All @@ -75,17 +84,17 @@ TEST(ListsGroup, CtListInfo_2)
ASSERT_EQ(6, curr_list_info.startoffs);
ASSERT_EQ(0, curr_list_info.count_nl);

curr_list_info = ct_list.get_paragraph_list_info(pBuffer->get_iter_at_offset(23));
curr_list_info = ct_list.get_paragraph_list_info(pBuffer->get_iter_at_offset(31));
ASSERT_EQ(CtListType::Bullet, curr_list_info.type);
ASSERT_EQ(0, curr_list_info.level);
ASSERT_EQ(23, curr_list_info.startoffs);
ASSERT_EQ(31, curr_list_info.startoffs);
ASSERT_EQ(0, curr_list_info.count_nl);

Glib::ustring out_html = CtExport2Html::html_process_slot(&ct_config,
nullptr/*pCtMainWin*/,
0, bufferContent_2.size()-1,
pBuffer);
ASSERT_STREQ("ciao\n<ul><li>primo elemento</li><li>secondo elemento</li></ul>", out_html.c_str());
ASSERT_STREQ("ciao\n<ul><li>primo elemento <strong>con</strong> tag</li><li>secondo elemento</li></ul>", out_html.c_str());
}

TEST(ListsGroup, CtListInfo_1)
Expand Down

0 comments on commit 3f952ab

Please sign in to comment.