[gnote] Implement text_content in NoteBase



commit bf07cedeb81fec02a76e1521495c18667cdc7a94
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sat Jul 11 19:08:14 2020 +0300

    Implement text_content in NoteBase

 src/notebase.cpp             | 31 ++++++++++++++++++++++++++++++-
 src/notebase.hpp             |  1 +
 src/test/unit/noteutests.cpp | 30 +++++++++++++++++++++++++++++-
 3 files changed, 60 insertions(+), 2 deletions(-)
---
diff --git a/src/notebase.cpp b/src/notebase.cpp
index 8f5e64de..2672832f 100644
--- a/src/notebase.cpp
+++ b/src/notebase.cpp
@@ -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)
diff --git a/src/notebase.hpp b/src/notebase.hpp
index cb903df5..13432300 100644
--- a/src/notebase.hpp
+++ b/src/notebase.hpp
@@ -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);
 
diff --git a/src/test/unit/noteutests.cpp b/src/test/unit/noteutests.cpp
index f5ca228f..ee6c808d 100644
--- a/src/test/unit/noteutests.cpp
+++ b/src/test/unit/noteutests.cpp
@@ -1,7 +1,7 @@
 /*
  * 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);
+  }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]