Exemplo n.º 1
0
class EvervimEditor(object):
    """ editing buffertext """
    _instance = None

    def __init__(self):
        if EvervimEditor._instance is not None:
            raise RuntimeError("EvervimPref must be one object!!!")
        self.api = None

    @classmethod
    def getInstance(self):
        if EvervimEditor._instance is None:
            EvervimEditor._instance = EvervimEditor()

        return EvervimEditor._instance

    def setAPI(self):
        pref = EvervimPref.getInstance()
        if EvervimPref.getInstance().devtoken is None:
            raise AttributeError("devtoken must be set!!")

        self.api = EvernoteAPI(pref.devtoken)

    def note2buffer(self, note):
        """ return strings array for buffer from note. """
        """ note has attribute title, tagNames, content, notebookName """
        bufStrings = []
        pref = EvervimPref.getInstance()
        doc = minidom.parseString(note.content)
        ennote = doc.getElementsByTagName("en-note")[0]

        if pref.usemarkdown == '0':
            bufStrings.append(note.title)
            bufStrings.append("Tags:" + ",".join(note.tagNames))
            bufStrings.append('Notebook:' + note.notebookName)
            contentxml = ennote.toprettyxml(indent=pref.xmlindent, encoding='utf-8')
            contentxml = re.sub('^' + pref.xmlindent, '', contentxml, flags=re.MULTILINE)
            bufStrings.extend([line for line in contentxml.splitlines()[1:-1] if line.strip()])
        else:
            bufStrings.append('# ' + note.title)
            bufStrings.append("Tags:" + ",".join(note.tagNames))
            bufStrings.append('Notebook:' + note.notebookName)
            content = markdownAndENML.parseENML(ennote).encode('utf-8')
            bufStrings.extend(content.splitlines())
        return bufStrings

    def buffer2note(self, note, buflines):
        """ return note that set title, tags, content, notebook from buftext """
        pref = EvervimPref.getInstance()
        if pref.usemarkdown == '0':
            note.title = buflines[0]
            note = self.api.editTag(note, buflines[1].replace('Tags:', ''))
            note = self.api.editNotebook(note, buflines[2].replace('Notebook:', ''))
            note.content  = EvernoteAPI.NOTECONTENT_HEADER + "\n".join(buflines[3:]) + EvernoteAPI.NOTECONTENT_FOOTER
        else:
            note.title = re.sub(r'^#', '',buflines[0]).strip()
            note = self.api.editTag(note, buflines[1].replace('Tags:', ''))
            note = self.api.editNotebook(note, buflines[2].replace('Notebook:', ''))
            parsedContent = markdownAndENML.parseMarkdown("\n".join(buflines[3:]))
            note.content  = EvernoteAPI.NOTECONTENT_HEADER + parsedContent.encode('utf-8') + EvernoteAPI.NOTECONTENT_FOOTER

        return note