From 154cff33478c0508bd1bb316d6784404dd9c40dd Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Mon, 7 Jun 2021 17:08:14 -0700 Subject: [PATCH] Add prev/next button script This isn't currently needed anywhere else, so the list of filenames is just hard-coded into the script. Instead of making substitutions in the source data, this just generates the full data. --- docs/sgtutorial/prevnext.py | 132 ++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100755 docs/sgtutorial/prevnext.py diff --git a/docs/sgtutorial/prevnext.py b/docs/sgtutorial/prevnext.py new file mode 100755 index 0000000..5c7e722 --- /dev/null +++ b/docs/sgtutorial/prevnext.py @@ -0,0 +1,132 @@ +#!/usr/bin/python3 +# +# Sets the contents of the "prevnext" sections, which have the prev/next +# buttons. Run this in the directory where the HTML files live. +# +# (This is currently just for the sgtutorial files; it's not written to +# be a general-purpose utility.) +# + +import filecmp +import os.path +import re +import sys + +# List of files to process, in order. +gFileList = [ + "about-disasm.html", + "using-sourcegen.html", + "moving-around.html", + "simple-edits.html", + "labels-symbols.html", + "editing-data.html", + "generating-code.html", + "digging-deeper.html", + "string-formatting.html", + "local-variables.html", + "inline-data.html", + "odds-ends.html", + "advanced-topics.html", + "address-tables.html", + "extension-scripts.html", + "visualizations.html", +] + +class LocalError(Exception): + """Errors generated internally""" + pass + +# Regex pattern for prevnext section. +findChunk = re.compile( + '^\s*
\s*$.' + '(.*?)' + '^\s*<\/div>', + re.DOTALL | re.MULTILINE) +GROUP_ALL = 0 +GROUP_CHUNK = 1 + + +def editFile(index, outFileName): + """ Edit a file, replacing blocks with the contents of other files. """ + + inFileName = gFileList[index] + try: + with open(inFileName, "r") as inFile: + fileData = inFile.read() + outFile = open(outFileName, "x") + except IOError as err: + raise LocalError(err) + + # Find first (and presumably only) matching chunk. + match = findChunk.search(fileData) + if not match: + print("== No prevnext section found") + return + + replSpan = match.span(GROUP_CHUNK) + + chunk = fileData[replSpan[0] : replSpan[1]] + print("== Matched {0}:{1}".format(replSpan[0], replSpan[1])) + + + # copy everything up to the chunk + outFile.write(fileData[ : replSpan[0]]) + # insert the file, tweaking active ID if appropriate + generatePrevNext(index, outFile) + # copy the rest of the file, including the
+ outFile.write(fileData[match.end(GROUP_CHUNK) : ]) + + print("== done") + outFile.close() + + return + + +def generatePrevNext(index, outFile): + """ Generate prev/next button HTML """ + + # « Previous + # Next » + if index > 0: + outFile.write(' « Previous\n') + + if index + 1 < len(gFileList): + outFile.write(' Next »\n') + + return + + +def main(): + """ main """ + + outFileName = None + + try: + for index in range(len(gFileList)): + name = gFileList[index] + print("Processing #{0}: {1}".format(index, name)) + outFileName = name + "_NEW" + editFile(index, outFileName) + + # See if the file has changed. If it hasn't, keep the original + # so the file dates don't change. + if filecmp.cmp(name, outFileName, False): + print("== No changes, removing new") + os.remove(outFileName) + else: + print("== Changed, keeping new") + os.rename(outFileName, name) + except LocalError as err: + print("ERROR: {0}".format(err)) + if outFileName: + print(" check " + outFileName) + sys.exit(1) + + sys.exit(0) + + +main() # does not return