feat: add support for show-code and conditional code #44
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
show-source is implemented as described in #42
Support for conditional code (#43) uses C-inspired directives
#if,#elif(else-if),#elseand#endifSyntax:
#if variable operator valuelt,le,ne,eq,gt,ge(Operator names borrowed from Fortran)x), two-digit (x.y) or three-digit (x.y.z) version numberImplementation: The
cleanupCodealgorithm was improved in order to preprocess the sources before feeding them to prism.js. The implementation uses two stacks: a stack of guards (guards) and a stack of directives (thestack). Guards are three-valued:istrue,isfalseorwastrue.When an
#ifdirective is found, it's immediately pushed onto the directives stack.#elifand#elsedirectives replace the top of the directives stack, but only if we can transition into them (in order to avoid malformed blocks such asif-else-else); otherwise the top of stack is replaced witherror, which can only be popped.#endifpops the stack.When an
#ifdirective is found, the condition is evaluated and the result is pushed ontoguards.#elifand#elsedirectives replace the top of the guards stack with the evaluation result (#elseevaluates toistrue), but only if the top of the guards stackisfalse; otherwise they replace the top of stack withwastrue.#endifpops the guards stack.Directives are always removed from the rendered sources. Other lines (which are not directives) are removed if any guard in the stack
isfalseorwastrue.