/
file.go
177 lines (148 loc) · 3.14 KB
/
file.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"github.com/ahmetalpbalkan/go-linq"
"os"
"path/filepath"
"reflect"
"strings"
"time"
)
const (
FileTypeMarkdown = iota
FileTypeYAMLConf
FileTypeTemplate
FileTypeHTML
FileTypePlain
)
type FileMeta map[string]interface{}
type File struct {
Dir string
Base string
Ext string
Type int
Meta FileMeta
Info os.FileInfo
Content []byte
}
type Files []*File
func (fs Files) Filter(args ...interface{}) linq.Query {
var query linq.Query
queryOrT := args[len(args)-1]
if _, ok := queryOrT.(linq.Query); ok {
query = queryOrT.(linq.Query)
} else {
query = linq.From(fs)
}
var _args []string
for _, a := range args[:len(args)-1] {
_args = append(_args, a.(string))
}
exprs, err := parseExprs(_args)
if err != nil {
panic(err)
}
for _, expr := range exprs {
whereFunc := expr.WhereFunc()
query = query.Where(whereFunc)
}
return query
}
func (fs Files) Sort(args ...interface{}) linq.Query {
query := args[len(args)-1].(linq.Query)
var _args []string
for _, a := range args[:len(args)-1] {
_args = append(_args, a.(string))
}
exprs, err := parseExprs(_args)
if err != nil {
panic(err)
}
for _, expr := range exprs {
orderByFunc := expr.OrderByFunc()
query = query.OrderBy(orderByFunc)
}
return query
}
func (fs Files) Group(key string, query linq.Query) map[linq.T][]linq.T {
groupByFunc := func(t linq.T) linq.T {
file := *(t.(*File))
keyValue := reflect.ValueOf(file).FieldByName(key)
return keyValue
}
q, err := query.GroupBy(groupByFunc, groupByFunc)
if err != nil {
panic(err)
}
return q
}
func (fs Files) Results(query linq.Query) []linq.T {
results, err := query.Results()
if err != nil {
panic(err)
}
return results
}
func (fs Files) Count(query linq.Query) int {
cnt, err := query.Count()
if err != nil {
panic(err)
}
return cnt
}
func NewFile(path string, info os.FileInfo) *File {
dir := strings.Replace(filepath.Dir(path), SrcPath, "", 1)
if dir == "" {
dir = "/"
}
base := filepath.Base(path)
ext := filepath.Ext(path)
file := &File{Dir: dir, Base: base, Ext: ext, Info: info, Meta: make(map[string]interface{})}
var fileType int
switch ext {
case ".md":
fileType = FileTypeMarkdown
case ".tmpl":
fileType = FileTypeTemplate
case ".html":
fileType = FileTypeHTML
default:
fileType = FileTypePlain
}
if base == "_.yaml" {
fileType = FileTypeYAMLConf
}
file.Type = fileType
return file
}
func (f File) IsWrite() bool {
if f.Type == FileTypeYAMLConf || f.Type == FileTypeTemplate {
return false
}
return true
}
func (f File) IsReadContent() bool {
if f.Type == FileTypeTemplate {
return false
}
return true
}
func (f File) Path() string {
return filepath.Join(SrcPath, f.Dir, f.Base)
}
func (f File) Url() string {
var fileName string
if f.Ext == ".md" {
fileName = filepath.Join("/", f.Dir, strings.Replace(f.Base, f.Ext, ".html", 1))
} else {
fileName = filepath.Join("/", f.Dir, f.Base)
}
return fileName
}
func (f File) Date() (time.Time, error) {
const layout = "2006-01-01"
if date, ok := f.Meta["date"]; ok {
t, err := time.Parse(layout, date.(string))
return t, err
}
return f.Info.ModTime(), nil
}