-
Notifications
You must be signed in to change notification settings - Fork 12
/
document.go
124 lines (108 loc) · 3.75 KB
/
document.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
package asciidocgo
// Asciidoc Document, onced loaded from an IO, string or array
type Document struct {
monitorData *monitorData
data []string
options map[string]string
}
type monitorData struct {
readTime int
parseTime int
renderTime int
writeTime int
}
// Error returned when accessing times on a Document not monitored
type NotMonitoredError struct {
msg string // description of error
}
// Print description of a non-monitored error
func (e *NotMonitoredError) Error() string { return e.msg }
// Check if a Document is supposed to be monitored
func (d *Document) IsMonitored() bool {
return (d.monitorData != nil)
}
// Setup a monitor for the document
// (or does nothing if the monitor already exists).
// Returns self, for easy composition
func (d *Document) Monitor() *Document {
if d.monitorData == nil {
d.monitorData = new(monitorData)
}
return d
}
/*
Initialize an Asciidoc object.
- data: The Array of Strings holding the Asciidoc source document. (default: [])
- options - A Hash of options to control processing, such as setting the safe mode (:safe), suppressing the header/footer (:header_footer) and attribute overrides (:attributes)
(default: {})
Examples
data = File.readlines(filename)
doc = Asciidoctor::Document.new(data)
puts doc.render
*/
func NewDocument(data []string, options map[string]string) *Document {
document := &Document{nil, data, options}
return document
}
// Time to read the document from IO source
// Error if document didn't activated the monitoring
func (d *Document) ReadTime() (readTime int, err error) {
if d.IsMonitored() == false {
return 0, &NotMonitoredError{"No readTime: current document is not monitored"}
}
return d.monitorData.readTime, nil
}
// Time to parse the document once read from IO source
// Error if document didn't activated the monitoring
func (d *Document) ParseTime() (parseTime int, err error) {
if d.IsMonitored() == false {
return 0, &NotMonitoredError{"No parseTime: current document is not monitored"}
}
return d.monitorData.parseTime, nil
}
// Load means Read plus Parse times
// Error if document didn't activated the monitoring
func (d *Document) LoadTime() (loadTime int, err error) {
if d.IsMonitored() == false {
return 0, &NotMonitoredError{"No loadTime: current document is not monitored"}
}
readTime, _ := d.ReadTime()
parseTime, _ := d.ParseTime()
return readTime + parseTime, nil
}
// Time to render the document once loaded
// Error if document didn't activated the monitoring
func (d *Document) RenderTime() (renderTime int, err error) {
if d.IsMonitored() == false {
return 0, &NotMonitoredError{"No ploadTime: current document is not monitored"}
}
return d.monitorData.renderTime, nil
}
// LoadRender means Load plus Render times
// Error if document didn't activated the monitoring
func (d *Document) LoadRenderTime() (loadRenderTime int, err error) {
if d.IsMonitored() == false {
return 0, &NotMonitoredError{"No loadTime: current document is not monitored"}
}
loadTime, _ := d.LoadTime()
renderTime, _ := d.RenderTime()
return loadTime + renderTime, nil
}
// Time to write the document once rendered
// Error if document didn't activated the monitoring
func (d *Document) WriteTime() (writeTime int, err error) {
if d.IsMonitored() == false {
return 0, &NotMonitoredError{"No ploadTime: current document is not monitored"}
}
return d.monitorData.writeTime, nil
}
// Total means LoadRender plus Write times
// Error if document didn't activated the monitoring
func (d *Document) TotalTime() (totalTime int, err error) {
if d.IsMonitored() == false {
return 0, &NotMonitoredError{"No loadTime: current document is not monitored"}
}
loadRenderTime, _ := d.LoadRenderTime()
writeTime, _ := d.WriteTime()
return loadRenderTime + writeTime, nil
}