Gherkin Syntax Highlighting
Asked by Andreas:

Since we started using Cucumber for various projects, we felt the need for a nice representation of Gherkin code within Confluence.

So I created a custom gherkin brush file based on the [SyntaxHighlighter manual](http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/custom.html) and added it to the Code Macro:

```
SyntaxHighlighter.brushes.Custom = function()
? {
??? this.regexList = [
????? { regex: SyntaxHighlighter.regexLib.singleLinePerlComments,?? ?css: 'comments' },?? ??? ?// #comments
????? { regex: /(['\"]{3})([^\1])*?\1/gm, ?? ??? ??? ??? ??? ??? ?css: 'string' },?? ??? ?// multi-line strings like in python with """
????? { regex: SyntaxHighlighter.regexLib.doubleQuotedString,?? ??? ?css: 'string' },?? ??? ?// strings
????? { regex: SyntaxHighlighter.regexLib.singleQuotedString,?? ??? ?css: 'string' },?? ??? ?// strings
????? { regex: /@.*$/gmi,?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?css: 'color1' },?? ??? ?// @tags
????? { regex: /^\s*(But |And |Then |When |Given |Scenarios|Examples|Scenario Template|Scenario Outline|Scenario|Background|Feature)/gmi,?? ?css: 'keyword'? } // english
??? ];
? };
SyntaxHighlighter.brushes.Custom.aliases? = ['gherkin'];

SyntaxHighlighter.brushes.Custom.prototype = new SyntaxHighlighter.Highlighter();
```

It currently looks ok but I wonder if someone from the community uses a similar/ better solution for that.

*Feel free to use this, btw: [gist](https://gist.github.com/rocketworm/e1bf9d55469f3c3bbae0bbcc0fa2956e)*


Answers:
author: Andreas
body: [Prism](https://marketplace.atlassian.com/apps/1217659/prism-code-syntax-highlighting?hosting=server&tab=overview) does support Gherkin for JIRA, [@Nos Him](/t5/user/viewprofilepage/user-id/3128102).


author: Nos Him
body: How to implement this JS in Jira ?


author: Andreas
body: You're right [@Ben Erridge](/t5/user/viewprofilepage/user-id/1775344), this one looks even better. I've updated the [gist](https://gist.github.com/rocketworm/e1bf9d55469f3c3bbae0bbcc0fa2956e)*.*

Thank you!


author: Glib
body: Hi [@Andreas](/t5/user/viewprofilepage/user-id/960599) ,

Since you extensively use Cucumber you may want to consider [AssertThat BDD \& Test Management in Jira](https://marketplace.atlassian.com/apps/1219033/assertthat-bdd-test-management-in-jira?hosting=cloud&tab=overview "AssertThat BDD & Test Management in Jira") plugin which provides end-t-end BDD/cucumber solution. From interactive gherkin editor to test automation frameworks integration and uploading cucumber reports back to Jira.

More information can be found on the [website](https://www.assertthat.com/) and [wiki](https://assertthat.atlassian.net/wiki/spaces/ABTM/overview).

Regards,

Glib


author: Ben Erridge
body: I found this one to be better can't remember where I found it

```
/**
 * Author: Michael Hall
 * URL: http://www.bankofcanada.ca/
 * License: GPL-2 | GPL-3
 */
SyntaxHighlighter.brushes.Gherkin = function()
{
 /**
 * Filters out the delimiters and values out of tables.
 * @param match: the contents of the matching text
 * @param regexInfo: regular expression info
 * @returns: list of SyntaxHighlighter.Match objects
 */
 function processTable(match, regexInfo){
 return [
 new SyntaxHighlighter.Match('|',match.index,'keyword'),
 new SyntaxHighlighter.Match(match[0].substr(1),match.index+1,'value')
 ];
 }
 
 var keywords = 'Given When And Then Feature Scenario Outline Template Background But Examples';
 this.regexList = [
 { regex: SyntaxHighlighter.regexLib.singleLinePerlComments, css: 'comments' }, // comments using #
 { regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'string' }, // "string"
 { regex: /"""[^"]*"""/gm, css: 'string' }, // multiline string (""" ..... """)
 { regex: /\b([\d]+(\.[\d]+)?|0x[a-f0-9]+)\b/gi, css: 'value' }, // numbers
 { regex: new RegExp(this.getKeywords(keywords), 'gmi'), css: 'keyword' }, // keywords
 { regex: /@[^@\r\n\t ]+/gi, css: 'preprocessor' }, //annotations
 { regex: /\|.*?(?=\|)/g, func: processTable }, //Tables
 { regex: /\|/g, css: 'keyword' }, //Excess table delimiters
 { regex: /(&lt;|<).*?(&gt;|>)/gi, css: 'value' } //values from tables
 ];
};
SyntaxHighlighter.brushes.Gherkin.prototype = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.Gherkin.aliases = ['gherkin', 'behat'];
```


author: Benjamin Jurg
body: Found the following ticket: <https://jira.atlassian.com/browse/CONFCLOUD-69840>

Aside from this cool workaround let's all also vote for getting the feature into the standard code macro.


author: adam-pike
body: Sorry - my bad, the Prism plugin was not properly installed. I was just getting regular Jira code macro functionality.


author: adam-pike
body: [@Andreas](/t5/user/viewprofilepage/user-id/960599) according to their documents it does <https://prismjs.com/#languages-list>

But on applying it with:

```
{code:gherkin}.. {code}
```

I get

```
Unable to find source-code formatter for language: gherkin. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
```

Which is only a fraction of the claimed supported formats.


author: Andreas
body: Thanks for the interesting suggestion, [@Glib](/t5/user/viewprofilepage/user-id/1304791) .

We're running JIRA on premises though and are currently not looking for additional tools in that technology stack.


Tags: code-macro

cucumber

syntax-highlighting