155 lines
3.9 KiB
Go
155 lines
3.9 KiB
Go
// Package ref parses and formats yetano: references (ADR-0004):
|
|||
|
|
//
|
||
|
|
// yetano:<kind>/<path>[#<section>][@<version>]
|
||
|
|
//
|
||
|
|
// It only knows the syntax; resolving a reference against a catalog and
|
||
|
|
// rendering it for a client lives in package knowledge.
|
||
|
|
package ref
|
||
|
|
|
||
|
|
import (
|
||
|
|
"errors"
|
||
|
|
"fmt"
|
||
|
|
"regexp"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Kinds of references.
|
||
|
|
const (
|
||
|
|
Package = "package"
|
||
|
|
Module = "module"
|
||
|
|
Manual = "manual"
|
||
|
|
Microsite = "microsite"
|
||
|
|
Solution = "solution"
|
||
|
|
Task = "task"
|
||
|
|
Profile = "profile"
|
||
|
|
Capability = "capability"
|
||
|
|
Example = "example"
|
||
|
|
BigPicture = "bigpicture"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Kinds lists the valid kinds and how many path segments each takes.
|
||
|
|
var Kinds = map[string]int{
|
||
|
|
Package: 1, Module: 2, Manual: -2, Microsite: 1, Solution: 1, Task: 1,
|
||
|
|
Profile: 2, Capability: 1, Example: 2, BigPicture: 1,
|
||
|
|
}
|
||
|
|
|
||
|
|
// Ref is a parsed reference.
|
||
|
|
type Ref struct {
|
||
|
|
Kind string `json:"kind"`
|
||
|
|
Path []string `json:"path"`
|
||
|
|
Section string `json:"section,omitempty"`
|
||
|
|
Version string `json:"version,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// ErrSyntax is returned for malformed references.
|
||
|
|
var ErrSyntax = errors.New("invalid yetano reference")
|
||
|
|
|
||
|
|
var segRE = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]*$`)
|
||
|
|
|
||
|
|
// MaxLen bounds a reference (SEC-03).
|
||
|
|
const MaxLen = 512
|
||
|
|
|
||
|
|
// Parse parses s.
|
||
|
|
func Parse(s string) (Ref, error) {
|
||
|
|
if len(s) > MaxLen {
|
||
|
|
return Ref{}, fmt.Errorf("%w: longer than %d bytes", ErrSyntax, MaxLen)
|
||
|
|
}
|
||
|
|
rest, ok := strings.CutPrefix(s, "yetano:")
|
||
|
|
if !ok {
|
||
|
|
return Ref{}, fmt.Errorf("%w %q: must start with yetano:", ErrSyntax, s)
|
||
|
|
}
|
||
|
|
var r Ref
|
||
|
|
if i := strings.LastIndex(rest, "@"); i >= 0 {
|
||
|
|
r.Version, rest = rest[i+1:], rest[:i]
|
||
|
|
if r.Version == "" {
|
||
|
|
return Ref{}, fmt.Errorf("%w %q: empty version", ErrSyntax, s)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if i := strings.Index(rest, "#"); i >= 0 {
|
||
|
|
r.Section, rest = rest[i+1:], rest[:i]
|
||
|
|
if !segRE.MatchString(r.Section) {
|
||
|
|
return Ref{}, fmt.Errorf("%w %q: bad section", ErrSyntax, s)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
parts := strings.Split(rest, "/")
|
||
|
|
r.Kind = parts[0]
|
||
|
|
r.Path = parts[1:]
|
||
|
|
want, ok := Kinds[r.Kind]
|
||
|
|
if !ok {
|
||
|
|
return Ref{}, fmt.Errorf("%w %q: unknown kind %q", ErrSyntax, s, r.Kind)
|
||
|
|
}
|
||
|
|
switch {
|
||
|
|
case want > 0 && len(r.Path) != want:
|
||
|
|
return Ref{}, fmt.Errorf("%w %q: %s takes %d path segment(s)", ErrSyntax, s, r.Kind, want)
|
||
|
|
case want < 0 && (len(r.Path) < 2):
|
||
|
|
return Ref{}, fmt.Errorf("%w %q: %s takes <package>/<topic>", ErrSyntax, s, r.Kind)
|
||
|
|
}
|
||
|
|
for _, p := range r.Path {
|
||
|
|
if !segRE.MatchString(p) {
|
||
|
|
return Ref{}, fmt.Errorf("%w %q: bad path segment %q", ErrSyntax, s, p)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return r, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// MustParse is Parse for constants in tests.
|
||
|
|
func MustParse(s string) Ref {
|
||
|
|
r, err := Parse(s)
|
||
|
|
if err != nil {
|
||
|
|
panic(err)
|
||
|
|
}
|
||
|
|
return r
|
||
|
|
}
|
||
|
|
|
||
|
|
// String formats r.
|
||
|
|
func (r Ref) String() string {
|
||
|
|
var b strings.Builder
|
||
|
|
b.WriteString("yetano:")
|
||
|
|
b.WriteString(r.Kind)
|
||
|
|
for _, p := range r.Path {
|
||
|
|
b.WriteByte('/')
|
||
|
|
b.WriteString(p)
|
||
|
|
}
|
||
|
|
if r.Section != "" {
|
||
|
|
b.WriteByte('#')
|
||
|
|
b.WriteString(r.Section)
|
||
|
|
}
|
||
|
|
if r.Version != "" {
|
||
|
|
b.WriteByte('@')
|
||
|
|
b.WriteString(r.Version)
|
||
|
|
}
|
||
|
|
return b.String()
|
||
|
|
}
|
||
|
|
|
||
|
|
// Target is the reference without section and version (what Backlinks index).
|
||
|
|
func (r Ref) Target() string {
|
||
|
|
r.Section, r.Version = "", ""
|
||
|
|
return r.String()
|
||
|
|
}
|
||
|
|
|
||
|
|
// For builds a reference.
|
||
|
|
func For(kind string, path ...string) string {
|
||
|
|
return Ref{Kind: kind, Path: path}.String()
|
||
|
|
}
|
||
|
|
|
||
|
|
var translit = strings.NewReplacer("ä", "ae", "ö", "oe", "ü", "ue", "ß", "ss", "é", "e", "è", "e", "à", "a", "ç", "c")
|
||
|
|
|
||
|
|
// Slug turns a heading into a section ID: lower case ASCII letters and
|
||
|
|
// digits; every run of anything else becomes one "-", trimmed.
|
||
|
|
func Slug(heading string) string {
|
||
|
|
var b strings.Builder
|
||
|
|
dash := false
|
||
|
|
heading = translit.Replace(strings.ToLower(heading))
|
||
|
|
for _, r := range heading {
|
||
|
|
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
|
||
|
|
b.WriteRune(r)
|
||
|
|
dash = false
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if !dash && b.Len() > 0 {
|
||
|
|
b.WriteByte('-')
|
||
|
|
dash = true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return strings.TrimRight(b.String(), "-")
|
||
|
|
}
|