gpoore / codebraid

Live code in Pandoc Markdown
BSD 3-Clause "New" or "Revised" License
367 stars 13 forks source link

Make StopIteration Errors more informative #38

Closed amichuda closed 3 years ago

amichuda commented 3 years ago

Oftentimes when running codebraid, I get a StopIteration error and it is difficult to tell where this error comes from. Is there a way to make the StopIteration error more informative, like giving the snippet of text that causes the issue or the line number?

gpoore commented 3 years ago

The StopIteration errors happen when codebraid fails to sync code with a corresponding line number in the document. Pandoc's AST doesn't contain any information about source document locations, so as codebraid goes through the Pandoc AST looking for code to execute, it also performs searches to determine the current location in the document. Whenever it finds code or text in the AST, it does a search from the last location reached in the document to find that code or text. The issue is that sometimes Pandoc modifies code or text in one way or another while creating the AST, so the search can fail and result in StopIteration.

The real solution for this will be a better, more robust search algorithm. I had hoped that the fix for #36 would handle all remaining issues, but apparently that's not the case. A new algorithm should probably give up when text isn't found within a couple of lines. I believe the searches for code should all succeed at this point, since HTML is skipped to avoid modifications made by Pandoc. There should also be a fallback to using something like ?? for source document line numbers in the event that things still fail.

gpoore commented 3 years ago

This entire category of errors from syncing code to source line numbers should be permanently eliminated by the last commit. Sorry for the long delay.

amichuda commented 3 years ago

No worries, this is great!