collectLocalIncludeDependenciesRecursive recursively processes @include directives in package content
(content, baseDir string, dependencies *[]IncludeDependency, seen map[string]struct {
}, verbose bool)
| 41 | |
| 42 | // collectLocalIncludeDependenciesRecursive recursively processes @include directives in package content |
| 43 | func collectLocalIncludeDependenciesRecursive(content, baseDir string, dependencies *[]IncludeDependency, seen map[string]struct { |
| 44 | }, verbose bool) error { |
| 45 | scanner := bufio.NewScanner(strings.NewReader(content)) |
| 46 | for scanner.Scan() { |
| 47 | line := scanner.Text() |
| 48 | if matches := includePattern.FindStringSubmatch(line); matches != nil { |
| 49 | isOptional := matches[1] == "?" |
| 50 | includePath := strings.TrimSpace(matches[2]) |
| 51 | |
| 52 | // Handle section references (file.md#Section) |
| 53 | var filePath string |
| 54 | if strings.Contains(includePath, "#") { |
| 55 | parts := strings.SplitN(includePath, "#", 2) |
| 56 | filePath = parts[0] |
| 57 | } else { |
| 58 | filePath = includePath |
| 59 | } |
| 60 | |
| 61 | // Resolve the full source path relative to base directory |
| 62 | fullSourcePath := filepath.Join(baseDir, filePath) |
| 63 | |
| 64 | // Skip if we've already processed this file |
| 65 | if setutil.Contains(seen, fullSourcePath) { |
| 66 | continue |
| 67 | } |
| 68 | seen[fullSourcePath] = struct { |
| 69 | }{} |
| 70 | |
| 71 | // Add dependency |
| 72 | dep := IncludeDependency{ |
| 73 | SourcePath: fullSourcePath, |
| 74 | TargetPath: filePath, // Keep relative path for target |
| 75 | IsOptional: isOptional, |
| 76 | } |
| 77 | *dependencies = append(*dependencies, dep) |
| 78 | |
| 79 | if verbose { |
| 80 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Found include dependency: %s -> %s", fullSourcePath, filePath))) |
| 81 | } |
| 82 | |
| 83 | // Read the included file and process its includes recursively |
| 84 | includedContent, err := os.ReadFile(fullSourcePath) |
| 85 | if err != nil { |
| 86 | if verbose { |
| 87 | fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Could not read include file %s: %v", fullSourcePath, err))) |
| 88 | } |
| 89 | continue |
| 90 | } |
| 91 | |
| 92 | // Extract markdown content from the included file |
| 93 | markdownContent, err := parser.ExtractMarkdownContent(string(includedContent)) |
| 94 | if err != nil { |
| 95 | if verbose { |
| 96 | fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("Could not extract markdown from %s: %v", fullSourcePath, err))) |
| 97 | } |
| 98 | continue |
| 99 | } |
| 100 |