Thursday, May 31, 2018

How to highlight code in blogger or blogspot.com?

<pre class="prettyprint"><code class="language-html">YOUR CODE HRER</code></pre>
Above the code is for HTML. If you want to use syntax highlighter for specific style or language, you must replace language-HTML with language-value. Given the value and attribute of some style or language:
Highlight ForvalueAttribute
HTMLhtmllanguage-html
CSScshlanguage-
JavaScriptjslanguage-js
XMLxmllanguage-xml
xHTMLxhtmllanguage-xhtml
Cclanguage-c
C++cpplanguage-cpp
C#cslanguage-cs
PHPphpLanguage-php
Pythonpylanguage-py
Javajavalanguage-java
Rubyrblanguage-rb
Perlperllanguage-perl
Resource Link: https://www.compromath.com/code-formatting-syntax-highlighter-for-blogger/

How to optimize if condition in groovy

Main Code:
        if (text != null) {
            return text.toString().replaceAll("<[Bb][Rr]>", "\n")
        } else {
            return text
        }
Optimization#1:
return text?text.toString().replaceAll("<[Bb][Rr]>", "\n"):text
Optimization#2:
return text?toString().replaceAll("<[Bb][Rr]>", "\n")
It will give null pointer exception. because text?.toString() also gives null. But it is not checked. So do it like below:

return text?toString()?.replaceAll("<[Bb][Rr]>", "\n")
Test Case:
@Unroll
    def "#caseNo : check replaceBrTagToNewLine method"() {
        when:
        def result = itemScreenService.replaceBrTagToNewLine(text)

        then:
        assert result == expected

        where:
        caseNo | text               | expected
        1      | null               | null
        2      | ""                 | ""
        3      | "test"             | "test"
        4      | "
"             | "\n"
        5      | "test
line"     | "test\nline"
        6      | "test
line"     | "test\nline"
        7      | "test
line"     | "test\nline"
        8      | "test
line"     | "test\nline"
        9      | "



" | "\n\n\n\n"
    }