diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-21 12:20:21 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-10-22 20:46:14 +0200 |
commit | d1661b823af25c50d3bbe5366ea40a3cdd52e237 (patch) | |
tree | cd84d18229fb9c294ff1be56d7c0ce92a8f46761 /common/herrors/line_number_extractors.go | |
parent | 7930d2132a3c36c1aaca20f16f56978c84656b0a (diff) |
hugolib: Continue the file context/line number errors work
See #5324
Diffstat (limited to 'common/herrors/line_number_extractors.go')
-rw-r--r-- | common/herrors/line_number_extractors.go | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/common/herrors/line_number_extractors.go b/common/herrors/line_number_extractors.go index 01a7450f9..8740afdf7 100644 --- a/common/herrors/line_number_extractors.go +++ b/common/herrors/line_number_extractors.go @@ -14,14 +14,13 @@ package herrors import ( - "fmt" "regexp" "strconv" ) var lineNumberExtractors = []lineNumberExtractor{ // Template/shortcode parse errors - newLineNumberErrHandlerFromRegexp("(.*?:)(\\d+)(:.*)"), + newLineNumberErrHandlerFromRegexp("(.*?:)(\\d+)(:)(\\d+)?(.*)"), // TOML parse errors newLineNumberErrHandlerFromRegexp("(.*Near line )(\\d+)(\\s.*)"), @@ -30,7 +29,7 @@ var lineNumberExtractors = []lineNumberExtractor{ newLineNumberErrHandlerFromRegexp("(line )(\\d+)(:)"), } -type lineNumberExtractor func(e error, offset int) (int, string) +type lineNumberExtractor func(e error) (int, int) func newLineNumberErrHandlerFromRegexp(expression string) lineNumberExtractor { re := regexp.MustCompile(expression) @@ -38,22 +37,26 @@ func newLineNumberErrHandlerFromRegexp(expression string) lineNumberExtractor { } func extractLineNo(re *regexp.Regexp) lineNumberExtractor { - return func(e error, offset int) (int, string) { + return func(e error) (int, int) { if e == nil { panic("no error") } + col := 1 s := e.Error() m := re.FindStringSubmatch(s) - if len(m) == 4 { - i, _ := strconv.Atoi(m[2]) - msg := e.Error() - if offset != 0 { - i = i + offset - msg = re.ReplaceAllString(s, fmt.Sprintf("${1}%d${3}", i)) + if len(m) >= 4 { + lno, _ := strconv.Atoi(m[2]) + if len(m) > 4 { + col, _ = strconv.Atoi(m[4]) } - return i, msg + + if col <= 0 { + col = 1 + } + + return lno, col } - return -1, "" + return -1, col } } |