Add JSON support

main
Leonid Maslakov 4 months ago
parent e7f4f63bab
commit 3b2db4bfd6
  1. 1
      README.md
  2. 4
      highlight.go
  3. 62
      highlight_json.go
  4. 102
      highlight_json_test.go
  5. 2
      highlight_share_nums.go
  6. 2
      highlight_share_word.go

@ -62,6 +62,7 @@ After highlighting:
- `go.mod`
- INI
- Java
- JSON
- Python
- `robots.txt`
- SQL

@ -53,6 +53,7 @@ const (
// | GoMod | go.mod |
// | INI config | ini |
// | Java | java |
// | JSON | json |
// | Python | python, python3 |
// | RobotsTxt | robots.txt |
// | SQL | sql |
@ -78,6 +79,9 @@ func ByName(code string, language string) (string, error) {
case "java":
return Java(code), nil
case "json":
return JSON(code), nil
case "python", "python3":
return Python(code), nil

@ -0,0 +1,62 @@
// Copyright (C) 2022 Leonid Maslakov.
// This file is part of Highlight.
// Highlight is free software: you can redistribute it
// and/or modify it under the terms of the
// GNU Affero Public License as published by the
// Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
// Highlight is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero Public License for more details.
// You should have received a copy of the GNU Affero Public License along with Highlight.
// If not, see <https://www.gnu.org/licenses/>.
package highlight
// JSON processes JSON config files (*.json files).
// Read more: https://en.wikipedia.org/wiki/JSON
//
// Supported values (const StyleValue):
// true
// false
// null
// All numbers (100, 1.3, 2.25)
//
// Also supported:
// Single-line comments (//)
// Multi-line comments (/* */)
// Single-line brackets (", ')
func JSON(code string) string {
// Shild HTML
code = shieldHTML(code)
// Values
values := []string{
"true", "false", "null",
}
// Multi-line comments
code = formatOpenClose(code, "/*", "*/", StyleComment)
// Single-line comments
code = formatOpenClose(code, "//", "\n", StyleComment)
// Single-line brackets
code = formatOpenClose(code, `"`, `"`, StyleBrackets)
code = formatOpenClose(code, `'`, `'`, StyleBrackets)
// Numbers
code = formatNumber(code, defaultNumberChars, defaultNumberChars)
// true, false and null
for _, word := range values {
code = formatWord(code, word, defaultKeywordChars, defaultKeywordChars, StyleValue)
}
return code
}

@ -0,0 +1,102 @@
// Copyright (C) 2022 Leonid Maslakov.
// This file is part of Highlight.
// Highlight is free software: you can redistribute it
// and/or modify it under the terms of the
// GNU Affero Public License as published by the
// Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
// Highlight is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero Public License for more details.
// You should have received a copy of the GNU Affero Public License along with Highlight.
// If not, see <https://www.gnu.org/licenses/>.
package highlight
import (
"testing"
)
func TestJSON(t *testing.T) {
testData := []testDataType{
{
Input: `
test
`,
ExpectResult: `
test
`,
},
{
Input: `
{
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}
}
`,
ExpectResult: `
{
{
<span class='` + StyleBrackets + `'>"firstName"</span>: <span class='` + StyleBrackets + `'>"John"</span>,
<span class='` + StyleBrackets + `'>"lastName"</span>: <span class='` + StyleBrackets + `'>"Smith"</span>,
<span class='` + StyleBrackets + `'>"isAlive"</span>: <span class='` + StyleValue + `'>true</span>,
<span class='` + StyleBrackets + `'>"age"</span>: <span class='` + StyleValue + `'>27</span>,
<span class='` + StyleBrackets + `'>"address"</span>: {
<span class='` + StyleBrackets + `'>"streetAddress"</span>: <span class='` + StyleBrackets + `'>"21 2nd Street"</span>,
<span class='` + StyleBrackets + `'>"city"</span>: <span class='` + StyleBrackets + `'>"New York"</span>,
<span class='` + StyleBrackets + `'>"state"</span>: <span class='` + StyleBrackets + `'>"NY"</span>,
<span class='` + StyleBrackets + `'>"postalCode"</span>: <span class='` + StyleBrackets + `'>"10021-3100"</span>
},
<span class='` + StyleBrackets + `'>"phoneNumbers"</span>: [
{
<span class='` + StyleBrackets + `'>"type"</span>: <span class='` + StyleBrackets + `'>"home"</span>,
<span class='` + StyleBrackets + `'>"number"</span>: <span class='` + StyleBrackets + `'>"212 555-1234"</span>
},
{
<span class='` + StyleBrackets + `'>"type"</span>: <span class='` + StyleBrackets + `'>"office"</span>,
<span class='` + StyleBrackets + `'>"number"</span>: <span class='` + StyleBrackets + `'>"646 555-4567"</span>
}
],
<span class='` + StyleBrackets + `'>"children"</span>: [],
<span class='` + StyleBrackets + `'>"spouse"</span>: <span class='` + StyleValue + `'>null</span>
}
}
`,
},
}
runTest(JSON, testData, t)
}

@ -20,7 +20,7 @@ package highlight
var defaultNumberChars = []string{
" ", "\t", "\n",
".", "<", ">", ":", ";",
".", ",", "<", ">", ":", ";",
"=", "+", "-", "*", "/", "&", "^",
"(", ")", "[", "]", "{", "}",
}

@ -20,7 +20,7 @@ package highlight
var defaultKeywordChars = []string{
"", " ", "\t", "\n",
"<", ">", ":", ";",
".", ",", "<", ">", ":", ";",
"=", "+", "-", "*", "/", "&", "^",
"(", ")", "[", "]", "{", "}",
}

Loading…
Cancel
Save