Skip to content

Commit

Permalink
Add XSLT lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Aug 24, 2023
1 parent 0825b97 commit 89d08ae
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/language/chroma/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func RegisterAll() {
SketchDrawing{},
Slim{},
XAML{},
XSLT{},
}

// Register all lexers.
Expand Down
15 changes: 15 additions & 0 deletions pkg/language/chroma/testdata/xslt.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

<xsl:template match="/">
Article - <xsl:value-of select="/Article/Title"/>
Authors: <xsl:apply-templates select="/Article/Authors/Author"/>
</xsl:template>

<xsl:template match="Author">
- <xsl:value-of select="." />
</xsl:template>

</xsl:stylesheet>
37 changes: 37 additions & 0 deletions pkg/language/chroma/xslt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package chroma

import (
"strings"

"github.com/wakatime/wakatime-cli/pkg/xml"

"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/lexers"
)

// XSLT lexer.
type XSLT struct{}

// Register the lexer.
func (XSLT) Register() {
_ = lexers.Register(chroma.MustNewLexer(
&chroma.Config{
Name: "XSLT",
Aliases: []string{"xslt"},
// xpl is XProc
Filenames: []string{"*.xsl", "*.xslt", "*.xpl"},
MimeTypes: []string{"application/xsl+xml", "application/xslt+xml"},
},
func() chroma.Rules {
return chroma.Rules{
"root": {},
}
},
)).SetAnalyser(func(text string) float32 {
if xml.MatchString(text) && strings.Contains(text, "<xsl") {
return 0.8
}

return 0
})
}
28 changes: 28 additions & 0 deletions pkg/language/chroma/xslt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package chroma_test

import (
"os"
"testing"

"github.com/wakatime/wakatime-cli/pkg/language/chroma"

c "github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/stretchr/testify/assert"
)

func init() {
chroma.XSLT{}.Register()
}

func TestXSLT_AnalyseText(t *testing.T) {
data, err := os.ReadFile("testdata/xslt.xsl")
assert.NoError(t, err)

l := lexers.Get("XSLT")

analyser, ok := l.(c.Analyser)
assert.True(t, ok)

assert.Equal(t, float32(0.8), analyser.AnalyseText(string(data)))
}

0 comments on commit 89d08ae

Please sign in to comment.