@@ -2,20 +2,9 @@ package docs
22
33import (
44 _ "embed"
5- "errors"
65 "fmt"
7- "log"
8- "strings"
96
10- "github.com/smartcontractkit/chainlink-common/pkg/config"
11- )
12-
13- const (
14- fieldDefault = "# Default"
15- fieldExample = "# Example"
16-
17- tokenAdvanced = "**ADVANCED**"
18- tokenExtended = "**EXTENDED**"
7+ "github.com/smartcontractkit/chainlink-common/pkg/config/configdoc"
198)
209
2110var (
@@ -42,233 +31,26 @@ var (
4231
4332// GenerateConfig returns MarkDown documentation generated from core.toml & chains-*.toml.
4433func GenerateConfig () (string , error ) {
45- return generateDocs (docsTOML , `[//]: # (Documentation generated from docs/*.toml - DO NOT EDIT.)
34+ evmDefaults , err := evmChainDefaults ()
35+ if err != nil {
36+ return "" , fmt .Errorf ("failed to generate evm chain defaults: %w" , err )
37+ }
38+ return configdoc .Generate (docsTOML , `[//]: # (Documentation generated from docs/*.toml - DO NOT EDIT.)
4639
4740This document describes the TOML format for configuration.
4841
4942See also [SECRETS.md](SECRETS.md)
50- ` , exampleConfig )
43+ ` , exampleConfig , map [ string ] string { "EVM" : evmDefaults } )
5144}
5245
5346// GenerateSecrets returns MarkDown documentation generated from secrets.toml.
5447func GenerateSecrets () (string , error ) {
55- return generateDocs (secretsTOML , `[//]: # (Documentation generated from docs/secrets.toml - DO NOT EDIT.)
48+ return configdoc . Generate (secretsTOML , `[//]: # (Documentation generated from docs/secrets.toml - DO NOT EDIT.)
5649
5750This document describes the TOML format for secrets.
5851
5952Each secret has an alternative corresponding environment variable.
6053
6154See also [CONFIG.md](CONFIG.md)
62- ` , exampleSecrets )
63- }
64-
65- // generateDocs returns MarkDown documentation generated from the TOML string.
66- func generateDocs (toml , header , example string ) (string , error ) {
67- items , err := parseTOMLDocs (toml )
68- var sb strings.Builder
69-
70- sb .WriteString (header )
71- sb .WriteString (`
72- ## Example
73-
74- ` )
75- sb .WriteString ("```toml\n " )
76- sb .WriteString (example )
77- sb .WriteString ("```\n \n " )
78-
79- for _ , item := range items {
80- sb .WriteString (item .String ())
81- sb .WriteString ("\n \n " )
82- }
83-
84- return sb .String (), err
85- }
86-
87- func advancedWarning (msg string ) string {
88- return fmt .Sprintf (":warning: **_ADVANCED_**: _%s_\n " , msg )
89- }
90-
91- // lines holds a set of contiguous lines
92- type lines []string
93-
94- func (d lines ) String () string {
95- return strings .Join (d , "\n " )
96- }
97-
98- type table struct {
99- name string
100- codes lines
101- adv bool
102- desc lines
103- ext bool
104- }
105-
106- func newTable (line string , desc lines ) * table {
107- t := & table {
108- name : strings .Trim (line , "[]" ),
109- codes : []string {line },
110- desc : desc ,
111- }
112- if len (desc ) > 0 {
113- if strings .HasPrefix (strings .TrimSpace (desc [0 ]), tokenAdvanced ) {
114- t .adv = true
115- t .desc = t .desc [1 :]
116- } else if strings .HasPrefix (strings .TrimSpace (desc [len (desc )- 1 ]), tokenExtended ) {
117- t .ext = true
118- t .desc = t .desc [:len (desc )- 1 ]
119- }
120- }
121- return t
122- }
123-
124- func newArrayOfTables (line string , desc lines ) * table {
125- t := & table {
126- name : strings .Trim (strings .Trim (line , fieldExample ), "[]" ),
127- codes : []string {line },
128- desc : desc ,
129- }
130- if len (desc ) > 0 {
131- if strings .HasPrefix (strings .TrimSpace (desc [0 ]), tokenAdvanced ) {
132- t .adv = true
133- t .desc = t .desc [1 :]
134- } else if strings .HasPrefix (strings .TrimSpace (desc [len (desc )- 1 ]), tokenExtended ) {
135- t .ext = true
136- t .desc = t .desc [:len (desc )- 1 ]
137- }
138- }
139- return t
140- }
141-
142- func (t table ) advanced () string {
143- if t .adv {
144- return advancedWarning ("Do not change these settings unless you know what you are doing." )
145- }
146- return ""
147- }
148-
149- func (t table ) code () string {
150- if ! t .ext {
151- return fmt .Sprint ("```toml\n " , t .codes , "\n ```\n " )
152- }
153- return ""
154- }
155-
156- func (t table ) extended () string {
157- if t .ext {
158- if t .name != "EVM" {
159- log .Fatalf ("%s: no extended description available" , t .name )
160- }
161- s , err := evmChainDefaults ()
162- if err != nil {
163- log .Fatalf ("%s: failed to generate evm chain defaults: %v" , t .name , err )
164- }
165- return s
166- }
167- return ""
168- }
169-
170- // String prints a table as an H2, followed by a code block and description.
171- func (t * table ) String () string {
172- return fmt .Sprint ("## " , t .name , "\n " ,
173- t .advanced (),
174- t .code (),
175- t .desc ,
176- t .extended ())
177- }
178-
179- type keyval struct {
180- name string
181- code string
182- adv bool
183- desc lines
184- }
185-
186- func newKeyval (line string , desc lines ) keyval {
187- line = strings .TrimSpace (line )
188- kv := keyval {
189- name : line [:strings .Index (line , " " )],
190- code : line ,
191- desc : desc ,
192- }
193- if len (desc ) > 0 && strings .HasPrefix (strings .TrimSpace (desc [0 ]), tokenAdvanced ) {
194- kv .adv = true
195- kv .desc = kv .desc [1 :]
196- }
197- return kv
198- }
199-
200- func (k keyval ) advanced () string {
201- if k .adv {
202- return advancedWarning ("Do not change this setting unless you know what you are doing." )
203- }
204- return ""
205- }
206-
207- // String prints a keyval as an H3, followed by a code block and description.
208- func (k keyval ) String () string {
209- name := k .name
210- if i := strings .LastIndex (name , "." ); i > - 1 {
211- name = name [i + 1 :]
212- }
213- return fmt .Sprint ("### " , name , "\n " ,
214- k .advanced (),
215- "```toml\n " ,
216- k .code ,
217- "\n ```\n " ,
218- k .desc )
219- }
220-
221- func parseTOMLDocs (s string ) (items []fmt.Stringer , err error ) {
222- defer func () { _ , err = config .MultiErrorList (err ) }()
223- globalTable := table {name : "Global" }
224- currentTable := & globalTable
225- items = append (items , currentTable )
226- var desc lines
227- for _ , line := range strings .Split (s , "\n " ) {
228- if strings .HasPrefix (line , "#" ) {
229- // comment
230- desc = append (desc , strings .TrimSpace (line [1 :]))
231- } else if strings .TrimSpace (line ) == "" {
232- // empty
233- if len (desc ) > 0 {
234- items = append (items , desc )
235- desc = nil
236- }
237- } else if strings .HasPrefix (line , "[[" ) {
238- currentTable = newArrayOfTables (line , desc )
239- items = append (items , currentTable )
240- desc = nil
241- } else if strings .HasPrefix (line , "[" ) {
242- currentTable = newTable (line , desc )
243- items = append (items , currentTable )
244- desc = nil
245- } else {
246- kv := newKeyval (line , desc )
247- shortName := kv .name
248- if currentTable != & globalTable {
249- // update to full name
250- kv .name = currentTable .name + "." + kv .name
251- }
252- if len (kv .desc ) == 0 {
253- err = errors .Join (err , fmt .Errorf ("%s: missing description" , kv .name ))
254- } else if ! strings .HasPrefix (kv .desc [0 ], shortName ) {
255- err = errors .Join (err , fmt .Errorf ("%s: description does not begin with %q" , kv .name , shortName ))
256- }
257- if ! strings .HasSuffix (line , fieldDefault ) && ! strings .HasSuffix (line , fieldExample ) {
258- err = errors .Join (err , fmt .Errorf (`%s: is not one of %v` , kv .name , []string {fieldDefault , fieldExample }))
259- }
260-
261- items = append (items , kv )
262- currentTable .codes = append (currentTable .codes , kv .code )
263- desc = nil
264- }
265- }
266- if len (globalTable .codes ) == 0 {
267- // drop it
268- items = items [1 :]
269- }
270- if len (desc ) > 0 {
271- items = append (items , desc )
272- }
273- return
55+ ` , exampleSecrets , nil )
27456}
0 commit comments