-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
43 lines (37 loc) · 1.02 KB
/
format.go
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
package doc
import (
"github.com/hackborn/doc/parser"
)
// Expose the parser format to consumers of the doc package
// without requiring them to import the parser package.
// Conceptually, parser is completely private.
type Format = parser.Format
// FormatWithDefaults answers the supplied format, adding
// default behaviour for anything the supplied format leaves unhandled.
// f can be nil if you just want the default formatting.
func FormatWithDefaults(f Format) Format {
fallback := parser.DefaultFormat()
if f == nil {
return fallback
}
return &compositeFormat{main: f, fallback: fallback}
}
// compositeFormat provides two levels of formatting rules.
type compositeFormat struct {
main Format
fallback Format
}
func (f *compositeFormat) Keyword(s string) string {
ans := f.main.Keyword(s)
if ans != "" {
return ans
}
return f.fallback.Keyword(s)
}
func (f *compositeFormat) Value(v interface{}) (string, error) {
s, err := f.main.Value(v)
if err == nil {
return s, err
}
return f.fallback.Value(v)
}