Chili 2.0 Released Today

UPDATE: Chili 2.1 has been released

Changes

  • added support for a much better recipe format
  • optimized regular expressions for speed
  • moved project hosting to Google Code: jquery-chili-js
  • removed support for the previous recipe format
  • removed support for metaobjects
  • removed support for nearly obsolete features

Links

A new recipe format

Chili 2.0 supports a new recipe format which is a bit more structured than the old one and will make hard-to-highlight languages a thing of the past. BTW, old recipes won’t be accepted by Chili 2.0. (although a simple manual conversion is possible)

How to convert old recipes to the new format

First of all let’s see an example of how to convert a recipe in the old format to the new one.

What follows is a piece of recipe (for JavaScript) in the old format

{[ .js-old | .hilite( =javascript= ) ]}

And here is the same piece in the new format

{[ .js-new | .hilite( =javascript= ) ]}

As you see, they are very similar. Here are all the differences:

  1. property names beginning with an underscore are reserved words
  2. _name didn’t exist before
  3. _case replaces the old ignoreCase; _case is TRUE if the language is case sensitive, and FALSE otherwise; _case defaults to FALSE
  4. _main replaces the old steps
  5. _match replaces the old exp
  6. _style didn’t exist before

_style makes a big difference with respect to the past: CSS styles can now be embedded into the recipe. Separate stylesheets are no longer supported by the autoloading engine, but you can load them by yourself, if you prefer to keep them apart.

Another important difference about CSS is that now Chili builds the class associated to a step, by prefixing the _name to the step name, separated by __ (a double underscore). So, for example, the class for the multiline comment will be js__ml_comment.

More expressive power with the new recipe format

Besides the minor changes described in the previous section, the new format is much more powerful thanks to the major improvement I’m going to describe here.

The old recipe format supported an optional replacement property of a step, by means of which you could customize how the highlighting was applied to the matched text. Such a feature was useful when you captured subexpressions and wanted to highlight them separately.

Now _replace replaces replacement (sic) and it’s still optional.

As before, if you don’t specify a _replace property, Chili will default to <span class=”$0″>$$</span>, where $0 and $$ refer to the name of the current step and the matched text respectively.

As before, _replace can also be a different string expression, like in the following step, extracted from the MySQL recipe

{[ .replace-string | .hilite( =javascript= ) ]}

What to note in the above example:

  1. you can specify a style for each of the involved classes, by using properties of an object
  2. you must use a span for applying style to a text run
A big improvement

In Chili 2.0, _replace can also be a function, like in the following step, extracted from the new HTML recipe

{[ .replace-function | .hilite( =javascript= ) ]}

What to note in the above example:

  1. when _replace is a function, it receives match and submatches as arguments, and inside
  2. there is a valid this object which contains a magic x method, by which
  3. a string can be escaped for HTML (like open and close), or
  4. a string can be transformed by an expression (like content)

Chili 2.0 recipes are modular

A Chili 2.0 recipe contains blocks (like _main), which contain steps (like tag_start). An expression can be built for referencing each module, be it a recipe, a block, or a step. For example, /tag_attrs is the tag_attrs block in the current recipe.

One method to highlight them all

The JavaScript code inside a _replace function can use the x method of this.

x takes two arguments: a subject to process, and an optional module to use.

x returns the subject escaped for HTML if no module is given, or the module is not available, else it returns the result of applying the module to the subject using Chili 2.0.

If the ChiliBook option recipeLoading is true, any unavailable module will be automatically loaded.

The new HTML recipe

As an example, here is the new HTML recipe

{[ .html-new | .hilite( =javascript= ) ]}

What to note in the above example:

  1. a _replace function can be used for applying a recipe to a text run inside another recipe, like the script an style steps, where highlighting of script and style elements is delegated to js and css recipes respectively
  2. a _replace function can be used for isolating the parsing of a text run, like the tag_attrs step, where highlighting of name/value pairs happens only in the context of tag attributes
Module paths

A module path is an expression that identifies a Chili 2.0 module. A path has three components (though some can be hidden) separated by a / (forward slash), each with a specific meaning: recipe / block / step. (white space added for clarity)

Here is a list of all the combinations in a module path:

  • recipe
    a module path like css refers to the entire css recipe
  • recipe / block
    a module path like css/definition refers to the definition block of the css recipe
  • recipe / block / step
    a module path like css/definition/property refers to the property step of the definition block of the css recipe
  • / block
    a module path like /definition refers to the definition block of the current recipe
  • / block / step
    a module path like /definition/property refers to the property step of the definition block of the current recipe
  • / / step
    a module path like //property refers to the property step of the current block of the current recipe

As you see, leading slashes have a meaning.

Remember
  • a recipe module invocation tries to match all the steps of the _main block
  • a block module invocation tries to match all its steps
  • a step module invocation tries to match just itself

Help request

I think that Chili 2.0 is pretty good at highlighting, but it needs more fine recipes to succeed. For this release I’ve rewritten some from scratch, and converted some others. I’m not a good programmer in languages other than the ones for which I rewrote a recipe. But if you are and have time and will, then you could write a Chili 2.0 recipe for your favorite language, together with a couple of working samples, and send all to me. I’d be very happy to add your contributed recipes to the project as soon as they are available.

Rewritten
  • CSS
  • HTML
  • JavaScript
  • PHP
Converted
  • C++
  • C#
  • Delphi
  • Java
  • LotusScript
  • MySQL

Setup and Examples

Here is the start page for Chili 2.0 where you’ll find setup instructions and some examples.

How to load JavaScript in WordPress plugins

If you want to load some JavaScript files from your WordPress plugin you have at least two options.

Head hooks

When building up a page, just before the closing tag of the head element, a theme should call actions hooked on wp-head. Those actions only need to output what they want to include into the head element, like script files.

What if a theme does not do that? Well, I’d include it into the so called “known issues”. For example, my last WordPress plugin, won’t work in this case. I think it’s better not to find a workaround, so that theme authors understand that they must follow WordPress standards.

On the other hand, there is also an admin-head hook, which is the wp-head hook counterpart when building up admin pages. For example, you could use the following function.

{[ .load-into-head /chili-web.php ]}

Script API

WordPress provides also a good Script API that will let you do anything you want with script files, following WordPress standards. These are the main functions:

  1. wp_deregister_script
  2. wp_register_script
  3. wp_enqueue_script
  4. wp_print_scripts (action hook)
  5. print_scripts_array (filter hook)

The general idea is that you write a function for loading your scripts using wp_deregister_script, wp_register_script, and wp_enqueue_script, and hook it on wp_print_scripts. If you also need to fine tune the order in which files are loaded or if they have to be loaded at all, then you can write another function and hook it on print_scripts_array.

deregister, register, and enqueue

wp_register_script let’s you create an alias and define dependencies for each script file. If a script file with the same alias was already registered, then your registration is ignored. This is a policy for conflict resolution (the first registration wins) that may help you, if you know it.

wp_deregister_script let’s you remove an alias. If the alias you give doesn’t exist, nothing happens. Tipically you use this function for forcing a new registration of an already registered alias: first you deregister and then register again.

wp_enqueue_script prepares the loading of a script. If the script was registered with some dependencies, this function automatically prepares their loading too, recursively, making sure each script will be loaded only once and before any script depending on it.

For example, jQuery 1.2.3 is registered by WordPress 2.5, but let’s say that you want to always download the latest version. You could use the following function.

{[ .load-with-api /chili-web.php ]}

Keeping jQuery and Prototype compatible is a needed functionality, because WordPress uses both and they both use $ as a global symbol. In fact, the jQuery file packed with WordPress includes the compatibility setting.

Fine tuning

If you want your scripts to be loaded in a particular order, with one script before or after another, maybe with respect to one that was registered by WordPress itself, or by other plugins, then you can write a function for the print_scripts_array filter.

For example, to load the jquery_no_conflict file just after the jquery file, you can use the following function.

{[ .correct-order /chili-web.php ]}

My Own Theme

If you want to see all this in action, you can install the My Own Theme plugin.

How to force jQuery.extend deep recursion

jQuery.extend extends a target object with properties from other objects and it’s widely used in every piece of jQuery code.

Really it’s very useful and simple to undestand and use for flat properties.

{[ .extend-flat | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}

On the contrary, if the involved objects have object properties, jQuery.extend is less intuitive and less useful too.

{[ .extend-object | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}

Luckily, an undocumented feature (deep) makes jQuery.extend recurr object properties.

{[ .extend-deep-std | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}

Unluckily, deep only works for the first level. (not really a ‘bug’)

{[ .extend-deep-bug | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}

If you need deep recursion use jQuery.extend_deep instead.

{[ .extend-deep-fix | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}

Here is the jQuery.extend_deep plugin:

{[ .extend-deep-plugin | @how-to-highlight-code-in-wordpress.hilite( =javascript= ) ]}