")
if ((not any([ol_lvl,ul_lvl,table_lvl])) and (ol_diff > 0 or ul_diff > 0 or table_diff > 0 or headerpat.match(book_lines[line_no]) or book_lines[line_no][:3]=="" or book_lines[line_no][:3]=="
")):
prefix_start.append(line_no)
else:
prefix_start.append(prefix_start[-1])
ol_lvl += ol_diff
ul_lvl += ul_diff
table_lvl += table_diff
if l[:4]=="":
if l[-5:]!="
":
warn("expecting entire line bracketed in ..
")
if args.numbered_chapters:
num = l.split()[1]
if num[-1] == ":":
num = num[:-1]
chap_no = int(num)
else:
chap_no += 1
sec_no = 0
ssec_no = 0
sssec_no = 0
ssssec_no = 0
this_location=f"Chapter {chap_no}"
locations.append(this_location)
log_and_print(f"found location {this_location}")
continue
if l[:4]=="":
if l[-5:]!="
":
warn("expecting entire line bracketed in ..
")
if chap_no == 0:
nesting_warnings += 1
warn("Section without enclosing chapter")
sec_no += 1
ssec_no = 0
sssec_no = 0
ssssec_no = 0
this_location = f"{chap_no}.{sec_no}"
locations.append(this_location)
log_and_print(f"found location {this_location}")
continue
if l[:4]=="":
if l[-5:]!="
":
warn("expecting entire line bracketed in ..
")
if sec_no == 0:
nesting_warnings += 1
if chap_no == 0:
warn("Subsection without enclosing chapter or section")
else:
warn("Subsection without enclosing section")
ssec_no += 1
sssec_no = 0
ssssec_no = 0
this_location = f"{chap_no}.{sec_no}.{ssec_no}"
locations.append(this_location)
log_and_print(f"found location {this_location}")
continue
if l[:4]=="":
if l[-5:]!="
":
warn("expecting entire line bracketed in ..
")
if ssec_no ==0:
nesting_warnings += 1
if sec_no == 0:
if chap_no == 0:
warn("Subsubsection without enclosing chapter, section, or subsection")
else:
warn("Subsubsection without enclosing section or subsection")
else:
warn("Subsubsection without enclosing subsection")
sssec_no += 1
ssssec_no = 0
this_location = f"{chap_no}.{sec_no}.{ssec_no}.{sssec_no}"
locations.append(this_location)
log_and_print(f"found location {this_location}")
continue
if l[:4]=="":
if l[-5:]!="
":
warn("expecting entire line bracketed in ..
")
if sssec_no == 0:
nesting_warnings += 1
if ssec_no == 0:
if sec_no == 0:
if chap_no == 0:
warn("Subsubsubsection without enclosing chapter, section, subsection, or subsubsection")
else:
warn("Subsubsubsection without enclosing section, subsection, or subsubsection")
else:
warn("Subsubsubsection without enclosing subsection or subsubsection")
else:
warn("Subsubsubsection without enclosing subsubsection")
ssssec_no += 1
this_location = f"{chap_no}.{sec_no}.{ssec_no}.{sssec_no}.{ssssec_no}"
locations.append(this_location)
log_and_print(f"found location {this_location}")
continue
locations.append(this_location)
if nesting_warnings:
#
# if there were any improper nestings, just quit ... should have used
# OOoutline first and fixed those issues!
#
if nesting_warnings >1:
log_and_print(f"Exiting: there were {nesting_warnings} improperly nested section warnings (use OOoutline to track them down and fix them before running OOfig_finder again, please!)")
if nesting_warnings == 1:
log_and_print(f"Exiting: there was an improperly nested section warning (use OOoutline to track it down and fix it before running OOfig_finder again, please!)")
quit()
#
# load a few variables then loop through the book looking for the caption line
# of each figure number
#
prefix_start.append(len(book_lines))
fig_info = []
fig_info_headers= ["Figure number", "section", "line number in source file", "figure type", "caption", "alt text if image", "image filename", "link URL", "link text", "image description link", "seems OK in B/W"]
fig_info.append(fig_info_headers)
ytlpat=re.compile("https?://(www.)?youtu(be.com|.be)/")
ytpat =re.compile("[yY]ou[tT]ube")
figures = 0
unknowns = 0
imgs = 0
img_descrips = 0
img_descrip_pat = re.compile("image desc", re.IGNORECASE)
multiple_imgs = 0
tables = 0
youtubes = 0
links = 0
if args.allow_subfig_letters:
figname_suffices = ['', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
else:
figname_suffices = ['']
for i in range(1,args.Max_chap_no+1):
for j in range(1,args.max_fig_no+1):
for k in figname_suffices:
figname_pat=re.compile(f"([fF]igure[s]? {i}\.{j}{k}(, [1-9][0-9]?\.[1-9][0-9]?[a-h]?)*((,)? and [1-9][0-9]?\.[1-9][0-9]?[a-h]?)?)[^0-9a-h]")
#
# having chosen a figure number and defined a patern to match, loop
# through the book looking for the corresponding caption line
#
line_no = 0
html_this_fig = []
while True:
line_no += 1
if line_no>=len(book_lines):
break
l = book_lines[line_no]
last = book_lines[line_no-1]
prefix = line_no-1
prefix_end = line_no-1
m=figname_pat.match(l)
if m:
#
# we've found the caption line!
#
urls=[]
log_and_print(f"found something for {m.group(1)}")
figures += 1
html_this_fig.append(last.replace('
tag
#
if ytlpat.search(l):
#
# this line has a YouTube link, so the previous line's
# was probably a thumbnail of the video
#
for atagl in atagsl:
ytlm=ytlpat.match(atagl.get("href"))
if ytlm:
url=atagl.get("href")
if not (url in urls):
urls.append(url)
fig_info.append([m.group(1), locations[line_no], line_no, "YT", l[3:-4], "", "", url, str(atagl.string), ""])
youtubes += 1
else:
if ytlpat.search(last) or ytpat.search(last):
#
# or maybe the previous line was just a link to YouTube
#
if atagslast:
for ataglast in atagslast:
ytlm=ytlpat.match(ataglast.get("href"))
if ytlm:
url=ataglast.get("href")
if not (url in urls):
urls.append(url)
fig_info.append([m.group(1), locations[line_no], line_no, "YT", l[3:-4], "", "", url, str(ataglast.string), ""])
youtubes += 1
else:
if last[:8]=="
tags
#
imslast=souplast.find_all("img")
if len(imslast)>1:
multiple_imgs += 1
for imlast in imslast:
ids = ''
for atagl in atagsl:
link_text=atagl.get_text()
if img_descrip_pat.search(link_text):
if ids:
ids += ", "
ids += atagl.get("href")
img_descrips += 1
if not ids and line_no":
#
# or maybe the figure was just a table
#
tables += 1
fig_info.append([m.group(1), locations[line_no], line_no, "table", l[3:-4], "", "", "", "", ""])
elif ytlpat.match(last):
if not (last in urls):
urls.append(last)
fig_info.append([m.group(1), locations[line_no], line_no, "YT", l[3:-4], "", "", last,"", ""])
youtubes += 1
else:
#
# unrecognized figure type
#
fig_info.append([m.group(1), locations[line_no], line_no, "unknown", l[3:-4], "", "", "", "", ""])
unknowns += 1
break
break
if html_this_fig:
#
# if we did find something, we wil have it in this variable
#
html_out.write('
\n
\n')
if args.context:
html_out.writelines(prefixl.replace('
=len(book_lines):
break
if prefix_start[ln] == lnp:
html_out.write(book_lines[ln].replace('
\n
\n')
html_out.write('Please note that the material on this page is not by Jonathan Poritz but is instead by various Open Oregon Educational Resources authors, and those authors (or their employers) are the rightsholders and have chosen the copyright status of their work. Excerpts are posted here with the permission of Open Oregon Educaitonal Resources and are only for OOER internal use.
\n')
html_out.write(f"{when_work} at {time.strftime('%H:%M:%S')}, did {what_work}
\n")
html_out.write(f'''among {figures} figures, found
- {imgs} images
-
among which {multiple_imgs} multiple imags
-
{img_descrips} image description file links
-
{youtubes} YT references
-
{tables} tables
-
{links} links
-
{unknowns} unknowns
''')
html_out.write("\n\n")
with open(out_fn_base+".csv","w",newline="") as csv_out_fh:
csv_writer = csv.writer(csv_out_fh)
csv_writer.writerows(fig_info)
log_and_print(f'''among {figures} figures, found
{imgs} images
among which {multiple_imgs} multiple images
{img_descrips} image description file links
{youtubes} YT references
{tables} tables
{links} links
{unknowns} unknowns''')
log_and_print("Done (on "+time.strftime('%d/%m/%Y')+")!")
args.logfile.write("------------------------------------\n")
args.logfile.close()