-
Notifications
You must be signed in to change notification settings - Fork 6
/
heredoc.go
113 lines (100 loc) · 2.39 KB
/
heredoc.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
// Copyright (c) 2014-2024 TSUYUSATO Kitsune
// This software is released under the MIT License.
// http://opensource.org/licenses/mit-license.php
// Package heredoc provides creation of here-documents from raw strings.
//
// Golang supports raw-string syntax.
//
// doc := `
// Foo
// Bar
// `
//
// But raw-string cannot recognize indentation. Thus such content is an indented string, equivalent to
//
// "\n\tFoo\n\tBar\n"
//
// I don't want this!
//
// However this problem is solved by package heredoc.
//
// doc := heredoc.Doc(`
// Foo
// Bar
// `)
//
// Is equivalent to
//
// "Foo\nBar\n"
package heredoc
import (
"fmt"
"strings"
)
const maxInt = int(^uint(0) >> 1)
// Doc returns un-indented string as here-document.
func Doc(raw string) string {
skipFirstLine := false
if len(raw) > 0 && raw[0] == '\n' {
raw = raw[1:]
} else {
skipFirstLine = true
}
rawLines := strings.Split(raw, "\n")
lines := rawLines
if skipFirstLine {
lines = lines[1:]
}
minIndentSize := getMinIndent(lines)
lines = removeIndentation(lines, minIndentSize)
return strings.Join(rawLines, "\n")
}
// isSpace checks whether the rune represents space or not.
// Only white spcaes (U+0020) and horizontal tabs are treated as space character.
// It is the same as Go.
//
// See https://github.com/MakeNowJust/heredoc/issues/6#issuecomment-524231625.
func isSpace(r rune) bool {
switch r {
case ' ', '\t':
return true
default:
return false
}
}
// getMinIndent calculates the minimum indentation in lines, excluding empty lines.
func getMinIndent(lines []string) int {
minIndentSize := maxInt
for i, line := range lines {
indentSize := 0
for _, r := range line {
if isSpace(r) {
indentSize++
} else {
break
}
}
if len(line) == indentSize {
if i == len(lines)-1 && indentSize < minIndentSize {
lines[i] = ""
}
} else if indentSize < minIndentSize {
minIndentSize = indentSize
}
}
return minIndentSize
}
// removeIndentation removes n characters from the front of each line in lines.
func removeIndentation(lines []string, n int) []string {
for i, line := range lines {
if len(lines[i]) >= n {
lines[i] = line[n:]
}
}
return lines
}
// Docf returns unindented and formatted string as here-document.
// Formatting is done as for fmt.Printf().
func Docf(raw string, args ...interface{}) string {
return fmt.Sprintf(Doc(raw), args...)
}