@@ -4,21 +4,12 @@ package main
4
4
// Dylan Whichard, and contributors; (GPLv3) see LICENSE or doc.go
5
5
6
6
import (
7
- "html/template"
8
- "io"
7
+ "github.com/SashaCrofter/staticdir"
9
8
"io/ioutil"
10
- "os"
11
- "path"
12
- "strings"
13
9
)
14
10
15
- // CompileStatic traverses a directory, gathering a list of all
16
- // files. It then performs actions based on their filenames, and
17
- // copies them to a temporary directory. It returns the name of the
18
- // temporary directory.
19
- //
20
- // Files with the ".tmpl" extention are run through 'html/template'
21
- // engine with the data argument being the supplied *Config.
11
+ // CompileStatic uses staticdir to translate a resource directory into
12
+ // static content. It returns the directory into which it copies.
22
13
func CompileStatic (dir string , conf * Config ) (tmpdir string , err error ) {
23
14
// Create a directory in the OS's tempdir, (`/tmp`), with the
24
15
// prefix being the name of the map.
@@ -27,144 +18,13 @@ func CompileStatic(dir string, conf *Config) (tmpdir string, err error) {
27
18
return
28
19
}
29
20
30
- // Next, get a slice of all files in the directory, and any in its
31
- // subdirectories (recursively).
32
- files := make ([]string , 0 )
33
- err = crawlDirectories (& files , tmpdir , dir )
34
- if err != nil {
35
- return
36
- }
37
-
38
- // Now, process the files one at a time, writing the result to
39
- // `<tmpdir>/path/to/file`. The path is the path to the file
40
- // within the given dir.
41
- err = processFiles (files , conf , tmpdir , dir )
42
- if err != nil {
43
- return
44
- }
45
-
46
- return
47
- }
48
-
49
- // processFiles uses the file extension(s) to transform each file,
50
- // placing the result in the outdir with the same path, with the
51
- // prefix sliced off from the filename. Templates are executed with
52
- // conf as data.It will panic if any of the files are shorter than
53
- // len(prefix).
54
- func processFiles (files []string , conf * Config ,
55
- outdir , prefix string ) error {
56
-
57
- tmpldata := struct {
21
+ // Create a translator via package staticdir.
22
+ t := staticdir .New (dir , tmpdir )
23
+ t .CopyFunc = staticdir .TemplateCopy
24
+ t .CopyData = struct {
58
25
* Config
59
26
Version string
60
27
}{conf , Version }
61
28
62
- for _ , filename := range files {
63
- // Now, process based on file extension.
64
- switch path .Ext (filename ) {
65
- case ".tmpl" :
66
- // First, open the outfile. html/template handles the
67
- // infile. Note that it strips out the ".tmpl" extension.
68
- out , err := os .Create (path .Join (outdir ,
69
- filename [len (prefix ):len (filename )- 5 ]))
70
- if err != nil {
71
- return err
72
- }
73
- defer out .Close ()
74
-
75
- // Next, parse the template from the file.
76
- t , err := template .ParseFiles (filename )
77
- if err != nil {
78
- return err
79
- }
80
-
81
- // Finally, write it to the file using conf as data.
82
- err = t .Execute (out , tmpldata )
83
- if err != nil {
84
- return err
85
- }
86
- default :
87
- // Begin by opening the in file and creating the out file.
88
- in , err := os .Open (filename )
89
- if err != nil {
90
- return err
91
- }
92
- defer in .Close ()
93
- out , err := os .Create (path .Join (outdir , filename [len (prefix ):]))
94
- if err != nil {
95
- return err
96
- }
97
- defer out .Close ()
98
-
99
- // If the file extension was not recognized, just copy it
100
- // directly.
101
- _ , err = io .Copy (out , in )
102
- if err != nil {
103
- return err
104
- }
105
- }
106
- }
107
- return nil
108
- }
109
-
110
- // crawlDirectories uses os.Readdir() on the given dir to retrieve a
111
- // list of os.FileInfo types. It goes through that list, sorting them
112
- // by file extension and inserting them in the given list of files. If
113
- // it encounters a directory, it creates that directory under the
114
- // tmpdir, and recursively calls itself until it has a listed every
115
- // file.
116
- func crawlDirectories (files * []string , tmpdir , dir string ) (err error ) {
117
- // First, open the file so that we can use Readdir().
118
- f , err := os .Open (dir )
119
- if err != nil {
120
- return
121
- }
122
-
123
- // Next, get a list of os.FileInfo types, and close the file as
124
- // soon as possible.
125
- fis , err := f .Readdir (0 )
126
- f .Close ()
127
- if err != nil {
128
- return
129
- }
130
-
131
- // Initialize a slice for new filenames with length zero, and
132
- // capacity len(fi). That way, actual files can be appended easily
133
- // without leaving blanks.
134
- newFiles := make ([]string , 0 , len (fis ))
135
-
136
- // Loop through the slice of os.FileInfo types and check if each
137
- // is a directory. If it isn't, append it to newFiles. If it is,
138
- // call crawlDirectories() on it again.
139
- for _ , fi := range fis {
140
- if len (fi .Name ()) > 0 &&
141
- (strings .HasPrefix (fi .Name (), "." ) ||
142
- strings .HasPrefix (fi .Name (), "#" ) ||
143
- strings .HasSuffix (fi .Name (), "~" )) {
144
- // Make sure not to include files that start with . or #
145
- // Also make sure not to include files that end in ~
146
- } else if ! fi .IsDir () {
147
- // If a file...
148
- newFiles = append (newFiles , path .Join (dir , fi .Name ()))
149
- } else {
150
- // If a directory...
151
- newdir := path .Join (tmpdir , fi .Name ())
152
- err = os .Mkdir (newdir , 0777 )
153
- if err != nil {
154
- return
155
- }
156
- // Here, we tell the next call that the tmpdir is the one
157
- // we just created in our tmpdir, and the directory to
158
- // crawl is the one we just discovered.
159
- err = crawlDirectories (files ,
160
- newdir , path .Join (dir , fi .Name ()))
161
- if err != nil {
162
- return
163
- }
164
- }
165
- }
166
-
167
- // Finally, append newFiles to the target slice.
168
- * files = append (* files , newFiles ... )
169
- return
29
+ return tmpdir , t .Translate ()
170
30
}
0 commit comments