def checklistInSoupToENML(soup):
'''
Transforms github style checklists `* [ ]` in the BeautifulSoup tree to
enml.
'''
checktodo_re = re.compile(r'\[(.)\]')
# To be more github compatible, if in a list all elements begins with `[ ]``
# transform it to normal `[ ]` evernote elements
for ul in soup.find_all('ul'):
tasks = []; istodo = True
for li in ul.find_all('li'):
task = soup.new_tag('div')
todo_tag = soup.new_tag('en-todo')
reg = checktodo_re.match(li.get_text())
istodo = istodo and reg
character = reg.group(1) if reg else None
if character == "x": todo_tag['checked']="true"
task.append(todo_tag)
if reg: task.append(NavigableString(li.get_text()[3:].strip()))
tasks.append(task)
if istodo:
for task in tasks: ul.insert_after(task)
ul.extract()
# For the rest of elements just replace `[ ]` with the appropriate element
for todo in soup.find_all(text=checktodo_re):
str_re = re.match(r'(.*)\[(.)\](.*)',todo)
pre = str_re.group(1)
post = str_re.group(3)
todo_tag = soup.new_tag('en-todo')
if str_re.group(2) == "x": todo_tag['checked']="true"
todo.replace_with(todo_tag)
todo_tag.insert_before(pre)
todo_tag.insert_after(post)
评论列表
文章目录