Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions external.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"regexp"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion markup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions markup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package markup

import (
"encoding/base64"
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -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) {
Expand Down