diff --git a/pkg/prebuild/directive/core.go b/pkg/prebuild/directive/core.go index d14dd486..b81ae3cc 100644 --- a/pkg/prebuild/directive/core.go +++ b/pkg/prebuild/directive/core.go @@ -61,11 +61,29 @@ func NewOption(file *paths.Path, match []string) *Option { } } -// Clean the selected directive from profile. +// Clean removes selected directive line from input string. // Useful to remove directive text applied on some condition only -func (o *Option) Clean(profile string) string { - reg := regexp.MustCompile(`\s*` + Keyword + o.Name + ` .*$`) - return strings.Replace(profile, o.Raw, reg.ReplaceAllString(o.Raw, ""), 1) +func (o *Option) Clean(input string) string { + return strings.Replace(input, o.Raw, o.cleanKeyword(o.Raw), 1) +} + +// cleanKeyword removes the dirextive keywork (#aa:...) from the input string +func (o *Option) cleanKeyword(input string) string { + reg := regexp.MustCompile(`\s*` + Keyword + o.Name + `( .*)?$`) + return reg.ReplaceAllString(input, "") +} + +// Check if the directive is inline or if it is a paragraph +func (o *Option) IsInline() bool { + inline := true + tmp := strings.Split(o.Raw, Keyword) + if len(tmp) >= 1 { + left := strings.TrimSpace(tmp[0]) + if len(left) == 0 { + inline = false + } + } + return inline } func RegisterDirective(d Directive) { diff --git a/pkg/prebuild/directive/filter.go b/pkg/prebuild/directive/filter.go index 39e6c006..1aa2e1c7 100644 --- a/pkg/prebuild/directive/filter.go +++ b/pkg/prebuild/directive/filter.go @@ -49,16 +49,7 @@ func filter(only bool, opt *Option, profile string) (string, error) { return opt.Clean(profile), nil } - inline := true - tmp := strings.Split(opt.Raw, Keyword) - if len(tmp) >= 1 { - left := strings.TrimSpace(tmp[0]) - if len(left) == 0 { - inline = false - } - } - - if inline { + if opt.IsInline() { profile = strings.Replace(profile, opt.Raw, "", -1) } else { regRemoveParagraph := regexp.MustCompile(`(?s)` + opt.Raw + `\n.*?\n\n`)