Golang library for highlighting code syntax of programming languages inside HTML documents. Good documentation is included.
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Go to file
Leonid Maslakov eace5a6d60 Update CHANGELOG.md and README.md 1 year ago
.github Update GitHub Actions 2 years ago
CHANGELOG.md Update CHANGELOG.md and README.md 1 year ago
LICENSE first commit 2 years ago
README.md Update CHANGELOG.md and README.md 1 year ago
example_test.go Update docs 2 years ago
go.mod Add go.mod 2 years ago
highlight.go Add JSON support 1 year ago
highlight_c.go Fix: number highlight 2 years ago
highlight_c_test.go Now true, false, nil and numbers are marked as StyleValue 2 years ago
highlight_dockerfile.go Finish write unify parser 2 years ago
highlight_dockerfile_test.go Fix: UTF-8 support (part 2) 2 years ago
highlight_golang.go Add Golang build in functions support 2 years ago
highlight_golang_test.go Add Golang build in functions support 2 years ago
highlight_gomod.go Fix: number highlight 2 years ago
highlight_gomod_test.go Now true, false, nil and numbers are marked as StyleValue 2 years ago
highlight_ini.go Add INI support 1 year ago
highlight_ini_test.go Add INI support 1 year ago
highlight_java.go Add Java support 1 year ago
highlight_java_test.go Add Java support 1 year ago
highlight_json.go Add JSON support 1 year ago
highlight_json_test.go Add JSON support 1 year ago
highlight_python.go Now true, false, nil and numbers are marked as StyleValue 2 years ago
highlight_python_test.go Now true, false, nil and numbers are marked as StyleValue 2 years ago
highlight_robotstxt.go Add C tests 2 years ago
highlight_robotstxt_test.go Change CSS class name 2 years ago
highlight_share.go Code refactoring 2 years ago
highlight_share_nums.go Add JSON support 1 year ago
highlight_share_nums_test.go Add TestShareFormatWord test 2 years ago
highlight_share_openclose.go Fix: UTF-8 in formatWord 2 years ago
highlight_share_openclose_test.go Add TestShareFormatOpenClose test 2 years ago
highlight_share_word.go Add JSON support 1 year ago
highlight_share_word_test.go Fix: UTF-8 in formatWord 2 years ago
highlight_sql.go Fix: number highlight 2 years ago
highlight_sql_test.go Now true, false, nil and numbers are marked as StyleValue 2 years ago
highlight_test.go first commit 2 years ago

README.md

Go report Go Reference Release License

highlight is a golang library for highlighting code syntax of different programming and markup languages inside HTML documents.

Install

Supported Go versions:

  • 1.11
  • 1.17
  • 1.18

Add to go.mod file:

require github.com/lcomrade/highlight v1

How it works?

The <span> tag is used to highlight syntax when converting to HTML. This tag does not do anything on its own, but you can assign different properties to it in CSS. Including color and font.

All extraneous HTML tags will be protected.

Before highlighting:

User-agent: * # comment
Disallow: /faq

# comment 1
# comment 2

Allow: /
Crawl-delay: 10
Sitemap: https://example.org/sitemap.xml
Host: https://mirror.example.org

After highlighting:

<span class='code-keyword'>User-agent:</span> * <span class='code-c'># comment</span>
<span class='code-keyword'>Disallow:</span> /faq

<span class='code-comment'># comment 1</span>
<span class='code-comment'># comment 2</span>

<span class='code-keyword'>Allow:</span> /
<span class='code-keyword'>Crawl-delay:</span> 10
<span class='code-keyword'>Sitemap:</span> https://example.org/sitemap.xml
<span class='code-keyword'>Host:</span> https://mirror.example.org

Supported languages

  • C
  • Dockerfile
  • Golang
  • go.mod
  • INI
  • Java
  • JSON
  • Python
  • robots.txt
  • SQL

Code example

package main

import (
	"fmt"
	"github.com/lcomrade/highlight"
)

func main() {
	// Code written in the C language
	myCode := `
#include <stdio.h>

// Comment

int main() {
	printf("Hello, world!");

	return 0;
}
`

	// Highlight
	fmt.Println(highlight.C(myCode))
}

Read more in the documentation

CSS classes

Example of a CSS file:

.code-operator, .code-keyword, .code-build-in-func, .code-key {
	color: Firebrick;
}

.code-var-type, .code-build-in-var {
	color: RoyalBlue;
}

.code-brackets {
	color: ForestGreen;
}

.code-comment {
	color: DimGray;
}

Documentation