Commit bf07cede authored by Aurimas Černius's avatar Aurimas Černius

Implement text_content in NoteBase

parent 1f8b511d
......@@ -83,6 +83,35 @@ std::vector<Glib::ustring> NoteBase::parse_tags(const xmlNodePtr tagnodes)
return tags;
}
Glib::ustring NoteBase::parse_text_content(const Glib::ustring & content)
{
xmlDocPtr doc = xmlParseDoc((const xmlChar*)content.c_str());
if(!doc) {
return "";
}
Glib::ustring ret;
sharp::XmlReader reader(doc);
while(reader.read()) {
switch(reader.get_node_type()) {
case XML_READER_TYPE_ELEMENT:
if(reader.get_name() == "list-item") {
ret += "\n";
}
break;
case XML_READER_TYPE_TEXT:
case XML_READER_TYPE_WHITESPACE:
case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
ret += reader.get_value();
break;
default:
break;
}
}
return ret;
}
NoteBase::NoteBase(const Glib::ustring & filepath, NoteManagerBase & _manager)
: m_manager(_manager)
......@@ -291,7 +320,7 @@ void NoteBase::set_xml_content(const Glib::ustring & xml)
Glib::ustring NoteBase::text_content()
{
return "";
return parse_text_content(xml_content());
}
void NoteBase::load_foreign_note_xml(const Glib::ustring & foreignNoteXml, ChangeType changeType)
......
......@@ -193,6 +193,7 @@ public:
static Glib::ustring url_from_path(const Glib::ustring &);
static std::vector<Glib::ustring> parse_tags(const xmlNodePtr tagnodes);
static Glib::ustring parse_text_content(const Glib::ustring & content);
NoteBase(const Glib::ustring & filepath, NoteManagerBase & manager);
......
/*
* gnote
*
* Copyright (C) 2017,2019 Aurimas Cernius
* Copyright (C) 2017,2019-2020 Aurimas Cernius
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -40,5 +40,33 @@ SUITE(Note)
xmlFreeDoc(doc);
}
}
TEST(parse_text_content_simple)
{
Glib::ustring content = "<note-content><note-title>note_title</note-title>\n\ntext content</note-content>";
auto text = gnote::NoteBase::parse_text_content(std::move(content));
CHECK_EQUAL("note_title\n\ntext content", text);
}
TEST(parse_text_content_whitespace)
{
Glib::ustring content = "<note-content><note-title>note_title</note-title>\n\n </note-content>";
auto text = gnote::NoteBase::parse_text_content(std::move(content));
CHECK_EQUAL("note_title\n\n ", text);
}
TEST(parse_text_content_tags)
{
Glib::ustring content = "<note-content><note-title>note_title</note-title>\n\ntext <b>cont</b>ent</note-content>";
auto text = gnote::NoteBase::parse_text_content(std::move(content));
CHECK_EQUAL("note_title\n\ntext content", text);
}
TEST(parse_text_content_list)
{
Glib::ustring content = "<note-content><note-title>note_title</note-title>\n\ntext content:<list><list-item>item1</list-item><list-item>item2</list-item></list></note-content>";
auto text = gnote::NoteBase::parse_text_content(std::move(content));
CHECK_EQUAL("note_title\n\ntext content:\nitem1\nitem2", text);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment