44 "fmt"
55 "net/url"
66 "os"
7- "regexp"
87 "sort"
98 "strings"
109
@@ -30,18 +29,20 @@ func init() {
3029}
3130
3231func generateAllDocs () error {
33- if err := generateReadmeDocs ("README.md" ); err != nil {
34- return fmt .Errorf ("failed to generate README docs: %w" , err )
35- }
36-
37- if err := generateRemoteServerDocs ("docs/remote-server.md" ); err != nil {
38- return fmt .Errorf ("failed to generate remote-server docs: %w" , err )
39- }
40-
41- if err := generateDeprecatedAliasesDocs ("docs/deprecated-tool-aliases.md" ); err != nil {
42- return fmt .Errorf ("failed to generate deprecated aliases docs: %w" , err )
32+ for _ , doc := range []struct {
33+ path string
34+ fn func (string ) error
35+ }{
36+ // File to edit, function to generate its docs
37+ {"README.md" , generateReadmeDocs },
38+ {"docs/remote-server.md" , generateRemoteServerDocs },
39+ {"docs/deprecated-tool-aliases.md" , generateDeprecatedAliasesDocs },
40+ } {
41+ if err := doc .fn (doc .path ); err != nil {
42+ return fmt .Errorf ("failed to generate docs for %s: %w" , doc .path , err )
43+ }
44+ fmt .Printf ("Successfully updated %s with automated documentation\n " , doc .path )
4345 }
44-
4546 return nil
4647}
4748
@@ -66,18 +67,23 @@ func generateReadmeDocs(readmePath string) error {
6667 }
6768
6869 // Replace toolsets section
69- updatedContent := replaceSection (string (content ), "START AUTOMATED TOOLSETS" , "END AUTOMATED TOOLSETS" , toolsetsDoc )
70+ updatedContent , err := replaceSection (string (content ), "START AUTOMATED TOOLSETS" , "END AUTOMATED TOOLSETS" , toolsetsDoc )
71+ if err != nil {
72+ return err
73+ }
7074
7175 // Replace tools section
72- updatedContent = replaceSection (updatedContent , "START AUTOMATED TOOLS" , "END AUTOMATED TOOLS" , toolsDoc )
76+ updatedContent , err = replaceSection (updatedContent , "START AUTOMATED TOOLS" , "END AUTOMATED TOOLS" , toolsDoc )
77+ if err != nil {
78+ return err
79+ }
7380
7481 // Write back to file
7582 err = os .WriteFile (readmePath , []byte (updatedContent ), 0600 )
7683 if err != nil {
7784 return fmt .Errorf ("failed to write README.md: %w" , err )
7885 }
7986
80- fmt .Println ("Successfully updated README.md with automated documentation" )
8187 return nil
8288}
8389
@@ -90,20 +96,12 @@ func generateRemoteServerDocs(docsPath string) error {
9096 toolsetsDoc := generateRemoteToolsetsDoc ()
9197
9298 // Replace content between markers
93- startMarker := "<!-- START AUTOMATED TOOLSETS -->"
94- endMarker := "<!-- END AUTOMATED TOOLSETS -->"
95-
96- contentStr := string (content )
97- startIndex := strings .Index (contentStr , startMarker )
98- endIndex := strings .Index (contentStr , endMarker )
99-
100- if startIndex == - 1 || endIndex == - 1 {
101- return fmt .Errorf ("automation markers not found in %s" , docsPath )
99+ updatedContent , err := replaceSection (string (content ), "START AUTOMATED TOOLSETS" , "END AUTOMATED TOOLSETS" , toolsetsDoc )
100+ if err != nil {
101+ return err
102102 }
103103
104- newContent := contentStr [:startIndex ] + startMarker + "\n " + toolsetsDoc + "\n " + endMarker + contentStr [endIndex + len (endMarker ):]
105-
106- return os .WriteFile (docsPath , []byte (newContent ), 0600 ) //#nosec G306
104+ return os .WriteFile (docsPath , []byte (updatedContent ), 0600 ) //#nosec G306
107105}
108106
109107func generateToolsetsDoc (tsg * toolsets.ToolsetGroup ) string {
@@ -275,15 +273,24 @@ func indentMultilineDescription(description, indent string) string {
275273 return buf .String ()
276274}
277275
278- func replaceSection (content , startMarker , endMarker , newContent string ) string {
279- startPattern := fmt .Sprintf (`<!-- %s -->` , regexp .QuoteMeta (startMarker ))
280- endPattern := fmt .Sprintf (`<!-- %s -->` , regexp .QuoteMeta (endMarker ))
281-
282- re := regexp .MustCompile (fmt .Sprintf (`(?s)%s.*?%s` , startPattern , endPattern ))
276+ func replaceSection (content , startMarker , endMarker , newContent string ) (string , error ) {
277+ start := fmt .Sprintf ("<!-- %s -->" , startMarker )
278+ end := fmt .Sprintf ("<!-- %s -->" , endMarker )
283279
284- replacement := fmt .Sprintf ("<!-- %s -->\n %s\n <!-- %s -->" , startMarker , newContent , endMarker )
280+ startIdx := strings .Index (content , start )
281+ endIdx := strings .Index (content , end )
282+ if startIdx == - 1 || endIdx == - 1 {
283+ return "" , fmt .Errorf ("markers not found: %s / %s" , start , end )
284+ }
285285
286- return re .ReplaceAllString (content , replacement )
286+ var buf strings.Builder
287+ buf .WriteString (content [:startIdx ])
288+ buf .WriteString (start )
289+ buf .WriteString ("\n " )
290+ buf .WriteString (newContent )
291+ buf .WriteString ("\n " )
292+ buf .WriteString (content [endIdx :])
293+ return buf .String (), nil
287294}
288295
289296func generateRemoteToolsetsDoc () string {
@@ -346,15 +353,17 @@ func generateDeprecatedAliasesDocs(docsPath string) error {
346353 aliasesDoc := generateDeprecatedAliasesTable ()
347354
348355 // Replace content between markers
349- updatedContent := replaceSection (string (content ), "START AUTOMATED ALIASES" , "END AUTOMATED ALIASES" , aliasesDoc )
356+ updatedContent , err := replaceSection (string (content ), "START AUTOMATED ALIASES" , "END AUTOMATED ALIASES" , aliasesDoc )
357+ if err != nil {
358+ return err
359+ }
350360
351361 // Write back to file
352362 err = os .WriteFile (docsPath , []byte (updatedContent ), 0600 )
353363 if err != nil {
354364 return fmt .Errorf ("failed to write deprecated aliases docs: %w" , err )
355365 }
356366
357- fmt .Println ("Successfully updated docs/deprecated-tool-aliases.md with automated documentation" )
358367 return nil
359368}
360369
0 commit comments