Skip to content

Instantly share code, notes, and snippets.

@jozsefsallai
Last active July 26, 2024 19:17
Show Gist options
  • Save jozsefsallai/5b09fb0099158344512aaec8121220a1 to your computer and use it in GitHub Desktop.
Save jozsefsallai/5b09fb0099158344512aaec8121220a1 to your computer and use it in GitHub Desktop.
Go HTML template syntax highlighting for Sublime Text

Standalone Go HTML template syntax highlighting for Sublime Text

These files are part of the GoSublime package by DisposaBoy.

Hello friends. It is always very annoying to write HTML templates for a Go application in Sublime Text, and only have the HTML part of the code highlighted. Those white lines of code -- which are the most important parts of the template files -- are just not pleasant to the eye.

Luckily, DisposaBoy has made a syntax highlighter for Go HTML templates in the GoSublime package (which can be installed using Package Control), but what if you don't actually want to install the whole package just to have syntax highlighting?

The files from this gist do all the job for you and you won't even have to install the whole package! (I highly recommend you do, though -- it's really useful!)

Just save these two files into the Packages/User folder (Preferences -> Browse Packages, and then the User folder) and you're all set!

Example

GoSublime (c) DisposaBoy.

%YAML 1.2
---
name: 'GoHTML'
file_extensions:
- gohtml
- html.go
scope: text.html.gohtml
contexts:
main:
- match: ''
push: 'Packages/HTML/HTML.sublime-syntax'
with_prototype:
- match: '{{'
captures:
0: punctuation.section.embedded.begin.gotemplate
push: Packages/User/GoTemplate.sublime-syntax
with_prototype:
- match: '}}'
captures:
0: punctuation.section.embedded.end.gotemplate
pop: true
%YAML 1.2
---
name: 'GoTemplate'
scope: source.gotemplate
hidden: true
contexts:
main:
- match: ":="
scope: keyword.operator.initialize.gotemplate
- match: \|
scope: keyword.operator.pipe.gotemplate
- match: '[.$][\w]*'
scope: variable.other.gotemplate
- match: \b(if|else|range|template|with|end|nil|define)\b
scope: keyword.control.gotemplate
- match: \b(and|call|html|index|js|len|not|or|print|printf|println|urlquery|eq|ne|lt|le|gt|ge)\b
scope: support.function.builtin.gotemplate
- match: /\*
push:
- meta_scope: comment.block.gotemplate
- match: \*/
pop: true
- match: '"'
captures:
0: punctuation.definition.string.begin.gotemplate
push:
- meta_scope: string.quoted.double.gotemplate
- match: '"'
captures:
0: punctuation.definition.string.end.gotemplate
pop: true
- include: string_placeholder
- include: string_escaped_char
- match: "`"
captures:
0: punctuation.definition.string.begin.gotemplate
push:
- meta_scope: string.quoted.raw.gotemplate
- match: "`"
captures:
0: punctuation.definition.string.end.gotemplate
pop: true
- include: string_placeholder
string_escaped_char:
- match: '\\(\\|[abfnrtv''"]|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4}|U[0-9a-fA-F]{8}|[0-7]{3})'
scope: constant.character.escape.gotemplate
- match: \\.
scope: invalid.illegal.unknown-escape.gotemplate
string_placeholder:
- match: |-
(?x)%
(\d+\$)? # field (argument #)
[#0\- +']* # flags
[,;:_]? # separator character (AltiVec)
((-?\d+)|\*(-?\d+\$)?)? # minimum field width
(\.((-?\d+)|\*(-?\d+\$)?)?)? # precision
[diouxXDOUeEfFgGaAcCsSqpnvtTbyYhHmMzZ%] # conversion type
scope: constant.other.placeholder.gotemplate
- match: "%"
scope: invalid.illegal.placeholder.gotemplate
@jamessimas
Copy link

Thank you for this. This was helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment