Refactor item compilation rules

Refactor the rules that are evaluated when items are compiled. Less code is
duplicated, and the code should now be more amenible to changes such as adding a
code syntax highlighting filter.