Part 4: Power Coders

Updating enzymes

Converting enzymes from version 2 to version 3 can be tedious but it’s quite easy.

Case 1: version confusion

Nzymes and Enzymes 2 share most of the syntax and it can happen that the former, which always runs before the latter, tries and fails to process an injection that was really meant for the other.

Example.
The injection´{[ .console-output | .pre() ]}´ is supposed to escape for HTML and wrap into a PRE element the value of the ´console-output´ custom field. It’s a valid injection in both versions because an execution enzyme requires parentheses but not arguments. In fact, if there was an argument Nzymes would reject it and Enzymes 2 could process it, because arguments are differently expressed.

Here is what was the value of the ´pre´ custom field in version 2.

return  '<pre style="padding: 20px;">' . htmlspecialchars( $this->pathway ) . '</pre>';

And here is what could be the value of the ´pre´ custom field directly converted from version 2 to version 3.

list( $stuff ) = $arguments;
return  '<pre style="padding: 20px;">' . htmlspecialchars( $stuff ) . '</pre>';

Notice that a hack like that one is necessary for not changing existing injections. If we could change all existing injections, instead of accessing the internal stack directly we could use the ´$arguments´ array as usual in the ´pre´ custom field, together with an injection like ´{[ … | 1.pre(1) ]}´.


Case 2: migration

Here is an example about how to migrate an Enzymes 2 injection which does not use templates. For migrating an Enzymes 2 injection which does, refer to the full blown injection show in another section.

Example.
The injection

´{[ .code-snippet | 1.hilite(=php,ln-1=) ]}´

is supposed to escape for HTML and wrap into a PRE + CODE element the value of the ´code-snippet´ custom field, setting CSS classes so that my code highlighter can do its job from JavaScript.

Here is what was the value of the ´hilite´ custom field (version 2).

$arguments = explode( ',', $this->substrate );
list( $language, $numbers ) = array_pad( $arguments, 2, null );

$pre_class = '';
$numbers   = trim( $numbers );
if ( $numbers ) {
    $pre_class = ' class="' . $numbers . '"';
}

$code_class = '';
$language   = trim( $language );
if ( $language ) {
    $code_class = ' class="' . $language . '"';
}

$code     = htmlspecialchars( $this->pathway );
$template = '<pre%s><code%s>%s</code></pre>';
$result   = sprintf( $template, $pre_class, $code_class, $code );

return $result;

And here is what could be the value of the migrated ´hilite´ custom field (version 3).

list( $code, $language, $numbers ) = array_pad( $arguments, 3, null );

$pre_class = '';
$numbers   = trim( $numbers );
if ( $numbers ) {
    $pre_class = ' class="' . $numbers . '"';
}

$code     = str_replace( '{' . '[', '{' . '-[', $code );  // escape Enzymes 3 injections just in case
$code     = htmlspecialchars( $code );

$code_class = '';
$language   = trim( $language );
if ( $language ) {
    $code_class = ' class="' . $language . ($language == 'ruby' ? ' hljs' : '') . '"';
    $template = '<pre%s><code%s>%s</code></pre>';
    $args = array($template, $pre_class, $code_class, $code);
} else {
    $template = '<pre%s>%s</pre>';
    $args = array($template, $pre_class, $code);
}

$result   = call_user_func_array( 'sprintf', $args );
return $result;

Notice that in this case the version 3 injection must be syntactically different:

´{[ .code-snippet | =php= | =ln-1= | 1.hilite(3)  ]}´.


Nzymes | WordPress Plugin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.