Documentation
¶
Overview ¶
Package link finds the things in a piece of text that point somewhere.
It reports byte ranges and normalized destinations without deciding how a caller presents or opens them. Detection is a surprising amount of care for what looks like two regular expressions.
Two kinds, because they are two things ¶
A URL and a file path are not one destination with two spellings. A URL is opened by a browser; a file is opened by an editor, potentially at a line and column.
What is not here ¶
Opening one. Which browser, which editor, whether the process is sandboxed, whether a detected path should be opened at all — none of that belongs to detection. The answer is a byte range and a destination; what happens next is the caller's.
Nor is the filesystem. This package reads text and nothing else, which is why the one rule that needs the filesystem takes it as an argument — see Detect.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Link ¶
type Link struct {
// Start and End are the byte range of the text that was matched, so a caller can
// attach the destination to its own representation of that range.
Start, End int
// Kind says which sort of destination Target is.
Kind Kind
// Target is the destination. It is not always the matched text: a bare host is
// given the scheme it was written without, a quoted path loses its quotes, and a
// line and column suffix is taken off and reported separately.
Target string
// Line and Column are where in a file the reference points, counting from one, or
// zero when it did not say. Both are always zero for a URL.
//
// "src/main.go:42" is the shape a compiler, a stack trace and a model all use, and
// a link that opened the file at the top would throw away the useful half of what
// was written.
Line, Column int
}
Link is one thing in a piece of text that points somewhere.
type Links ¶ added in v0.0.5
type Links []Link
Links is an ordered, non-overlapping set of links detected in one text.
func Detect ¶
Detect finds every link in s, in the order it appears, as a byte range into s.
exists is given a path exactly as it was written, relative and unexpanded, and answers whether there is a file there. Nil asks nothing and leaves ambiguous bare filenames out, which is the right answer for text whose paths belong to somebody else's machine. URLs and self-evident rooted or qualified paths need no lookup.
It is an argument rather than a call into the operating system because this package reads text. A library that quietly stat'd every word of a model's output would be doing something no reader of its documentation had reason to expect.