@@ -5,46 +5,31 @@ import (
55 "fmt"
66 "os"
77 "path/filepath"
8- "regexp"
98 "sort"
109 "strings"
1110
1211 "github.com/mongodb/code-example-tooling/audit-cli/internal/pathresolver"
1312 "github.com/mongodb/code-example-tooling/audit-cli/internal/rst"
1413)
1514
16- // Regular expressions for matching directives
17- var (
18- // includeRegex matches: .. include:: /path/to/file.rst
19- includeRegex = regexp .MustCompile (`^\.\.\s+include::\s+(.+)$` )
20-
21- // literalIncludeRegex matches: .. literalinclude:: /path/to/file.ext
22- literalIncludeRegex = regexp .MustCompile (`^\.\.\s+literalinclude::\s+(.+)$` )
23-
24- // ioCodeBlockRegex matches: .. io-code-block::
25- ioCodeBlockRegex = regexp .MustCompile (`^\.\.\s+io-code-block::` )
26-
27- // inputRegex matches: .. input:: /path/to/file.ext (within io-code-block)
28- inputRegex = regexp .MustCompile (`^\.\.\s+input::\s+(.+)$` )
29-
30- // outputRegex matches: .. output:: /path/to/file.ext (within io-code-block)
31- outputRegex = regexp .MustCompile (`^\.\.\s+output::\s+(.+)$` )
32- )
33-
3415// AnalyzeReferences finds all files that reference the target file.
3516//
3617// This function searches through all RST files (.rst, .txt) and YAML files (.yaml, .yml)
3718// in the source directory to find files that reference the target file using include,
3819// literalinclude, or io-code-block directives. YAML files are included because extract
3920// and release files contain RST directives within their content blocks.
4021//
22+ // By default, only content inclusion directives are searched. Set includeToctree to true
23+ // to also search for toctree entries (navigation links).
24+ //
4125// Parameters:
4226// - targetFile: Absolute path to the file to analyze
27+ // - includeToctree: If true, include toctree entries in the search
4328//
4429// Returns:
4530// - *ReferenceAnalysis: The analysis results
4631// - error: Any error encountered during analysis
47- func AnalyzeReferences (targetFile string ) (* ReferenceAnalysis , error ) {
32+ func AnalyzeReferences (targetFile string , includeToctree bool ) (* ReferenceAnalysis , error ) {
4833 // Get absolute path
4934 absTargetFile , err := filepath .Abs (targetFile )
5035 if err != nil {
@@ -83,7 +68,7 @@ func AnalyzeReferences(targetFile string) (*ReferenceAnalysis, error) {
8368 }
8469
8570 // Search for references in this file
86- refs , err := findReferencesInFile (path , absTargetFile , sourceDir )
71+ refs , err := findReferencesInFile (path , absTargetFile , sourceDir , includeToctree )
8772 if err != nil {
8873 // Log error but continue processing other files
8974 fmt .Fprintf (os .Stderr , "Warning: failed to process %s: %v\n " , path , err )
@@ -110,17 +95,19 @@ func AnalyzeReferences(targetFile string) (*ReferenceAnalysis, error) {
11095// findReferencesInFile searches a single file for references to the target file.
11196//
11297// This function scans through the file line by line looking for include,
113- // literalinclude, io-code-block, and toctree directives that reference the target file.
98+ // literalinclude, and io-code-block directives that reference the target file.
99+ // If includeToctree is true, also searches for toctree entries.
114100//
115101// Parameters:
116102// - filePath: Path to the file to search
117103// - targetFile: Absolute path to the target file
118104// - sourceDir: Source directory (for resolving relative paths)
105+ // - includeToctree: If true, include toctree entries in the search
119106//
120107// Returns:
121108// - []FileReference: List of references found in this file
122109// - error: Any error encountered during processing
123- func findReferencesInFile (filePath , targetFile , sourceDir string ) ([]FileReference , error ) {
110+ func findReferencesInFile (filePath , targetFile , sourceDir string , includeToctree bool ) ([]FileReference , error ) {
124111 file , err := os .Open (filePath )
125112 if err != nil {
126113 return nil , err
@@ -140,15 +127,15 @@ func findReferencesInFile(filePath, targetFile, sourceDir string) ([]FileReferen
140127 line := scanner .Text ()
141128 trimmedLine := strings .TrimSpace (line )
142129
143- // Check for toctree start (use shared regex from rst package )
144- if rst .ToctreeDirectiveRegex .MatchString (trimmedLine ) {
130+ // Check for toctree start (only if includeToctree is enabled )
131+ if includeToctree && rst .ToctreeDirectiveRegex .MatchString (trimmedLine ) {
145132 inToctree = true
146133 toctreeStartLine = lineNum
147134 continue
148135 }
149136
150137 // Check for io-code-block start
151- if ioCodeBlockRegex .MatchString (trimmedLine ) {
138+ if rst . IOCodeBlockDirectiveRegex .MatchString (trimmedLine ) {
152139 inIOCodeBlock = true
153140 ioCodeBlockStartLine = lineNum
154141 continue
@@ -165,7 +152,7 @@ func findReferencesInFile(filePath, targetFile, sourceDir string) ([]FileReferen
165152 }
166153
167154 // Check for include directive
168- if matches := includeRegex .FindStringSubmatch (trimmedLine ); matches != nil {
155+ if matches := rst . IncludeDirectiveRegex .FindStringSubmatch (trimmedLine ); matches != nil {
169156 refPath := strings .TrimSpace (matches [1 ])
170157 if referencesTarget (refPath , targetFile , sourceDir , filePath ) {
171158 references = append (references , FileReference {
@@ -179,7 +166,7 @@ func findReferencesInFile(filePath, targetFile, sourceDir string) ([]FileReferen
179166 }
180167
181168 // Check for literalinclude directive
182- if matches := literalIncludeRegex .FindStringSubmatch (trimmedLine ); matches != nil {
169+ if matches := rst . LiteralIncludeDirectiveRegex .FindStringSubmatch (trimmedLine ); matches != nil {
183170 refPath := strings .TrimSpace (matches [1 ])
184171 if referencesTarget (refPath , targetFile , sourceDir , filePath ) {
185172 references = append (references , FileReference {
@@ -195,7 +182,7 @@ func findReferencesInFile(filePath, targetFile, sourceDir string) ([]FileReferen
195182 // Check for input/output directives within io-code-block
196183 if inIOCodeBlock {
197184 // Check for input directive
198- if matches := inputRegex .FindStringSubmatch (trimmedLine ); matches != nil {
185+ if matches := rst . InputDirectiveRegex .FindStringSubmatch (trimmedLine ); matches != nil {
199186 refPath := strings .TrimSpace (matches [1 ])
200187 if referencesTarget (refPath , targetFile , sourceDir , filePath ) {
201188 references = append (references , FileReference {
@@ -209,7 +196,7 @@ func findReferencesInFile(filePath, targetFile, sourceDir string) ([]FileReferen
209196 }
210197
211198 // Check for output directive
212- if matches := outputRegex .FindStringSubmatch (trimmedLine ); matches != nil {
199+ if matches := rst . OutputDirectiveRegex .FindStringSubmatch (trimmedLine ); matches != nil {
213200 refPath := strings .TrimSpace (matches [1 ])
214201 if referencesTarget (refPath , targetFile , sourceDir , filePath ) {
215202 references = append (references , FileReference {
0 commit comments