@@ -20,100 +20,115 @@ import (
20
20
"strings"
21
21
)
22
22
23
+ type ParseOption int
24
+
25
+ const (
26
+ ParseService ParseOption = iota
27
+ ParseMessage
28
+ )
29
+
23
30
// Markdown saves 'Proto' to markdown documentation.
24
31
// lopts are a slice of language options (C++, Java, Python, Go, Ruby, C#).
25
- func (p * Proto ) Markdown (title , fpath string , lopts ... string ) error {
32
+ func (p * Proto ) Markdown (title string , parseOpts [] ParseOption , lopts ... string ) ( string , error ) {
26
33
p .Sort ()
27
34
28
35
buf := new (bytes.Buffer )
29
- buf .WriteString (fmt .Sprintf ("### %s\n \n \n " , title ))
30
-
31
- for _ , svs := range p .Services {
32
- buf .WriteString (fmt .Sprintf ("##### service `%s`\n \n " , svs .Name ))
33
- if svs .Description != "" {
34
- buf .WriteString (svs .Description )
35
- buf .WriteString ("\n \n " )
36
- }
37
-
38
- if len (svs .Methods ) > 0 {
39
- hd1 := "| Method | Request Type | Response Type | Description |"
40
- hd2 := "| ------ | ------------ | ------------- | ----------- |"
41
- buf .WriteString (hd1 + "\n " )
42
- buf .WriteString (hd2 + "\n " )
43
- for _ , elem := range svs .Methods {
44
- line := fmt .Sprintf ("| %s | %s | %s | %s |" , elem .Name , elem .RequestType , elem .ResponseType , elem .Description )
45
- buf .WriteString (line + "\n " )
46
- }
47
- } else {
48
- buf .WriteString ("Empty method.\n " )
49
- }
50
-
51
- buf .WriteString ("\n \n <br>\n \n " )
36
+ if len (title ) > 0 {
37
+ buf .WriteString (fmt .Sprintf ("### %s\n \n \n " , title ))
52
38
}
53
39
54
- for _ , msg := range p .Messages {
55
- buf .WriteString (fmt .Sprintf ("##### message `%s`\n \n " , msg .Name ))
56
- if msg .Description != "" {
57
- buf .WriteString (msg .Description )
58
- buf .WriteString ("\n \n " )
59
- }
40
+ for _ , opt := range parseOpts {
41
+ switch opt {
42
+ case ParseService :
43
+ for _ , svs := range p .Services {
44
+ buf .WriteString (fmt .Sprintf ("##### service `%s`\n \n " , svs .Name ))
45
+ if svs .Description != "" {
46
+ buf .WriteString (svs .Description )
47
+ buf .WriteString ("\n \n " )
48
+ }
60
49
61
- if len (msg .Fields ) > 0 {
62
- hd1 := "| Field | Description | Type |"
63
- hd2 := "| ----- | ----------- | ---- |"
64
- for _ , lopt := range lopts {
65
- hd1 += fmt .Sprintf (" %s |" , lopt )
66
- ds := strings .Repeat ("-" , len (lopt ))
67
- if len (ds ) < 3 {
68
- ds = "---"
50
+ if len (svs .Methods ) > 0 {
51
+ hd1 := "| Method | Request Type | Response Type | Description |"
52
+ hd2 := "| ------ | ------------ | ------------- | ----------- |"
53
+ buf .WriteString (hd1 + "\n " )
54
+ buf .WriteString (hd2 + "\n " )
55
+ for _ , elem := range svs .Methods {
56
+ line := fmt .Sprintf ("| %s | %s | %s | %s |" , elem .Name , elem .RequestType , elem .ResponseType , elem .Description )
57
+ buf .WriteString (line + "\n " )
58
+ }
59
+ } else {
60
+ buf .WriteString ("Empty method.\n " )
69
61
}
70
- hd2 += fmt .Sprintf (" %s |" , ds )
62
+
63
+ buf .WriteString ("\n \n \n " )
71
64
}
72
- buf .WriteString (hd1 + "\n " )
73
- buf .WriteString (hd2 + "\n " )
74
- for _ , elem := range msg .Fields {
75
- ts := elem .ProtoType .String ()
76
- if elem .UserDefinedProtoType != "" {
77
- ts = elem .UserDefinedProtoType
78
- }
79
- if elem .Repeated {
80
- ts = "(slice of) " + ts
65
+
66
+ case ParseMessage :
67
+ for _ , msg := range p .Messages {
68
+ buf .WriteString (fmt .Sprintf ("##### message `%s`\n \n " , msg .Name ))
69
+ if msg .Description != "" {
70
+ buf .WriteString (msg .Description )
71
+ buf .WriteString ("\n \n " )
81
72
}
82
- line := fmt .Sprintf ("| %s | %s | %s |" , elem .Name , elem .Description , ts )
83
- for _ , lopt := range lopts {
84
- if elem .UserDefinedProtoType != "" {
85
- line += " |"
86
- continue
87
- }
88
- formatSt := " %s |"
89
- if elem .Repeated {
90
- formatSt = " (slice of) %s |"
73
+
74
+ if len (msg .Fields ) > 0 {
75
+ hd1 := "| Field | Description | Type |"
76
+ hd2 := "| ----- | ----------- | ---- |"
77
+ for _ , lopt := range lopts {
78
+ hd1 += fmt .Sprintf (" %s |" , lopt )
79
+ ds := strings .Repeat ("-" , len (lopt ))
80
+ if len (ds ) < 3 {
81
+ ds = "---"
82
+ }
83
+ hd2 += fmt .Sprintf (" %s |" , ds )
91
84
}
92
- switch lopt {
93
- case "C++" :
94
- line += fmt .Sprintf (formatSt , elem .ProtoType .Cpp ())
95
- case "Java" :
96
- line += fmt .Sprintf (formatSt , elem .ProtoType .Java ())
97
- case "Python" :
98
- line += fmt .Sprintf (formatSt , elem .ProtoType .Python ())
99
- case "Go" :
100
- line += fmt .Sprintf (formatSt , elem .ProtoType .Go ())
101
- case "Ruby" :
102
- line += fmt .Sprintf (formatSt , elem .ProtoType .Ruby ())
103
- case "C#" :
104
- line += fmt .Sprintf (formatSt , elem .ProtoType .Csharp ())
105
- default :
106
- return fmt .Errorf ("%q is unknown (must be C++, Java, Python, Go, Ruby, C#)" , lopt )
85
+ buf .WriteString (hd1 + "\n " )
86
+ buf .WriteString (hd2 + "\n " )
87
+ for _ , elem := range msg .Fields {
88
+ ts := elem .ProtoType .String ()
89
+ if elem .UserDefinedProtoType != "" {
90
+ ts = elem .UserDefinedProtoType
91
+ }
92
+ if elem .Repeated {
93
+ ts = "(slice of) " + ts
94
+ }
95
+ line := fmt .Sprintf ("| %s | %s | %s |" , elem .Name , elem .Description , ts )
96
+ for _ , lopt := range lopts {
97
+ if elem .UserDefinedProtoType != "" {
98
+ line += " |"
99
+ continue
100
+ }
101
+ formatSt := " %s |"
102
+ if elem .Repeated {
103
+ formatSt = " (slice of) %s |"
104
+ }
105
+ switch lopt {
106
+ case "C++" :
107
+ line += fmt .Sprintf (formatSt , elem .ProtoType .Cpp ())
108
+ case "Java" :
109
+ line += fmt .Sprintf (formatSt , elem .ProtoType .Java ())
110
+ case "Python" :
111
+ line += fmt .Sprintf (formatSt , elem .ProtoType .Python ())
112
+ case "Go" :
113
+ line += fmt .Sprintf (formatSt , elem .ProtoType .Go ())
114
+ case "Ruby" :
115
+ line += fmt .Sprintf (formatSt , elem .ProtoType .Ruby ())
116
+ case "C#" :
117
+ line += fmt .Sprintf (formatSt , elem .ProtoType .Csharp ())
118
+ default :
119
+ return "" , fmt .Errorf ("%q is unknown (must be C++, Java, Python, Go, Ruby, C#)" , lopt )
120
+ }
121
+ }
122
+ buf .WriteString (line + "\n " )
107
123
}
124
+ } else {
125
+ buf .WriteString ("Empty field.\n " )
108
126
}
109
- buf .WriteString (line + "\n " )
127
+
128
+ buf .WriteString ("\n \n \n " )
110
129
}
111
- } else {
112
- buf .WriteString ("Empty field.\n " )
113
130
}
114
-
115
- buf .WriteString ("\n \n <br>\n \n " )
116
131
}
117
132
118
- return toFile ( buf .String (), fpath )
133
+ return buf .String (), nil
119
134
}
0 commit comments