Summary Files Commits Branches @commit @diff @stats
path: root / ext / highlight.awk
blob: a1f15fa1129a575429fd1d1e11233d2307a4e7db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
BEGIN {
	highlight_cmd   = ENVIRON["HIGHLIGHT"] " -O html -f -S " # expected language name in the end!
	highlight_out   = "/tmp/__fragment"
	highlight_langs = "awk,bat,bison,y,bash,sh,zsh,yacc,c,cpp,h,hpp,csharp,cs,css,"    \
	                  "diff,patch,dockerfile,email,go,haskell,hs,html,htm,xhtml,"    \
	                  "httpd,ini,cfg,desktop,java,gradle,javascript,js,json,kotlin," \
	                  "lisp,cl,clisp,sbcl,lua,makefile,mk,markdown,md,meson,mssql,"  \
	                  "nix,perl,pl,php,python,py,qmake,rust,rs,s,sql,svg,tex,toml,xml,yaml"
}

ENVIRON["HIGHLIGHT"] && language && blocks_names[blocks_opened] == "CODE" {
	hl_lang = tolower(language)
	if (hl_lang == "yacc")  hl_lang = "y"
	if (hl_lang == "latex") hl_lang = "tex"

	transcludes_file = toupper($1) == "'INCLUDE" || toupper($1) == "'SNIPPET"

	if (highlight_langs ~ hl_lang && (transcludes_file || !/^[ \t]*'/)) {
		__tmp = FILE_EXTENSION
		FILE_EXTENSION = ""

		if (transcludes_file)
			process_command(toupper(substr($1, 2)))

		highlight = highlight_cmd hl_lang " > " highlight_out

		print | highlight
		close(highlight)

		$0 = getfile(highlight_out)
		gsub(/\\/, "\\\\")

		FILE_EXTENSION = __tmp
	}
}