Repository navigation
Expand file tree
/
Copy pathinline.go
More file actions
178 lines (172 loc) · 3.98 KB
/
Copy pathinline.go
File metadata and controls
178 lines (172 loc) · 3.98 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package osf
import "strings"
/** plainText returns s as a single unformatted text run, with no
* emphasis parsing.
*
* Parameters:
* s (string) — the text
*
* Returns:
* []*Text — one run holding s unchanged
*
* Example:
* runs := plainText("a *boneyard* note")
*/
func plainText(s string) []*Text {
return []*Text{{InnerText: s}}
}
/** StringToTextArray turns a Fountain string into OSF text runs. Inline
* emphasis becomes attributes on a run: ***x*** is bold italic, **x** is
* bold, *x* is italic and _x_ is underline; they nest, and a backslash
* escapes a marker (\*, \_ and \\). A marker that is unmatched, or that
* sits next to a space (as in "5 * 3"), or sits inside a word (snake_case),
* stays literal text.
*
* Parameters:
* s (string) — Fountain text for one element
*
* Returns:
* []*Text — the runs in order, never empty (an empty s gives one empty run)
*
* Example:
* runs := StringToTextArray("He *runs* far") // "He ", italic "runs", " far"
*/
func StringToTextArray(s string) []*Text {
disabled := map[int]bool{}
for {
texts, unclosed := scanInline(s, disabled)
if len(unclosed) == 0 {
if len(texts) == 0 {
return plainText("")
}
return texts
}
for _, pos := range unclosed {
disabled[pos] = true
}
}
}
func isSpace(b byte) bool {
return b == ' ' || b == '\t' || b == '\n' || b == '\r'
}
func isWordByte(b byte) bool {
return b >= '0' && b <= '9' || b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b >= 0x80
}
// scanInline makes one pass over s. Markers at the positions in disabled
// are literal. It returns the runs and the positions of openers that were
// never closed, which the caller disables before scanning again.
func scanInline(s string, disabled map[int]bool) ([]*Text, []int) {
var (
texts []*Text
buf strings.Builder
bold, ital, undr bool
openBold = -1
openItal = -1
openUndr = -1
)
flush := func() {
if buf.Len() == 0 {
return
}
t := &Text{InnerText: buf.String()}
if bold {
t.Bold = BoldStyle
}
if ital {
t.Italic = ItalicStyle
}
if undr {
t.Underline = UnderlineStyle
}
texts = append(texts, t)
buf.Reset()
}
// canOpen: the marker is followed by a non-space; canClose: it follows one.
canOpen := func(i, n int) bool {
return i+n < len(s) && !isSpace(s[i+n]) && s[i+n] != s[i]
}
canClose := func(i int) bool { return i > 0 && !isSpace(s[i-1]) }
for i := 0; i < len(s); {
c := s[i]
if c == '\\' && i+1 < len(s) && (s[i+1] == '*' || s[i+1] == '_' || s[i+1] == '\\') {
buf.WriteByte(s[i+1])
i += 2
continue
}
if (c != '*' && c != '_') || disabled[i] {
buf.WriteByte(c)
i++
continue
}
n := 0
switch {
case c == '_':
wordStart := i == 0 || !isWordByte(s[i-1])
switch {
case undr && canClose(i):
flush()
undr, openUndr = false, -1
n = 1
case !undr && wordStart && canOpen(i, 1):
flush()
undr, openUndr = true, i
n = 1
}
default:
run := 0
for i+run < len(s) && s[i+run] == '*' {
run++
}
if run > 3 {
run = 3
}
for m := run; m >= 1 && n == 0; m-- {
var wantBold, wantItal bool
switch m {
case 3:
wantBold, wantItal = true, true
case 2:
wantBold = true
default:
wantItal = true
}
closing := (!wantBold || bold) && (!wantItal || ital)
opening := (!wantBold || !bold) && (!wantItal || !ital)
switch {
case closing && canClose(i):
flush()
if wantBold {
bold, openBold = false, -1
}
if wantItal {
ital, openItal = false, -1
}
n = m
case opening && canOpen(i, m):
flush()
if wantBold {
bold, openBold = true, i
}
if wantItal {
ital, openItal = true, i
}
n = m
}
}
}
if n == 0 {
buf.WriteByte(c)
i++
continue
}
i += n
}
flush()
var unclosed []int
for _, pos := range []int{openBold, openItal, openUndr} {
if pos >= 0 {
unclosed = append(unclosed, pos)
}
}
return texts, unclosed
}