added Apps config to pre format file
This commit is contained in:
parent
57cfdf3e16
commit
21b7e666bb
2 changed files with 99 additions and 9 deletions
17
README.md
17
README.md
|
@ -64,8 +64,23 @@ Hear goes content. It can be written in html, markdown or whatever what can be p
|
|||
|
||||
There are two main commands: `PopulateTree` to prepare source (format, add metadata and add Id) and separate images and other data files and `BuildTree` to get object of tree.
|
||||
|
||||
Also both commands use `Config` object that can be prepared by `NewConfig` command. With it can be configurable readable formats, attachment directory, custom meta and excludes (not movable files by regexp).
|
||||
Also both commands use `Config` object that can be prepared by `NewConfig` command. With it can be configurable readable formats, attachment directory, custom meta, excludes (not movable files by regexp) and list of functions to apply before file formatting (read bellow).
|
||||
|
||||
`Tree` object has methods: `FileById` to get `File` by Id, `Slice` to get sub-tree of given path and `Filter` to filter tree by filter.
|
||||
Filter contains array of meta key and searching value. `tag` key is searched as equal and other meta values of keys can contain part. If `-` is before key - filter is filter out.
|
||||
|
||||
By full file path and config meta there is possibility to get file params: `Id`, `Meta` and `Tags`:
|
||||
|
||||
```
|
||||
GetFileParams(fullPath string, meta []string) (string, []string, map[string]string, error)
|
||||
```
|
||||
|
||||
### Functions to apply before file formatting
|
||||
|
||||
There is possibility to add custom functions to apply before file formatting. That is passed with custom function what returns string.
|
||||
|
||||
```
|
||||
func(dir string, content string, info os.FileInfo) (string, error)
|
||||
```
|
||||
|
||||
`dir` is path to directory of file, `content` is file content and `info` is file info.
|
||||
|
|
91
tree.go
91
tree.go
|
@ -24,8 +24,11 @@ type Config struct {
|
|||
AttachmentDirName string
|
||||
CustomMeta map[string]func() string
|
||||
Excludes []string
|
||||
Apps map[string]appCallback
|
||||
}
|
||||
|
||||
type appCallback func(dir string, content string, info os.FileInfo) (string, error)
|
||||
|
||||
type File struct {
|
||||
Id string
|
||||
Name string
|
||||
|
@ -164,6 +167,13 @@ func PopulateTree(sourcePath string, meta []string, config Config) error {
|
|||
var err error
|
||||
var attachmentRegistry map[string]string
|
||||
|
||||
for _, app := range config.Apps {
|
||||
err = applyApp(app, sourcePath, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
attachmentRegistry, err = moveAttachments(sourcePath, config)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -250,6 +260,57 @@ func moveAttachments(dir string, config Config) (map[string]string, error) {
|
|||
return attachmentRegistry, err
|
||||
}
|
||||
|
||||
func applyApp(app appCallback, dir string, config Config) error {
|
||||
err := filepath.Walk(dir, func(fullPath string, info os.FileInfo, e error) error {
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
if info.Mode().IsRegular() && slices.Contains(config.ReadableFormats, path.Ext(fullPath)) {
|
||||
osf, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// remove all empty lines and separate split line
|
||||
content := ""
|
||||
format := true
|
||||
scanner := bufio.NewScanner(osf)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if line == "---" {
|
||||
format = false
|
||||
}
|
||||
|
||||
if format {
|
||||
line = strings.Trim(line, " ")
|
||||
if line != "" {
|
||||
content = content + "\n" + line
|
||||
}
|
||||
} else {
|
||||
content = content + "\n" + line
|
||||
}
|
||||
}
|
||||
|
||||
content, err = app(dir, content, info)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data := []byte(content)
|
||||
|
||||
err = os.WriteFile(fullPath, data, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func fixFormat(dir string, attachmentRegistry map[string]string, config Config) error {
|
||||
err := filepath.Walk(dir, func(fullPath string, info os.FileInfo, e error) error {
|
||||
if e != nil {
|
||||
|
@ -516,13 +577,30 @@ func readFile(file fs.DirEntry, fullPath string, category []string, meta []strin
|
|||
Name: file.Name(),
|
||||
FullPath: fullPath,
|
||||
Category: category,
|
||||
Meta: map[string]string{},
|
||||
}
|
||||
|
||||
id, tags, fileMeta, err := GetFileParams(fullPath, meta)
|
||||
if err != nil {
|
||||
return f, err
|
||||
}
|
||||
|
||||
f.Id = id
|
||||
f.Tags = tags
|
||||
f.Meta = fileMeta
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func GetFileParams(fullPath string, meta []string) (string, []string, map[string]string, error) {
|
||||
fileMeta := map[string]string{}
|
||||
tags := []string{}
|
||||
id := ""
|
||||
|
||||
osf, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
return File{}, err
|
||||
return id, tags, fileMeta, err
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(osf)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
for scanner.Scan() {
|
||||
|
@ -534,27 +612,24 @@ func readFile(file fs.DirEntry, fullPath string, category []string, meta []strin
|
|||
if strings.HasPrefix(line, "* tags:") {
|
||||
line = strings.TrimPrefix(line, "* tags:")
|
||||
t := strings.Split(line, ",")
|
||||
tags := []string{}
|
||||
for _, tag := range t {
|
||||
tags = append(tags, strings.Trim(tag, " "))
|
||||
}
|
||||
|
||||
f.Tags = tags
|
||||
}
|
||||
if strings.HasPrefix(line, "* id:") {
|
||||
line = strings.TrimPrefix(line, "* id:")
|
||||
f.Id = strings.Trim(line, " ")
|
||||
id = strings.Trim(line, " ")
|
||||
}
|
||||
for _, option := range meta {
|
||||
if strings.HasPrefix(line, "* "+option) {
|
||||
line = strings.TrimPrefix(line, "* "+option+":")
|
||||
f.Meta[option] = strings.Trim(line, " ")
|
||||
fileMeta[option] = strings.Trim(line, " ")
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = osf.Close()
|
||||
|
||||
return f, nil
|
||||
return id, tags, fileMeta, nil
|
||||
}
|
||||
|
||||
func ReadFileContent(file File) (string, error) {
|
||||
|
|
Loading…
Reference in a new issue