set_spacing.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package at
  3. import (
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8. func parse_set_spacing(args []string) (map[string]any, error) {
  9. ans := make(map[string]any, len(args))
  10. mapper := make(map[string][]string, 32)
  11. types := [2]string{"margin", "padding"}
  12. for _, q := range types {
  13. mapper[q] = []string{q + "-left", q + "-top", q + "-right", q + "-bottom"}
  14. mapper[q+"-h"] = []string{q + "-left", q + "-right"}
  15. mapper[q+"-v"] = []string{q + "-top", q + "-bottom"}
  16. mapper[q+"-left"] = []string{q + "-left"}
  17. mapper[q+"-right"] = []string{q + "-right"}
  18. mapper[q+"-top"] = []string{q + "-top"}
  19. mapper[q+"-bottom"] = []string{q + "-bottom"}
  20. }
  21. for _, arg := range args {
  22. k, v, found := strings.Cut(arg, "=")
  23. if !found {
  24. return nil, fmt.Errorf("%s is not a valid setting", arg)
  25. }
  26. k = strings.ToLower(k)
  27. v = strings.ToLower(v)
  28. which, found := mapper[k]
  29. if !found {
  30. return nil, fmt.Errorf("%s is not a valid edge specification", k)
  31. }
  32. if v == "default" {
  33. for _, q := range which {
  34. ans[q] = nil
  35. }
  36. } else {
  37. val, err := strconv.ParseFloat(v, 64)
  38. if err != nil {
  39. return nil, fmt.Errorf("%s is not a number", v)
  40. }
  41. for _, q := range which {
  42. ans[q] = val
  43. }
  44. }
  45. }
  46. return ans, nil
  47. }