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),#else
and#endif
Syntax:
#if variable operator value
lt
,le
,ne
,eq
,gt
,ge
(Operator names borrowed from Fortran)x
), two-digit (x.y
) or three-digit (x.y.z
) version numberImplementation: The
cleanupCode
algorithm 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
,isfalse
orwastrue
.When an
#if
directive is found, it's immediately pushed onto the directives stack.#elif
and#else
directives 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.#endif
pops the stack.When an
#if
directive is found, the condition is evaluated and the result is pushed ontoguards
.#elif
and#else
directives replace the top of the guards stack with the evaluation result (#else
evaluates toistrue
), but only if the top of the guards stackisfalse
; otherwise they replace the top of stack withwastrue
.#endif
pops 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
isfalse
orwastrue
.