Skip to content

Commit

Permalink
fix import from gnote/tomboy (#2152, #1991, #1686)
Browse files Browse the repository at this point in the history
  • Loading branch information
giuspen committed Nov 13, 2022
1 parent 6dbe132 commit a69ab5b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 71 deletions.
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ MAKE_APPIMAGE=""
[[ "${MSYSTEM}" =~ "MINGW" ]] && IS_MSYS2_BUILD="Y"
[ -n "${IS_MSYS2_BUILD}" ] && DEFAULT_BUILD_TYPE="Release" || DEFAULT_BUILD_TYPE="Debug"

if [ "${ARG_VAL_LOWER}" == "debug" ]
if [ "${ARG_VAL_LOWER}" == "debug" || "${ARG_VAL_LOWER}" == "dbg" ]
then
CMAKE_BUILD_TYPE="Debug"
elif [ "${ARG_VAL_LOWER}" == "release" ]
elif [ "${ARG_VAL_LOWER}" == "release" || "${ARG_VAL_LOWER}" == "rel" ]
then
CMAKE_BUILD_TYPE="Release"
else
Expand Down
12 changes: 9 additions & 3 deletions src/ct/ct_actions_import.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,14 @@ void CtActions::_import_from_file(CtImporterInterface* importer, const bool dumm
args.filter_mime = importer->file_mime_types();
const std::string filepath = CtDialogs::file_select_dialog(args);
if (filepath.empty()) return;
spdlog::debug("{} {}", __FUNCTION__, filepath);
_pCtConfig->pickDirImport = Glib::path_get_dirname(filepath);

try {
std::unique_ptr<CtImportedNode> node = importer->import_file(filepath);
if (!node) return;
_create_imported_nodes(node.get(), dummy_root);
std::unique_ptr<CtImportedNode> pNode = importer->import_file(filepath);
if (pNode) {
_create_imported_nodes(pNode.get(), dummy_root);
}
}
catch (std::exception& ex) {
spdlog::error("import exception: {}", ex.what());
Expand All @@ -258,6 +260,10 @@ void CtActions::_import_from_dir(CtImporterInterface* importer, const std::strin

void CtActions::_create_imported_nodes(CtImportedNode* imported_nodes, const bool dummy_root)
{
if (not imported_nodes) {
return;
}

// to apply functions to nodes
std::function<void(CtImportedNode*, std::function<void(CtImportedNode*)>)> foreach_nodes;
foreach_nodes = [&](CtImportedNode* imported_node, std::function<void(CtImportedNode*)> fun_apply) {
Expand Down
154 changes: 88 additions & 66 deletions src/ct/ct_imports.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,18 @@ std::unique_ptr<CtImportedNode> CtTomboyImport::import_file(const fs::path& file
tomboy_doc.parse_file(file.string());
}
catch (std::exception& ex) {
spdlog::error("CtTomboyImport: cannot parse xml file ({}): {}", ex.what(), file);
spdlog::error("{}: cannot parse xml file ({}): {}", __FUNCTION__, ex.what(), file.string());
return nullptr;
}

// find note
xmlpp::Node* note_el = tomboy_doc.get_document()->get_root_node()->get_first_child("note");
xmlpp::Node* note_el = tomboy_doc.get_document()->get_root_node();
if (note_el->get_name() != "note") {
note_el->get_first_child("note");
if (not note_el) {
return nullptr;
}
}

// find note name
Glib::ustring node_name = "???";
Expand All @@ -286,30 +292,33 @@ std::unique_ptr<CtImportedNode> CtTomboyImport::import_file(const fs::path& file

// find note's parent
Glib::ustring parent_name;
if (xmlpp::Node* tags_el = note_el->get_first_child("tags"))
if (xmlpp::Node* tags_el = note_el->get_first_child("tags")) {
if (xmlpp::Node* tag_el = tags_el->get_first_child("tag")) {
Glib::ustring tag_name = dynamic_cast<xmlpp::Element*>(tag_el)->get_child_text()->get_content();
if (tag_name.size() > 16 && str::startswith(tag_name, "system:notebook:"))
parent_name = tag_name.substr(16);
}
if (parent_name.empty())
parent_name = "ORPHANS";
}

// parse note's content
if (xmlpp::Node* text_el = note_el->get_first_child("text")) {
if (xmlpp::Node* content_el = text_el->get_first_child("note-content")) {
auto parent_node = std::make_unique<CtImportedNode>(file, parent_name);
auto node = std::make_unique<CtImportedNode>(file, node_name);

auto thisNode = std::make_unique<CtImportedNode>(file, node_name);

_current_node = node->xml_content->create_root_node("root")->add_child("slot");
_current_node = thisNode->xml_content->create_root_node("root")->add_child("slot");
_curr_attributes.clear();
_chars_counter = 0;
_is_list_item = false;
_is_link_to_node = false;
_iterate_tomboy_note(dynamic_cast<xmlpp::Element*>(content_el), node);
_iterate_tomboy_note(dynamic_cast<xmlpp::Element*>(content_el), thisNode);

parent_node->children.emplace_back(std::move(node));
return parent_node;
if (not parent_name.empty()) {
auto parent_node = std::make_unique<CtImportedNode>(file, parent_name);
parent_node->children.emplace_back(std::move(thisNode));
return parent_node;
}
return thisNode;
}
}
return nullptr;
Expand All @@ -318,68 +327,81 @@ std::unique_ptr<CtImportedNode> CtTomboyImport::import_file(const fs::path& file
void CtTomboyImport::_iterate_tomboy_note(xmlpp::Element* iter, std::unique_ptr<CtImportedNode>& node)
{
for (auto dom_iter : iter->get_children()) {
auto dom_iter_el = dynamic_cast<xmlpp::Element*>(dom_iter);
if (dom_iter->get_name() == "#text") {
if (auto dom_iter_el = dynamic_cast<xmlpp::Element*>(dom_iter)) {
if (dom_iter->get_name() == "bold") {
_curr_attributes[CtConst::TAG_WEIGHT] = CtConst::TAG_PROP_VAL_HEAVY;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_WEIGHT] = "";
}
else if (dom_iter->get_name() == CtConst::TAG_PROP_VAL_ITALIC) {
_curr_attributes[CtConst::TAG_STYLE] = CtConst::TAG_PROP_VAL_ITALIC;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_STYLE] = "";
}
else if (dom_iter->get_name() == CtConst::TAG_STRIKETHROUGH) {
_curr_attributes[CtConst::TAG_STRIKETHROUGH] = CtConst::TAG_PROP_VAL_TRUE;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_STRIKETHROUGH] = "";
}
else if (dom_iter->get_name() == "highlight") {
_curr_attributes[CtConst::TAG_BACKGROUND] = CtConst::COLOR_48_YELLOW;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_BACKGROUND] = "";
}
else if (dom_iter->get_name() == CtConst::TAG_PROP_VAL_MONOSPACE) {
_curr_attributes[CtConst::TAG_FAMILY] = dom_iter->get_name();
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_FAMILY] = "";
}
else if (dom_iter->get_name() == "size:small") {
_curr_attributes[CtConst::TAG_SCALE] = CtConst::TAG_PROP_VAL_SMALL;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_SCALE] = "";
}
else if (dom_iter->get_name() == "size:large") {
_curr_attributes[CtConst::TAG_SCALE] = CtConst::TAG_PROP_VAL_H2;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_SCALE] = "";
}
else if (dom_iter->get_name() == "size:huge") {
_curr_attributes[CtConst::TAG_SCALE] = CtConst::TAG_PROP_VAL_H1;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_SCALE] = "";
}
else if (dom_iter->get_name() == "link:url") {
_curr_attributes[CtConst::TAG_LINK] = "webs ";
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_LINK] = "";
}
else if (dom_iter->get_name() == "list-item") {
_is_list_item = true;
_iterate_tomboy_note(dom_iter_el, node);
_is_list_item = false;
}
else if (dom_iter->get_name() == "link:internal") {
_is_link_to_node = true;
_iterate_tomboy_note(dom_iter_el, node);
_is_link_to_node = false;
}
else {
spdlog::debug(dom_iter->get_name());
_iterate_tomboy_note(dom_iter_el, node);
}
}
else {
Glib::ustring text_data = dynamic_cast<xmlpp::TextNode*>(dom_iter)->get_content();
if (_curr_attributes[CtConst::TAG_LINK] == "webs ")
if (_curr_attributes[CtConst::TAG_LINK] == "webs ") {
_curr_attributes[CtConst::TAG_LINK] += text_data;
else if (_is_list_item)
}
else if (_is_list_item) {
text_data = _config->charsListbul[0] + CtConst::CHAR_SPACE + text_data;

}
xmlpp::Element* el = _rich_text_serialize(text_data);
if (_is_link_to_node)
if (_is_link_to_node) {
node->add_broken_link(text_data, el);

}
_chars_counter += text_data.size();
}
else if (dom_iter->get_name() == "bold") {
_curr_attributes[CtConst::TAG_WEIGHT] = CtConst::TAG_PROP_VAL_HEAVY;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_WEIGHT] = "";
} else if (dom_iter->get_name() == CtConst::TAG_PROP_VAL_ITALIC) {
_curr_attributes[CtConst::TAG_STYLE] = CtConst::TAG_PROP_VAL_ITALIC;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_STYLE] = "";
} else if (dom_iter->get_name() == CtConst::TAG_STRIKETHROUGH) {
_curr_attributes[CtConst::TAG_STRIKETHROUGH] = CtConst::TAG_PROP_VAL_TRUE;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_STRIKETHROUGH] = "";
} else if (dom_iter->get_name() == "highlight") {
_curr_attributes[CtConst::TAG_BACKGROUND] = CtConst::COLOR_48_YELLOW;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_BACKGROUND] = "";
} else if (dom_iter->get_name() == CtConst::TAG_PROP_VAL_MONOSPACE) {
_curr_attributes[CtConst::TAG_FAMILY] = dom_iter->get_name();
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_FAMILY] = "";
} else if (dom_iter->get_name() == "size:small") {
_curr_attributes[CtConst::TAG_SCALE] = CtConst::TAG_PROP_VAL_SMALL;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_SCALE] = "";
} else if (dom_iter->get_name() == "size:large") {
_curr_attributes[CtConst::TAG_SCALE] = CtConst::TAG_PROP_VAL_H2;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_SCALE] = "";
} else if (dom_iter->get_name() == "size:huge") {
_curr_attributes[CtConst::TAG_SCALE] = CtConst::TAG_PROP_VAL_H1;
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_SCALE] = "";
} else if (dom_iter->get_name() == "link:url") {
_curr_attributes[CtConst::TAG_LINK] = "webs ";
_iterate_tomboy_note(dom_iter_el, node);
_curr_attributes[CtConst::TAG_LINK] = "";
} else if (dom_iter->get_name() == "list-item") {
_is_list_item = true;
_iterate_tomboy_note(dom_iter_el, node);
_is_list_item = false;
} else if (dom_iter->get_name() == "link:internal") {
_is_link_to_node = true;
_iterate_tomboy_note(dom_iter_el, node);
_is_link_to_node = false;
} else {
spdlog::debug(dom_iter->get_name());
_iterate_tomboy_note(dom_iter_el, node);
}
}
}

Expand Down

0 comments on commit a69ab5b

Please sign in to comment.