-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumn_format.go
154 lines (135 loc) · 3.02 KB
/
column_format.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-FileCopyrightText: 2020 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package asciidoctor
import (
"strings"
"git.sr.ht/~shulhan/pakakeh.go/lib/ascii"
"git.sr.ht/~shulhan/pakakeh.go/lib/math/big"
)
const (
colAlignTop int = iota // '<'
colAlignMiddle // '^'
colAlignBottom // '>'
)
const (
colStyleDefault int = iota << 1 // 'd'
colStyleAsciidoc // 'a'
colStyleEmphasis // 'e'
colStyleHeader // 'h'
colStyleLiteral // 'l'
colStyleMonospaced // 'm'
colStyleStrong // 's'
colStyleVerse // 'v'
)
var _colStyles = map[byte]int{
'a': colStyleAsciidoc,
'e': colStyleEmphasis,
'h': colStyleHeader,
'l': colStyleLiteral,
'm': colStyleMonospaced,
's': colStyleStrong,
'v': colStyleVerse,
}
type columnFormat struct {
width *big.Rat
classes []string
alignHor int
alignVer int
style int
// isDefault will true if its contains '*'.
isDefault bool
// isAutowidth will true if its contains '~'.
isAutowidth bool
}
func newColumnFormat() *columnFormat {
return &columnFormat{
width: big.NewRat(1),
}
}
func (f *columnFormat) htmlClasses() string {
return strings.Join(f.classes, ` `)
}
func (f *columnFormat) merge(other *columnFormat) {
if other.width != nil {
f.width = big.NewRat(other.width)
}
if other.alignHor != 0 {
f.alignHor = other.alignHor
}
if other.alignVer != 0 {
f.alignVer = other.alignVer
}
if other.style != 0 {
f.style = other.style
}
}
// parseColumnFormat parse single "cols" format value, for example "3*.>" or
// ".^3l".
func parseColumnFormat(s string) (ncols int, format *columnFormat) {
format = newColumnFormat()
if len(s) == 0 {
return 0, format
}
if ascii.IsDigit(s[0]) {
format.width, s = parseColumnDigits(s)
}
var (
x int
isAlignVertical bool
)
for x = 0; x < len(s); x++ {
switch s[x] {
case '*':
format.isDefault = true
ncols = int(format.width.Int64())
format.width = big.NewRat(1)
case '.':
isAlignVertical = true
case '<':
if isAlignVertical {
format.alignVer = colAlignTop
isAlignVertical = false
} else {
format.alignHor = colAlignTop
}
case '^':
if isAlignVertical {
format.alignVer = colAlignMiddle
isAlignVertical = false
} else {
format.alignHor = colAlignMiddle
}
case '>':
if isAlignVertical {
format.alignVer = colAlignBottom
isAlignVertical = false
} else {
format.alignHor = colAlignBottom
}
case '~':
format.isAutowidth = true
case 'a', 'e', 'h', 'l', 'm', 's', 'v':
format.style = _colStyles[s[x]]
default:
if ascii.IsDigit(s[x]) {
format.width, s = parseColumnDigits(s[x:])
}
}
}
return ncols, format
}
func parseColumnDigits(s string) (w *big.Rat, rest string) {
var (
x int
n []byte
)
for ; x < len(s); x++ {
if !ascii.IsDigit(s[x]) {
break
}
n = append(n, s[x])
}
w = big.NewRat(string(n))
rest = s[x:]
return w, rest
}