diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2019-06-01 00:07:55 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2019-06-01 00:07:55 +0200 |
commit | 2f20895889f53e7d8191655e4d758abeb1e73e21 (patch) | |
tree | 4b347f409238e385935fa6589c7fb0373dbfdba2 | |
parent | 61904c3386e00e8c213dad4261ffa1450f28bc2b (diff) | |
download | epubgrep-2f20895889f53e7d8191655e4d758abeb1e73e21.tar.gz |
Add multi-line option.
-rwxr-xr-x | epubgrep.py | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/epubgrep.py b/epubgrep.py index f2da727..78728d7 100755 --- a/epubgrep.py +++ b/epubgrep.py @@ -28,25 +28,38 @@ def grep_book(filename: str, pattern: str, flags: int): assert os.path.isfile(filename), "{} is not EPub file.".format(filename) sought_RE = re.compile(pattern, flags) + mline = flags & re.M == re.M + metadata = epub_meta.get_epub_metadata(filename) book = zipfile.ZipFile(filename) printed_booktitle = False for zif in book.infolist(): with book.open(zif) as inf: - printed_title = False - for line in inf: - decoded_line = line.decode(errors='replace').strip() - if sought_RE.search(decoded_line): + if mline: + decoded_str = inf.read().decode(errors='replace') + res = sought_RE.search(decoded_str) + if res: if not printed_booktitle: print('{}'.format(filename)) printed_booktitle = True - if not printed_title: - chap_info = get_chapter_title(metadata.toc, - zif.filename) - print("{}. {}:\n".format(chap_info[1], chap_info[0])) - printed_title = True - print(decoded_line) + chap_info = get_chapter_title(metadata.toc, zif.filename) + print("{}. {}:\n".format(chap_info[1], chap_info[0])) + print('{}\n'.format(res.group(0))) + else: + printed_title = False + for line in inf: + decoded_line = line.decode(errors='replace').strip() + if sought_RE.search(decoded_line): + if not printed_booktitle: + print('{}'.format(filename)) + printed_booktitle = True + if not printed_title: + chap_info = get_chapter_title(metadata.toc, + zif.filename) + print("{}. {}:\n".format(chap_info[1], chap_info[0])) + printed_title = True + print(decoded_line) if __name__ == "__main__": @@ -56,11 +69,18 @@ if __name__ == "__main__": parser.add_argument('-i', '--ignore-case', action='store_true', help="make search case insensitive") + parser.add_argument('-m', '--multi-line', + action='store_true', + help="make search multi line") args = parser.parse_args() + log.debug('args = %s', args) search_flags = 0 if args.ignore_case: search_flags |= re.I + if args.multi_line: + search_flags |= re.M | re.S + book_fname = os.path.realpath(args.filename) grep_book(book_fname, args.pattern, search_flags) |