예제 #1
0
 def _match(self, search_terms, content, operator):
     content = content.lower()
     content = Tools.strReplace(content, self.REPL_MAP)
     word_list = content.split(' ')
     word_list = [Tools.chop(w, '#') for w in word_list]
     search_terms = [s.lower().encode('unicode_escape') for s in search_terms]
     match = False
     matches = list()
     for st in search_terms:
         search_str = st.replace('*', str())
         # search if search term contains a whitespace
         if ' ' in st:
             regexp = re.compile(r'({0})'.format(st), re.I)
             match = True if len(re.findall(regexp, content)) > 0 else False
         # search if wildcard search in the end
         elif st.endswith('*'):
             match_list = [x for x in word_list if x.startswith(search_str)]
             match = True if len(match_list) > 0 else False
         # search if wildcard search in front
         elif st.startswith('*'):
             match_list = [x for x in word_list if x.endswith(search_str)]
             match = True if len(match_list) > 0 else False
         # search if exact match is true
         elif self.exact_match:
             match = True if search_str in word_list else False
         # search with exact match is false
         else:
             match = True if search_str in str(word_list) else False
         matches.append(match)
     match = all(matches) if operator == 'AND' else any(matches)
     return match
예제 #2
0
    def _create_evernote_note(self, notebook, filename):
        # Create the new note
        note = Types.Note()
        # Chop extension from filename and use it as note title
        note.title = Tools.chop(os.path.basename(filename), self.ext)
        note.notebookGuid = notebook.guid
        note.content = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
        note.content += '<en-note>'
        md_content = self._get_md_content(filename)
        # Get Tag List from YAML Fonter
        tag_list = self._get_tag_list(md_content)
        # Remove YAML Fronter tags
        md_content = self._remove_yaml_fronter(md_content)
        # Read image links from MD
        file_list_in_md = self._get_images_in_md(md_content)

        # TODO: move to method
        file_hash_dict = dict()
        res = list()
        for f in file_list_in_md:
            file_path = os.path.join(self.notes_path, self._url_decode(f))
            with open(file_path, 'rb') as the_file:
                image = the_file.read()
            md5 = hashlib.md5()
            md5.update(image)
            the_hash = md5.digest()

            data = Types.Data()
            data.size = len(image)
            data.bodyHash = the_hash
            data.body = image

            resource = Types.Resource()
            resource.mime = mimetypes.guess_type(file_path)[0]
            resource.data = data
            # Now, add the new Resource to the note's list of resources
            res.append(resource)
            hash_hex = binascii.hexlify(the_hash)
            file_hash_dict.update({f: hash_hex})

        # Replace MD Link with ENML Link
        note.resources = res
        # Add Tag list from YAML
        note.tagNames = tag_list
        for md_link, hash_hex in file_hash_dict.items():
            en_link = '<en-media type="image/png" hash="' + hash_hex + '"/>'
            md_content = self._exhange_image_links(
                md_content, md_link, en_link)

        enml = markdown2.markdown(md_content).encode('utf-8')
        note.content += self.remove_invalid_urls(enml)
        note.content += '</en-note>'
        return note
예제 #3
0
from Alfred import Tools as Tools

# create MD search object
md_search = MyNotes.Search()

# Load environment variables
ext = md_search.getNotesExtension()
# p = md_search.getNotesPath()
query = Tools.getArgv(1)
todos = md_search.todoSearch(query)

wf = Items()
if len(todos) > 0:
    for i in todos:
        md_path = urllib.pathname2url(i['path'])
        md_title = i['title'] if i['title'] != str() else Tools.chop(
            i['filename'], ext)
        wf.setItem(title=i['todo'],
                   subtitle=u'\u2192 {0} (Created: {1})'.format(
                       md_title.decode('utf-8'), Tools.getDateStr(i['ctime'])),
                   arg=i['path'],
                   valid=True,
                   type='file')
        wf.setIcon('icons/unchecked.png', 'image')
        # Mod for CMD - new action menu
        wf.addMod(key="cmd",
                  arg="{0}>{1}".format(i['path'], query),
                  subtitle="Actions..",
                  icon_path="icons/action.png",
                  icon_type="image")
        # TODO: REmove
        # wf.addModsToItem()
# Load environment variables
ext = md_search.getNotesExtension()
p = md_search.getNotesPath()
query = Tools.getArgv(1)
# TODO: For testing, delete 3 lines
#reload(sys)
#sys.setdefaultencoding('utf-8')
#query = "Hädcount".encode('utf-8')
todos = md_search.todoSearch(query)

wf = Items()
if len(todos) > 0:
    for i in todos:
        md_path = urllib.pathname2url(i['path'])
        md_title = i['title'] if i['title'] != str(
        ) else Tools.chop(i['filename'], ext)
        wf.setItem(
            title=i['todo'],
            subtitle=u'\u2192 {0} (Created: {1})'.format(
                md_title.decode('utf-8'), Tools.getDateStr(i['ctime'])),
            arg=i['path'],
            valid=True,
            type='file'
        )
        wf.setIcon('icons/unchecked.png', 'image')
        # Mod for CMD - new action menu
        wf.addMod(
            key="cmd",
            arg="{0}>{1}".format(i['path'], query),
            subtitle="Actions..",
            icon_path="icons/action.png",