Skip to content

Commit

Permalink
add function to add TODO item and check/unchek TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
Nriver committed Feb 22, 2022
1 parent 7375816 commit e097b08
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ Simply delete a note by id.
ea.delete_note("note1")
```

# TODO List

## Add TODO item

You can use `add_todo` to add a TODO item, param is the TODO description

```
ea.add_todo("买暖宝宝")
```

## Check/Uncheck a TODO item

param is the index of the TODO item

```
ea.todo_check(0)
ea.todo_uncheck(1)
```

# Develop

Install with pip egg link to make package change without reinstall.
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# For a discussion on single-sourcing the version across setup.py and the
# project code, see
# https://packaging.python.org/guides/single-sourcing-package-version/
version='0.5.3', # Required
version='0.5.5', # Required

# This is a one-line description or tagline of what your project does. This
# corresponds to the "Summary" metadata field:
Expand Down Expand Up @@ -150,7 +150,7 @@
#
# For an analysis of "install_requires" vs pip's requirements files see:
# https://packaging.python.org/discussions/install-requires-vs-requirements/
# install_requires=['peppercorn'], # Optional
install_requires=['BeautifulSoup4'], # Optional

# List additional groups of dependencies here (e.g. development
# dependencies). Users will be able to install these using the "extras"
Expand Down
37 changes: 37 additions & 0 deletions src/trilium_py/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,40 @@ def todo_check(self, todo_index, check=True):
def todo_uncheck(self, todo_index):
"""uncheck a todo"""
return self.todo_check(todo_index, check=False)

def add_todo(self, todo_description):
"""append item to todo list"""
try:
content = self.get_today_note_content()
print(content)
soup = BeautifulSoup(content, 'html.parser')
todo_labels = soup.find_all("label", {"class": "todo-list__label"})
# append todo item after last todo item
# special case 1: no todo available, add it to the beginning of document
# special case 2: if last todo item is empty, update it

todo_item_html = f'''<li><label class="todo-list__label"><input type="checkbox"/><span class="todo-list__label__description">{todo_description}</span></label></li>'''

if not todo_labels:
todo_item_html = f'''<p>TODO:</p><ul class="todo-list">{todo_item_html}</ul>'''
todo_item = BeautifulSoup(todo_item_html, 'html.parser')
soup.insert(0, todo_item)
else:
last_todo = todo_labels[-1]
if not last_todo.text.strip():
target_span = last_todo.find_next("span", {"class": "todo-list__label__description"})
target_span.string = todo_description
else:
todo_item = BeautifulSoup(todo_item_html, 'html.parser')
last_todo.append(todo_item)

new_content = str(soup)
# free mem
soup.decompose()
del soup

return self.set_today_note_content(new_content)

except Exception as e:
print(e)
return False

0 comments on commit e097b08

Please sign in to comment.