#!/usr/bin/env python3
#
# Copyright (C) 2023 Jonathan A. Poritz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# importing
#
import argparse
import warnings
import fileinput
import sys
import os
import re
import time
if not sys.warnoptions:
warnings.simplefilter("ignore")
#
# setting up arguments
#
parser = argparse.ArgumentParser(description='Makes a report on all tags found in an html file such as one produced by OOprep subject to various selection criteria.')
parser.add_argument("input_file", help="Source html file", type=argparse.FileType('r'))
parser.add_argument("-l", "--logfile", help='Filename for logfile to which will be appended detailed progress information; default is "find_img_tags.log".', default="find_img_tags.log", type=argparse.FileType('a'))
parser.add_argument('-v', '--verbose', help="print on console all information also going in to the logfile", action='store_true')
parser.add_argument('-a', '--all_img_tags', help="find all lines with
tags", action='store_true')
parser.add_argument('-b', '--bad_img_locations', help='find
tags which are not in the standard configuration of a line containing exactly "
Figure X.Y" and caption lines of that format which are not preceded by a line with an tag', action='store_true')
parser.add_argument('-i', '--include_speech_balloons', help='include the normally ignored
tags with alt text that begins "Speech balloons"', action='store_true')
parser.add_argument('-m', '--multiline', help='find lines with
tags which are in sequences of more than one line containting the
tag (Note: we assume "Speech balloon" alt texts never occur in multiline
tag blocks)', action='store_true')
parser.add_argument('-n', '--numbers', help="print out the line numbers of the tags found", action='store_true')
parser.add_argument('-s', '--show_tag_lines', help="print out the line(s) with those desired
tags", action='store_true')
parser.add_argument('-t', '--timestamps', help="print timestamps of actions when reporting in logfile and/or on console", action='store_true')
args = parser.parse_args()
#
# setting up logfile and logging helper
#
args.logfile.write("------------------------------------\n")
def log_and_print(s):
if args.timestamps:
t=time.strftime('%H:%M:%S')+" "+s
else:
t=s
args.logfile.write(t+"\n")
if args.verbose:
print(t)
log_and_print("On "+time.strftime('%d/%m/%Y')+", doing ")
log_and_print(' '.join(sys.argv)+" in directory "+os.getcwd())
#
# get input file contents
#
book_lines = args.input_file.read().split("\n")
#
# set up some patterns we use
#
img_tag_pat = re.compile("
]*)>", re.IGNORECASE)
speech_balloons_pat = re.compile('alt="Speech balloons')
caption_pat = re.compile("
Figure [1-9][0-9]*\.[1-9][0-9]*")
#
# handle print-out of all lines with tags
#
if args.all_img_tags:
lines_with_tags = 0
if args.numbers or args.show_tag_lines:
log_and_print("Here are all lines with
tags:")
line_no = 0
for l in book_lines:
line_no += 1
m = img_tag_pat.search(l)
if m and (args.include_speech_balloons or not speech_balloons_pat.search(m.group(1))):
lines_with_tags += 1
if args.numbers:
if args.show_tag_lines:
log_and_print(f'{line_no}: {l}')
else:
log_and_print(str(line_no))
elif args.show_tag_lines:
log_and_print(l)
if not lines_with_tags:
log_and_print('...\nThere were no lines with an
tag')
elif lines_with_tags == 1:
log_and_print('There was one line with an
tag')
else:
log_and_print(f'There were a total of {lines_with_tags} lines with
tags')
#
# handle print-out of lines with
tags in a bad location
#
if args.bad_img_locations:
lines_with_bl_tags = 0
if args.numbers or args.show_tag_lines:
log_and_print("Here are badly located
tags:")
line_no = 0
for l in book_lines:
line_no += 1
m = img_tag_pat.search(l)
if m and (m.start() != 3 or l[:3]!="
" or m.end() != len(l)-4 or l[m.end():] != "
"): if not args.include_speech_balloons and speech_balloons_pat.search(m.group(1)): continue lines_with_bl_tags += 1 if args.numbers: if args.show_tag_lines: log_and_print(f'{line_no}: {l}') else: log_and_print(str(line_no)) elif args.show_tag_lines: log_and_print(l) if not lines_with_bl_tags: log_and_print('...\nThere were no lines with a badly located