-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.go
90 lines (76 loc) · 1.94 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package muskel
import (
"fmt"
"path"
"path/filepath"
"strings"
"gitlab.com/gomidi/muskel/score"
)
func init() {
score.FindInclude = findInclude
}
var FILE_EXTENSION = ".mskl"
func absNormalized(file string) (string, error) {
s, err := filepath.Abs(file)
if err != nil {
return "", err
}
return strings.ReplaceAll(s, "\\", "/"), nil
}
func _findInclude(relDir string, file string) (resolved string, err error) {
if filepath.IsAbs(file) {
if score.FileExists(file) {
return absNormalized(file)
}
return "", fmt.Errorf("file not found: %q (abs path)", file)
}
relDir = strings.ReplaceAll(relDir, "\\", "/")
file = strings.ReplaceAll(file, "\\", "/")
try0 := path.Join(relDir, file)
try0 = strings.ReplaceAll(try0, "\\", "/")
if score.FileExists(try0) {
//fmt.Printf("found: %q\n", try0)
return absNormalized(try0)
}
try1 := path.Join(WORKING_DIR, file)
try1 = strings.ReplaceAll(try1, "\\", "/")
if score.FileExists(try1) {
//fmt.Printf("found: %q\n", try1)
return absNormalized(try1)
}
try2 := path.Join(USER_DIR, file)
try2 = strings.ReplaceAll(try2, "\\", "/")
if score.FileExists(try2) {
//fmt.Printf("found: %q\n", try2)
return absNormalized(try2)
}
return "", fmt.Errorf("file not found: %q", file)
}
func findInclude(relDir string, file string) (resolved string, err error) {
if file == "" {
panic("can't find empty file")
}
switch path.Ext(file) {
case FILE_EXTENSION, ".md", ".mskl", ".xlsx", ".csv":
return _findInclude(relDir, file)
default:
fallbacks := []string{FILE_EXTENSION}
switch FILE_EXTENSION {
case ".md":
fallbacks = append(fallbacks, ".mskl")
case ".mskl":
fallbacks = append(fallbacks, ".md")
case ".csv", ".xlsx":
fallbacks = append(fallbacks, ".md")
fallbacks = append(fallbacks, ".mskl")
}
for _, ext := range fallbacks {
resolved, err = _findInclude(relDir, file+ext)
if err == nil {
//fmt.Printf("found: %q\n", resolved)
return
}
}
return
}
}