Example #1
0
    def start(self):
        self.backup_html = self.html

        # definition lists have some quirks
        has_def_list = False
        if "<dl>" in self.html:
            has_def_list = True
            self.create_correct_md_for_def_list()
            self.html = self.note.fields[self.current_field]

        # first, we reverse engineer the Markdown from the rendered card
        clean_md = utility.strip_html_from_markdown(self.html)

        if has_def_list:
            clean_md = utility.remove_leading_whitespace_from_dd_element(
                clean_md)
        clean_md = utility.remove_whitespace_before_abbreviation_definition(
            clean_md)
        clean_md_escaped = utility.escape_html_chars(clean_md)

        if not clean_md:
            return

        # HTML --> Markdown
        if self.has_data and self.isconverted == "True":
            # check if the stored data and the current text differ from each other
            compare_md = self.convert_markdown_to_html(self.md)
            # handle quirks
            compare_md = utility.put_colons_in_html_def_list(compare_md)
            compare_md = utility.strip_html_from_markdown(compare_md)
            if has_def_list:
                compare_md = utility.remove_leading_whitespace_from_dd_element(
                    compare_md)
            compare_md = utility.remove_whitespace_before_abbreviation_definition(
                compare_md)

            # escape HTML if we haven't done so already
            if not any(x in compare_md
                       for x in ("&amp;", "&quot;", "&apos;", "&gt;", "&lt;")):
                compare_md = utility.escape_html_chars(compare_md)
            if utility.is_same_markdown(clean_md_escaped,
                                        compare_md) or self.p.get(
                                            const.MARKDOWN_ALWAYS_REVERT):
                self.revert_to_stored_markdown()
            else:
                self.handle_conflict()

        # Markdown --> HTML
        else:
            new_html = self.convert_markdown_to_html(clean_md)
            # needed for proper display of images
            if "<img" in new_html:
                new_html = utility.unescape_html(new_html)
            html_with_data = utility.insert_md_data(self.note_id_field, "True",
                                                    clean_md_escaped, new_html)
            self.insert_into_field(html_with_data, self.current_field)

            # resolve quirks
            self.align_elements()
Example #2
0
 def apply_markdown(self):
     self.cancel_html = self.html
     has_def_list = False
     if "<dl>" in self.html:
         has_def_list = True
         self.create_correct_md_for_def_list()
         self.html = self.note.fields[self.current_field]
     clean_md = utility.convert_html_to_markdown(self.html)
     if has_def_list:
         clean_md = utility.remove_leading_whitespace_from_dd_element(
             clean_md)
     clean_md = utility.remove_whitespace_before_abbreviation_definition(
         clean_md)
     clean_md_escaped = utility.escape_html_chars(clean_md)
     if not clean_md:
         return
     # check for changed Markdown between the stored data and the current text
     if self.has_data and self.isconverted == "True":
         compare_md = utility.convert_markdown_to_html(self.md)
         compare_md = utility.put_colons_in_html_def_list(compare_md)
         compare_md = utility.convert_html_to_markdown(compare_md)
         if has_def_list:
             compare_md = utility.remove_leading_whitespace_from_dd_element(
                 compare_md)
         compare_md = utility.remove_whitespace_before_abbreviation_definition(
             compare_md)
         if not any(x in compare_md
                    for x in ("&amp;", "&quot;", "&apos;", "&gt;", "&lt;")):
             compare_md_escaped = utility.escape_html_chars(compare_md)
             compare_md = compare_md_escaped
         if (utility.is_same_markdown(clean_md_escaped, compare_md)
                 or self.p.get(const.MARKDOWN_ALWAYS_REVERT)):
             self.revert_to_stored_markdown()
         else:
             self.handle_conflict()
     else:
         # make abbreviations behave correctly
         new_html = utility.convert_markdown_to_html(clean_md)
         # needed for proper display of images
         if "<img" in new_html:
             new_html = utility.unescape_html(new_html)
         html_with_data = utility.make_data_ready_to_insert(
             self.current_note_id_and_field, "True", clean_md_escaped,
             new_html)
         self.insert_markup_in_field(html_with_data,
                                     self.editor_instance.currentField)
         self.align_elements()
         const.MARKDOWN_PREFS["disable_buttons"] = True
         self.warn_about_changes(self.editor_instance, self.current_field,
                                 const.MARKDOWN_BG_COLOR)
Example #3
0
 def apply_markdown(self):
     self.cancel_html = self.html
     has_def_list = False
     if "<dl>" in self.html:
         has_def_list = True
         self.create_correct_md_for_def_list()
         self.html = self.note.fields[self.current_field]
     clean_md = utility.convert_html_to_markdown(self.html)
     if has_def_list:
         clean_md = utility.remove_leading_whitespace_from_dd_element(clean_md)
     clean_md = utility.remove_whitespace_before_abbreviation_definition(
             clean_md)
     clean_md_escaped = utility.escape_html_chars(clean_md)
     if not clean_md:
         return
     # check for changed Markdown between the stored data and the current text
     if (self.has_data and self.isconverted == "True"):
         compare_md = utility.convert_markdown_to_html(self.md)
         compare_md = utility.put_colons_in_html_def_list(compare_md)
         compare_md = utility.convert_html_to_markdown(compare_md)
         if has_def_list:
             compare_md = utility.remove_leading_whitespace_from_dd_element(compare_md)
         compare_md = utility.remove_whitespace_before_abbreviation_definition(
                 compare_md)
         if not any(x in compare_md for x in("&amp;", "&quot;", "&apos;",
                                             "&gt;", "&lt;")):
             compare_md_escaped = utility.escape_html_chars(compare_md)
             compare_md = compare_md_escaped
         if (utility.is_same_markdown(clean_md_escaped, compare_md) or
                preferences.PREFS.get(const.MARKDOWN_ALWAYS_REVERT)):
             self.revert_to_stored_markdown()
         else:
             self.handle_conflict()
     else:
         # make abbreviations behave correctly
         new_html = utility.convert_markdown_to_html(clean_md)
         # needed for proper display of images
         if "<img" in new_html:
             new_html = utility.unescape_html(new_html)
         html_with_data = utility.make_data_ready_to_insert(
                 self.current_note_id_and_field, "True",
                 clean_md_escaped, new_html)
         self.insert_markup_in_field(
                 html_with_data, self.editor_instance.currentField)
         self.align_elements()
         const.MARKDOWN_PREFS["disable_buttons"] = True
         self.warn_about_changes(self.editor_instance,
                                 self.current_field,
                                 const.MARKDOWN_BG_COLOR)
Example #4
0
 def overwrite_stored_data(self):
     """
     Create new Markdown from the current HTML.
     """
     clean_md = utility.strip_html_from_markdown(self.html, keep_empty_lines=True)
     clean_md = utility.remove_whitespace_before_abbreviation_definition(clean_md)
     if "<dl" in self.html:
         clean_md = utility.remove_leading_whitespace_from_dd_element(clean_md, add_newline=True)
     if re.search(const.IS_LINK_OR_IMG_REGEX, clean_md):
         clean_md = utility.escape_html_chars(clean_md)
     new_html = utility.convert_clean_md_to_html(clean_md, put_breaks=True)
     self.insert_into_field(new_html, self.current_field)
     self.remove_warn_msg(self.editor, self.current_field)
Example #5
0
 def overwrite_stored_data(self):
     """
     Create new Markdown from the current HTML.
     """
     clean_md = utility.convert_html_to_markdown(self.html,
                                                 keep_empty_lines=True)
     clean_md = utility.remove_whitespace_before_abbreviation_definition(
         clean_md)
     if "<dl" in self.html:
         clean_md = utility.remove_leading_whitespace_from_dd_element(
             clean_md, add_newline=True)
     if re.search(const.IS_LINK_OR_IMG_REGEX, clean_md):
         clean_md = utility.escape_html_chars(clean_md)
     new_html = utility.convert_clean_md_to_html(clean_md, put_breaks=True)
     self.insert_markup_in_field(new_html, self.current_field)
     const.MARKDOWN_PREFS["disable_buttons"] = False
     const.MARKDOWN_PREFS["isconverted"] = False
     self.remove_warn_msg(self.editor_instance, self.current_field)
Example #6
0
 def overwrite_stored_data(self):
     """
     Create new Markdown from the current HTML.
     """
     clean_md = utility.convert_html_to_markdown(
             self.html, keep_empty_lines=True)
     clean_md = utility.remove_whitespace_before_abbreviation_definition(
             clean_md)
     if "<dl" in self.html:
         clean_md = utility.remove_leading_whitespace_from_dd_element(
                 clean_md, add_newline=True)
     if re.search(const.IS_LINK_OR_IMG_REGEX, clean_md):
         clean_md = utility.escape_html_chars(clean_md)
     new_html = utility.convert_clean_md_to_html(clean_md,
                                                 put_breaks=True)
     self.insert_markup_in_field(new_html, self.current_field)
     const.MARKDOWN_PREFS["disable_buttons"] = False
     const.MARKDOWN_PREFS["isconverted"] = False
     self.remove_warn_msg(self.editor_instance, self.current_field)