From 1260b7d429f23d60946fd042625b48f1fd75735a Mon Sep 17 00:00:00 2001 From: Andrew Nesbitt Date: Mon, 23 Nov 2026 09:13:33 +0000 Subject: [PATCH] Report RST as unsupported when no rst2html command is installed --- external.go | 6 ++++++ markup.go | 2 +- markup_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/external.go b/external.go index e76c94b..3b22bad 100644 --- a/external.go +++ b/external.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "regexp" + "slices" "strings" "sync" "time" @@ -215,6 +216,11 @@ func NewRSTRenderer() *RSTRenderer { return &RSTRenderer{} } +// Available returns true if an rst2html command is on $PATH. +func (r *RSTRenderer) Available() bool { + return slices.ContainsFunc(rstCommands, toolAvailable) +} + func (r *RSTRenderer) Render(content []byte) (string, error) { return rstRender(content) } diff --git a/markup.go b/markup.go index d997a56..5709b1d 100644 --- a/markup.go +++ b/markup.go @@ -209,7 +209,7 @@ func (r *Registry) Supported(filename string) bool { if !ok { return false } - if ext, ok := entry.renderer.(*ExternalRenderer); ok { + if ext, ok := entry.renderer.(interface{ Available() bool }); ok { return ext.Available() } return true diff --git a/markup_test.go b/markup_test.go index 70444da..d5c840e 100644 --- a/markup_test.go +++ b/markup_test.go @@ -2,6 +2,7 @@ package markup import ( "encoding/base64" + "errors" "io" "os" "path/filepath" @@ -199,6 +200,29 @@ func TestRegistrySupported(t *testing.T) { } } +func TestRegistrySupportedRST(t *testing.T) { + binDir := t.TempDir() + t.Setenv("PATH", binDir) + toolCache.Clear() + t.Cleanup(toolCache.Clear) + + r := NewDefaultRegistry() + if r.Supported("README.rst") { + t.Error("expected RST to be unsupported with no rst2html command installed") + } + if _, err := r.Render("README.rst", []byte("Title\n=====\n")); !errors.Is(err, ErrToolNotFound) { + t.Errorf("Render() error = %v, want %v", err, ErrToolNotFound) + } + + if err := os.WriteFile(filepath.Join(binDir, "rst2html"), []byte("#!/bin/sh\n"), 0o755); err != nil { + t.Fatal(err) + } + toolCache.Clear() + if !r.Supported("README.rst") { + t.Error("expected RST to be supported once rst2html is installed") + } +} + // --- Markdown rendering --- func TestRenderMarkdown(t *testing.T) {