2018-10-03 08:58:09 -04:00
|
|
|
// Copyright 2018 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitatio ns under the License.
|
|
|
|
|
|
|
|
package herrors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
var lineNumberExtractors = []lineNumberExtractor{
|
|
|
|
// Template/shortcode parse errors
|
2018-10-22 11:42:06 -04:00
|
|
|
newLineNumberErrHandlerFromRegexp(".*:(\\d+):(\\d*):"),
|
|
|
|
newLineNumberErrHandlerFromRegexp(".*:(\\d+):"),
|
2018-10-03 08:58:09 -04:00
|
|
|
|
|
|
|
// TOML parse errors
|
2018-10-22 11:42:06 -04:00
|
|
|
newLineNumberErrHandlerFromRegexp(".*Near line (\\d+)(\\s.*)"),
|
2018-10-03 08:58:09 -04:00
|
|
|
|
|
|
|
// YAML parse errors
|
2018-10-22 11:42:06 -04:00
|
|
|
newLineNumberErrHandlerFromRegexp("line (\\d+):"),
|
|
|
|
|
|
|
|
// i18n bundle errors
|
|
|
|
newLineNumberErrHandlerFromRegexp("\\((\\d+),\\s(\\d*)"),
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
type lineNumberExtractor func(e error) (int, int)
|
2018-10-03 08:58:09 -04:00
|
|
|
|
|
|
|
func newLineNumberErrHandlerFromRegexp(expression string) lineNumberExtractor {
|
|
|
|
re := regexp.MustCompile(expression)
|
|
|
|
return extractLineNo(re)
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractLineNo(re *regexp.Regexp) lineNumberExtractor {
|
2018-10-21 06:20:21 -04:00
|
|
|
return func(e error) (int, int) {
|
2018-10-03 08:58:09 -04:00
|
|
|
if e == nil {
|
|
|
|
panic("no error")
|
|
|
|
}
|
2018-10-21 06:20:21 -04:00
|
|
|
col := 1
|
2018-10-03 08:58:09 -04:00
|
|
|
s := e.Error()
|
|
|
|
m := re.FindStringSubmatch(s)
|
2018-10-22 11:42:06 -04:00
|
|
|
if len(m) >= 2 {
|
|
|
|
lno, _ := strconv.Atoi(m[1])
|
|
|
|
if len(m) > 2 {
|
|
|
|
col, _ = strconv.Atoi(m[2])
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
2018-10-21 06:20:21 -04:00
|
|
|
|
|
|
|
if col <= 0 {
|
|
|
|
col = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return lno, col
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
return -1, col
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
}
|