Displaying XML in WordPress

Displaying XML in WordPress

a.k.a. the making of ‘CheckStyle – Enforcing a coding style: part 2’

Happy New Year and welcome to 2018!

For my last post I wanted to write a commentary on the sections of CheckStyle’s own comprehensive and annotated rule configuration. It quickly became clear however that at over 580 lines of xml this was going to be something of a challenge to make it easily consumable. I had hoped that this would be a quick post to explain how I managed to do that. Sadly with Christmas, life and family in the way I have struggled to find time to do this and it has taken me over 2 weeks. Oh well, here goes:
Taking just a small section (lines 156-161) for demonstration purposes;

In the Visual Editor

  • Pasting it directly in to the Visual editor:

<module name=”SuppressWarnings”>
<property name=”format” value=”^((?!unchecked|deprecation|rawtypes).)*$”/>
<message key=”suppressed.warning.not.allowed”
value=”The warning ”{0}” cannot be suppressed at this location. Only few javac warnings are allowed to suppress. If try to suppress checkstyle/pmd/….. violation please do this in their config file. If you try to suppress IntelliJ IDEA inspection, please use javadoc block tag @noinspection”
/>
</module>

  • By adding Blockquote and ‘preformatted style’ to separate it from the main body text in the Visual editor:

    (As used for previous code blocks)

<module name="SuppressWarnings">
<property name="format" value="^((?!unchecked|deprecation|rawtypes).)*$"/>
<message key="suppressed.warning.not.allowed"
value="The warning ''{0}'' cannot be suppressed at this location. Only few javac warnings are allowed to suppress. If try to suppress checkstyle/pmd/..... violation please do this in their config file. If you try to suppress IntelliJ IDEA inspection, please use javadoc block tag @noinspection"
/>
</module>

This is ok. At least it maintains long lines and now provides a scrollbar to reach the end of them.

Switching to the Text Editor

  • Using the [code] shortcode in the Text editor:


<module name="SuppressWarnings">
<property name="format" value="^((?!unchecked|deprecation|rawtypes).)*$"/>
<message key="suppressed.warning.not.allowed" value="The warning ''{0}'' cannot be suppressed at this location. Only few javac warnings are allowed to suppress. If try to suppress checkstyle/pmd/..... violation please do this in their config file. If you try to suppress IntelliJ IDEA inspection, please use javadoc block tag @noinspection" />
</module>

This doesn’t help much.

  • Using the [sourcecode language=”xml”] shortcode in the Text editor:


<module name="SuppressWarnings">
<property name="format" value="^((?!unchecked|deprecation|rawtypes).)*$"/>
<message key="suppressed.warning.not.allowed" value="The warning ''{0}'' cannot be suppressed at this location. Only few javac warnings are allowed to suppress. If try to suppress checkstyle/pmd/..... violation please do this in their config file. If you try to suppress IntelliJ IDEA inspection, please use javadoc block tag @noinspection" />
</module>

Aha, now we are getting somewhere. Some proper code formatting and use of colours, although we still have word wrap enabled on the long line.

Time to try some plugins. Apparently the plugin type is called a ‘Syntax Highlighter’ and Alex Gorbatchev’s SyntaxHighlighter is the standard although apparently no longer maintained. I downloaded a slighted modified version Alex Mill’s SyntaxHighlighter Evolved.

  • Using the [xml] shortcode from ‘SyntaxHighlighter Evolved’ in the Text editor:


<module name="SuppressWarnings">
<property name="format" value="^((?!unchecked|deprecation|rawtypes).)*$"/>
<message key="suppressed.warning.not.allowed" value="The warning ''{0}'' cannot be suppressed at this location. Only few javac warnings are allowed to suppress. If try to suppress checkstyle/pmd/..... violation please do this in their config file. If you try to suppress IntelliJ IDEA inspection, please use javadoc block tag @noinspection" />
</module>

It seems that this provides the same results as the longer [sourcecode language=”xml”] shortcode so perhaps the plugin was not required. But it does include some additional options including collapsing.

  • Using the [xml] shortcode from ‘SyntaxHighlighter Evolved’ in the Text editor with collapse function:

This is a help, but there appears to be no way to collapse a section again. Maybe it’s just me, but if I’ve looked and decided that this isn’t the section I’m looking for, I want to collapse it again.

Time for my second plugin: Collapse-O-Matic by twinpictures and badon03.

  • Using the [expand] shortcode from ‘Collapse-O-Matic’ and the [xml] shortcode from ‘SyntaxHighlighter Evolved’ in the Text editor:

Using Collapse-O-Matic and SyntaxHighlighter Evolved
[xml] <module name="SuppressWarnings">
<property name="format" value="^((?!unchecked|deprecation|rawtypes).)*$"/>
<message key="suppressed.warning.not.allowed" value="The warning ”{0}” cannot be suppressed at this location. Only few javac warnings are allowed to suppress. If try to suppress checkstyle/pmd/….. violation please do this in their config file. If you try to suppress IntelliJ IDEA inspection, please use javadoc block tag @noinspection" />
</module>
[/xml]

And I guess that will do. It would be nice if I could turn off the linewrap, but I’m not sure I would anyway. Unfortunately, the downside of this approach (and all of the other Syntax Highlighters I have read about) is that once you have some shortcodes in your page, making changes in the Visual editor can mess everything up. Obviously then backups as you go along become important.

As it is I can’t wait to get back to posting about testing rather than dev work. On the plus side, I now have a page with some transformations that will provide some

Progress made:

  • Documenting how to solve a problem using WordPress plugins.

Lessons learnt:

  • Sometimes life gets in the way of blogging.
  • In future I think I will keep my content around testing. There are plenty of other posting about WordPress.
  • Renew my commitment to post weekly, even if it is a minor update. Perfectionism can get in the way of achieving anything.

Next time: How I should have gone about writing my own rules in the first place; using Eclipse-CS plugin.

Post-publish edit: corrected a couple of typos and added tags.

Comments are closed.