def test_get_contents(): note = NOTE_WITH_MACRO.split('\n') content = parse_note.get_contents(note) expected_content = [[ '====================', ':Description of note', '', 'Some content.', '>Move content.', '', '{book:work} {textwidth:40}', '{test} {unit}', '====================' ]] assert content == expected_content
def shift_lines(path_from, name_to_note): # load text, return note, delete, save new from_note = load_text(path_from) replace_note = get_contents(from_note)[0] replace_note = remove_moving_lines(replace_note) replace_text(path_from, path_from, replace_note) # load text, return note, apply new, delete old, save new apply_to_note = load_text(name_to_note) # Get contents will be tricky to refactor apply_to_note = get_contents(apply_to_note)[0] # This is not tested. Need a more robust pattern # - check if a macro exists (macro book tag) and insert above that # if it exists. line_index = len(apply_to_note) - 3 # Insert moved lines into other note. apply_to_note[line_index:line_index] = get_moving_lines(from_note) replace_text(name_to_note, name_to_note, apply_to_note)
def new_component(text): """ Creates the new note data structure. Here is where one would add more note information. :param list[str] note: containing a single note. :return: the dict note element. """ # Text should be a string. # Passing to parse text should return basic components. # This function should then append components not found in note titles = get_title(text) contents = get_contents(text) # This loops through multiple notes note_component = {} for note_title, content in zip(titles, contents): note_title = restrict_title(note_title) title = unique_text(note_title) save_text(title, content) note_component[title] = {'created': datetime.now()} note_component[title]['views'] = 1 note_component[title]['modified'] = datetime.now() note_component[title]['length'] = len(content) description = note_description(content) if description: note_component[title]['description'] = description tags = note_tags(content) if tags: note_component[title]['tags'] = tags book = get_macro('book', content) if book: note_component[title]['book'] = book else: note_component[title]['book'] = 'general' textwidth = get_macro('textwidth', content) if textwidth: set_width = ":set textwidth={}" note_component[title]['vim_cmds'] = [set_width.format(textwidth)] sub_headings = parse_sub_headings(content) if sub_headings: note_component[title]['sub_headings'] = sub_headings note_component[title]['num_sub'] = len(sub_headings) add_component(note_component) return titles, contents
def update_note_hooks(note, stored_data): """ If the name of note has been changed, update the hook in the meta_data (hash key). """ note_edited = load_text(note) new_name = restrict_title(get_title(note_edited)[0]) new_content = get_contents(note_edited)[0] new_name = avoid_conflict(note, new_name, stored_data) # Note name has been changed, update the meta_data hook. if new_name != note: stored_data[new_name] = stored_data[note] stored_data.pop(note, None) replace_text(note, new_name, new_content) return new_name, new_content
def test_note_tags(): note = NOTE_WITH_MACRO.split('\n') content = parse_note.get_contents(note)[0] tags = parse_note.note_tags(content) expected_tags = ['test', 'unit'] assert tags == expected_tags
def migrate_meta(): """ OLD: data = { 'test_note': { 'description': 'description', 'tags': ['tag'], 'timestamp': 'now'} } } NEW: data = { 'test_note': { 'description': 'description', 'tags': ['tag'], 'created': datetime(), 'modified': datetime(), 'views': 1, } } """ time_now = datetime.now() meta_data = load_meta() # Backup data before migrating save_meta(meta_data, backup=True) for key, value in meta_data.items(): text = load_text(key) content = get_contents(text)[0] print('Migrating: {}.'.format(key)) # Adding new meta data # Typo change meta_fields = value.keys() if 'create' in meta_fields and 'created' in meta_fields: value.pop('create', None) elif 'create' in meta_fields and 'created' not in meta_fields: meta_data[key]['created'] = meta_data[key]['create'] value.pop('create', None) elif 'create' not in meta_fields and 'created' not in meta_fields: meta_data[key]['created'] = time_now if 'length' not in meta_fields: meta_data[key]['length'] = len(content) if 'views' not in meta_fields: meta_data[key]['views'] = 1 if 'modified' not in meta_fields: meta_data[key]['modified'] = time_now if 'book' not in meta_fields: meta_data[key]['book'] = 'general' # Removing outdated data if 'timestamp' in meta_fields: value.pop('timestamp', None) if 'updated' in meta_fields: value.pop('updated', None) if 'sub_headings' in meta_fields: # Sub heading indexes given a dummy value on migration. sh = meta_data[key]['sub_headings'] meta_data[key]['sub_headings'] = [(n[0], n[1], 1, 1) for n in sh] meta_data[key]['num_sub'] = len( meta_data[key]['sub_headings'] ) save_meta(meta_data) print('All meta data has been migrated.')