forked from benbjohnson/ego
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtext_block.go
More file actions
43 lines (39 loc) · 789 Bytes
/
text_block.go
File metadata and controls
43 lines (39 loc) · 789 Bytes
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
package egon
import (
"bytes"
"fmt"
)
// TextBlock represents a UTF-8 encoded block of text that is written to the writer as-is.
type TextBlock struct {
Pos Pos
Content string
}
func stripWhitespace(s string) string {
out := make([]byte, 0)
seenSpace := false
for _, c := range s {
switch c {
case '\t', '\n', '\v', '\f', '\r':
seenSpace = true
case ' ':
if !seenSpace {
out = append(out, byte(c))
seenSpace = true
}
default:
out = append(out, byte(c))
seenSpace = false
}
}
return string(out)
}
func (b *TextBlock) write(buf *bytes.Buffer) error {
if (Config.Minify) {
b.Content = stripWhitespace(b.Content)
}
if (len(b.Content) > 0) {
b.Pos.write(buf)
fmt.Fprintf(buf, `io.WriteString(w, %q)`+"\n", b.Content)
}
return nil
}