Example #1
0
    def _add_note(self, fields, tags, markdown=True, deck=None):
        """Add new note to collection"""
        note = self.col.newNote(forDeck=False)

        if deck is not None:
            note.model()['did'] = self.deck_name_to_id[deck]

        if markdown:
            note.fields = [markdown_to_html(x) for x in fields]
        else:
            note.fields = [plain_to_html(x) for x in fields]

        tags = tags.strip().split()
        for tag in tags:
            note.addTag(tag)

        if not note.dupeOrEmpty():
            self.col.addNote(note)
            self.modified = True
        else:
            click.secho('Dupe detected, note was not added!', fg='red')
            click.echo('Question:')
            click.echo(list(fields)[0])

        return Note(self, note)
Example #2
0
    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)
Example #3
0
    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)
Example #4
0
File: note.py Project: Aylexx/apy
    def toggle_markdown(self, index=None):
        """Toggle markdown on a field"""
        if index is None:
            fields = self.fields
            field = choose(fields, 'Toggle markdown for field:')
            index = fields.index(field)

        field_value = self.n.fields[index]

        if is_generated_html(field_value):
            self.n.fields[index] = html_to_markdown(field_value)
        else:
            self.n.fields[index] = markdown_to_html(field_value)

        self.n.flush()
        self.a.modified = True
Example #5
0
    def toggle_markdown(self, index=None):
        """Toggle markdown on a field"""
        from apy.utilities import choose
        from apy.convert import is_generated_html
        from apy.convert import html_to_markdown
        from apy.convert import markdown_to_html

        if index is None:
            fields = self.fields
            field = choose(fields, 'Toggle markdown for field:')
            index = fields.index(field)

        field_value = self.n.fields[index]

        if is_generated_html(field_value):
            self.n.fields[index] = html_to_markdown(field_value)
        else:
            self.n.fields[index] = markdown_to_html(field_value)

        self.n.flush()
        self.a.modified = True