Skip to content

Commit c5d7e67

Browse files
Copilotmnkiefer
andcommitted
Phase 1: Extract import_directive.go (57 lines)
Co-authored-by: mnkiefer <[email protected]>
1 parent cc3e260 commit c5d7e67

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

pkg/parser/frontmatter.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"fmt"
88
"os"
99
"path/filepath"
10-
"regexp"
1110
"strings"
1211

1312
"github.com/githubnext/gh-aw/pkg/console"
@@ -17,57 +16,6 @@ import (
1716

1817
var log = logger.New("parser:frontmatter")
1918

20-
// IncludeDirectivePattern matches @include, @import (deprecated), or {{#import (new) directives
21-
// The colon after #import is optional and ignored if present
22-
var IncludeDirectivePattern = regexp.MustCompile(`^(?:@(?:include|import)(\?)?\s+(.+)|{{#import(\?)?\s*:?\s*(.+?)\s*}})$`)
23-
24-
// LegacyIncludeDirectivePattern matches only the deprecated @include and @import directives
25-
var LegacyIncludeDirectivePattern = regexp.MustCompile(`^@(?:include|import)(\?)?\s+(.+)$`)
26-
27-
// ImportDirectiveMatch holds the parsed components of an import directive
28-
type ImportDirectiveMatch struct {
29-
IsOptional bool
30-
Path string
31-
IsLegacy bool
32-
Original string
33-
}
34-
35-
// ParseImportDirective parses an import directive and returns its components
36-
func ParseImportDirective(line string) *ImportDirectiveMatch {
37-
trimmedLine := strings.TrimSpace(line)
38-
39-
// Check if it matches the import pattern at all
40-
matches := IncludeDirectivePattern.FindStringSubmatch(trimmedLine)
41-
if matches == nil {
42-
return nil
43-
}
44-
45-
// Check if it's legacy syntax
46-
isLegacy := LegacyIncludeDirectivePattern.MatchString(trimmedLine)
47-
48-
var isOptional bool
49-
var path string
50-
51-
if isLegacy {
52-
// Legacy syntax: @include? path or @import? path
53-
// Group 1: optional marker, Group 2: path
54-
isOptional = matches[1] == "?"
55-
path = strings.TrimSpace(matches[2])
56-
} else {
57-
// New syntax: {{#import?: path}} or {{#import: path}} (colon is optional)
58-
// Group 3: optional marker, Group 4: path
59-
isOptional = matches[3] == "?"
60-
path = strings.TrimSpace(matches[4])
61-
}
62-
63-
return &ImportDirectiveMatch{
64-
IsOptional: isOptional,
65-
Path: path,
66-
IsLegacy: isLegacy,
67-
Original: trimmedLine,
68-
}
69-
}
70-
7119
// ImportsResult holds the result of processing imports from frontmatter
7220
type ImportsResult struct {
7321
MergedTools string // Merged tools configuration from all imports

pkg/parser/import_directive.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package parser
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
)
7+
8+
// IncludeDirectivePattern matches @include, @import (deprecated), or {{#import (new) directives
9+
// The colon after #import is optional and ignored if present
10+
var IncludeDirectivePattern = regexp.MustCompile(`^(?:@(?:include|import)(\?)?\s+(.+)|{{#import(\?)?\s*:?\s*(.+?)\s*}})$`)
11+
12+
// LegacyIncludeDirectivePattern matches only the deprecated @include and @import directives
13+
var LegacyIncludeDirectivePattern = regexp.MustCompile(`^@(?:include|import)(\?)?\s+(.+)$`)
14+
15+
// ImportDirectiveMatch holds the parsed components of an import directive
16+
type ImportDirectiveMatch struct {
17+
IsOptional bool
18+
Path string
19+
IsLegacy bool
20+
Original string
21+
}
22+
23+
// ParseImportDirective parses an import directive and returns its components
24+
func ParseImportDirective(line string) *ImportDirectiveMatch {
25+
trimmedLine := strings.TrimSpace(line)
26+
27+
// Check if it matches the import pattern at all
28+
matches := IncludeDirectivePattern.FindStringSubmatch(trimmedLine)
29+
if matches == nil {
30+
return nil
31+
}
32+
33+
// Check if it's legacy syntax
34+
isLegacy := LegacyIncludeDirectivePattern.MatchString(trimmedLine)
35+
36+
var isOptional bool
37+
var path string
38+
39+
if isLegacy {
40+
// Legacy syntax: @include? path or @import? path
41+
// Group 1: optional marker, Group 2: path
42+
isOptional = matches[1] == "?"
43+
path = strings.TrimSpace(matches[2])
44+
} else {
45+
// New syntax: {{#import?: path}} or {{#import: path}} (colon is optional)
46+
// Group 3: optional marker, Group 4: path
47+
isOptional = matches[3] == "?"
48+
path = strings.TrimSpace(matches[4])
49+
}
50+
51+
return &ImportDirectiveMatch{
52+
IsOptional: isOptional,
53+
Path: path,
54+
IsLegacy: isLegacy,
55+
Original: trimmedLine,
56+
}
57+
}

0 commit comments

Comments
 (0)