forked from ego008/go-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_block.go
153 lines (131 loc) · 2.91 KB
/
state_block.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
// Copyright 2015 The Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package markdown
const (
ptRoot = iota
ptList
ptBlockQuote
)
type StateBlock struct {
StateCore
BMarks []int // offsets of the line beginnings
EMarks []int // offsets of the line endings
TShift []int // indents for each line
BlkIndent int // required block content indent (in a list etc.)
Line int // line index in the source string
LineMax int // number of lines
Tight bool // loose or tight mode for lists
ParentType byte // parent block type
Level int
}
func (s *StateBlock) IsLineEmpty(n int) bool {
return s.BMarks[n]+s.TShift[n] >= s.EMarks[n]
}
func (s *StateBlock) SkipEmptyLines(from int) int {
for from < s.LineMax && s.IsLineEmpty(from) {
from++
}
return from
}
func (s *StateBlock) SkipSpaces(pos int) int {
src := s.Src
for pos < len(src) && src[pos] == ' ' {
pos++
}
return pos
}
func (s *StateBlock) SkipBytes(pos int, b byte) int {
src := s.Src
for pos < len(src) && src[pos] == b {
pos++
}
return pos
}
func (s *StateBlock) SkipBytesBack(pos int, b byte, min int) int {
for pos > min {
pos--
if s.Src[pos] != b {
return pos + 1
}
}
return pos
}
func (s *StateBlock) Lines(begin, end, indent int, keepLastLf bool) string {
if begin == end {
return ""
}
src := s.Src
if begin+1 == end {
shift := s.TShift[begin]
if shift < 0 {
shift = 0
} else if shift > indent {
shift = indent
}
first := s.BMarks[begin] + shift
last := s.EMarks[begin]
if keepLastLf && last < len(src) {
last++
}
return src[first:last]
}
size := 0
var firstFirst int
var previousLast int
adjoin := true
for line := begin; line < end; line++ {
shift := s.TShift[line]
if shift < 0 {
shift = 0
} else if shift > indent {
shift = indent
}
first := s.BMarks[line] + shift
last := s.EMarks[line]
if line+1 < end || (keepLastLf && last < len(src)) {
last++
}
size += last - first
if line == begin {
firstFirst = first
} else if previousLast != first {
adjoin = false
}
previousLast = last
}
if adjoin {
return src[firstFirst:previousLast]
}
buf := make([]byte, size)
i := 0
for line := begin; line < end; line++ {
shift := s.TShift[line]
if shift < 0 {
shift = 0
} else if shift > indent {
shift = indent
}
first := s.BMarks[line] + shift
last := s.EMarks[line]
if line+1 < end || (keepLastLf && last < len(src)) {
last++
}
i += copy(buf[i:], src[first:last])
}
return string(buf)
}
func (s *StateBlock) PushToken(tok Token) {
tok.SetLevel(s.Level)
s.Tokens = append(s.Tokens, tok)
}
func (s *StateBlock) PushOpeningToken(tok Token) {
tok.SetLevel(s.Level)
s.Level++
s.Tokens = append(s.Tokens, tok)
}
func (s *StateBlock) PushClosingToken(tok Token) {
s.Level--
tok.SetLevel(s.Level)
s.Tokens = append(s.Tokens, tok)
}