How to effectively identify code block scripting language in the community forum

I’m using OH for about two years now and learning a lot of things on the road while adding more and more devices and functionality to the system. However I’m still scratching my head when looking for scripting examples in specific questions. My eyes are not trained to find out just by looking at the code which one is used in the actual code block, and many times that is not provided in the post containing it.
I’ve tried to look the for it in the page source, but no success.
It would be a great help if the type of code would be displayed for the code block as it is on the official documentation pages.

1 Like

I do fully understand this request, however
(A) this has to be done by each Individual when posting code. Nowadays I try to label my questions on code with the used language ( I didn’t do that when there was virtually only DSL for rules🤔).
(B) there were times when rules were written in DSL only, so nobody cared about indicating the language. Nobody will change ALL the old posts.

Thanks for your reply, good point on old posts, that is history.
I’m just trying to find a way to navigate easier through this huge amount of knowledge here, and in a convenient way as much as possible.

In case a new post is added, the editor has several types of code fences, which could be used to indicate this, assuming of course that the users select the appropriate one :wink:
I’m wondering if that could be displayed in the code block - similar to the official documentation - which could be extended later with more types if needed.

As openhab evolves, new scripting languages are added, and it gets more and more difficult to navigate through these different kind of codes posted by the community.

I try to make it very clear when I post examples which language is being used.

The code fences types probably won’t help much because there isn’t actually a type for Rules DSL. If you click on the icon for rules, it will actually use “php” as the language type, and Rules DSL has nothing to do with PHP. But the structure of the language is close enough I guess that it does some reasonable syntax highlighting. Or the PHP highlighter was modified to work with Rules DSL.

I can provide a few rules of thumb which is about the best that can be offered right now.

Rules DSL

  • If no language is mentioned you can usually assume it’s Rules DSL.

  • If you see keywords then and end

  • If you see calls to openHAB stuff without something in front of it it is Rules DSL. For example logInfo("Foo", "bar") is Rules DSL, in other languages it will be something like Log.logInfo("Foo", "bar"). As another example, createTimer( is Rules DSL, ScriptExecution.createTimer( is not Rules DSL

Jython (i.e. Python for Java)

  • If you see @ all over the place it’s Jython, for example @rule.

  • If you see : instead of { after an if statement or a for each

  • Indentation is used to denote code blocks instead of { }

  • If you see # or ''' at the start of a comment instead of // or /*

Nashorn JavaScript

  • The lines usually end in ;

  • You see the function keyword, lines like var callme = function(){

  • You see calls to load( to bring in a library

  • You see calls to Java.type to bring in Java classes

There are other differences of course but those should get you pretty close to the language being used between those three at least. All the rest of the supported languages are rather niche at this time so those examples, what few there really are, will almost always mention what language is being discussed.