fix: custom states not parsed correctly has been fixed (#4)

This commit is contained in:
Rasmus Wejlgaard 2025-11-08 22:21:00 +00:00 committed by GitHub
parent eb5f9a16ce
commit 2f98d8e0f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 3 deletions

View file

@ -40,7 +40,7 @@ func main() {
}
// Parse the org file
orgFile, err := parser.ParseOrgFile(filePath)
orgFile, err := parser.ParseOrgFile(filePath, cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing org file: %v\n", err)
os.Exit(1)

View file

@ -6,12 +6,12 @@ import (
"regexp"
"strings"
"github.com/rwejlgaard/org/internal/config"
"github.com/rwejlgaard/org/internal/model"
)
// Parser patterns
var (
headingPattern = regexp.MustCompile(`^(\*+)\s+(?:(TODO|PROG|BLOCK|DONE)\s+)?(?:\[#([A-C])\]\s+)?(.+?)(?:\s+(:[[:alnum:]_@#%:]+:)\s*)?$`)
scheduledPattern = regexp.MustCompile(`SCHEDULED:\s*<([^>]+)>`)
deadlinePattern = regexp.MustCompile(`DEADLINE:\s*<([^>]+)>`)
clockPattern = regexp.MustCompile(`CLOCK:\s*\[([^\]]+)\](?:--\[([^\]]+)\])?`)
@ -23,8 +23,29 @@ var (
codeBlockEnd = regexp.MustCompile(`^\s*#\+END_SRC`)
)
// buildHeadingPattern creates a regex pattern that matches configured states
func buildHeadingPattern(cfg *config.Config) *regexp.Regexp {
stateNames := cfg.GetStateNames()
var statesPattern string
if len(stateNames) > 0 {
// Escape state names and join with |
escapedStates := make([]string, len(stateNames))
for i, state := range stateNames {
escapedStates[i] = regexp.QuoteMeta(state)
}
statesPattern = strings.Join(escapedStates, "|")
} else {
// Fallback to default states if none configured
statesPattern = "TODO|PROG|BLOCK|DONE"
}
pattern := `^(\*+)\s+(?:(` + statesPattern + `)\s+)?(?:\[#([A-C])\]\s+)?(.+?)(?:\s+(:[[:alnum:]_@#%:]+:)\s*)?$`
return regexp.MustCompile(pattern)
}
// ParseOrgFile reads and parses an org-mode file
func ParseOrgFile(path string) (*model.OrgFile, error) {
func ParseOrgFile(path string, cfg *config.Config) (*model.OrgFile, error) {
headingPattern := buildHeadingPattern(cfg)
file, err := os.Open(path)
if err != nil {
// If file doesn't exist, return empty org file