about summary refs log tree commit diff stats
path: root/internal/options
diff options
context:
space:
mode:
authorAlan Pearce2024-05-05 18:11:56 +0200
committerAlan Pearce2024-05-05 18:11:56 +0200
commit2430f46a9948b38b06880606a95dec357d01f619 (patch)
tree2d8e9078b99ade7e9fe339be805890c635f0e235 /internal/options
parent158904f480e558ca00f680e7c577bb6329605eff (diff)
downloadsearchix-2430f46a9948b38b06880606a95dec357d01f619.tar.lz
searchix-2430f46a9948b38b06880606a95dec357d01f619.tar.zst
searchix-2430f46a9948b38b06880606a95dec357d01f619.zip
feat: render markdown in option descriptions
Diffstat (limited to 'internal/options')
-rw-r--r--internal/options/option.go57
-rw-r--r--internal/options/process.go109
2 files changed, 166 insertions, 0 deletions
diff --git a/internal/options/option.go b/internal/options/option.go
new file mode 100644
index 0000000..5255c65
--- /dev/null
+++ b/internal/options/option.go
@@ -0,0 +1,57 @@
+package options
+
+import (
+	"encoding/json"
+	"strings"
+
+	"github.com/pkg/errors"
+	"github.com/yuin/goldmark"
+)
+
+type NixValue struct {
+	Type string `json:"_type" mapstructure:"_type"`
+	Text string `json:"text"`
+}
+
+type HTML struct {
+	HTML string
+}
+
+func (html *HTML) UnmarshalText(text []byte) error {
+	var out strings.Builder
+	err := goldmark.Convert(text, &out)
+	if err != nil {
+		return errors.WithMessage(err, "failed to convert markdown to HTML")
+	}
+
+	html.HTML = out.String()
+
+	return nil
+}
+
+func (html *HTML) UnmarshalJSON(raw []byte) error {
+	var v struct {
+		HTML string
+	}
+	err := json.Unmarshal(raw, &v)
+	if err != nil {
+		return errors.WithMessage(err, "error unmarshaling json")
+	}
+	html.HTML = v.HTML
+
+	return nil
+}
+
+type NixOption struct {
+	Option          string
+	Declarations    []string
+	Default         NixValue
+	Description     HTML
+	Example         NixValue
+	ReadOnly        bool
+	Type            string
+	Loc             []string
+	RelatedPackages HTML
+}
+
+type NixOptions []NixOption
diff --git a/internal/options/process.go b/internal/options/process.go
new file mode 100644
index 0000000..fde73e4
--- /dev/null
+++ b/internal/options/process.go
@@ -0,0 +1,109 @@
+package options
+
+import (
+	"encoding/json"
+	"io"
+	"os"
+
+	"github.com/bcicen/jstream"
+	"github.com/mitchellh/mapstructure"
+	"github.com/pkg/errors"
+)
+
+func ValueTypeToString(valueType jstream.ValueType) string {
+	switch valueType {
+	case jstream.Unknown:
+		return "unknown"
+	case jstream.Null:
+		return "null"
+	case jstream.String:
+		return "string"
+	case jstream.Number:
+		return "number"
+	case jstream.Boolean:
+		return "boolean"
+	case jstream.Array:
+		return "array"
+	case jstream.Object:
+		return "object"
+	}
+
+	return "very strange"
+}
+
+func Process(inpath string, outpath string) error {
+	infile, err := os.Open(inpath)
+	if err != nil {
+		return errors.WithMessagef(err, "failed to open input file %s", inpath)
+	}
+	defer infile.Close()
+	outfile, err := os.Create(outpath)
+	if err != nil {
+		return errors.WithMessagef(err, "failed to open output file %s", outpath)
+	}
+	if outpath != "/dev/stdout" {
+		defer outfile.Close()
+	}
+
+	dec := jstream.NewDecoder(infile, 1).EmitKV()
+	var opt NixOption
+	ms, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
+		ErrorUnused: true,
+		Result:      &opt,
+		Squash:      true,
+		DecodeHook:  mapstructure.TextUnmarshallerHookFunc(),
+	})
+	if err != nil {
+		return errors.WithMessage(err, "could not create mapstructure decoder")
+	}
+
+	_, err = outfile.WriteString("[\n")
+	if err != nil {
+		return errors.WithMessage(err, "could not write to output")
+	}
+	for mv := range dec.Stream() {
+		if err := dec.Err(); err != nil {
+			return errors.WithMessage(err, "could not decode JSON")
+		}
+		if mv.ValueType != jstream.Object {
+			return errors.Errorf("unexpected object type %s", ValueTypeToString(mv.ValueType))
+		}
+		kv := mv.Value.(jstream.KV)
+		if kv.Key == "_module.args" {
+			continue
+		}
+		x := kv.Value.(map[string]interface{})
+		x["option"] = kv.Key
+
+		err = ms.Decode(x)
+		if err != nil {
+			return errors.WithMessagef(err, "failed to decode option %#v", x)
+		}
+
+		b, err := json.MarshalIndent(opt, "", "  ")
+		if err != nil {
+			return errors.WithMessagef(err, "failed to encode option %#v", opt)
+		}
+
+		_, err = outfile.Write(b)
+		if err != nil {
+			return errors.WithMessage(err, "failed to write to output")
+		}
+		_, err = outfile.WriteString(",\n")
+		if err != nil {
+			return errors.WithMessage(err, "failed to write to output")
+		}
+	}
+
+	_, err = outfile.Seek(-2, io.SeekCurrent)
+	if err != nil {
+		return errors.WithMessage(err, "could not write to output")
+	}
+
+	_, err = outfile.WriteString("]\n")
+	if err != nil {
+		return errors.WithMessage(err, "could not write to output")
+	}
+
+	return nil
+}