def reference_scan(self, md_text): """Scan Markdown document for reference Adapted from <https://github.com/smathot/academicmarkdown> """ data = utils.read_json(self.zotquery.json_data) keys = data.keys() ref_count = 1 zot_items = [] found_cks = [] # Needs to match patter created in QUICK_COPY regexp = re.compile(r'{@([^_]*?)_(\d*?)_([A-Z1-9]{3})}') for reg_obj in re.finditer(regexp, md_text): family, date, key_end = reg_obj.groups() citekey = '{@' + '_'.join([family, date, key_end]) + '}' if key_end in found_cks: continue ref_count += 1 possible_keys = [key for key in keys if key.endswith(key_end)] if len(possible_keys) > 1: for key in possible_keys: item = data.get(key) try: if item['data']['date'] == date: key = key break except KeyError: pass else: key = possible_keys[0] zot_items.append({'key': key, 'citekey': citekey}) found_cks.append(key_end) return zot_items
def open_attachment(self): """Open item's attachment in default app""" if os.path.isfile(self.arg): subprocess.check_output(['open', self.arg]) # if self.input is item key else: data = utils.read_json(self.zotquery.json_data) item_id = self.arg.split('_')[1] item = data.get(item_id, None) if item: for att in item['attachments']: if os.path.exists(att['path']): subprocess.check_output(['open', att['path']])
def search_items(self): """Search individual items. """ # Get JSON data of user's Zotero library data = utils.read_json(self.zotquery.json_data) # Get keys of all items that match specific query keys = self._get_items() for key_rank in keys: # Ignore rank score in (rank, key) key = key_rank[-1] # Get JSON info for that item item = data.get(key, None) if item: # Prepare dictionary for Alfred alfred = self._prepare_item_feedback(item) self.wf.add_item(**alfred)
def search_in_groups(self): """Search for items within selected group. """ # Get name of group stored in cache group = self._get_group_name() # Get keys of all items that match query in `group` keys = self._get_in_group(group) # Get JSON data of user's Zotero library data = utils.read_json(self.zotquery.json_data) for key in keys: # Ignore rank score key = key[-1] # Get JSON info for that item item = data.get(key, None) if item: # Prepare dictionary for Alfred alfred = self._prepare_item_feedback(item) self.wf.add_item(**alfred)