track multi line, comment blocks with breaks

This commit is contained in:
dre 2021-07-11 14:12:56 +08:00
parent 70bb52bca9
commit bbc1752bb8
2 changed files with 48 additions and 11 deletions

View file

@ -17,6 +17,13 @@ categories: [a,b]
<!-- a <!-- a
comment --> comment -->
<!--
multi line comment block, terminated.
With a break line,
and [with a](link);
-->
> this is > this is
a quote a quote
@ -37,7 +44,7 @@ this is multi
line code line code
` + "```" + ` ` + "```" + `
and and<!-- inline comment-->
this is code too` this is code too`
@ -45,6 +52,10 @@ and
<!-- a comment --> <!-- a comment -->
<!-- multi line comment block, terminated.
With a break line, and [with a](link);
-->
> this is a quote > this is a quote
*not a list* *not a list*
@ -63,7 +74,7 @@ this is multi
line code line code
` + "```" + ` ` + "```" + `
and and<!-- inline comment-->
` + "```" + ` ` + "```" + `
this is code too this is code too

View file

@ -18,8 +18,9 @@ type fsm struct {
out chan pipe.StreamItem out chan pipe.StreamItem
// combining multiple input lines // combining multiple input lines
blockBuffer []byte multiLineBlockMode bool
sendBuffer []byte blockBuffer []byte
sendBuffer []byte
// if we have a termination rule to abide, e.g. implied code fences // if we have a termination rule to abide, e.g. implied code fences
pending []byte pending []byte
} }
@ -43,7 +44,7 @@ func (m *fsm) pipeline(in chan pipe.StreamItem) chan pipe.StreamItem {
continue continue
} }
m.state = m.state(m, b.Payload()) m.state = m.state(wrap(m, b.Payload()))
m.sync() m.sync()
} }
}() }()
@ -51,6 +52,16 @@ func (m *fsm) pipeline(in chan pipe.StreamItem) chan pipe.StreamItem {
return m.out return m.out
} }
func wrap(m *fsm, data []byte) (*fsm, []byte) {
if hasCommentStart(data) {
m.multiLineBlockMode = true
}
if hasCommentEnd(data) {
m.multiLineBlockMode = false
}
return m, data
}
func (m *fsm) sync() { func (m *fsm) sync() {
if len(m.sendBuffer) > 0 { if len(m.sendBuffer) > 0 {
m.sendBuffer = append(m.sendBuffer, '\n') m.sendBuffer = append(m.sendBuffer, '\n')
@ -60,6 +71,13 @@ func (m *fsm) sync() {
} }
} }
func (m *fsm) softBlockFlush() {
if m.multiLineBlockMode {
return
}
m.blockFlush()
}
func (m *fsm) blockFlush() { func (m *fsm) blockFlush() {
// blockBuffer to sendbuffer // blockBuffer to sendbuffer
m.sendBuffer = append(m.sendBuffer, m.blockBuffer...) m.sendBuffer = append(m.sendBuffer, m.blockBuffer...)
@ -114,6 +132,14 @@ func needsFence(data []byte) bool {
return len(data) >= 4 && string(data[0:4]) == " " return len(data) >= 4 && string(data[0:4]) == " "
} }
func hasCommentStart(data []byte) bool {
return bytes.Contains(data, []byte("<!--"))
}
func hasCommentEnd(data []byte) bool {
return bytes.Contains(data, []byte("-->"))
}
func normalText(m *fsm, data []byte) stateFn { func normalText(m *fsm, data []byte) stateFn {
if len(bytes.TrimSpace(data)) == 0 { if len(bytes.TrimSpace(data)) == 0 {
return normal return normal
@ -121,7 +147,7 @@ func normalText(m *fsm, data []byte) stateFn {
if data, isList := handleList(data); isList { if data, isList := handleList(data); isList {
m.blockBuffer = append(m.blockBuffer, data...) m.blockBuffer = append(m.blockBuffer, data...)
m.blockFlush() m.softBlockFlush()
return list return list
} }
@ -148,7 +174,7 @@ func normalText(m *fsm, data []byte) stateFn {
} }
m.blockBuffer = append(m.blockBuffer, append(data, '\n')...) m.blockBuffer = append(m.blockBuffer, append(data, '\n')...)
m.blockFlush() m.softBlockFlush()
return normal return normal
} }
@ -164,7 +190,7 @@ func list(m *fsm, data []byte) stateFn {
return list return list
} }
m.blockFlush() m.softBlockFlush()
return normalText(m, data) return normalText(m, data)
} }
@ -173,7 +199,7 @@ func fence(m *fsm, data []byte) stateFn {
m.blockBuffer = append(m.blockBuffer, append(data, '\n')...) m.blockBuffer = append(m.blockBuffer, append(data, '\n')...)
// second fence returns to normal // second fence returns to normal
if isFence(data) { if isFence(data) {
m.blockFlush() m.softBlockFlush()
return normal return normal
} }
@ -189,7 +215,7 @@ func toFence(m *fsm, data []byte) stateFn {
return toFence return toFence
} }
m.blockFlush() m.softBlockFlush()
return normalText(m, data) return normalText(m, data)
} }
@ -200,7 +226,7 @@ func paragraph(m *fsm, data []byte) stateFn {
m.blockBuffer = bytes.TrimSpace(m.blockBuffer) m.blockBuffer = bytes.TrimSpace(m.blockBuffer)
// TODO, remove double spaces inside paragraphs // TODO, remove double spaces inside paragraphs
m.blockBuffer = append(m.blockBuffer, '\n') m.blockBuffer = append(m.blockBuffer, '\n')
m.blockFlush() m.softBlockFlush()
return normal return normal
} }