cite

package module
v0.0.0-...-d9d574f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2023 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

cite

Cite snippets in your godoc

go.dev Reference Build status Coverage Go Report Card

Install

go install github.com/mmcloughlin/cite/cmd/cite

Usage

To reference something, add an insert line into your godoc.

package example

import "fmt"

// Greet says hello to who.
//
// Insert: https://github.com/mmcloughlin/cite/blob/master/example/grinch.txt#L6-L8
func Greet(who string) {
	fmt.Printf("Hello, %s!\n", who)
}

Then run

$ cite process example.go

It will fetch the reference and insert it into your code.

package example

import "fmt"

// Greet says hello to who.
//
// Reference: https://github.com/mmcloughlin/cite/blob/master/example/grinch.txt#L6-L8
//
//	Every Who Down in Whoville Liked Christmas a lot...
//	But the Grinch,Who lived just north of Whoville, Did NOT!
//	The Grinch hated Christmas! The whole Christmas season!
//
func Greet(who string) {
	fmt.Printf("Hello, %s!\n", who)
}

It should look nice in your godoc.

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

Examples

Constants

View Source
const (
	GithubScheme = "https"
	GithubHost   = "github.com"
)

Github URL parameters.

Variables

View Source
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.

View Source
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")
)
View Source
var ErrFetchNonStatusOK = errors.New("cite: non-200 HTTP response")

ErrFetchNonStatusOK is returned from Fetch on a non-200 HTTP status code.

View Source
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.

View Source
var ErrUnknownAction = errors.New("cite: unknown action")

ErrUnknownAction is returned when an action is encountered for which no handlers are registered.

View Source
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

func CanonicalAction(s string) string

CanonicalAction returns the canonical casing of an action string. This allows for arbitrary casing in the godoc.

func Fetch

func Fetch(r Resource) ([]string, error)

Fetch downloads a resource and extracts the referenced snippet from it.

func FormatSnippet

func FormatSnippet(lines []string) []string

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

func InsertHandler(r Resource, lines []string) ([]string, []string, error)

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.

func SkipReferenceHandler

func SkipReferenceHandler(r Resource, lines []string) ([]string, []string, error)

SkipReferenceHandler handles "reference" actions by calculating the length of the snippet and skipping that many lines below.

Types

type Citation

type Citation struct {
	URL   *url.URL
	Extra string
}

Citation is a reference to an external resource, perhaps specifying a particular segment of it with some extra data.

func (Citation) String

func (c Citation) String() string

type CodeBlock

type CodeBlock struct {
	CommentBlock CommentBlock
	Code         []string
}

CodeBlock represents arbitrary lines of Golang source code preceded by a CommentBlock.

func (CodeBlock) String

func (b CodeBlock) String() string

String converts the CodeBlock into source code.

type CommentBlock

type CommentBlock struct {
	Leader string
	Lines  []string
}

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

type Directive struct {
	ActionRaw string
	Citation  Citation
}

Directive represents a line in godoc to be processed by cite. This is an action together with a citation.

func ParseDirective

func ParseDirective(line string) (*Directive, error)

ParseDirective tests the given line for a Directive and returns it if found.

func (Directive) Action

func (d Directive) Action() string

Action returns the canonical representation of the action in the directive.

func (Directive) String

func (d Directive) String() string

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

type Handler func(Resource, []string) ([]string, []string, error)

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

type LinePredicate interface {
	LineIncluded(int) bool
}

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

func NewLineRange(s, e int) (LineRange, error)

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

func NewSingleLine(n int) LineRange

NewSingleLine returns a LineRange object for a single line.

func ParseLineRange

func ParseLineRange(s string) (LineRange, error)

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) End

func (l LineRange) End() int

End returns the last line in the range (inclusive).

func (LineRange) LineIncluded

func (l LineRange) LineIncluded(n int) bool

LineIncluded returns whether line n is included in the range.

func (LineRange) NumLines

func (l LineRange) NumLines() int

NumLines returns the number of lines in the range.

func (LineRange) Start

func (l LineRange) Start() int

Start returns the first line in the range (inclusive).

func (LineRange) String

func (l LineRange) String() string

type LineSelection

type LineSelection interface {
	LinePredicate
	NumLines() int
}

LineSelection specifies a subset of lines of interest (like LinePredicate) of a known size.

type PlainResource

type PlainResource struct {
	FileURL   *url.URL
	LineRange LineRange
}

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

func (p Processor) AddHandler(action string, handler Handler)

AddHandler registers a handler for the given action type. Note casing of the action string does not matter.

func (Processor) Process

func (p Processor) Process(src Source) (Source, error)

Process transforms source code using registered handlers.

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

func BuildGithubResourceFromCitation(c Citation) (Resource, error)

BuildGithubResourceFromCitation constructs a GithubResource from a reference to it.

func BuildPlainResourceFromCitation

func BuildPlainResourceFromCitation(c Citation) (Resource, error)

BuildPlainResourceFromCitation builds a PlainResource from a reference to it. The line range is represented in the Extra field of the Citation.

type ResourceBuilder

type ResourceBuilder func(Citation) (Resource, error)

ResourceBuilder is a function that constructs a resource from a reference to it.

type Source

type Source struct {
	Blocks []CodeBlock
}

Source represents a Golang source file broken up into CodeBlock objects, by splitting on comment blocks.

func ParseCode

func ParseCode(r io.Reader) Source

ParseCode parses the code from the given io.Reader into a Source objects by splitting on comment blocks.

func (Source) String

func (s Source) String() string

String converts Source into source code.

Directories

Path Synopsis
cmd
cite command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址