Ejemplo n.º 1
0
    def create_note(self):
        # - open editor
        editor = select_editor(self.config)

        try:
            content = editor.open(
                fn_tmp=self.config.editor.fn_tempfile, init_content=note_template
            )
        except UserCancelledException:
            sys.exit(1)

        # No changes in content, nothing to add. Exit the program normally.
        if content == note_template:
            sys.exit(0)

        # - remove template string
        regex = re.compile(re.escape(note_template.strip()))
        content = regex.sub('', content).strip()

        # - parse title
        title = parse_title(content, self.config)

        # - auto parsing tags from note content
        tags, content = parse_tags(content, self.config)

        # - remove leading and trailing spaces again
        content = content.strip()

        try:
            if content == '':
                msg = 'Empty content detected, do you want to save this note?'
                if not query_yes_no(msg, default='no', back_n_lines=1):
                    raise KeyboardInterrupt
        except KeyboardInterrupt:
            print()
            sys.exit(1)

        continue_editing_tags = False
        try:
            print('Tags: %s' % str(tags))
            continue_editing_tags = query_yes_no(
                'Continue editing tags?', default='yes', back_n_lines=1
            )
        except KeyboardInterrupt:
            print()
            sys.exit(1)

        if not continue_editing_tags:
            return Note.create(title, content, tags)

        # - let user edit tags
        try:
            raw_tags = editor.open(
                fn_tmp=self.config.editor.fn_tempfile, init_content=str(tags)
            )
        except UserCancelledException:
            sys.exit(1)

        tags = Tags.from_string_content(raw_tags)
        return Note.create(title, content, tags)
Ejemplo n.º 2
0
def select_editor(config, editor_name=None):
    if editor_name is None:
        editor_name = config.editor.executable

    try:
        editor = get_editor(editor_name)
    except (EditorNotFoundException, EditorNotSupportedException) as ex:
        # fallback
        msg = (
            '%s '
            'You can set your favorite one in the config file or use the '
            'default editor and continue editing.\n' % str(ex)
        )
        print(msg)
        try:
            use_default_editor = query_yes_no(
                'Continue editing?', default='yes', back_n_lines=2
            )
        except KeyboardInterrupt:
            print()
            sys.exit(1)

        if not use_default_editor:
            sys.exit(2)     # no available editors and execution is aborted

        # Create interface `open`
        def wrapper(func):
            func.open = func.__call__
            return func
        editor = wrapper(open_default_editor)

    return editor
Ejemplo n.º 3
0
    def edit_note(self, note, editor_name=None):
        from hashlib import md5 as func_md5

        editor = select_editor(self.config, editor_name)

        old_utime = note.update_time
        old_content = note.content.to_format(str)
        hash_old = func_md5(old_content.encode()).hexdigest()

        fn_tmp = self.config.editor.fn_tempfile
        init_content = old_content + '\n'*2 + '^^^%s^^^' % str(note.tags)
        new_content = editor.open(fn_tmp=fn_tmp, init_content=init_content)

        # - parse title
        title = parse_title(new_content, self.config)

        # - auto parsing tags from note content
        tags, new_content = parse_tags(new_content, self.config)

        # - remove leading and trailing spaces again
        new_content = new_content.strip()

        try:
            if new_content == '':
                msg = 'Empty content detected, do you want to save this note?'
                if not query_yes_no(msg, default='no', back_n_lines=1):
                    raise KeyboardInterrupt
        except KeyboardInterrupt:
            print()
            sys.exit(1)

        continue_editing_tags = False
        tags_changed = set(tags) != set(note.tags)
        if tags_changed:
            try:
                msg_tags = str(tags) if len(tags) > 0 else '(no tag)'
                print('Current tags: %s' % msg_tags)
                msg = (
                    'Tags are modified, do you want to edit them? '
                    '(press ctrl+c to abort changes)'
                )
                continue_editing_tags = query_yes_no(msg, default='yes', back_n_lines=1)
            except KeyboardInterrupt:
                print()
                pass

        if continue_editing_tags:
            try:
                raw_tags = editor.open(fn_tmp=fn_tmp, init_content=str(tags))
            except UserCancelledException:
                sys.exit(1)
            try:
                tags = Tags.from_string_content(raw_tags)
                tags_changed = set(tags) != set(note.tags)
            except:
                print('Failed to parse new tags, fallback to orignal ones.')

        hash_new = func_md5(new_content.encode()).hexdigest()
        if hash_old == hash_new and not tags_changed:
            msg = 'Content and tags were not changed.'
            raise SafeExitException(msg)

        note.update_content(new_content)
        note.tags = tags
        return note
Ejemplo n.º 4
0
 def confirm_to_remove_tags(self, tags):
     msg_tags = '\n'.join([str(v) for v in tags])
     msg = (
         '%s\nAre you sure you want to remove those tags listed above?' % msg_tags
     )
     return query_yes_no(msg, default='no', back_n_lines=1)
Ejemplo n.º 5
0
 def confirm_to_clear(self, nb_name):
     msg = (
         'Are you sure you want to delete all notes in this notebook "%s"?' % nb_name
     )
     return query_yes_no(msg, default='no', back_n_lines=1)