2021-12-10 10:31:34 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import argparse
|
|
|
|
|
2021-12-16 15:43:18 +00:00
|
|
|
# Parse Options
|
2021-12-10 10:31:34 +00:00
|
|
|
parser = argparse.ArgumentParser(description='Get Options.')
|
|
|
|
parser.add_argument('-i', type=argparse.FileType("r"),\
|
|
|
|
dest='clp', default='./My Clippings.txt',\
|
|
|
|
help='Input Clippings')
|
|
|
|
parser.add_argument('-b', type=str,\
|
|
|
|
dest='book', \
|
2021-12-10 10:52:26 +00:00
|
|
|
help='Booktitle to search for')
|
2021-12-16 15:43:18 +00:00
|
|
|
parser.add_argument('-w', type=int,\
|
|
|
|
dest='width', default='80',\
|
|
|
|
help='Width for long strings')
|
|
|
|
parser.add_argument('--page', type=str,\
|
|
|
|
dest='page', default='Seite',\
|
|
|
|
help='Localisation for term »page«')
|
|
|
|
parser.add_argument('--pos', type=str,\
|
|
|
|
dest='pos', default='Position',\
|
|
|
|
help='Localisation for term »position«')
|
|
|
|
parser.add_argument('--mark', type=str,\
|
|
|
|
dest='mark', default='Markierung',\
|
|
|
|
help='Localisation for term »highlight«')
|
|
|
|
parser.add_argument('--note', type=str,\
|
|
|
|
dest='note', default='Notiz',\
|
|
|
|
help='Localisation for term »note«')
|
|
|
|
parser.add_argument('--head', type=str,\
|
|
|
|
dest='head', default='Anmerkung',\
|
|
|
|
help='Localisation for term »note«')
|
2021-12-10 10:31:34 +00:00
|
|
|
|
|
|
|
args=parser.parse_args()
|
|
|
|
|
|
|
|
iline = args.clp.readlines()
|
|
|
|
|
2021-12-16 15:43:18 +00:00
|
|
|
# def for printing lines with fixed width
|
2021-12-10 10:31:34 +00:00
|
|
|
def printLines(s):
|
|
|
|
pre = " "
|
2021-12-16 15:43:18 +00:00
|
|
|
width = args.width
|
2021-12-10 10:31:34 +00:00
|
|
|
words=s.split()
|
|
|
|
line = ''
|
|
|
|
for word in words:
|
|
|
|
if len(word) > width and len(line) == 0:
|
|
|
|
print(pre + word)
|
|
|
|
else:
|
|
|
|
if len(word) + len(line) < width:
|
|
|
|
if len(line) == 0:
|
|
|
|
line = pre + word
|
|
|
|
else:
|
|
|
|
line = line + " " + word
|
|
|
|
else:
|
|
|
|
print(line)
|
|
|
|
line = pre + word
|
|
|
|
if len(line) > 0:
|
|
|
|
print(line)
|
|
|
|
|
|
|
|
|
|
|
|
newEntry = 0
|
|
|
|
foundBook = False
|
|
|
|
foundBookPast = False
|
2021-12-10 10:52:26 +00:00
|
|
|
bookOld = ""
|
2021-12-16 15:43:18 +00:00
|
|
|
# read every line in the clipfile
|
2021-12-10 10:31:34 +00:00
|
|
|
for line in iline:
|
2021-12-16 15:43:18 +00:00
|
|
|
# search for new entry to begin with ==…
|
|
|
|
# if you find it, search for the right booktitle or just print it rightaway
|
|
|
|
# if you are in an entry, print the position/page and so on
|
2021-12-10 10:31:34 +00:00
|
|
|
if line.find("==========") == -1:
|
|
|
|
if newEntry == 1:
|
|
|
|
foundBook = False
|
|
|
|
book = line
|
2021-12-10 10:52:26 +00:00
|
|
|
if args.book:
|
|
|
|
if book.find(args.book) != -1:
|
|
|
|
foundBook = True
|
|
|
|
if not foundBookPast:
|
|
|
|
print("# " + line)
|
2021-12-16 15:43:18 +00:00
|
|
|
print("## " + args.head)
|
2021-12-10 10:52:26 +00:00
|
|
|
foundBookPast = True
|
|
|
|
else:
|
|
|
|
if bookOld != line:
|
|
|
|
foundBook = True
|
2021-12-10 10:31:34 +00:00
|
|
|
print("# " + line)
|
2021-12-16 15:43:18 +00:00
|
|
|
print("## " + args.head)
|
2021-12-10 10:52:26 +00:00
|
|
|
bookOld = line
|
2021-12-10 10:31:34 +00:00
|
|
|
newEntry = newEntry + 1
|
|
|
|
elif foundBook:
|
|
|
|
if newEntry == 2:
|
2021-12-16 15:43:18 +00:00
|
|
|
if line.find(args.page) != -1:
|
|
|
|
splt = args.page + ' '
|
2021-12-10 10:52:26 +00:00
|
|
|
else:
|
2021-12-16 15:43:18 +00:00
|
|
|
splt = args.pos + ' '
|
2021-12-10 10:52:26 +00:00
|
|
|
page = line.split(splt)[1].split('|', 1)[0].strip()
|
2021-12-10 10:31:34 +00:00
|
|
|
if page.find('-') != -1:
|
|
|
|
if page.split('-')[0] == page.split('-')[1]:
|
|
|
|
page = page.split('-')[0]
|
2021-12-16 15:43:18 +00:00
|
|
|
if line.find(args.mark) != -1:
|
2021-12-10 10:52:26 +00:00
|
|
|
print("- " + splt + page + ":")
|
2021-12-16 15:43:18 +00:00
|
|
|
if line.find(args.note) != -1:
|
|
|
|
print("- " + args.page + " " + page + " (*" \
|
|
|
|
+ args.note + "*):")
|
2021-12-10 10:31:34 +00:00
|
|
|
elif newEntry > 2:
|
|
|
|
if line.strip() != "":
|
|
|
|
printLines(line)
|
|
|
|
newEntry = newEntry + 1
|
|
|
|
else:
|
|
|
|
newEntry = 1
|