shithub: hugo

Download patch

ref: 1d18eb0574a57c3e9f468659d076a666a3dd76f2
parent: 33a7b36fd42ee31dd79115ec6639bed24247332f
author: Bjørn Erik Pedersen <[email protected]>
date: Thu Nov 1 07:28:30 EDT 2018

Add file (line/col) info to ref/relref errors

See #5371

--- a/commands/server_errors.go
+++ b/commands/server_errors.go
@@ -72,7 +72,7 @@
 		<main>
 			{{ highlight .Error "apl" "noclasses=true,style=monokai" }}
 			{{ with .File }}
-			{{ $params := printf "noclasses=true,style=monokai,linenos=table,hl_lines=%d,linenostart=%d" (add .Pos 1) (sub .LineNumber .Pos) }}
+			{{ $params := printf "noclasses=true,style=monokai,linenos=table,hl_lines=%d,linenostart=%d" (add .LinesPos 1) (sub .LineNumber .LinesPos) }}
 			{{ $lexer := .ChromaLexer | default "go-html-template" }}
 			{{  highlight (delimit .Lines "\n") $lexer $params }}
 			{{ end }}
--- a/common/herrors/error_locator.go
+++ b/common/herrors/error_locator.go
@@ -15,72 +15,21 @@
 package herrors
 
 import (
-	"fmt"
 	"io"
 	"io/ioutil"
-	"os"
 	"strings"
 
-	"github.com/gohugoio/hugo/common/terminal"
+	"github.com/gohugoio/hugo/common/text"
 	"github.com/gohugoio/hugo/helpers"
 
 	"github.com/spf13/afero"
 )
 
-var fileErrorFormatFunc func(e ErrorContext) string
-
-func createFileLogFormatter(formatStr string) func(e ErrorContext) string {
-
-	if formatStr == "" {
-		formatStr = "\":file::line::col\""
-	}
-
-	var identifiers = []string{":file", ":line", ":col"}
-	var identifiersFound []string
-
-	for i := range formatStr {
-		for _, id := range identifiers {
-			if strings.HasPrefix(formatStr[i:], id) {
-				identifiersFound = append(identifiersFound, id)
-			}
-		}
-	}
-
-	replacer := strings.NewReplacer(":file", "%s", ":line", "%d", ":col", "%d")
-	format := replacer.Replace(formatStr)
-
-	f := func(e ErrorContext) string {
-		args := make([]interface{}, len(identifiersFound))
-		for i, id := range identifiersFound {
-			switch id {
-			case ":file":
-				args[i] = e.Filename
-			case ":line":
-				args[i] = e.LineNumber
-			case ":col":
-				args[i] = e.ColumnNumber
-			}
-		}
-
-		msg := fmt.Sprintf(format, args...)
-
-		if terminal.IsTerminal(os.Stdout) {
-			return terminal.Notice(msg)
-		}
-
-		return msg
-	}
-
-	return f
-}
-
-func init() {
-	fileErrorFormatFunc = createFileLogFormatter(os.Getenv("HUGO_FILE_LOG_FORMAT"))
-}
-
 // LineMatcher contains the elements used to match an error to a line
 type LineMatcher struct {
-	FileError  FileError
+	Position text.Position
+	Error    error
+
 	LineNumber int
 	Offset     int
 	Line       string
@@ -91,14 +40,14 @@
 
 // SimpleLineMatcher simply matches by line number.
 var SimpleLineMatcher = func(m LineMatcher) bool {
-	return m.FileError.LineNumber() == m.LineNumber
+	return m.Position.LineNumber == m.LineNumber
 }
 
+var _ text.Positioner = ErrorContext{}
+
 // ErrorContext contains contextual information about an error. This will
 // typically be the lines surrounding some problem in a file.
 type ErrorContext struct {
-	// The source filename.
-	Filename string
 
 	// If a match will contain the matched line and up to 2 lines before and after.
 	// Will be empty if no match.
@@ -105,19 +54,20 @@
 	Lines []string
 
 	// The position of the error in the Lines above. 0 based.
-	Pos int
+	LinesPos int
 
-	// The linenumber in the source file from where the Lines start. Starting at 1.
-	LineNumber int
+	position text.Position
 
-	// The column number in the source file. Starting at 1.
-	ColumnNumber int
-
 	// The lexer to use for syntax highlighting.
 	// https://gohugo.io/content-management/syntax-highlighting/#list-of-chroma-highlighting-languages
 	ChromaLexer string
 }
 
+// Position returns the text position of this error.
+func (e ErrorContext) Position() text.Position {
+	return e.position
+}
+
 var _ causer = (*ErrorWithFileContext)(nil)
 
 // ErrorWithFileContext is an error with some additional file context related
@@ -128,7 +78,11 @@
 }
 
 func (e *ErrorWithFileContext) Error() string {
-	return fileErrorFormatFunc(e.ErrorContext) + ": " + e.cause.Error()
+	pos := e.Position()
+	if pos.IsValid() {
+		return pos.String() + ": " + e.cause.Error()
+	}
+	return e.cause.Error()
 }
 
 func (e *ErrorWithFileContext) Cause() error {
@@ -163,24 +117,27 @@
 
 	var errCtx ErrorContext
 
-	if le.Offset() != -1 {
+	posle := le.Position()
+
+	if posle.Offset != -1 {
 		errCtx = locateError(r, le, func(m LineMatcher) bool {
-			if le.Offset() >= m.Offset && le.Offset() < m.Offset+len(m.Line) {
-				fe := m.FileError
-				m.FileError = ToFileErrorWithOffset(fe, -fe.LineNumber()+m.LineNumber)
+			if posle.Offset >= m.Offset && posle.Offset < m.Offset+len(m.Line) {
+				lno := posle.LineNumber - m.Position.LineNumber + m.LineNumber
+				m.Position = text.Position{LineNumber: lno}
 			}
 			return matcher(m)
 		})
-
 	} else {
 		errCtx = locateError(r, le, matcher)
 	}
 
-	if errCtx.LineNumber == -1 {
+	pos := &errCtx.position
+
+	if pos.LineNumber == -1 {
 		return e, false
 	}
 
-	errCtx.Filename = realFilename
+	pos.Filename = realFilename
 
 	if le.Type() != "" {
 		errCtx.ChromaLexer = chromaLexerFromType(le.Type())
@@ -233,7 +190,7 @@
 		panic("must provide an error")
 	}
 
-	errCtx := ErrorContext{LineNumber: -1, ColumnNumber: 1, Pos: -1}
+	errCtx := ErrorContext{position: text.Position{LineNumber: -1, ColumnNumber: 1, Offset: -1}, LinesPos: -1}
 
 	b, err := ioutil.ReadAll(r)
 	if err != nil {
@@ -240,10 +197,13 @@
 		return errCtx
 	}
 
+	pos := &errCtx.position
+	lepos := le.Position()
+
 	lines := strings.Split(string(b), "\n")
 
-	if le != nil && le.ColumnNumber() >= 0 {
-		errCtx.ColumnNumber = le.ColumnNumber()
+	if le != nil && lepos.ColumnNumber >= 0 {
+		pos.ColumnNumber = lepos.ColumnNumber
 	}
 
 	lineNo := 0
@@ -252,13 +212,14 @@
 	for li, line := range lines {
 		lineNo = li + 1
 		m := LineMatcher{
-			FileError:  le,
+			Position:   le.Position(),
+			Error:      le,
 			LineNumber: lineNo,
 			Offset:     posBytes,
 			Line:       line,
 		}
-		if errCtx.Pos == -1 && matches(m) {
-			errCtx.LineNumber = lineNo
+		if errCtx.LinesPos == -1 && matches(m) {
+			pos.LineNumber = lineNo
 			break
 		}
 
@@ -265,19 +226,19 @@
 		posBytes += len(line)
 	}
 
-	if errCtx.LineNumber != -1 {
-		low := errCtx.LineNumber - 3
+	if pos.LineNumber != -1 {
+		low := pos.LineNumber - 3
 		if low < 0 {
 			low = 0
 		}
 
-		if errCtx.LineNumber > 2 {
-			errCtx.Pos = 2
+		if pos.LineNumber > 2 {
+			errCtx.LinesPos = 2
 		} else {
-			errCtx.Pos = errCtx.LineNumber - 1
+			errCtx.LinesPos = pos.LineNumber - 1
 		}
 
-		high := errCtx.LineNumber + 2
+		high := pos.LineNumber + 2
 		if high > len(lines) {
 			high = len(lines)
 		}
--- a/common/herrors/error_locator_test.go
+++ b/common/herrors/error_locator_test.go
@@ -21,18 +21,6 @@
 	"github.com/stretchr/testify/require"
 )
 
-func TestCreateFileLogFormatter(t *testing.T) {
-	assert := require.New(t)
-
-	ctx := ErrorContext{Filename: "/my/file.txt", LineNumber: 12, ColumnNumber: 13}
-
-	assert.Equal("/my/file.txt|13|12", createFileLogFormatter(":file|:col|:line")(ctx))
-	assert.Equal("13|/my/file.txt|12", createFileLogFormatter(":col|:file|:line")(ctx))
-	assert.Equal("好:13", createFileLogFormatter("好::col")(ctx))
-	assert.Equal("\"/my/file.txt:12:13\"", createFileLogFormatter("")(ctx))
-
-}
-
 func TestErrorLocator(t *testing.T) {
 	assert := require.New(t)
 
@@ -53,8 +41,9 @@
 	location := locateErrorInString(lines, lineMatcher)
 	assert.Equal([]string{"LINE 3", "LINE 4", "This is THEONE", "LINE 6", "LINE 7"}, location.Lines)
 
-	assert.Equal(5, location.LineNumber)
-	assert.Equal(2, location.Pos)
+	pos := location.Position()
+	assert.Equal(5, pos.LineNumber)
+	assert.Equal(2, location.LinesPos)
 
 	assert.Equal([]string{"This is THEONE"}, locateErrorInString(`This is THEONE`, lineMatcher).Lines)
 
@@ -62,14 +51,14 @@
 This is THEONE
 L2
 `, lineMatcher)
-	assert.Equal(2, location.LineNumber)
-	assert.Equal(1, location.Pos)
+	assert.Equal(2, location.Position().LineNumber)
+	assert.Equal(1, location.LinesPos)
 	assert.Equal([]string{"L1", "This is THEONE", "L2", ""}, location.Lines)
 
 	location = locateErrorInString(`This is THEONE
 L2
 `, lineMatcher)
-	assert.Equal(0, location.Pos)
+	assert.Equal(0, location.LinesPos)
 	assert.Equal([]string{"This is THEONE", "L2", ""}, location.Lines)
 
 	location = locateErrorInString(`L1
@@ -76,7 +65,7 @@
 This THEONE
 `, lineMatcher)
 	assert.Equal([]string{"L1", "This THEONE", ""}, location.Lines)
-	assert.Equal(1, location.Pos)
+	assert.Equal(1, location.LinesPos)
 
 	location = locateErrorInString(`L1
 L2
@@ -83,11 +72,11 @@
 This THEONE
 `, lineMatcher)
 	assert.Equal([]string{"L1", "L2", "This THEONE", ""}, location.Lines)
-	assert.Equal(2, location.Pos)
+	assert.Equal(2, location.LinesPos)
 
 	location = locateErrorInString("NO MATCH", lineMatcher)
-	assert.Equal(-1, location.LineNumber)
-	assert.Equal(-1, location.Pos)
+	assert.Equal(-1, location.Position().LineNumber)
+	assert.Equal(-1, location.LinesPos)
 	assert.Equal(0, len(location.Lines))
 
 	lineMatcher = func(m LineMatcher) bool {
@@ -106,8 +95,8 @@
 J`, lineMatcher)
 
 	assert.Equal([]string{"D", "E", "F", "G", "H"}, location.Lines)
-	assert.Equal(6, location.LineNumber)
-	assert.Equal(2, location.Pos)
+	assert.Equal(6, location.Position().LineNumber)
+	assert.Equal(2, location.LinesPos)
 
 	// Test match EOF
 	lineMatcher = func(m LineMatcher) bool {
@@ -120,8 +109,8 @@
 `, lineMatcher)
 
 	assert.Equal([]string{"B", "C", ""}, location.Lines)
-	assert.Equal(4, location.LineNumber)
-	assert.Equal(2, location.Pos)
+	assert.Equal(4, location.Position().LineNumber)
+	assert.Equal(2, location.LinesPos)
 
 	offsetMatcher := func(m LineMatcher) bool {
 		return m.Offset == 1
@@ -134,7 +123,7 @@
 E`, offsetMatcher)
 
 	assert.Equal([]string{"A", "B", "C", "D"}, location.Lines)
-	assert.Equal(2, location.LineNumber)
-	assert.Equal(1, location.Pos)
+	assert.Equal(2, location.Position().LineNumber)
+	assert.Equal(1, location.LinesPos)
 
 }
--- a/common/herrors/file_error.go
+++ b/common/herrors/file_error.go
@@ -16,10 +16,14 @@
 import (
 	"encoding/json"
 
+	"github.com/gohugoio/hugo/common/text"
+
 	"github.com/pkg/errors"
 )
 
-var _ causer = (*fileError)(nil)
+var (
+	_ causer = (*fileError)(nil)
+)
 
 // FileError represents an error when handling a file: Parsing a config file,
 // execute a template etc.
@@ -26,16 +30,8 @@
 type FileError interface {
 	error
 
-	// Offset gets the error location offset in bytes, starting at 0.
-	// It will return -1 if not provided.
-	Offset() int
+	text.Positioner
 
-	// LineNumber gets the error location, starting at line 1.
-	LineNumber() int
-
-	// Column number gets the column location, starting at 1.
-	ColumnNumber() int
-
 	// A string identifying the type of file, e.g. JSON, TOML, markdown etc.
 	Type() string
 }
@@ -43,35 +39,18 @@
 var _ FileError = (*fileError)(nil)
 
 type fileError struct {
-	offset       int
-	lineNumber   int
-	columnNumber int
-	fileType     string
+	position text.Position
 
+	fileType string
+
 	cause error
 }
 
-type fileErrorWithLineOffset struct {
-	FileError
-	offset int
+// Position returns the text position of this error.
+func (e fileError) Position() text.Position {
+	return e.position
 }
 
-func (e *fileErrorWithLineOffset) LineNumber() int {
-	return e.FileError.LineNumber() + e.offset
-}
-
-func (e *fileError) LineNumber() int {
-	return e.lineNumber
-}
-
-func (e *fileError) Offset() int {
-	return e.offset
-}
-
-func (e *fileError) ColumnNumber() int {
-	return e.columnNumber
-}
-
 func (e *fileError) Type() string {
 	return e.fileType
 }
@@ -89,7 +68,8 @@
 
 // NewFileError creates a new FileError.
 func NewFileError(fileType string, offset, lineNumber, columnNumber int, err error) FileError {
-	return &fileError{cause: err, fileType: fileType, offset: offset, lineNumber: lineNumber, columnNumber: columnNumber}
+	pos := text.Position{Offset: offset, LineNumber: lineNumber, ColumnNumber: columnNumber}
+	return &fileError{cause: err, fileType: fileType, position: pos}
 }
 
 // UnwrapFileError tries to unwrap a FileError from err.
@@ -111,7 +91,9 @@
 // ToFileErrorWithOffset will return a new FileError with a line number
 // with the given offset from the original.
 func ToFileErrorWithOffset(fe FileError, offset int) FileError {
-	return &fileErrorWithLineOffset{FileError: fe, offset: offset}
+	pos := fe.Position()
+	pos.LineNumber = pos.LineNumber + offset
+	return &fileError{cause: fe, fileType: fe.Type(), position: pos}
 }
 
 // ToFileError will convert the given error to an error supporting
@@ -123,6 +105,7 @@
 		if fileType == "" {
 			fileType = typ
 		}
+
 		if lno > 0 || offset != -1 {
 			return NewFileError(fileType, offset, lno, col, err)
 		}
--- a/common/herrors/file_error_test.go
+++ b/common/herrors/file_error_test.go
@@ -42,9 +42,6 @@
 	} {
 
 		got := ToFileError("template", test.in)
-		if test.offset > 0 {
-			got = ToFileErrorWithOffset(got.(FileError), test.offset)
-		}
 
 		errMsg := fmt.Sprintf("[%d][%T]", i, got)
 		le, ok := got.(FileError)
@@ -51,8 +48,9 @@
 		assert.True(ok)
 
 		assert.True(ok, errMsg)
-		assert.Equal(test.lineNumber, le.LineNumber(), errMsg)
-		assert.Equal(test.columnNumber, le.ColumnNumber(), errMsg)
+		pos := le.Position()
+		assert.Equal(test.lineNumber, pos.LineNumber, errMsg)
+		assert.Equal(test.columnNumber, pos.ColumnNumber, errMsg)
 		assert.Error(errors.Cause(got))
 	}
 
--- /dev/null
+++ b/common/text/position.go
@@ -1,0 +1,99 @@
+// 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
+// limitations under the License.
+
+package text
+
+import (
+	"fmt"
+	"os"
+	"strings"
+
+	"github.com/gohugoio/hugo/common/terminal"
+)
+
+// Positioner represents a thing that knows its position in a text file or stream,
+// typically an error.
+type Positioner interface {
+	Position() Position
+}
+
+// Position holds a source position in a text file or stream.
+type Position struct {
+	Filename     string // filename, if any
+	Offset       int    // byte offset, starting at 0. It's set to -1 if not provided.
+	LineNumber   int    // line number, starting at 1
+	ColumnNumber int    // column number, starting at 1 (character count per line)
+}
+
+func (pos Position) String() string {
+	if pos.Filename == "" {
+		pos.Filename = "<stream>"
+	}
+	return positionStringFormatfunc(pos)
+}
+
+// IsValid returns true if line number is > 0.
+func (pos Position) IsValid() bool {
+	return pos.LineNumber > 0
+}
+
+var positionStringFormatfunc func(p Position) string
+
+func createPositionStringFormatter(formatStr string) func(p Position) string {
+
+	if formatStr == "" {
+		formatStr = "\":file::line::col\""
+	}
+
+	var identifiers = []string{":file", ":line", ":col"}
+	var identifiersFound []string
+
+	for i := range formatStr {
+		for _, id := range identifiers {
+			if strings.HasPrefix(formatStr[i:], id) {
+				identifiersFound = append(identifiersFound, id)
+			}
+		}
+	}
+
+	replacer := strings.NewReplacer(":file", "%s", ":line", "%d", ":col", "%d")
+	format := replacer.Replace(formatStr)
+
+	f := func(pos Position) string {
+		args := make([]interface{}, len(identifiersFound))
+		for i, id := range identifiersFound {
+			switch id {
+			case ":file":
+				args[i] = pos.Filename
+			case ":line":
+				args[i] = pos.LineNumber
+			case ":col":
+				args[i] = pos.ColumnNumber
+			}
+		}
+
+		msg := fmt.Sprintf(format, args...)
+
+		if terminal.IsTerminal(os.Stdout) {
+			return terminal.Notice(msg)
+		}
+
+		return msg
+	}
+
+	return f
+}
+
+func init() {
+	positionStringFormatfunc = createPositionStringFormatter(os.Getenv("HUGO_FILE_LOG_FORMAT"))
+}
--- /dev/null
+++ b/common/text/position_test.go
@@ -1,0 +1,33 @@
+// 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
+// limitations under the License.
+
+package text
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestPositionStringFormatter(t *testing.T) {
+	assert := require.New(t)
+
+	pos := Position{Filename: "/my/file.txt", LineNumber: 12, ColumnNumber: 13, Offset: 14}
+
+	assert.Equal("/my/file.txt|13|12", createPositionStringFormatter(":file|:col|:line")(pos))
+	assert.Equal("13|/my/file.txt|12", createPositionStringFormatter(":col|:file|:line")(pos))
+	assert.Equal("好:13", createPositionStringFormatter("好::col")(pos))
+	assert.Equal("\"/my/file.txt:12:13\"", createPositionStringFormatter("")(pos))
+	assert.Equal("\"/my/file.txt:12:13\"", pos.String())
+
+}
--- /dev/null
+++ b/common/urls/ref.go
@@ -1,0 +1,22 @@
+// 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
+// limitations under the License.
+
+package urls
+
+// RefLinker is implemented by those who support reference linking.
+// args must contain a path, but can also point to the target
+// language or output format.
+type RefLinker interface {
+	Ref(args map[string]interface{}) (string, error)
+	RelRef(args map[string]interface{}) (string, error)
+}
--- a/go.mod
+++ b/go.mod
@@ -12,6 +12,7 @@
 	github.com/bep/debounce v1.1.0
 	github.com/bep/gitmap v1.0.0
 	github.com/bep/go-tocss v0.5.0
+	github.com/bep/mapstructure v0.0.0-20180511142126-bb74f1db0675
 	github.com/chaseadamsio/goorgeous v1.1.0
 	github.com/cpuguy83/go-md2man v1.0.8 // indirect
 	github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
--- a/go.sum
+++ b/go.sum
@@ -20,6 +20,8 @@
 github.com/bep/gitmap v1.0.0/go.mod h1:g9VRETxFUXNWzMiuxOwcudo6DfZkW9jOsOW0Ft4kYaY=
 github.com/bep/go-tocss v0.5.0 h1:yDIYy1G9hWi7KVTJjvPPDVZB7gxXRRRiv3ds5mqAaCY=
 github.com/bep/go-tocss v0.5.0/go.mod h1:c/+hEVoVvkufrV9Is/CPRHWGGdpcTwNuB48hfxzyYBI=
+github.com/bep/mapstructure v0.0.0-20180511142126-bb74f1db0675 h1:FsEl9Z/kzas/wM6yhI0AQ0H+hHOL0b5EERNs3N5KYcU=
+github.com/bep/mapstructure v0.0.0-20180511142126-bb74f1db0675/go.mod h1:i5WrwxccbJYFpJIcQxfKglzwXT0NE71ElOzBJrO0ODE=
 github.com/chaseadamsio/goorgeous v1.1.0 h1:J9UrYDhzucUMHXsCKG+kICvpR5dT1cqZdVFTYvSlUBk=
 github.com/chaseadamsio/goorgeous v1.1.0/go.mod h1:6QaC0vFoKWYDth94dHFNgRT2YkT5FHdQp/Yx15aAAi0=
 github.com/cpuguy83/go-md2man v1.0.8 h1:DwoNytLphI8hzS2Af4D0dfaEaiSq2bN05mEm4R6vf8M=
--- a/hugolib/hugo_sites_build_errors_test.go
+++ b/hugolib/hugo_sites_build_errors_test.go
@@ -25,7 +25,7 @@
 
 func (t testSiteBuildErrorAsserter) assertLineNumber(lineNumber int, err error) {
 	fe := t.getFileError(err)
-	t.assert.Equal(lineNumber, fe.LineNumber, fmt.Sprintf("[%s]  got => %s\n%s", t.name, fe, trace()))
+	t.assert.Equal(lineNumber, fe.Position().LineNumber, fmt.Sprintf("[%s]  got => %s\n%s", t.name, fe, trace()))
 }
 
 func (t testSiteBuildErrorAsserter) assertErrorMessage(e1, e2 string) {
@@ -42,6 +42,7 @@
 	const (
 		yamlcontent = "yamlcontent"
 		tomlcontent = "tomlcontent"
+		jsoncontent = "jsoncontent"
 		shortcode   = "shortcode"
 		base        = "base"
 		single      = "single"
@@ -86,8 +87,8 @@
 			},
 			assertCreateError: func(a testSiteBuildErrorAsserter, err error) {
 				fe := a.getFileError(err)
-				assert.Equal(5, fe.LineNumber)
-				assert.Equal(1, fe.ColumnNumber)
+				assert.Equal(5, fe.Position().LineNumber)
+				assert.Equal(1, fe.Position().ColumnNumber)
 				assert.Equal("go-html-template", fe.ChromaLexer)
 				a.assertErrorMessage("\"layouts/_default/single.html:5:1\": parse failed: template: _default/single.html:5: unexpected \"}\" in operand", fe.Error())
 
@@ -101,8 +102,8 @@
 			},
 			assertBuildError: func(a testSiteBuildErrorAsserter, err error) {
 				fe := a.getFileError(err)
-				assert.Equal(5, fe.LineNumber)
-				assert.Equal(14, fe.ColumnNumber)
+				assert.Equal(5, fe.Position().LineNumber)
+				assert.Equal(14, fe.Position().ColumnNumber)
 				assert.Equal("go-html-template", fe.ChromaLexer)
 				a.assertErrorMessage("\"layouts/_default/single.html:5:14\": execute of template failed", fe.Error())
 
@@ -116,8 +117,8 @@
 			},
 			assertBuildError: func(a testSiteBuildErrorAsserter, err error) {
 				fe := a.getFileError(err)
-				assert.Equal(5, fe.LineNumber)
-				assert.Equal(14, fe.ColumnNumber)
+				assert.Equal(5, fe.Position().LineNumber)
+				assert.Equal(14, fe.Position().ColumnNumber)
 				assert.Equal("go-html-template", fe.ChromaLexer)
 				a.assertErrorMessage("\"layouts/_default/single.html:5:14\": execute of template failed", fe.Error())
 
@@ -141,7 +142,7 @@
 			},
 			assertBuildError: func(a testSiteBuildErrorAsserter, err error) {
 				fe := a.getFileError(err)
-				assert.Equal(7, fe.LineNumber)
+				assert.Equal(7, fe.Position().LineNumber)
 				assert.Equal("md", fe.ChromaLexer)
 				// Make sure that it contains both the content file and template
 				a.assertErrorMessage(`content/myyaml.md:7:10": failed to render shortcode "sc"`, fe.Error())
@@ -156,8 +157,8 @@
 			},
 			assertBuildError: func(a testSiteBuildErrorAsserter, err error) {
 				fe := a.getFileError(err)
-				assert.Equal(7, fe.LineNumber)
-				assert.Equal(14, fe.ColumnNumber)
+				assert.Equal(7, fe.Position().LineNumber)
+				assert.Equal(14, fe.Position().ColumnNumber)
 				assert.Equal("md", fe.ChromaLexer)
 				a.assertErrorMessage("\"content/myyaml.md:7:14\": failed to extract shortcode: template for shortcode \"nono\" not found", fe.Error())
 			},
@@ -180,7 +181,7 @@
 			},
 			assertBuildError: func(a testSiteBuildErrorAsserter, err error) {
 				fe := a.getFileError(err)
-				assert.Equal(6, fe.LineNumber)
+				assert.Equal(6, fe.Position().LineNumber)
 				assert.Equal("toml", fe.ErrorContext.ChromaLexer)
 
 			},
@@ -187,7 +188,7 @@
 		},
 		{
 			name:     "Invalid JSON front matter",
-			fileType: tomlcontent,
+			fileType: jsoncontent,
 			fileFixer: func(content string) string {
 				return strings.Replace(content, "\"description\":", "\"description\"", 1)
 			},
@@ -194,7 +195,7 @@
 			assertBuildError: func(a testSiteBuildErrorAsserter, err error) {
 				fe := a.getFileError(err)
 
-				assert.Equal(3, fe.LineNumber)
+				assert.Equal(3, fe.Position().LineNumber)
 				assert.Equal("json", fe.ErrorContext.ChromaLexer)
 
 			},
@@ -212,8 +213,8 @@
 				// This is fixed in latest Go source
 				if strings.Contains(runtime.Version(), "devel") {
 					fe := a.getFileError(err)
-					assert.Equal(5, fe.LineNumber)
-					assert.Equal(21, fe.ColumnNumber)
+					assert.Equal(5, fe.Position().LineNumber)
+					assert.Equal(21, fe.Position().ColumnNumber)
 				} else {
 					assert.Contains(err.Error(), `execute of template failed: panic in Execute`)
 				}
@@ -286,7 +287,7 @@
 
 `))
 
-		b.WithContent("myjson.md", f(tomlcontent, `{
+		b.WithContent("myjson.md", f(jsoncontent, `{
 	"title": "This is a title",
 	"description": "This is a description."
 }
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -21,8 +21,8 @@
 	"reflect"
 
 	"github.com/gohugoio/hugo/common/maps"
+	"github.com/gohugoio/hugo/common/urls"
 	"github.com/gohugoio/hugo/media"
-	_errors "github.com/pkg/errors"
 
 	"github.com/gohugoio/hugo/langs"
 
@@ -67,8 +67,16 @@
 
 	// Assert that it implements the interface needed for related searches.
 	_ related.Document = (*Page)(nil)
+
+	// Page supports ref and relref
+	_ urls.RefLinker = (*Page)(nil)
 )
 
+// Wraps a Page.
+type pageContainer interface {
+	page() *Page
+}
+
 const (
 	KindPage = "page"
 
@@ -1861,79 +1869,6 @@
 // an error if the ref is ambiguous.
 func (p *Page) GetPage(ref string) (*Page, error) {
 	return p.s.getPageNew(p, ref)
-}
-
-type refArgs struct {
-	Path         string
-	Lang         string
-	OutputFormat string
-}
-
-func (p *Page) decodeRefArgs(args map[string]interface{}) (refArgs, *SiteInfo, error) {
-	var ra refArgs
-	err := mapstructure.WeakDecode(args, &ra)
-	if err != nil {
-		return ra, nil, nil
-	}
-	s := p.Site
-
-	if ra.Lang != "" && ra.Lang != p.Lang() {
-		// Find correct site
-		found := false
-		for _, ss := range p.s.owner.Sites {
-			if ss.Lang() == ra.Lang {
-				found = true
-				s = &ss.Info
-			}
-		}
-
-		if !found {
-			p.s.siteRefLinker.logNotFound(ra.Path, fmt.Sprintf("no site found with lang %q", ra.Lang), p)
-			return ra, nil, nil
-		}
-	}
-
-	return ra, s, nil
-}
-
-func (p *Page) Ref(argsm map[string]interface{}) (string, error) {
-	args, s, err := p.decodeRefArgs(argsm)
-	if err != nil {
-		return "", _errors.Wrap(err, "invalid arguments to Ref")
-	}
-
-	if s == nil {
-		return p.s.siteRefLinker.notFoundURL, nil
-	}
-
-	if args.Path == "" {
-		return "", nil
-	}
-
-	if args.OutputFormat != "" {
-		return s.Ref(args.Path, p, args.OutputFormat)
-	}
-	return s.Ref(args.Path, p)
-}
-
-func (p *Page) RelRef(argsm map[string]interface{}) (string, error) {
-	args, s, err := p.decodeRefArgs(argsm)
-	if err != nil {
-		return "", _errors.Wrap(err, "invalid arguments to Ref")
-	}
-
-	if s == nil {
-		return p.s.siteRefLinker.notFoundURL, nil
-	}
-
-	if args.Path == "" {
-		return "", nil
-	}
-
-	if args.OutputFormat != "" {
-		return s.RelRef(args.Path, p, args.OutputFormat)
-	}
-	return s.RelRef(args.Path, p)
 }
 
 func (p *Page) String() string {
--- a/hugolib/page_content.go
+++ b/hugolib/page_content.go
@@ -17,13 +17,12 @@
 	"bytes"
 	"io"
 
-	"github.com/gohugoio/hugo/source"
-
 	errors "github.com/pkg/errors"
 
 	bp "github.com/gohugoio/hugo/bufferpool"
 
 	"github.com/gohugoio/hugo/common/herrors"
+	"github.com/gohugoio/hugo/common/text"
 	"github.com/gohugoio/hugo/parser/metadecoders"
 	"github.com/gohugoio/hugo/parser/pageparser"
 )
@@ -206,13 +205,13 @@
 
 }
 
-func (p *Page) posFromInput(input []byte, offset int) source.Position {
+func (p *Page) posFromInput(input []byte, offset int) text.Position {
 	lf := []byte("\n")
 	input = input[:offset]
 	lineNumber := bytes.Count(input, lf) + 1
 	endOfLastLine := bytes.LastIndex(input, lf)
 
-	return source.Position{
+	return text.Position{
 		Filename:     p.pathOrTitle(),
 		LineNumber:   lineNumber,
 		ColumnNumber: offset - endOfLastLine,
@@ -220,6 +219,6 @@
 	}
 }
 
-func (p *Page) posFromPage(offset int) source.Position {
+func (p *Page) posFromPage(offset int) text.Position {
 	return p.posFromInput(p.source.parsed.Input(), offset)
 }
--- /dev/null
+++ b/hugolib/page_ref.go
@@ -1,0 +1,100 @@
+// 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
+// limitations under the License.
+
+package hugolib
+
+import (
+	"fmt"
+
+	"github.com/gohugoio/hugo/common/text"
+
+	"github.com/bep/mapstructure"
+	"github.com/pkg/errors"
+)
+
+type refArgs struct {
+	Path         string
+	Lang         string
+	OutputFormat string
+}
+
+func (p *Page) decodeRefArgs(args map[string]interface{}) (refArgs, *Site, error) {
+	var ra refArgs
+	err := mapstructure.WeakDecode(args, &ra)
+	if err != nil {
+		return ra, nil, nil
+	}
+	s := p.s
+
+	if ra.Lang != "" && ra.Lang != p.Lang() {
+		// Find correct site
+		found := false
+		for _, ss := range p.s.owner.Sites {
+			if ss.Lang() == ra.Lang {
+				found = true
+				s = ss
+			}
+		}
+
+		if !found {
+			p.s.siteRefLinker.logNotFound(ra.Path, fmt.Sprintf("no site found with lang %q", ra.Lang), p, text.Position{})
+			return ra, nil, nil
+		}
+	}
+
+	return ra, s, nil
+}
+
+func (p *Page) Ref(argsm map[string]interface{}) (string, error) {
+	return p.ref(argsm, p)
+}
+
+func (p *Page) ref(argsm map[string]interface{}, source interface{}) (string, error) {
+	args, s, err := p.decodeRefArgs(argsm)
+	if err != nil {
+		return "", errors.Wrap(err, "invalid arguments to Ref")
+	}
+
+	if s == nil {
+		return p.s.siteRefLinker.notFoundURL, nil
+	}
+
+	if args.Path == "" {
+		return "", nil
+	}
+
+	return s.refLink(args.Path, source, false, args.OutputFormat)
+
+}
+
+func (p *Page) RelRef(argsm map[string]interface{}) (string, error) {
+	return p.relRef(argsm, p)
+}
+
+func (p *Page) relRef(argsm map[string]interface{}, source interface{}) (string, error) {
+	args, s, err := p.decodeRefArgs(argsm)
+	if err != nil {
+		return "", errors.Wrap(err, "invalid arguments to Ref")
+	}
+
+	if s == nil {
+		return p.s.siteRefLinker.notFoundURL, nil
+	}
+
+	if args.Path == "" {
+		return "", nil
+	}
+
+	return s.refLink(args.Path, source, true, args.OutputFormat)
+
+}
--- a/hugolib/shortcode.go
+++ b/hugolib/shortcode.go
@@ -19,8 +19,6 @@
 	"fmt"
 	"html/template"
 
-	"github.com/gohugoio/hugo/source"
-
 	"reflect"
 
 	"regexp"
@@ -34,6 +32,8 @@
 	"sync"
 
 	"github.com/gohugoio/hugo/common/maps"
+	"github.com/gohugoio/hugo/common/text"
+	"github.com/gohugoio/hugo/common/urls"
 	"github.com/gohugoio/hugo/output"
 
 	"github.com/gohugoio/hugo/media"
@@ -43,6 +43,12 @@
 	"github.com/gohugoio/hugo/tpl"
 )
 
+var (
+	_ urls.RefLinker  = (*ShortcodeWithPage)(nil)
+	_ pageContainer   = (*ShortcodeWithPage)(nil)
+	_ text.Positioner = (*ShortcodeWithPage)(nil)
+)
+
 // ShortcodeWithPage is the "." context in a shortcode template.
 type ShortcodeWithPage struct {
 	Params        interface{}
@@ -58,7 +64,7 @@
 	// pos is the position in bytes in the source file. Used for error logging.
 	posInit   sync.Once
 	posOffset int
-	pos       source.Position
+	pos       text.Position
 
 	scratch *maps.Scratch
 }
@@ -65,7 +71,7 @@
 
 // Position returns this shortcode's detailed position. Note that this information
 // may be expensive to calculate, so only use this in error situations.
-func (scp *ShortcodeWithPage) Position() source.Position {
+func (scp *ShortcodeWithPage) Position() text.Position {
 	scp.posInit.Do(func() {
 		scp.pos = scp.Page.posFromPage(scp.posOffset)
 	})
@@ -77,14 +83,16 @@
 	return scp.Page.Site
 }
 
-// Ref is a shortcut to the Ref method on Page.
+// Ref is a shortcut to the Ref method on Page. It passes itself as a context
+// to get better error messages.
 func (scp *ShortcodeWithPage) Ref(args map[string]interface{}) (string, error) {
-	return scp.Page.Ref(args)
+	return scp.Page.ref(args, scp)
 }
 
-// RelRef is a shortcut to the RelRef method on Page.
+// RelRef is a shortcut to the RelRef method on Page. It passes itself as a context
+// to get better error messages.
 func (scp *ShortcodeWithPage) RelRef(args map[string]interface{}) (string, error) {
-	return scp.Page.RelRef(args)
+	return scp.Page.relRef(args, scp)
 }
 
 // Scratch returns a scratch-pad scoped for this shortcode. This can be used
@@ -145,6 +153,10 @@
 		return x
 	}
 
+}
+
+func (scp *ShortcodeWithPage) page() *Page {
+	return scp.Page.Page
 }
 
 // Note - this value must not contain any markup syntax
--- a/hugolib/shortcode_test.go
+++ b/hugolib/shortcode_test.go
@@ -1056,9 +1056,9 @@
 	assert.Equal(1, len(s.RegularPages))
 
 	builder.AssertFileContent("public/page/index.html",
-		"File: content/page.md",
+		filepath.FromSlash("File: content/page.md"),
 		"Line: 7", "Column: 4", "Offset: 40",
-		"String: content/page.md:7:4",
+		filepath.FromSlash("String: \"content/page.md:7:4\""),
 	)
 
 }
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -28,6 +28,8 @@
 	"strings"
 	"time"
 
+	"github.com/gohugoio/hugo/common/text"
+
 	"github.com/gohugoio/hugo/hugofs"
 
 	"github.com/gohugoio/hugo/common/herrors"
@@ -493,17 +495,26 @@
 	return siteRefLinker{s: s, errorLogger: logger, notFoundURL: notFoundURL}, nil
 }
 
-func (s siteRefLinker) logNotFound(ref, what string, p *Page) {
-	if p == nil {
+func (s siteRefLinker) logNotFound(ref, what string, p *Page, position text.Position) {
+	if position.IsValid() {
+		s.errorLogger.Printf("[%s] REF_NOT_FOUND: Ref %q: %s: %s", s.s.Lang(), ref, position.String(), what)
+	} else if p == nil {
 		s.errorLogger.Printf("[%s] REF_NOT_FOUND: Ref %q: %s", s.s.Lang(), ref, what)
 	} else {
 		s.errorLogger.Printf("[%s] REF_NOT_FOUND: Ref %q from page %q: %s", s.s.Lang(), ref, p.pathOrTitle(), what)
 	}
-
 }
 
-func (s *siteRefLinker) refLink(ref string, page *Page, relative bool, outputFormat string) (string, error) {
+func (s *siteRefLinker) refLink(ref string, source interface{}, relative bool, outputFormat string) (string, error) {
 
+	var page *Page
+	switch v := source.(type) {
+	case *Page:
+		page = v
+	case pageContainer:
+		page = v.page()
+	}
+
 	var refURL *url.URL
 	var err error
 
@@ -520,14 +531,21 @@
 
 	if refURL.Path != "" {
 		target, err := s.s.getPageNew(page, refURL.Path)
+		var pos text.Position
+		if err != nil || target == nil {
+			if p, ok := source.(text.Positioner); ok {
+				pos = p.Position()
 
+			}
+		}
+
 		if err != nil {
-			s.logNotFound(refURL.Path, err.Error(), page)
+			s.logNotFound(refURL.Path, err.Error(), page, pos)
 			return s.notFoundURL, nil
 		}
 
 		if target == nil {
-			s.logNotFound(refURL.Path, "page not found", page)
+			s.logNotFound(refURL.Path, "page not found", page, pos)
 			return s.notFoundURL, nil
 		}
 
@@ -537,7 +555,7 @@
 			o := target.OutputFormats().Get(outputFormat)
 
 			if o == nil {
-				s.logNotFound(refURL.Path, fmt.Sprintf("output format %q", outputFormat), page)
+				s.logNotFound(refURL.Path, fmt.Sprintf("output format %q", outputFormat), page, pos)
 				return s.notFoundURL, nil
 			}
 			permalinker = o
--- a/source/position.go
+++ /dev/null
@@ -1,33 +1,0 @@
-// 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
-// limitations under the License.
-
-package source
-
-import "fmt"
-
-// Position holds a source position.
-type Position struct {
-	Filename     string // filename, if any
-	Offset       int    // byte offset, starting at 0
-	LineNumber   int    // line number, starting at 1
-	ColumnNumber int    // column number, starting at 1 (character count per line)
-}
-
-func (pos Position) String() string {
-	filename := pos.Filename
-	if filename == "" {
-		filename = "<stream>"
-	}
-	return fmt.Sprintf("%s:%d:%d", filename, pos.LineNumber, pos.ColumnNumber)
-
-}
--- a/tpl/template.go
+++ b/tpl/template.go
@@ -165,7 +165,7 @@
 	// Since this can be a composite of multiple template files (single.html + baseof.html etc.)
 	// we potentially need to look in both -- and cannot rely on line number alone.
 	lineMatcher := func(m herrors.LineMatcher) bool {
-		if m.FileError.LineNumber() != m.LineNumber {
+		if m.Position.LineNumber != m.LineNumber {
 			return false
 		}
 		if !hasMaster {
@@ -172,7 +172,7 @@
 			return true
 		}
 
-		identifiers := t.extractIdentifiers(m.FileError.Error())
+		identifiers := t.extractIdentifiers(m.Error.Error())
 
 		for _, id := range identifiers {
 			if strings.Contains(m.Line, id) {
--- a/tpl/tplimpl/embedded/templates.autogen.go
+++ b/tpl/tplimpl/embedded/templates.autogen.go
@@ -398,8 +398,8 @@
 </style>
 {{ end }}
 {{ end }}`},
-	{`shortcodes/ref.html`, `{{ ref .Page .Params }}`},
-	{`shortcodes/relref.html`, `{{ relref .Page .Params }}`},
+	{`shortcodes/ref.html`, `{{ ref . .Params }}`},
+	{`shortcodes/relref.html`, `{{ relref . .Params }}`},
 	{`shortcodes/twitter.html`, `{{- $pc := .Page.Site.Config.Privacy.Twitter -}}
 {{- if not $pc.Disable -}}
 {{- if $pc.Simple -}}
--- a/tpl/tplimpl/embedded/templates/shortcodes/ref.html
+++ b/tpl/tplimpl/embedded/templates/shortcodes/ref.html
@@ -1,1 +1,1 @@
-{{ ref .Page .Params }}
\ No newline at end of file
+{{ ref . .Params }}
\ No newline at end of file
--- a/tpl/tplimpl/embedded/templates/shortcodes/relref.html
+++ b/tpl/tplimpl/embedded/templates/shortcodes/relref.html
@@ -1,1 +1,1 @@
-{{ relref .Page .Params }}
\ No newline at end of file
+{{ relref . .Params }}
\ No newline at end of file
--- a/tpl/urls/urls.go
+++ b/tpl/urls/urls.go
@@ -20,10 +20,10 @@
 	"html/template"
 	"net/url"
 
+	"github.com/gohugoio/hugo/common/urls"
+	"github.com/gohugoio/hugo/deps"
 	_errors "github.com/pkg/errors"
 	"github.com/russross/blackfriday"
-
-	"github.com/gohugoio/hugo/deps"
 	"github.com/spf13/cast"
 )
 
@@ -91,14 +91,9 @@
 	return blackfriday.SanitizedAnchorName(s), nil
 }
 
-type reflinker interface {
-	Ref(args map[string]interface{}) (string, error)
-	RelRef(args map[string]interface{}) (string, error)
-}
-
 // Ref returns the absolute URL path to a given content item.
 func (ns *Namespace) Ref(in interface{}, args interface{}) (template.HTML, error) {
-	p, ok := in.(reflinker)
+	p, ok := in.(urls.RefLinker)
 	if !ok {
 		return "", errors.New("invalid Page received in Ref")
 	}
@@ -112,7 +107,7 @@
 
 // RelRef returns the relative URL path to a given content item.
 func (ns *Namespace) RelRef(in interface{}, args interface{}) (template.HTML, error) {
-	p, ok := in.(reflinker)
+	p, ok := in.(urls.RefLinker)
 	if !ok {
 		return "", errors.New("invalid Page received in RelRef")
 	}