Try this.
Spoiler:
Code:
import re
from sigil_bs4 import BeautifulSoup
def run(bk):
PageNumbers = r'(\d+)(?=</a>)'
for (id, href) in bk.text_iter():
print('Processing:', href)
html = bk.readfile(id)
soup = BeautifulSoup(html, 'html.parser')
modified = False
for p in soup.find_all('p', class_='inp'):
i = 0 # HERE RESET
def IncrementalNumbers(m):
nonlocal i
i += 1
return str(i)
new_html = re.sub(PageNumbers, IncrementalNumbers, str(p), count=0)
if new_html != str(p):
modified = True
p.replace_with(BeautifulSoup(new_html, 'html.parser'))
if modified:
print("Modified File -->", id)
bk.writefile(id, str(soup))
return 0
def main():
print("I reached main when I should not have\n")
return -1
if __name__ == "__main__":
sys.exit(main())
Look at line with "RESET HERE". Here we reset the counter and each paragraph is counted separately.