build: improve internal directive tool.

This commit is contained in:
Alexandre Pujol 2024-09-26 22:08:48 +01:00
parent 6f5604d59d
commit 05a489e021
No known key found for this signature in database
GPG Key ID: C5469996F0DF68EC
2 changed files with 23 additions and 14 deletions

View File

@ -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) {

View File

@ -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`)