Add Java support

main
Leonid Maslakov 4 months ago
parent 5f6b5bbd26
commit e7f4f63bab
  1. 1
      README.md
  2. 4
      highlight.go
  3. 200
      highlight_java.go
  4. 68
      highlight_java_test.go

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

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

@ -0,0 +1,200 @@
// 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
// Java processes Java source code (*.java files).
// Read more: https://en.wikipedia.org/wiki/Java_(programming_language)
//
// Supported keywords (const StyleKeyword):
// abstract
// assert
// break
// case
// catch
// class
// const
// continue
// default
// do
// else
// enum
// extends
// final
// finally
// for
// goto
// if
// implements
// import
// instanceof
// interface
// native
// new
// package
// private
// protected
// public
// return
// static
// strictfp
// super
// switch
// synchronized
// this
// throw
// throws
// transient
// try
// void
// volatile
// while
//
// Supported values (const StyleValue):
// true
// false
// null
// All numbers (100, 1.3, 2.25)
//
// Supported operators (const StyleOperator):
// !
// !=
// %
// %=
// &&
// &=
// */
// *=
// +
// ++
// +=
// -
// --
// -=
// /=
// <
// <<=
// <=
// =
// ==
// >
// >=
// >>=
// ^=
// |=
// ||
//
// Supported variable types (const StyleVarType):
// boolean
// byte
// char
// double
// float
// int
// long
// short
// String
//
// Also supported:
// Single-line comments (//)
// Multi-line comments (/* */)
// Single-line brackets (", ')
// Multi-line brackets (""" """)
func Java(code string) string {
// Shild HTML
code = shieldHTML(code)
// Keywords
keywords := []string{
"abstract", "assert", "break",
"case", "catch", "class",
"const", "continue", "default",
"do", "else", "enum",
"extends", "final", "finally",
"for", "goto", "if",
"implements", "import", "instanceof",
"interface", "native", "new",
"package", "private", "protected",
"public", "return", "static",
"strictfp", "super", "switch",
"synchronized", "this", "throw",
"throws", "transient", "try",
"void", "volatile", "while",
}
// Values
values := []string{
"true", "false", "null",
}
// Operators
operators := []string{
"!", "!=", "%", "%=",
"&amp&amp", "&amp=", "*/",
"*=", "+", "++", "+=",
"-", "--", "-=", "/=",
"&lt", "&lt&lt=", "&lt=",
"=", "==", "&gt", "&gt=",
"&gt&gt=", "^=", "|=", "||",
}
// Varibles types
varTypes := []string{
"boolean", "byte", "char",
"double", "float", "int",
"long", "short", "String",
}
// Multi-line comments
code = formatOpenClose(code, "/*", "*/", StyleComment)
// Multi-line brackets
code = formatOpenClose(code, `"""`, `"""`, StyleBrackets)
// Single-line comments
code = formatOpenClose(code, "//", "\n", StyleComment)
// Single-line brackets
code = formatOpenClose(code, `"`, `"`, StyleBrackets)
code = formatOpenClose(code, `'`, `'`, StyleBrackets)
// Keywords
for _, word := range keywords {
code = formatWord(code, word, defaultKeywordChars, defaultKeywordChars, StyleKeyword)
}
// Operators
for _, word := range operators {
code = formatWord(code, word, defaultOperatorChars, defaultOperatorChars, StyleOperator)
}
// Varibles types
for _, word := range varTypes {
code = formatWord(code, word, defaultKeywordChars, defaultKeywordChars, StyleVarType)
}
// 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,68 @@
// 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 TestJava(t *testing.T) {
testData := []testDataType{
{
Input: `
test
`,
ExpectResult: `
test
`,
},
{
Input: `
/*
---HELLO WORLD APP---
*/
public class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Prints the string to the console.
}
}
`,
ExpectResult: `
<span class='` + StyleComment + `'>/*
---HELLO WORLD APP---
*/</span>
<span class='` + StyleKeyword + `'>public</span> <span class='` + StyleKeyword + `'>class</span> HelloWorldApp {
<span class='` + StyleKeyword + `'>public</span> <span class='` + StyleKeyword + `'>static</span> <span class='` + StyleKeyword + `'>void</span> main(<span class='` + StyleVarType + `'>String</span>[] args) {
System.out.println(<span class='` + StyleBrackets + `'>"Hello World!"</span>); <span class='` + StyleComment + `'>// Prints the string to the console.</span>
}
}
`,
},
}
runTest(Java, testData, t)
}
Loading…
Cancel
Save