New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase performance of reorder-endnotes #116

Merged
merged 3 commits into from Oct 1, 2018
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+6 −1
Diff settings

Always

Just for now

Prev

Stop looking for more endnotes if we’ve made some changes and run out

The original loop is naïve and will keep on looking for endnotes in a situation where there definitely won’t be any (assuming that the endnote structure is correct). If we’ve already found one or more endnotes and made changes then if we fail to find the next in the loop we can assume that the rest don’t exist. Against current Pepys and incrementing all endnotes by one this commit drops the total time from 31.5s to 14.5s(!).
  • Loading branch information...
robinwhittleton committed Sep 30, 2018
commit 4bed0e65400e680f536ef600b3a87958e3d705f1
Copy path View file
@@ -79,13 +79,18 @@ def process_endnotes_in_file(filename: str, root: str, note_range: range, step:
with open(os.path.join(root, filename), "r+", encoding="utf-8") as file:
xhtml = file.read()
processed_xhtml = xhtml
processed_xhtml_is_modified = False

for endnote_number in note_range:
# If we’ve already changed some notes and can’t find the next then we don’t need to continue searching
if not "id=\"noteref-{}\"".format(endnote_number) in processed_xhtml and processed_xhtml_is_modified:
break
processed_xhtml = regex.sub(r"(<a[^>]*?>){}</a>".format(endnote_number), r"\g<1>{}</a>".format(endnote_number + step), processed_xhtml, flags=regex.DOTALL)
processed_xhtml = processed_xhtml.replace("id=\"noteref-{}\"".format(endnote_number), "id=\"noteref-{}\"".format(endnote_number + step), 1)
processed_xhtml = processed_xhtml.replace("#note-{}\"".format(endnote_number), "#note-{}\"".format(endnote_number + step), 1)
processed_xhtml_is_modified = processed_xhtml_is_modified or (processed_xhtml != xhtml)

if processed_xhtml != xhtml:
if processed_xhtml_is_modified:
file.seek(0)
file.write(processed_xhtml)
file.truncate()
ProTip! Use n and p to navigate between commits in a pull request.