Auto highlighting/formatting on code blocks in posts

Just a quick, but very handy tip

When using code in your posts. Lots of people are using the default code block. Which is great.

Clicking the code button / 4 spaces before code example

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${openhab.logdir:-logs}/openhab.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <fileNamePattern>${openhab.logdir:-logs}/openhab.%i.log</fileNamePattern>
        <minIndex>1</minIndex>
        <maxIndex>3</maxIndex>
    </rollingPolicy>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <maxFileSize>250KB</maxFileSize>
    </triggeringPolicy>
    <encoder>
        <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender>

The four spaces before the code is handy but can break if you then edit the code in the post.

Using three backticks at the start and end of the code

You can achieve the same thing by wrapping the code with three backticks (on my GB mac that is the key to left of the Z key)

This also means you don’t have to have spaces before every line :+1:

```
<code>goes here</code>
```

Though it is all a bit gloomy.

:cloud: :cloud: :cloud: :cloud: :cloud:

Using three ticks and the name of the code language

If you add the code language after the first three backticks then you get colour highlighting as well.

XML

```xml
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    <maxFileSize>250KB</maxFileSize>
</triggeringPolicy>
<encoder>
    <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
```

becomes

<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    <maxFileSize>250KB</maxFileSize>
</triggeringPolicy>
<encoder>
    <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>

slighlty less gloomy

:cloud: :cloud: :sunny: :cloud: :cloud:


Java

```java
rule goes here
```
import org.joda.time.*
import org.openhab.core.types.*
import org.openhab.core.library.items.*
import org.openhab.core.items.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.eclipse.xtext.xbase.lib.*

var Timer ReloadTimer = null
var Integer timeOut = 200

var boolean reloadOnce = true
rule "Refresh rules after persistance service started"
when
    System started
then


if(reloadOnce) {
    ReloadTimer = createTimer( now.plusSeconds(timeOut) )
    [
    executeCommandLine("./configurations/rules_refresh.sh")
    ReloadTimer=null
    ]

} else {
    println("reloadOnce is false")
    reloadOnce = false
}
end

The secret sauce : highlight.js

The forum software uses the popular highlight.js for this and currently the following are configured to work and highlight correctly(ish).

  • apache
  • bash
  • cs
  • cpp
  • css
  • coffeescript
  • diff
  • xml
  • http
  • ini
  • json
  • java
  • javascript
  • makefile
  • markdown
  • nginx
  • objectivec
  • ruby
  • perl
  • php
  • python
  • sql
  • handlebars

Enjoy

9 Likes