That's the badger!I thought about the text selection process, realised that I could probably replicate the generated HTML for a given text string, tokenising and formatting the text string again in isolation using the pygments library. Then, it is simply a matter of finding and modifying the selected region in the broader HTML document.
I have attached the given example code all marked up.

There is another wrinkle that I thought of too late: The bit of program you are trying to match is likely to contain special characters, which would need to be escaped in the search pattern. This potentially would spoil the readability of the markup when the whole file is viewed as plain text. I'm not sure how big a deal this might be in practice.
You probably won't even have to worry about that, as long as it can be separated cleanly by some external process.I haven't supported the embedded commentary so far, however.
Here is an actual Makefile I stuck together to demonstrate the principle. (NB: Make sure the indentation in the Makefile is done with tabs, not spaces!):
Code:
all: basic docsbasic: prog.basprog.bas: whole_filerm -f prog.basawk '!/^ *\*[|]/{print}' whole_file > prog.basdocs: doc.srcrm -f doc.html doc.pdfcat doc.src > doc.html # cat should be command to HTML-ify!cat doc.html > doc.pdf # cat should be command to PDF-ify!doc.src: whole_filerm -f doc.srcsed -n '/^ *\*[|]/s/^.. *//p' whole_file > doc.srcclean:rm -rf prog.bas doc.src doc.html doc.pdf.PHONY: all basic docs clean
The syntax is
target: dependency dependency dependency .....
*TAB* instructions to build target
*TAB* from all dependencies
A target is usually a file you are trying to create (but you can have "phony" targets, that don't actually create a same-named file); and a dependency can be either a file that must exist, or another makefile target that must be satisfied.
"all" is the first target, so `make` on its own will be taken as `make all`; and it depends on "basic" and "docs". There is no file in the folder called "basic" (and the .PHONY declaration at the end says even if there is a file with that name, ignore it. Otherwise, if a file existed called "basic", it could spoil the build process) so it tries to build the target "basic", which depends on "prog.bas". Now it tries to build "prog.bas", which depends on "whole_file". If there is no "prog.bas", or if "whole_file" was changed more recently than "prog.bas" (meaning prog.bas is now out of date), it follows the TAB-indented instructions. (Note the use of `rm -f`; this is to avoid an error if the file to be removed does not exist.)
Once the "basic" target is satisfied, it carries on with the target "docs". This depends on "doc.src"; which also doesn't exist the first time, so again it follows the TAB-indented instructions to build "doc.src". Then "docs" has its own set of instructions, which would HTML-ify doc.src and then PDF-ify doc.html (except here, I'm just copying the files verbatim, because I don't know exactly what you are doing with them).
The "clean" target wipes out all the files that have been created by the build process, so restoring everything to a known state.
.PHONY is a special target, and its "dependencies" are just the other targets whose names do not correspond to any real filename that gets generated during the build process. This way, if you ever did create a file called "all", "basic", "docs" or "clean", `make` would know to ignore it; otherwise, if a new enough file existed with one of those names, the corresponding target would never get built.
Statistics: Posted by julie_m — Sat Jul 13, 2024 11:31 am