-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtitlewidth.go
51 lines (43 loc) · 830 Bytes
/
titlewidth.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
package parsefb
import (
"bufio"
"errors"
"strings"
"unicode"
)
func TitleWidth(t string) int {
l := 0
for _, r := range t {
unicode.Is(unicode.Thai, r)
l++
}
return l
}
// First two line of the string may look like:
//
// Hello World
// #######
//
// will become
//
// Hello World
// ###########
//
// after processing of this func
func SetRstDocTitleWidth(s string) (r string, err error) {
var lines []string
scanner := bufio.NewScanner(strings.NewReader(s))
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err = scanner.Err(); err != nil {
return r, err
}
if strings.TrimLeft(lines[1], "#") != "" {
return r, errors.New("second line does not consist of all #")
}
lines[1] = strings.Repeat("#", TitleWidth(lines[0]))
r = strings.Join(lines, "\n")
r += "\n"
return
}