Now true, false, nil and numbers are marked as StyleValue

main
Leonid Maslakov 5 months ago
parent 6985bab960
commit 0cf97f2602
  1. 2
      highlight.go
  2. 2
      highlight_c_test.go
  3. 17
      highlight_golang.go
  4. 18
      highlight_golang_test.go
  5. 10
      highlight_gomod_test.go
  6. 21
      highlight_python.go
  7. 2
      highlight_python_test.go
  8. 8
      highlight_share_nums.go
  9. 3
      highlight_share_word.go
  10. 2
      highlight_sql_test.go

@ -37,7 +37,7 @@ const (
StyleBuildInFunc = "code-build-in-func"
StyleComment = "code-comment"
StyleBrackets = "code-brackets"
StyleNumber = "code-number"
StyleValue = "code-value"
)
// ByName helps to highlight code based on the language name.

@ -40,7 +40,7 @@ int main() {
<span class='` + StyleKeyword + `'>int</span> main() {
printf(<span class='` + StyleBrackets + `'>"Hello, world!"</span>);
<span class='` + StyleKeyword + `'>return</span> <span class='` + StyleNumber + `'>0</span>;
<span class='` + StyleKeyword + `'>return</span> <span class='` + StyleValue + `'>0</span>;
}
`,
},

@ -48,6 +48,12 @@ package highlight
// type
// var
//
// Supported values (const StyleValue):
// true
// false
// nil
// All numbers (100, 1.3, 2.25)
//
// Supported operators (const StyleOperator):
// !
// !=
@ -99,7 +105,6 @@ package highlight
// Multi-line comments (/* */)
// Single-line brackets (", ')
// Multi-line brackets (` `)
// Numbers (100, 1.2, 1.25)
func Golang(code string) string {
// Shild HTML
code = shieldHTML(code)
@ -117,6 +122,11 @@ func Golang(code string) string {
"var",
}
// Values
values := []string{
"true", "false", "nil",
}
// Operators
operators := []string{
"!", "!=", "%", "&amp",
@ -169,5 +179,10 @@ func Golang(code string) string {
// Numbers
code = formatNumber(code, defaultNumberChars, defaultNumberChars)
// true, false and nil
for _, word := range values {
code = formatWord(code, word, defaultKeywordChars, defaultKeywordChars, StyleValue)
}
return code
}

@ -100,6 +100,24 @@ end
fmt.Println(<span class='` + StyleBrackets + `'>"Unknown answer:("</span>)
}
}
`,
},
{
Input: `
if myBool == true {
println("OK")
} else {
println("FAIL")
}
`,
ExpectResult: `
<span class='` + StyleKeyword + `'>if</span> myBool <span class='` + StyleOperator + `'>==</span> <span class='` + StyleValue + `'>true</span> {
println(<span class='` + StyleBrackets + `'>"OK"</span>)
} <span class='` + StyleKeyword + `'>else</span> {
println(<span class='` + StyleBrackets + `'>"FAIL"</span>)
}
`,
},
}

@ -42,16 +42,16 @@ exclude example.com/thismodule v1.3.0
ExpectResult: `
<span class='` + StyleKeyword + `'>module</span> example.com/mymodule
<span class='` + StyleKeyword + `'>go</span> <span class='` + StyleNumber + `'>1</span>.<span class='` + StyleNumber + `'>14</span>
<span class='` + StyleKeyword + `'>go</span> <span class='` + StyleValue + `'>1</span>.<span class='` + StyleValue + `'>14</span>
<span class='` + StyleKeyword + `'>require</span> (
example.com/othermodule v<span class='` + StyleNumber + `'>1</span>.<span class='` + StyleNumber + `'>2</span>.<span class='` + StyleNumber + `'>3</span>
example.com/thismodule v<span class='` + StyleNumber + `'>1</span>.<span class='` + StyleNumber + `'>2</span>.<span class='` + StyleNumber + `'>3</span>
example.com/thatmodule v<span class='` + StyleNumber + `'>1</span>.<span class='` + StyleNumber + `'>2</span>.<span class='` + StyleNumber + `'>3</span>
example.com/othermodule v<span class='` + StyleValue + `'>1</span>.<span class='` + StyleValue + `'>2</span>.<span class='` + StyleValue + `'>3</span>
example.com/thismodule v<span class='` + StyleValue + `'>1</span>.<span class='` + StyleValue + `'>2</span>.<span class='` + StyleValue + `'>3</span>
example.com/thatmodule v<span class='` + StyleValue + `'>1</span>.<span class='` + StyleValue + `'>2</span>.<span class='` + StyleValue + `'>3</span>
)
<span class='` + StyleKeyword + `'>replace</span> example.com/thatmodule <span class='` + StyleOperator + `'>=&gt</span> ../thatmodule
<span class='` + StyleKeyword + `'>exclude</span> example.com/thismodule v<span class='` + StyleNumber + `'>1</span>.<span class='` + StyleNumber + `'>3</span>.<span class='` + StyleNumber + `'>0</span>
<span class='` + StyleKeyword + `'>exclude</span> example.com/thismodule v<span class='` + StyleValue + `'>1</span>.<span class='` + StyleValue + `'>3</span>.<span class='` + StyleValue + `'>0</span>
`,
},
}

@ -22,9 +22,6 @@ package highlight
// Read more: https://docs.python.org/3/reference/index.html
//
// Supported keywords (const StyleKeyword):
// False
// None
// True
// and
// as
// assert
@ -58,6 +55,12 @@ package highlight
// with
// yield
//
// Supported values (const StyleValue):
// True
// False
// None
// All numbers (100, 1.3, 2.25)
//
// Supported operators (const StyleOperator):
// +
// -
@ -168,14 +171,12 @@ package highlight
// Multi-line comments (''' ''')
// Single-line brackets (", ')
// Multi-line brackets (""" """)
// Numbers (100, 1.2, 1.25)
func Python(code string) string {
// Shild HTML
code = shieldHTML(code)
// Keywords
keywords := []string{
"False", "None", "True",
"and", "as", "assert",
"async", "await", "break",
"class", "continue", "def",
@ -189,6 +190,11 @@ func Python(code string) string {
"with", "yield",
}
// Values
values := []string{
"False", "None", "True",
}
// Operators
operators := []string{
"+", "-", "*", "**",
@ -270,5 +276,10 @@ func Python(code string) string {
// Numbers
code = formatNumber(code, defaultNumberChars, defaultNumberChars)
// True, False and None
for _, word := range values {
code = formatWord(code, word, defaultKeywordChars, defaultKeywordChars, StyleValue)
}
return code
}

@ -85,7 +85,7 @@ if __name__ == "__main__":
This is test.
"""</span>
<span class='` + StyleKeyword + `'>def</span> main() -&gt <span class='` + StyleKeyword + `'>None</span>:
<span class='` + StyleKeyword + `'>def</span> main() -&gt <span class='` + StyleValue + `'>None</span>:
name <span class='` + StyleOperator + `'>=</span> <span class='` + StyleBuildInFunc + `'>input</span>(<span class='` + StyleBrackets + `'>"Your name:"</span>)
<span class='` + StyleKeyword + `'>if</span> name <span class='` + StyleOperator + `'>==</span> <span class='` + StyleBrackets + `'>"Ivan"</span> <span class='` + StyleKeyword + `'>or</span> name <span class='` + StyleOperator + `'>==</span> <span class='` + StyleBrackets + `'>"Nikolai"</span>:

@ -20,7 +20,7 @@ package highlight
var defaultNumberChars = []string{
" ", "\t", "\n",
".", "=", ":", ";",
".", "=", "<", ">", ":", ";",
"(", ")", "[", "]", "{", "}",
}
@ -63,7 +63,7 @@ func formatNumber(text string, cmdStartChars []string, cmdEndChars []string) str
if bufferOpen == true && isNumber(charRune) == false {
// Good close
if isInStrList(cmdEndChars, char) && buffer != "" {
result = result + "<span class='" + StyleNumber + "'>" + buffer + "</span>"
result = result + "<span class='" + StyleValue + "'>" + buffer + "</span>"
// Bad close
} else {
@ -91,7 +91,7 @@ func formatNumber(text string, cmdStartChars []string, cmdEndChars []string) str
} else {
if bufferOpen == true {
if buffer != "" {
result = result + "<span class='" + StyleNumber + "'>" + buffer + "</span>"
result = result + "<span class='" + StyleValue + "'>" + buffer + "</span>"
buffer = ""
}
@ -103,7 +103,7 @@ func formatNumber(text string, cmdStartChars []string, cmdEndChars []string) str
}
if bufferOpen == true && buffer != "" {
result = result + "<span class='" + StyleNumber + "'>" + buffer + "</span>"
result = result + "<span class='" + StyleValue + "'>" + buffer + "</span>"
}
return result

@ -19,7 +19,8 @@
package highlight
var defaultKeywordChars = []string{
"", " ", "\t", "\n", ":", ";",
"", " ", "\t", "\n",
"<", ">", ":", ";",
"(", ")", "{", "}",
}

@ -49,7 +49,7 @@ HAVING
<span class='` + StyleKeyword + `'>GROUP BY</span>
DeptID
<span class='` + StyleKeyword + `'>HAVING</span>
SUM(SaleAmount) &gt <span class='` + StyleNumber + `'>1000</span>
SUM(SaleAmount) &gt <span class='` + StyleValue + `'>1000</span>
`,
},
}

Loading…
Cancel
Save