Add INI support

main
Leonid Maslakov 4 months ago
parent 7bec147638
commit 5f6b5bbd26
  1. 1
      README.md
  2. 5
      highlight.go
  3. 39
      highlight_ini.go
  4. 62
      highlight_ini_test.go

@ -60,6 +60,7 @@ After highlighting:
- Dockerfile
- Golang
- `go.mod`
- INI
- Python
- `robots.txt`
- SQL

@ -37,6 +37,7 @@ const (
StyleBuildInFunc = "code-build-in-func"
StyleComment = "code-comment"
StyleBrackets = "code-brackets"
StyleKey = "code-key"
StyleValue = "code-value"
)
@ -50,6 +51,7 @@ const (
// | Dockerfile | dockerfile |
// | Golang | go, golang |
// | GoMod | go.mod |
// | INI config | ini |
// | Python | python, python3 |
// | RobotsTxt | robots.txt |
// | SQL | sql |
@ -69,6 +71,9 @@ func ByName(code string, language string) (string, error) {
case "go.mod":
return GoMod(code), nil
case "ini":
return INI(code), nil
case "python", "python3":
return Python(code), nil

@ -0,0 +1,39 @@
// 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
// INI processes .INI configuration files.
// Read more: https://en.wikipedia.org/wiki/INI_file
//
// Supported:
// Sections ([....])
// Single-line comments (# and ;)
func INI(code string) string {
// Shild HTML
code = shieldHTML(code)
// Single-line comments
code = formatOpenClose(code, "#", "\n", StyleComment)
code = formatOpenClose(code, ";", "\n", StyleComment)
// Sections
code = formatOpenClose(code, "[", "]", StyleBrackets)
return code
}

@ -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
import (
"testing"
)
func TestINI(t *testing.T) {
testData := []testDataType{
{
Input: `
test
`,
ExpectResult: `
test
`,
},
{
Input: `
# My ini file
; last modified 1 April 2001 by John Doe
[owner]
name = John Doe
organization=Acme Widgets Inc.
`,
ExpectResult: `
<span class='` + StyleComment + `'># My ini file</span>
<span class='` + StyleComment + `'>; last modified 1 April 2001 by John Doe</span>
<span class='` + StyleBrackets + `'>[owner]</span>
name = John Doe
organization=Acme Widgets Inc.
`,
},
}
runTest(INI, testData, t)
}
Loading…
Cancel
Save