make preprocessor clip all comments

This commit is contained in:
dre 2021-07-11 14:52:51 +08:00
parent bbc1752bb8
commit 71f73cb35b
2 changed files with 34 additions and 23 deletions

View file

@ -14,13 +14,13 @@ title: "This is the Title!"
categories: [a,b]
---
<!-- a
<!-- a <!-- double -->
comment -->
<!--
multi line comment block, terminated.
With a break line,
With a break line, <!-- and inline comments -->
and [with a](link);
-->
@ -50,12 +50,6 @@ and<!-- inline comment-->
preproc = `--- title: "This is the Title!" categories: [a,b] ---
<!-- a comment -->
<!-- multi line comment block, terminated.
With a break line, and [with a](link);
-->
> this is a quote
*not a list*
@ -74,7 +68,7 @@ this is multi
line code
` + "```" + `
and<!-- inline comment-->
and
` + "```" + `
this is code too

View file

@ -18,7 +18,7 @@ type fsm struct {
out chan pipe.StreamItem
// combining multiple input lines
multiLineBlockMode bool
multiLineBlockMode int
blockBuffer []byte
sendBuffer []byte
// if we have a termination rule to abide, e.g. implied code fences
@ -53,15 +53,40 @@ func (m *fsm) pipeline(in chan pipe.StreamItem) chan pipe.StreamItem {
}
func wrap(m *fsm, data []byte) (*fsm, []byte) {
if hasCommentStart(data) {
m.multiLineBlockMode = true
var scount, ecount int
if scount = countStart(data, "<!--"); scount > 0 {
m.multiLineBlockMode += scount
}
if hasCommentEnd(data) {
m.multiLineBlockMode = false
if ecount = countEnd(data, "-->"); ecount > 0 {
m.multiLineBlockMode -= ecount
}
// clip entire line
if m.multiLineBlockMode > 0 && scount-ecount == 0 {
data = data[:0]
return m, data
}
// clip data past first occurrence
if scount > 0 {
data = data[:bytes.Index(data, []byte("<!--"))]
}
// clip data past last occurrence
if ecount = countEnd(data, "-->"); ecount > 0 {
data = data[bytes.LastIndex(data, []byte("-->"))+3:]
}
return m, data
}
func countStart(data []byte, pattern string) int {
return bytes.Count(data, []byte(pattern))
}
func countEnd(data []byte, pattern string) int {
return bytes.Count(data, []byte(pattern))
}
func (m *fsm) sync() {
if len(m.sendBuffer) > 0 {
m.sendBuffer = append(m.sendBuffer, '\n')
@ -72,7 +97,7 @@ func (m *fsm) sync() {
}
func (m *fsm) softBlockFlush() {
if m.multiLineBlockMode {
if m.multiLineBlockMode > 0 {
return
}
m.blockFlush()
@ -132,14 +157,6 @@ func needsFence(data []byte) bool {
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 {
if len(bytes.TrimSpace(data)) == 0 {
return normal