Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Merge branch 'lczaplinski/bugfix/todos' of https://github.com/scoiata…
Browse files Browse the repository at this point in the history
…el/geeknote into 2.0.17
  • Loading branch information
jeffkowalski committed Apr 29, 2019
2 parents 431a389 + fe3b887 commit 21cbc2b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
29 changes: 4 additions & 25 deletions geeknote/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,11 @@ def checklistInENMLtoSoup(soup):
'''
Transforms Evernote checklist elements to github `* [ ]` task list style
'''
transform_tags = ['p', 'div']
for section in soup.findAll('en-todo', checked='true'):
section.replace_with('<br />* [x]')

# soup.select cant be used with dashes: https://bugs.launchpad.net/beautifulsoup/+bug/1276211
for todo in soup.find_all('en-todo'):
parent = todo.parent
transform = parent.find() == todo and parent.name in transform_tags

checked = todo.attrs.get('checked', None) == "true"
todo.replace_with("[x] " if checked else "[ ] ")

# EN checklist can appear anywhere, but if they appear at the beginning
# of a block element, transform it so it ressembles github markdown syntax
if transform:
content = ''.join(unicode(child) for child in parent.children
if isinstance(child, NavigableString)
).strip()

new_tag = soup.new_tag("li")
new_tag.string = content
parent.replace_with(new_tag)
for section in soup.findAll('en-todo'):
section.replace_with('<br />* [ ]')

@staticmethod
def ENMLtoText(contentENML, format='default', imageOptions={'saveImages': False}, imageFilename=""):
Expand Down Expand Up @@ -133,12 +118,6 @@ def ENMLtoText(contentENML, format='default', imageOptions={'saveImages': False}

Editor.checklistInENMLtoSoup(soup)

for section in soup.findAll('en-todo', checked='true'):
section.replace_with('[x]')

for section in soup.findAll('en-todo'):
section.replace_with('[ ]')

# change <en-media> tags to <img> tags
if 'saveImages' in imageOptions and imageOptions['saveImages']:
for section in soup.findAll('en-media'):
Expand Down
10 changes: 8 additions & 2 deletions tests/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ def test_ENMLToText(self):
self.assertEqual(Editor.ENMLtoText(wrapped), self.MD_TEXT)

def test_TODO(self):
self.assertEqual(Editor.textToENML("- [ ] item 1\n- [x] item 2\n- [ ] item 3"),
Editor.wrapENML("<div><en-todo></en-todo>item 1</div><div><en-todo checked=\"true\"></en-todo>item 2</div><div><en-todo></en-todo>item 3</div>\n"))
MD_TODO = "\n* [ ]item 1\n\n* [x]item 2\n\n* [ ]item 3\n\n"
HTML_TODO = "<div><en-todo></en-todo>item 1</div><div><en-todo checked=\"true\"></en-todo>item 2</div><div><en-todo></en-todo>item 3</div>\n"
self.assertEqual(Editor.textToENML(MD_TODO),
Editor.wrapENML(HTML_TODO))

wrapped = Editor.wrapENML(HTML_TODO)
text = Editor.ENMLtoText(wrapped)
self.assertEqual(text, MD_TODO)

def test_htmlEscape(self):
wrapped = Editor.textToENML(content="<what ever>", format="markdown")
Expand Down

0 comments on commit 21cbc2b

Please sign in to comment.