build: improve internal directive tool.
This commit is contained in:
parent
6f5604d59d
commit
05a489e021
2 changed files with 23 additions and 14 deletions
|
|
@ -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
|
// Useful to remove directive text applied on some condition only
|
||||||
func (o *Option) Clean(profile string) string {
|
func (o *Option) Clean(input string) string {
|
||||||
reg := regexp.MustCompile(`\s*` + Keyword + o.Name + ` .*$`)
|
return strings.Replace(input, o.Raw, o.cleanKeyword(o.Raw), 1)
|
||||||
return strings.Replace(profile, o.Raw, reg.ReplaceAllString(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) {
|
func RegisterDirective(d Directive) {
|
||||||
|
|
|
||||||
|
|
@ -49,16 +49,7 @@ func filter(only bool, opt *Option, profile string) (string, error) {
|
||||||
return opt.Clean(profile), nil
|
return opt.Clean(profile), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
inline := true
|
if opt.IsInline() {
|
||||||
tmp := strings.Split(opt.Raw, Keyword)
|
|
||||||
if len(tmp) >= 1 {
|
|
||||||
left := strings.TrimSpace(tmp[0])
|
|
||||||
if len(left) == 0 {
|
|
||||||
inline = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if inline {
|
|
||||||
profile = strings.Replace(profile, opt.Raw, "", -1)
|
profile = strings.Replace(profile, opt.Raw, "", -1)
|
||||||
} else {
|
} else {
|
||||||
regRemoveParagraph := regexp.MustCompile(`(?s)` + opt.Raw + `\n.*?\n\n`)
|
regRemoveParagraph := regexp.MustCompile(`(?s)` + opt.Raw + `\n.*?\n\n`)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue