def edit_model_css(self, model_name): """Edit the CSS part of a given model.""" import tempfile import click from apy.utilities import editor model = self.get_model(model_name) with tempfile.NamedTemporaryFile(mode='w+', prefix='_apy_edit_', suffix='.css', delete=False) as tf: tf.write(model['css']) tf.flush() retcode = editor(tf.name) if retcode != 0: click.echo(f'Editor return with exit code {retcode}!') return with open(tf.name, 'r') as f: new_content = f.read() if model['css'] != new_content: model['css'] = new_content self.col.models.save(model, templates=True) self.modified = True
def add_notes_with_editor(self, tags='', model_name=None, deck_name=None, template=None): """Add new notes to collection with editor""" import tempfile import click from apy.utilities import editor, choose from apy.note import Note if isinstance(template, Note): input_string = template.get_template() else: if model_name is None or model_name.lower() == 'ask': model_name = choose(sorted(self.model_names), "Choose model:") model = self.set_model(model_name) if deck_name is None: deck_name = self.col.decks.current()['name'] elif deck_name.lower() == 'ask': deck_name = choose(sorted(self.deck_names), "Choose deck:") input_string = [f'model: {model_name}'] if self.n_decks > 1: input_string += [f'deck: {deck_name}'] input_string += [f'tags: {tags}'] if model_name != 'Basic': input_string += ['markdown: false'] input_string += ['\n# Note\n'] input_string += [ x for y in [[f'## {field["name"]}', ''] for field in model['flds']] for x in y ] input_string = '\n'.join(input_string) + '\n' with tempfile.NamedTemporaryFile(mode='w+', dir=os.getcwd(), prefix='note_', suffix='.md', delete=False) as tf: tf.write(input_string) tf.flush() retcode = editor(tf.name) if retcode != 0: click.echo(f'Editor return with exit code {retcode}!') return [] return self.add_notes_from_file(tf.name)
def edit(self): """Edit tags and fields of current note""" import os import tempfile import click from apy.utilities import editor from apy.convert import markdown_file_to_notes from apy.convert import markdown_to_html, plain_to_html with tempfile.NamedTemporaryFile(mode='w+', dir=os.getcwd(), prefix='edit_note_', suffix='.md') as tf: tf.write(str(self)) tf.flush() retcode = editor(tf.name) if retcode != 0: click.echo(f'Editor return with exit code {retcode}!') return notes = markdown_file_to_notes(tf.name) if not notes: click.echo(f'Something went wrong when editing note!') return if len(notes) > 1: self.a.add_notes_from_list(notes[1:]) click.confirm( f'\nAdded {len(notes) - 1} new notes while editing.' '\nPress <cr> to continue.', prompt_suffix='', show_default=False) note = notes[0] new_tags = note['tags'].split() if new_tags != self.n.tags: self.n.tags = new_tags for i, value in enumerate(note['fields'].values()): if note['markdown']: self.n.fields[i] = markdown_to_html(value) else: self.n.fields[i] = plain_to_html(value) self.n.flush() self.a.modified = True if self.n.dupeOrEmpty(): click.confirm('The updated note is now a dupe!', prompt_suffix='', show_default=False)
def edit(self): """Edit tags and fields of current note""" with tempfile.NamedTemporaryFile(mode='w+', dir=os.getcwd(), prefix='edit_note_', suffix='.md') as tf: tf.write(str(self)) tf.flush() retcode = editor(tf.name) if retcode != 0: click.echo(f'Editor return with exit code {retcode}!') return notes = markdown_file_to_notes(tf.name) if not notes: click.echo('Something went wrong when editing note!') return if len(notes) > 1: added_notes = self.a.add_notes_from_list(notes[1:]) click.echo(f'\nAdded {len(added_notes)} new notes while editing.') for note in added_notes: cards = note.n.cards() click.echo(f'* nid: {note.n.id} (with {len(cards)} cards)') for card in note.n.cards(): click.echo(f' * cid: {card.id}') click.confirm('\nPress <cr> to continue.', prompt_suffix='', show_default=False) note = notes[0] new_tags = note['tags'].split() if new_tags != self.n.tags: self.n.tags = new_tags new_deck = note.get('deck', None) if new_deck is not None and new_deck != self.get_deck(): self.set_deck(new_deck) for i, value in enumerate(note['fields'].values()): if note['markdown']: self.n.fields[i] = markdown_to_html(value) else: self.n.fields[i] = plain_to_html(value) self.n.flush() self.a.modified = True if self.n.dupeOrEmpty(): click.confirm('The updated note is now a dupe!', prompt_suffix='', show_default=False)