Exemplo n.º 1
0
def highlights(files, options):
    """Print notes from highlighted text.

    All is printed to output or error stream (usually stdout and stderr).

    Options:
    * options.recursive     Handle directories
    * options.use_title     Print filename/document title instead of full path
    * options.valid_types   PDF annotation types to process
    * options.filter_keys   Only print stated keys.
    * options.remove_key    Don't print key tags
    * options.with_path     Print the file path with each line
    * options.with_page     Print the page number with each line
    * options.buffered      Buffer output
    * options.list_keys     Print key only
    * options.stdout        Output stream
    * options.stderr        Error stream

    """
    for path in files:
        if os.path.isdir(path):
            if options.recursive:
                highlights([os.path.join(path, p) for p in os.listdir(path)],
                           options)
            continue  # Omit directories

        document = Pdf(path, options, pgm=sys.argv[0])

        if options.list_keys:
            options.remove_key = False
            for item in document.annotations(options):
                list_keys(item.note, options)
        else:
            for item in document.annotations(options):
                print_note(item.note, item.page, options)
Exemplo n.º 2
0
def anedit(path, target, options):
    """Edit notes from highlights in PDF files.

    All is printed to standard input or standard error.

    Options:
    * options.valid_types   PDF annotation types to process
    * options.use_title     Print filename/document title instead of filename
    * options.filter_keys   Only print stated keys.
    * options.remove_key    Don't print key tags
    * options.verbose       Print varnings

    """
    # FIXME: Who guarantees this method is only executed on valid files?
    # Also check for pusher, hillieo, hilliep, ...

    # wordlist for normalization
    wordlist = Dictionary()

    # open document
    document = Pdf(path, options, pgm=sys.argv[0])

    # fetch notes
    notes = []
    for n_annot, item in enumerate(document.annotations(options)):
        sugg = annotation_fixes(item.note, wordlist, options.verbose)
        notes.append((item, sugg))

    # Walk through notes
    has_changes = False
    while len(notes) > 0:
        item, sugg = notes.pop(0)

        print ""
        print "\033[94m> {}: page {}, ETA {}\033[0m".format(
            item.page[0], item.page[1], len(notes))
        print "Original: ", item.note
        if item.note != sugg:
            print "Suggested:", sugg
        elif options.diffs:
            continue

        valid_answers = 'nyecisq?'
        prompt = '[{}]: '.format('/'.join(valid_answers.title()))
        ans = 'NEIN'
        while ans not in valid_answers:
            ans = raw_input(prompt) \
                .strip() \
                .lower() \
                .replace('yes', 'y') \
                .replace('no', 'n') \
                .replace('quit', 'q') \
                .replace('ignore', 'i') \
                .replace('ign', 'i') \
                .replace('edit', 'e') \
                .replace('correct', 'c') \
                .replace('skip', 's')

            if ans == '':
                ans = valid_answers[0]  # default is 'n'

            if ans == '?':
                ans = 'NEIN'
                print '''Usage:

    n   no      Stick with the original text (the default)
    y   yes     Accept the suggested text
    e   edit    Edit the original text
    c   change  Edit the suggested text
    i   ignore  Ignore for now (again prompted later)
    s   skip    Save and exit
    q   quit    Abort and exit (changes are lost)

                '''

        if ans == 'y':  # Use suggestion
            has_changes = True
            if item.key is None:
                item.set_content(sugg)
            else:
                item.set_content('<{}>{}</{}>'.format(item.key, sugg,
                                                      item.key))
        elif ans == 'n':  # Use original
            pass
        elif ans in ('e', 'c'):  # Edit manually

            def hook():
                curr = ans == 'e' and item.note or sugg
                curr = curr.replace('\n', '\\n')
                readline.insert_text(curr)
                readline.redisplay()

            readline.set_pre_input_hook(hook)
            sugg = raw_input().strip().replace('\\n', '\n')
            readline.set_pre_input_hook(None)
            notes.insert(0, (item, sugg))
        elif ans == 'i':  # Ignore note for now
            notes.append((item, sugg))
        elif ans == 'q':  # Quit immediately, don't save
            return
        elif ans == 's':  # Skip the rest
            break

    # save changes
    if has_changes:
        document.save(target, options)