2022-05-02 10:07:52 -04:00
|
|
|
// Copyright 2022 The Hugo Authors. All rights reserved.
|
2018-10-03 08:58:09 -04:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-04-05 19:07:57 -04:00
|
|
|
// Package herrors contains common Hugo errors and error related utilities.
|
2018-10-03 08:58:09 -04:00
|
|
|
package herrors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2018-10-23 02:54:10 -04:00
|
|
|
"io/ioutil"
|
2018-11-15 03:28:02 -05:00
|
|
|
"path/filepath"
|
2018-10-03 08:58:09 -04:00
|
|
|
"strings"
|
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
"github.com/gohugoio/hugo/common/text"
|
2018-10-03 08:58:09 -04:00
|
|
|
)
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
// LineMatcher contains the elements used to match an error to a line
|
|
|
|
type LineMatcher struct {
|
2018-11-01 06:28:30 -04:00
|
|
|
Position text.Position
|
|
|
|
Error error
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
LineNumber int
|
|
|
|
Offset int
|
|
|
|
Line string
|
|
|
|
}
|
|
|
|
|
|
|
|
// LineMatcherFn is used to match a line with an error.
|
|
|
|
type LineMatcherFn func(m LineMatcher) bool
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
// SimpleLineMatcher simply matches by line number.
|
|
|
|
var SimpleLineMatcher = func(m LineMatcher) bool {
|
2018-11-01 06:28:30 -04:00
|
|
|
return m.Position.LineNumber == m.LineNumber
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorContext contains contextual information about an error. This will
|
|
|
|
// typically be the lines surrounding some problem in a file.
|
|
|
|
type ErrorContext struct {
|
|
|
|
|
|
|
|
// If a match will contain the matched line and up to 2 lines before and after.
|
|
|
|
// Will be empty if no match.
|
|
|
|
Lines []string
|
|
|
|
|
|
|
|
// The position of the error in the Lines above. 0 based.
|
2018-11-01 06:28:30 -04:00
|
|
|
LinesPos int
|
2018-10-03 08:58:09 -04:00
|
|
|
|
|
|
|
// The lexer to use for syntax highlighting.
|
|
|
|
// https://gohugo.io/content-management/syntax-highlighting/#list-of-chroma-highlighting-languages
|
|
|
|
ChromaLexer string
|
|
|
|
}
|
|
|
|
|
|
|
|
func chromaLexerFromType(fileType string) string {
|
2018-10-21 06:20:21 -04:00
|
|
|
switch fileType {
|
|
|
|
case "html", "htm":
|
|
|
|
return "go-html-template"
|
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
return fileType
|
|
|
|
}
|
|
|
|
|
2018-11-15 03:28:02 -05:00
|
|
|
func extNoDelimiter(filename string) string {
|
2019-02-02 02:19:12 -05:00
|
|
|
return strings.TrimPrefix(filepath.Ext(filename), ".")
|
2018-11-15 03:28:02 -05:00
|
|
|
}
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
func chromaLexerFromFilename(filename string) string {
|
|
|
|
if strings.Contains(filename, "layouts") {
|
|
|
|
return "go-html-template"
|
|
|
|
}
|
|
|
|
|
2018-11-15 03:28:02 -05:00
|
|
|
ext := extNoDelimiter(filename)
|
2018-10-21 06:20:21 -04:00
|
|
|
return chromaLexerFromType(ext)
|
|
|
|
}
|
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
func locateErrorInString(src string, matcher LineMatcherFn) (*ErrorContext, text.Position) {
|
2018-10-23 02:54:10 -04:00
|
|
|
return locateError(strings.NewReader(src), &fileError{}, matcher)
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
func locateError(r io.Reader, le FileError, matches LineMatcherFn) (*ErrorContext, text.Position) {
|
2018-10-23 02:54:10 -04:00
|
|
|
if le == nil {
|
|
|
|
panic("must provide an error")
|
2018-10-21 06:20:21 -04:00
|
|
|
}
|
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
errCtx := &ErrorContext{LinesPos: -1}
|
|
|
|
pos := text.Position{LineNumber: -1, ColumnNumber: 1, Offset: -1}
|
2018-10-23 02:54:10 -04:00
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
return errCtx, pos
|
2018-10-23 02:54:10 -04:00
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
lepos := le.Position()
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
lines := strings.Split(string(b), "\n")
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2019-08-02 11:20:36 -04:00
|
|
|
if lepos.ColumnNumber >= 0 {
|
2018-11-01 06:28:30 -04:00
|
|
|
pos.ColumnNumber = lepos.ColumnNumber
|
2018-10-23 02:54:10 -04:00
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
lineNo := 0
|
|
|
|
posBytes := 0
|
|
|
|
|
|
|
|
for li, line := range lines {
|
|
|
|
lineNo = li + 1
|
|
|
|
m := LineMatcher{
|
2018-11-01 06:28:30 -04:00
|
|
|
Position: le.Position(),
|
|
|
|
Error: le,
|
2018-10-23 02:54:10 -04:00
|
|
|
LineNumber: lineNo,
|
|
|
|
Offset: posBytes,
|
|
|
|
Line: line,
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
2018-11-01 06:28:30 -04:00
|
|
|
if errCtx.LinesPos == -1 && matches(m) {
|
|
|
|
pos.LineNumber = lineNo
|
2018-10-23 02:54:10 -04:00
|
|
|
break
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
posBytes += len(line)
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
if pos.LineNumber != -1 {
|
|
|
|
low := pos.LineNumber - 3
|
2018-10-23 02:54:10 -04:00
|
|
|
if low < 0 {
|
|
|
|
low = 0
|
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
if pos.LineNumber > 2 {
|
|
|
|
errCtx.LinesPos = 2
|
2018-10-23 02:54:10 -04:00
|
|
|
} else {
|
2018-11-01 06:28:30 -04:00
|
|
|
errCtx.LinesPos = pos.LineNumber - 1
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-11-01 06:28:30 -04:00
|
|
|
high := pos.LineNumber + 2
|
2018-10-23 02:54:10 -04:00
|
|
|
if high > len(lines) {
|
|
|
|
high = len(lines)
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:54:10 -04:00
|
|
|
errCtx.Lines = lines[low:high]
|
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
return errCtx, pos
|
2018-10-03 08:58:09 -04:00
|
|
|
}
|