File: validation.go

package info (click to toggle)
golang-github-johanneskaufmann-html-to-markdown 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,080 kB
  • sloc: makefile: 3
file content (101 lines) | stat: -rw-r--r-- 2,911 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package commonmark

import (
	"fmt"
	"strings"
)

func contains(values []string, searchVal string) bool {
	for _, val := range values {
		if val == searchVal {
			return true
		}
	}
	return false
}

// TODO: should this be with the commonmark package? Or more general?
// TODO: Maybe make it an interface? And also have a GetPluginName function?
type ValidateConfigError struct {
	Key   string
	Value string

	// By default is "Key:Value" but can be
	// overriden to e.g. "--key=value"
	KeyWithValue string

	patternDescription string
}

func (e *ValidateConfigError) setDefaultKeyWithValue() {
	e.KeyWithValue = fmt.Sprintf("%s:%q", e.Key, e.Value)
}
func (e *ValidateConfigError) Error() string {
	if e.KeyWithValue == "" {
		e.setDefaultKeyWithValue()
	}

	return fmt.Sprintf("invalid value for %s must be %s", e.KeyWithValue, e.patternDescription)
}

func validateConfig(cfg *config) error {
	if strings.Count(cfg.EmDelimiter, "_") != 1 && strings.Count(cfg.EmDelimiter, "*") != 1 {
		return &ValidateConfigError{
			Key:                "EmDelimiter",
			Value:              cfg.EmDelimiter,
			patternDescription: `exactly 1 character of "*" or "_"`,
		}
	}
	if strings.Count(cfg.StrongDelimiter, "_") != 2 && strings.Count(cfg.StrongDelimiter, "*") != 2 {
		return &ValidateConfigError{
			Key:                "StrongDelimiter",
			Value:              cfg.StrongDelimiter,
			patternDescription: `exactly 2 characters of "**" or "__"`,
		}
	}

	if strings.Count(cfg.HorizontalRule, "*") < 3 &&
		strings.Count(cfg.HorizontalRule, "_") < 3 &&
		strings.Count(cfg.HorizontalRule, "-") < 3 {
		return &ValidateConfigError{
			Key:                "HorizontalRule",
			Value:              cfg.HorizontalRule,
			patternDescription: `at least 3 characters of "*", "_" or "-"`,
		}
	}

	if !contains([]string{"-", "+", "*"}, cfg.BulletListMarker) {
		return &ValidateConfigError{
			Key:                "BulletListMarker",
			Value:              cfg.BulletListMarker,
			patternDescription: `one of "-", "+" or "*"`,
		}
	}

	if !contains([]string{"```", "~~~"}, cfg.CodeBlockFence) {
		return &ValidateConfigError{
			Key:                "CodeBlockFence",
			Value:              cfg.CodeBlockFence,
			patternDescription: "one of \"```\" or \"~~~\"",
		}
	}

	if !contains([]string{"atx", "setext"}, string(cfg.HeadingStyle)) {
		return &ValidateConfigError{
			Key:                "HeadingStyle",
			Value:              string(cfg.HeadingStyle),
			patternDescription: `one of "atx" or "setext"`,
		}
	}

	possibleLinkStyles := []string{string(LinkStyleInlined), string(LinkStyleReferencedIndex), string(LinkStyleReferencedShort)}
	if !contains(possibleLinkStyles, string(cfg.LinkStyle)) {
		return &ValidateConfigError{
			Key:                "LinkStyle",
			Value:              string(cfg.LinkStyle),
			patternDescription: `one of "inlined", "referenced_index" or "referenced_short"`,
		}
	}

	return nil
}