Golang library for highlighting code syntax of programming languages inside HTML documents. Good documentation is included.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.6 KiB

// 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 (
"errors"
"strings"
)
// These constants describe the name of the class
// that will be assigned to the HTML tag '<span>'.
//
// Examples:
// <span style='code-keyword'>User-Agent:</span>
// <span style='code-comment'># My comment</span>
const (
StyleKeyword = "code-keyword"
StyleOperator = "code-operator"
StyleVarType = "code-var-type"
StyleBuildInVar = "code-build-in-var"
StyleBuildInFunc = "code-build-in-func"
StyleComment = "code-comment"
StyleBrackets = "code-brackets"
StyleKey = "code-key"
StyleValue = "code-value"
)
// ByName helps to highlight code based on the language name.
// This can be useful for Markdown and some other cases.
// The name of the language is not case sensitive.
//
// | Function name | Language name |
// |---------------|-----------------|
// | C | c |
// | Dockerfile | dockerfile |
// | Golang | go, golang |
// | GoMod | go.mod |
// | INI config | ini |
// | Java | java |
// | JSON | json |
// | Python | python, python3 |
// | RobotsTxt | robots.txt |
// | SQL | sql |
func ByName(code string, language string) (string, error) {
language = strings.ToLower(language)
switch strings.ToLower(language) {
case "c":
return C(code), nil
case "dockerfile":
return Dockerfile(code), nil
case "go", "golang":
return Golang(code), nil
case "go.mod":
return GoMod(code), nil
case "ini":
return INI(code), nil
case "java":
return Java(code), nil
case "json":
return JSON(code), nil
case "python", "python3":
return Python(code), nil
case "robots.txt":
return RobotsTxt(code), nil
case "sql":
return SQL(code), nil
}
return code, errors.New("highlight: unknown language name: " + language)
}