Documentation
¶
Overview ¶
Package cite manages snippets in your godoc.
Example ¶
Here's a snippet from the LICENSE file in this project.
Reference: https://github.com/mmcloughlin/cite/blob/master/LICENSE#L5-L10
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Index ¶
- Constants
- Variables
- func CanonicalAction(s string) string
- func Fetch(r Resource) ([]string, error)
- func FormatSnippet(lines []string) []string
- func InsertHandler(r Resource, lines []string) ([]string, []string, error)
- func ReadLineSelection(r io.Reader, lines LinePredicate) ([]string, error)
- func SkipReferenceHandler(r Resource, lines []string) ([]string, []string, error)
- type Citation
- type CodeBlock
- type CommentBlock
- type Directive
- type GithubResource
- type Handler
- type LinePredicate
- type LineRange
- type LineSelection
- type PlainResource
- type Processor
- type Resource
- type ResourceBuilder
- type Source
Examples ¶
Constants ¶
const ( GithubScheme = "https" GithubHost = "github.com" )
Github URL parameters.
Variables ¶
var ( ErrGithubMalformedFragment = errors.New("cite: malformed github line range fragment") ErrGithubWrongScheme = errors.New("cite: incorrect scheme for github url") ErrGithubShortPath = errors.New("cite: github url path is too short") ErrGithubNotBlob = errors.New("cite: expected blob github url") ErrGithubMissingFragment = errors.New("cite: missing fragment in github url") )
Types of errors from parsing a github citation.
var ( // ErrBadLineRange is returned when bad upper and lower bounds are used to // construct a LineRange. ErrBadLineRange = errors.New("cite: bad line range") // ErrBadLineRangeFormat is returned for a parsing error. ErrBadLineRangeFormat = errors.New("cite: bad line range format") )
var ErrFetchNonStatusOK = errors.New("cite: non-200 HTTP response")
ErrFetchNonStatusOK is returned from Fetch on a non-200 HTTP status code.
var ErrShortReference = errors.New("cite: existing reference too short")
ErrShortReference occurs when the snippet following a reference is too short. That is, it does not contain as many lines as the reference specifies.
var ErrUnknownAction = errors.New("cite: unknown action")
ErrUnknownAction is returned when an action is encountered for which no handlers are registered.
var ErrUnknownResource = errors.New("cite: unknown resource type")
ErrUnknownResource is returned when the Processor is unable to map a Citation to the Resource it references.
Functions ¶
func CanonicalAction ¶
CanonicalAction returns the canonical casing of an action string. This allows for arbitrary casing in the godoc.
func FormatSnippet ¶
FormatSnippet formats the raw lines into the lines to be inserted into the comment. This includes indendation and blank lines above and below.
Example ¶
snippet := FormatSnippet([]string{"Hello,", "World!"})
for _, line := range snippet {
fmt.Printf(">%s\n", line)
}
Output: > > Hello, > World! >
func InsertHandler ¶
InsertHandler handles "insert" actions by fetching and inserting the snippet into the comment below.
func ReadLineSelection ¶
func ReadLineSelection(r io.Reader, lines LinePredicate) ([]string, error)
ReadLineSelection extracts the lines from r for which the given LinePredicate is true.
Types ¶
type Citation ¶
Citation is a reference to an external resource, perhaps specifying a particular segment of it with some extra data.
type CodeBlock ¶
type CodeBlock struct {
CommentBlock CommentBlock
Code []string
}
CodeBlock represents arbitrary lines of Golang source code preceded by a CommentBlock.
type CommentBlock ¶
CommentBlock represents a Golang block comment with "//" syntax. All lines of the comment have the same bytes preeeding the "//", which we call the leader.
func (CommentBlock) String ¶
func (c CommentBlock) String() string
String converts the CommentBlock into source code.
Example ¶
c := CommentBlock{
Leader: "/*leader*/ ",
Lines: []string{
"Hello,",
"World!",
},
}
fmt.Printf("%s", c)
Output: /*leader*/ //Hello, /*leader*/ //World!
type Directive ¶
Directive represents a line in godoc to be processed by cite. This is an action together with a citation.
func ParseDirective ¶
ParseDirective tests the given line for a Directive and returns it if found.
type GithubResource ¶
type GithubResource struct {
User string
Repository string
GitRef string
Path string
LineRange LineRange
}
GithubResource represents a snippet on github such as:
https://github.com/mmcloughlin/geohash/blob/master/LICENSE#L3-L10
func (GithubResource) Cite ¶
func (r GithubResource) Cite() Citation
Cite returns a reference to the resource.
func (GithubResource) Lines ¶
func (r GithubResource) Lines() LineSelection
Lines returns the selection of lines in the snippet.
func (GithubResource) URL ¶
func (r GithubResource) URL() *url.URL
URL returns the URL of the full (raw) file.
type Handler ¶
Handler does something in response to a directive. It takes the referenced Resource and all remaining lines in the comment block. It returns lines to be inserted, lines remaining to be processed, and possibly an error.
type LinePredicate ¶
LinePredicate has a boolean function on line numbers, specifying some subset of interest.
type LineRange ¶
type LineRange struct {
// contains filtered or unexported fields
}
LineRange represents a range of one or more lines.
func NewLineRange ¶
NewLineRange returns a LineRange object for the lines between s and e (inclusive). It errors if the start and end do not make sense (e < s).
func NewSingleLine ¶
NewSingleLine returns a LineRange object for a single line.
func ParseLineRange ¶
ParseLineRange constructs a LineRange from a string representation. An example for the typical case is "3-17" for the LineRange between 3 and 17. A single digit is parsed into a LineRange for a single line.
func (LineRange) LineIncluded ¶
LineIncluded returns whether line n is included in the range.
type LineSelection ¶
type LineSelection interface {
LinePredicate
NumLines() int
}
LineSelection specifies a subset of lines of interest (like LinePredicate) of a known size.
type PlainResource ¶
PlainResource is a snippet from an arbitrary URL.
func (PlainResource) Cite ¶
func (p PlainResource) Cite() Citation
Cite returns a reference to the resource.
func (PlainResource) Lines ¶
func (p PlainResource) Lines() LineSelection
Lines returns the selection of lines in the snippet.
func (PlainResource) URL ¶
func (p PlainResource) URL() *url.URL
URL returns the URL of the whole file.
type Processor ¶
type Processor struct {
Handlers map[string]Handler
ResourceBuilders []ResourceBuilder
}
Processor modifies source code by performing some configured actions on citations found in comment blocks.
func NewProcessor ¶
func NewProcessor(builders []ResourceBuilder) Processor
NewProcessor constructs a new Processor. It takes a list of ResourceBuilder, which defines the types of references the Processor understands.
func (Processor) AddHandler ¶
AddHandler registers a handler for the given action type. Note casing of the action string does not matter.
type Resource ¶
type Resource interface {
URL() *url.URL
Cite() Citation
Lines() LineSelection
}
Resource is (a subset of) an external resource which can be cited.
func BuildGithubResourceFromCitation ¶
BuildGithubResourceFromCitation constructs a GithubResource from a reference to it.
func BuildPlainResourceFromCitation ¶
BuildPlainResourceFromCitation builds a PlainResource from a reference to it. The line range is represented in the Extra field of the Citation.
type ResourceBuilder ¶
ResourceBuilder is a function that constructs a resource from a reference to it.