editor.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:txt2evernote 作者: Xunius 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号