mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-12 19:05:55 +02:00
refactor: nginx conf parser #45
This commit is contained in:
parent
b7d5590714
commit
5cc9068f5f
27 changed files with 942 additions and 1040 deletions
|
@ -1,49 +1,10 @@
|
|||
package nginx
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"github.com/emirpasic/gods/stacks/linkedliststack"
|
||||
"strings"
|
||||
"github.com/tufanbarisyildirim/gonginx"
|
||||
)
|
||||
|
||||
func fmtCode(content string) (fmtContent string) {
|
||||
fmtContent = fmtCodeWithIndent(content, 0)
|
||||
return
|
||||
}
|
||||
|
||||
func fmtCodeWithIndent(content string, indent int) (fmtContent string) {
|
||||
/*
|
||||
Format content
|
||||
1. TrimSpace for each line
|
||||
2. use stack to count how many \t should add
|
||||
*/
|
||||
stack := linkedliststack.New()
|
||||
|
||||
scanner := bufio.NewScanner(strings.NewReader(content))
|
||||
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
text = strings.TrimSpace(text)
|
||||
|
||||
before := stack.Size()
|
||||
|
||||
for _, char := range text {
|
||||
matchParentheses(stack, char)
|
||||
}
|
||||
|
||||
after := stack.Size()
|
||||
|
||||
fmtContent += strings.Repeat("\t", indent)
|
||||
|
||||
if before == after {
|
||||
fmtContent += strings.Repeat("\t", stack.Size()) + text + "\n"
|
||||
} else {
|
||||
fmtContent += text + "\n"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fmtContent = strings.Trim(fmtContent, "\n")
|
||||
|
||||
return
|
||||
func (c *NgxConfig) FmtCode() (fmtContent string) {
|
||||
fmtContent = gonginx.DumpConfig(c.c, gonginx.IndentedStyle)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue