How to use code fences

We all have to type this content over and over and over. Here is a posting we can all point to when the topic comes up.

Why use code fences?

Because rules, sitemap, logs, and configs are all but unreadable when code fences are not used.

When to use code fences?

Whenever one is posting any sort of code, config, log, and any text where line wrapping, indentation, and using an equal spaced font makes the text more legible.

How to use code fences?

Use 3 backticks (```)... Or use <code>... ...to Get
```
code block
some code line 1
some code line 2
```
<code>
code block
some code line 1
some code line 2
</code>


If you are using a bit of code inline in a sentence use single backticks:
This is a sentence with `some code` in it.

becomes:

This is a sentence with some code in it.

To use code fences for a block of code, first create the fences and then paste your code, logs, etc between the fences. To create the fences there are a number of icons at the top of the text entry box for writing a new topic or replying to a topic. Hover over the icon and it will tell you what the icon represents. Below is the icon, what the hover text says, and what it drops into your posting. If you just type in the “what it drops into posting” you do not need to use the icons.

image

    indent preformatted text by 4 spaces

This uses a monospaced font and preserves indentation and line wraps but dose not perform syntax highlighting.

image
openHAB *.items code fences

```csv
your items code goes here
```

code%20fences%20-%20rules
openHAB *.rules code fences

```php
your rules code goes here
```

code%20fences%20-%20sitemaps
openHAB *.sitemap code fences

```php
your sitemap code goes here
```

code%20fences%20-%20javascript
JavaScript code fences

```javascript
your javascript code goes here
```

code%20fences%20-%20generic
generic code fences

```   
your generic code goes here
```

If you are posting some other language that Discourse supports you can try to use that language in your code fences, it often will work.

```python
python code goes here
```
17 Likes

I always use the Preformatted Text </> icon. I’ll paste in code or a log, select it all, and then click the icon. But there is a trick for this and multi-line text… there needs to be a blank line between it and the previous text, or it won’t show up as fenced. So if your using the icon, and things aren’t displaying right in the preview window, put a blank line on top to separate it from the previous text.

2 Likes

Another important feature of code wrapped in a code fence is the elimination of smart quotes. If you are copying and pasting code samples that our outside of a code fence you most likely will pick up smart quotes instead of the expected regular quotes causing errors within OpenHAB.

Code fences are your friends!!

Squid

1 Like

The problem with pasting and then applying the code fences is that the forum will confer all of your quotes and double quotes to smart quotes which will not work in Rules.

Based on my experiments awhile back, code fences only if you lay down the fences before pasting in the code.

Hmmm… this must be OS or browser specific. In looking back at some of my posts, none of the quotes were converted to smart quotes. And I’ve only ever done a paste, select, and </>. I’m using Win10 and Chrome.

1 Like

Hi Rich,

Maybe some screen shot with the icons would help others:
For example:

Screenshot-2018-3-16%20How%20to%20use%20code%20fences

I would be happy to make the others, where should I post them?

Good idea. I’ve made the original posting into a wiki. You can add the screenshots and makes any other changes now.

Done, thank you

I found a bug! :bug:

On MacOS Mojave you can’t see:

*.rules code fences icon
*.sitemap code fences icon
JavaScript code fences icon
generic code fences icon

1 Like

Same on Ubuntu 2 latest Firefox updates

2 Likes

image

(or this was done on purpose by the forum Admins not to confuse users and make it easier for them to use the *.items code fences? … it’s a mystery ! :smiley:)

Hmm. I never even realized there were icons. LOL I always go old school and just manually type the beginning and ending ```

1 Like

Enclose your configs in:

<code>
This is my config
</code>

Note: Additional markdown for whoever is interested: https://commonmark.org/help/

The above will become:

This is my config

1 Like

Maybe that custom plugin does not work anymore …

If you check using F12 in Chrome (dev tools) you will see that some of the svg icons are missing… (I think) so the plugin may need some maintenance. good catch @sihui

We need @ThomDietrich back ! :smiley:

updated first wiki post to show immediately how to use the generic code fences

edit: added the <code> html flavor also on the table that shows up on the first page

1 Like

I need your advice on how to properly include long log in a post.

I have used the “Hide details” option of this editor to avoid having page long post but then the code fence does not seem to have effect on the hidden text.

I have seen some topics using fixed size blocks in which your can scroll horizontally and vertically the code/text but I have no idea how to include such block in this editor

Highlight your text and use the grafik button. Make sure you have an empty line before and after your code:

Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code Here goes your code

Next line …

Thanks but my log has 100 lines and I would like to have a smaller size block (let say 20 lines) in which the reader could scroll vertically through.

Will that happen automatically with the </> feature?

Yes, but it depends on the overall length of your code. You also can combine them with the “Hide details” function: first use the hide details, then the preformatted text:

Summary
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.1272019-11-10 22:29:50.595 [vent.ItemStateChangedEvent] - ESP8266_Five_Lux_Update changed from 0 lx (So, 22:28) to 0 lx (So, 22:29)
2019-11-10 22:29:50.598 [ome.event.ItemCommandEvent] - Item 'ShutterLuxWestDetected_Proxy' received command OFF
2019-11-10 22:29:54.646 [vent.ItemStateChangedEvent] - TemperatureShadow_Update changed from 4,0 °C (So, 22:28) to 4,0 °C (So, 22:29)
2019-11-10 22:29:54.650 [vent.ItemStateChangedEvent] - Shading_tempDiff_Proxy_Update changed from -0,2 °C (So, 22:28) to -0,2 °C (So, 22:29)
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.1272019-11-10 22:29:50.595 [vent.ItemStateChangedEvent] - ESP8266_Five_Lux_Update changed from 0 lx (So, 22:28) to 0 lx (So, 22:29)
2019-11-10 22:29:50.598 [ome.event.ItemCommandEvent] - Item 'ShutterLuxWestDetected_Proxy' received command OFF
2019-11-10 22:29:54.646 [vent.ItemStateChangedEvent] - TemperatureShadow_Update changed from 4,0 °C (So, 22:28) to 4,0 °C (So, 22:29)
2019-11-10 22:29:54.650 [vent.ItemStateChangedEvent] - Shading_tempDiff_Proxy_Update changed from -0,2 °C (So, 22:28) to -0,2 °C (So, 22:29)
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.1272019-11-10 22:29:50.595 [vent.ItemStateChangedEvent] - ESP8266_Five_Lux_Update changed from 0 lx (So, 22:28) to 0 lx (So, 22:29)
2019-11-10 22:29:50.598 [ome.event.ItemCommandEvent] - Item 'ShutterLuxWestDetected_Proxy' received command OFF
2019-11-10 22:29:54.646 [vent.ItemStateChangedEvent] - TemperatureShadow_Update changed from 4,0 °C (So, 22:28) to 4,0 °C (So, 22:29)
2019-11-10 22:29:54.650 [vent.ItemStateChangedEvent] - Shading_tempDiff_Proxy_Update changed from -0,2 °C (So, 22:28) to -0,2 °C (So, 22:29)
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.1272019-11-10 22:29:50.595 [vent.ItemStateChangedEvent] - ESP8266_Five_Lux_Update changed from 0 lx (So, 22:28) to 0 lx (So, 22:29)
2019-11-10 22:29:50.598 [ome.event.ItemCommandEvent] - Item 'ShutterLuxWestDetected_Proxy' received command OFF
2019-11-10 22:29:54.646 [vent.ItemStateChangedEvent] - TemperatureShadow_Update changed from 4,0 °C (So, 22:28) to 4,0 °C (So, 22:29)
2019-11-10 22:29:54.650 [vent.ItemStateChangedEvent] - Shading_tempDiff_Proxy_Update changed from -0,2 °C (So, 22:28) to -0,2 °C (So, 22:29)
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.1272019-11-10 22:29:50.595 [vent.ItemStateChangedEvent] - ESP8266_Five_Lux_Update changed from 0 lx (So, 22:28) to 0 lx (So, 22:29)
2019-11-10 22:29:50.598 [ome.event.ItemCommandEvent] - Item 'ShutterLuxWestDetected_Proxy' received command OFF
2019-11-10 22:29:54.646 [vent.ItemStateChangedEvent] - TemperatureShadow_Update changed from 4,0 °C (So, 22:28) to 4,0 °C (So, 22:29)
2019-11-10 22:29:54.650 [vent.ItemStateChangedEvent] - Shading_tempDiff_Proxy_Update changed from -0,2 °C (So, 22:28) to -0,2 °C (So, 22:29)
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.1272019-11-10 22:29:50.595 [vent.ItemStateChangedEvent] - ESP8266_Five_Lux_Update changed from 0 lx (So, 22:28) to 0 lx (So, 22:29)
2019-11-10 22:29:50.598 [ome.event.ItemCommandEvent] - Item 'ShutterLuxWestDetected_Proxy' received command OFF
2019-11-10 22:29:54.646 [vent.ItemStateChangedEvent] - TemperatureShadow_Update changed from 4,0 °C (So, 22:28) to 4,0 °C (So, 22:29)
2019-11-10 22:29:54.650 [vent.ItemStateChangedEvent] - Shading_tempDiff_Proxy_Update changed from -0,2 °C (So, 22:28) to -0,2 °C (So, 22:29)
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.1272019-11-10 22:29:50.595 [vent.ItemStateChangedEvent] - ESP8266_Five_Lux_Update changed from 0 lx (So, 22:28) to 0 lx (So, 22:29)
2019-11-10 22:29:50.598 [ome.event.ItemCommandEvent] - Item 'ShutterLuxWestDetected_Proxy' received command OFF
2019-11-10 22:29:54.646 [vent.ItemStateChangedEvent] - TemperatureShadow_Update changed from 4,0 °C (So, 22:28) to 4,0 °C (So, 22:29)
2019-11-10 22:29:54.650 [vent.ItemStateChangedEvent] - Shading_tempDiff_Proxy_Update changed from -0,2 °C (So, 22:28) to -0,2 °C (So, 22:29)
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.1272019-11-10 22:29:50.595 [vent.ItemStateChangedEvent] - ESP8266_Five_Lux_Update changed from 0 lx (So, 22:28) to 0 lx (So, 22:29)
2019-11-10 22:29:50.598 [ome.event.ItemCommandEvent] - Item 'ShutterLuxWestDetected_Proxy' received command OFF
2019-11-10 22:29:54.646 [vent.ItemStateChangedEvent] - TemperatureShadow_Update changed from 4,0 °C (So, 22:28) to 4,0 °C (So, 22:29)
2019-11-10 22:29:54.650 [vent.ItemStateChangedEvent] - Shading_tempDiff_Proxy_Update changed from -0,2 °C (So, 22:28) to -0,2 °C (So, 22:29)
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.1272019-11-10 22:29:50.595 [vent.ItemStateChangedEvent] - ESP8266_Five_Lux_Update changed from 0 lx (So, 22:28) to 0 lx (So, 22:29)
2019-11-10 22:29:50.598 [ome.event.ItemCommandEvent] - Item 'ShutterLuxWestDetected_Proxy' received command OFF
2019-11-10 22:29:54.646 [vent.ItemStateChangedEvent] - TemperatureShadow_Update changed from 4,0 °C (So, 22:28) to 4,0 °C (So, 22:29)
2019-11-10 22:29:54.650 [vent.ItemStateChangedEvent] - Shading_tempDiff_Proxy_Update changed from -0,2 °C (So, 22:28) to -0,2 °C (So, 22:29)
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.1272019-11-10 22:29:50.595 [vent.ItemStateChangedEvent] - ESP8266_Five_Lux_Update changed from 0 lx (So, 22:28) to 0 lx (So, 22:29)
2019-11-10 22:29:50.598 [ome.event.ItemCommandEvent] - Item 'ShutterLuxWestDetected_Proxy' received command OFF
2019-11-10 22:29:54.646 [vent.ItemStateChangedEvent] - TemperatureShadow_Update changed from 4,0 °C (So, 22:28) to 4,0 °C (So, 22:29)
2019-11-10 22:29:54.650 [vent.ItemStateChangedEvent] - Shading_tempDiff_Proxy_Update changed from -0,2 °C (So, 22:28) to -0,2 °C (So, 22:29)
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.1272019-11-10 22:29:50.595 [vent.ItemStateChangedEvent] - ESP8266_Five_Lux_Update changed from 0 lx (So, 22:28) to 0 lx (So, 22:29)
2019-11-10 22:29:50.598 [ome.event.ItemCommandEvent] - Item 'ShutterLuxWestDetected_Proxy' received command OFF
2019-11-10 22:29:54.646 [vent.ItemStateChangedEvent] - TemperatureShadow_Update changed from 4,0 °C (So, 22:28) to 4,0 °C (So, 22:29)
2019-11-10 22:29:54.650 [vent.ItemStateChangedEvent] - Shading_tempDiff_Proxy_Update changed from -0,2 °C (So, 22:28) to -0,2 °C (So, 22:29)
2019-11-10 22:29:54.651 [ome.event.ItemCommandEvent] - Item 'ShutterTempDetected_Proxy' received command OFF
2019-11-10 22:29:57.017 [vent.ItemStateChangedEvent] - Barometer changed from 1015.125 to 1015.127
1 Like