package knowledge import ( "fmt" "net/url" "strings" "yetano.dev/yetano/core/solutions/ref" ) // Resolution says what a reference points at and how each client opens // it (REF-02). Exists is false for targets the index does not know. type Resolution struct { Ref string `json:"ref"` Kind string `json:"kind"` Title string `json:"title,omitempty"` Summary string `json:"summary,omitempty"` Exists bool `json:"exists"` // SectionExists is false when the ref names a section that is not there. SectionExists bool `json:"sectionExists"` Web string `json:"web,omitempty"` // URL on yetano.app (only for pages that exist) CLI string `json:"cli,omitempty"` // command that shows it in a terminal File string `json:"file,omitempty"` // local Markdown file (manuals) Line int `json:"line,omitempty"` View string `json:"view"` // how a UI shows it: wizard | reader | graph | package | code | list Note string `json:"note,omitempty"` } // Resolve resolves a reference against the index. func (ix *Index) Resolve(s string) (Resolution, error) { r, err := ref.Parse(s) if err != nil { return Resolution{}, err } res := Resolution{Ref: s, Kind: r.Kind, SectionExists: r.Section == ""} web := strings.TrimRight(ix.Cat.Spec.WebBase, "/") + "/" if e := ix.byRef[r.Target()]; e != nil { res.Exists, res.Title, res.Summary = true, e.Title, e.Summary } switch r.Kind { case ref.Solution, ref.Task: res.View = "wizard" cmd := "solution" if r.Kind == ref.Task { cmd = "task" } res.CLI = fmt.Sprintf("yet %s show %s", cmd, r.Path[0]) case ref.Package: res.View = "package" res.CLI = "yet solution package " + r.Path[0] if p := ix.Cat.Packages[r.Path[0]]; p != nil && p.Spec.Microsite != "" { res.Web = web + p.Spec.Microsite } case ref.Module: res.View = "package" res.CLI = "yet solution package " + r.Path[0] case ref.Microsite: res.View = "package" if p := ix.Cat.Packages[r.Path[0]]; p != nil && p.Spec.Microsite != "" { res.Web = web + p.Spec.Microsite res.Exists = true } case ref.Capability: res.View = "list" res.CLI = "yet knowledge show " + s case ref.Profile, ref.Example: res.View = "code" res.CLI = "yet knowledge show " + s case ref.BigPicture: res.View = "graph" if bp := ix.Cat.BigPicture; bp != nil && bp.Has(r.Path[0]) { res.Exists = true res.Web = web + bp.WebPath + "#" + bp.Fragment(r.Path[0]) } case ref.Manual: res.View = "reader" source := r.Path[0] slug := strings.Join(r.Path[1:], ".") k := ix.Cat.Sources[source] if k == nil { res.Note = "unknown knowledge source " + source break } realSlug := strings.ReplaceAll(slug, ".", "/") if k.Spec.Program != "" { res.CLI = fmt.Sprintf("%s manual %s", k.Spec.Program, realSlug) } else { res.CLI = "yet knowledge show " + s } if k.Spec.Web != "" { res.Web = k.Spec.Web } t := ix.Topics[source+"/"+slug] if t == nil { if ix.missing(source) { res.Note = "the documents of " + source + " are not available locally; the reference is not checked" res.Exists = true // cannot tell; do not report as broken } break } res.Exists, res.Title, res.File = true, t.Title, t.File if r.Section != "" { for _, sec := range t.Sections { if sec.ID == r.Section { res.SectionExists, res.Title, res.Line = true, t.Title+" › "+sec.Title, sec.Start } } } } if res.Web != "" { if _, err := url.Parse(res.Web); err != nil { res.Web = "" } } return res, nil } func (ix *Index) missing(source string) bool { for _, m := range ix.Missing { if m == source { return true } } return false } // Read returns the Markdown of a manual reference: the whole topic, or // just the section (with its subsections). func (ix *Index) Read(s string) (title, markdown string, err error) { r, err := ref.Parse(s) if err != nil { return "", "", err } if r.Kind != ref.Manual { return "", "", fmt.Errorf("%s is not a manual reference", s) } t := ix.Topics[r.Path[0]+"/"+strings.Join(r.Path[1:], ".")] if t == nil { return "", "", fmt.Errorf("manual %s is not available locally", s) } if r.Section == "" { return t.Title, strings.Join(t.lines, "\n") + "\n", nil } for i, sec := range t.Sections { if sec.ID != r.Section { continue } end := len(t.lines) for _, next := range t.Sections[i+1:] { if next.Level <= sec.Level { end = next.Start - 1 break } } return sec.Title, strings.Join(t.lines[sec.Start-1:end], "\n") + "\n", nil } return "", "", fmt.Errorf("%s: no section %q", s, r.Section) } // Excerpt implements engine.ManualReader. func (ix *Index) Excerpt(s string, maxLines int) (string, string, bool) { title, md, err := ix.Read(s) if err != nil { return "", "", false } lines := strings.Split(strings.TrimRight(md, "\n"), "\n") if len(lines) > 0 && strings.HasPrefix(lines[0], "#") { lines = lines[1:] } for len(lines) > 0 && strings.TrimSpace(lines[0]) == "" { lines = lines[1:] } if len(lines) > maxLines { lines = append(lines[:maxLines], "…") } return title, strings.Join(lines, "\n"), true } // Link implements engine.ManualReader: a web URL, else the CLI command. func (ix *Index) Link(s string) string { res, err := ix.Resolve(s) if err != nil { return "`" + s + "`" } label := firstNonEmpty(res.Title, s) switch { case res.Web != "" && res.Kind != ref.Manual: return fmt.Sprintf("[%s](%s)", label, res.Web) case res.CLI != "": return fmt.Sprintf("%s (`%s`)", label, res.CLI) } return "`" + s + "`" }