-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.go
112 lines (91 loc) · 2.7 KB
/
converter.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
package vorlage
import (
vorlageproc "ellem.so/vorlageproc"
"os"
"regexp"
)
// its a io.Reader that will read from the file but will NOT read the macros.
type File interface {
// n will sometimes be < len(p) but that does not mean it's the end of the
// file. Only when 0, io.EOF is returned will it be the end of the file.
// As per io.Reader definition, Read can an will return a non nil error
// with n > 0
Read(p []byte) (n int, err error)
// returns to the beginning of the file
Reset() error
// must be called when conversion is done.
Close() error
}
// nonConvertedFile means the file to which was originally supplied by
// the user will be the one to which will be outputted.
type nonConvertedFile struct {
bytesRead int64
// remember to use the sourceFile fore doing file ops. Ie. calling
// sourceDocument.Read in nonConvertedFile.Read will cause a recursive crash.
sourceDocument *Document
// the file to read, close, rewind.
sourceFile File
// used for drawParser
variableReadBuffer []byte
// buffer used to hold what was read from the file when reading from
// definitions
tmpBuff []byte
// will be nil if not currently reading.
currentlyReadingDef vorlageproc.Definition
// definitionStack will be used to check for circular definitions within
// nested normal definitions and will error out if it does detect one.
definitionStack *[]string
}
type osFileHandle struct {
*os.File
resetPos int64
}
func osFileToFile(file *os.File, resetPos int64) File {
return osFileHandle{file, resetPos}
}
func (o osFileHandle) Read(p []byte) (int, error) {
return o.File.Read(p)
}
func (o osFileHandle) Reset() error {
_, err := o.File.Seek(o.resetPos, 0)
return err
}
func (o osFileHandle) Close() error {
return o.File.Close()
}
type DCInfo struct {
PathQualifier regexp.Regexp
Description string
}
type Converter interface {
Startup() DCInfo
/*
* Convert must not be dynamic. It must return the same file if given
* the same file, as it will be cached and it is not guarenteed to be called
* every request.
*/
Convert(File) (File, error)
Shutdown() error
}
type myconvert struct {
}
func (m myconvert) Startup() DCInfo {
panic("implement me")
}
func (m myconvert) Convert(file File) (File, error) {
panic("implement me")
}
func (m myconvert) Shutdown() error {
panic("implement me")
}
var _ File = &nonConvertedFile{}
func (doc *Document) getConverted(sourceFile File) (converedFile File, err *Error) {
// todo: switch on the source file Name to find a good converted (haml->html)
file := nonConvertedFile{
sourceFile: sourceFile,
sourceDocument: doc,
variableReadBuffer: make([]byte, MaxVariableLength),
definitionStack: new([]string),
}
return &file, nil
}