From 0606b691763a61db626f46ffe8c988b6ea3b37da Mon Sep 17 00:00:00 2001 From: Jesse Portnoy Date: Sun, 16 Jul 2023 16:26:54 +0100 Subject: [PATCH] Helper method to parse syntax files --- highlighter.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/highlighter.go b/highlighter.go index cf0fb48..eea429a 100644 --- a/highlighter.go +++ b/highlighter.go @@ -4,6 +4,7 @@ import ( "regexp" "strings" "unicode/utf8" + "path/filepath" ) // RunePos returns the rune index of a given byte index @@ -60,6 +61,29 @@ func NewHighlighter(def *Def) *Highlighter { // color's group (represented as one byte) type LineMatch map[int]Group +func ParseSyntaxFiles (dir string, defs *[]*highlight.Def) error{ + pattern := "*.yaml" + files, err := filepath.Glob(dir + "/" + pattern) + if err != nil { + return err + } + + for _, file_path := range(files){ + file, err := ioutil.ReadFile(file_path) + if err != nil { + return err + } + + d, err := highlight.ParseDef(file) + if err != nil { + log.Output(1, err.Error()) + continue + } + *defs = append(*defs, d) + return nil + } +} + func findIndex(regex *regexp.Regexp, skip *regexp.Regexp, str []rune, canMatchStart, canMatchEnd bool) []int { regexStr := regex.String() if strings.Contains(regexStr, "^") {