diff --git a/_test/extra.txt b/_test/extra.txt index 561c42e..1ffadda 100644 --- a/_test/extra.txt +++ b/_test/extra.txt @@ -488,3 +488,11 @@ _a[b_c_](d) //- - - - - - - - -//

_ab_c_

//= = = = = = = = = = = = = = = = = = = = = = = =// + +37: Tabs and spaces + OPTIONS: {"enableEscape": true} +//- - - - - - - - -// +\t\t x\n +//- - - - - - - - -// +
\t x\n
+//= = = = = = = = = = = = = = = = = = = = = = = =// diff --git a/parser/fcode_block.go b/parser/fcode_block.go index 2bb688f..7e44cec 100644 --- a/parser/fcode_block.go +++ b/parser/fcode_block.go @@ -87,8 +87,11 @@ func (b *fencedCodeBlockParser) Continue(node ast.Node, reader text.Reader, pc C return Close } } - pos, padding := util.DedentPositionPadding(line, reader.LineOffset(), segment.Padding, fdata.indent) - + pos, padding := util.IndentPositionPadding(line, reader.LineOffset(), segment.Padding, fdata.indent) + if pos < 0 { + pos = util.FirstNonSpacePosition(line) + padding = 0 + } seg := text.NewSegmentPadding(segment.Start+pos, segment.Stop, padding) // if code block line starts with a tab, keep a tab as it is. if padding != 0 { diff --git a/util/util.go b/util/util.go index 1af34b8..dc453be 100644 --- a/util/util.go +++ b/util/util.go @@ -151,30 +151,7 @@ func TabWidth(currentPos int) int { // width=2 is in the tab character. In this case, IndentPosition returns // (pos=1, padding=2) func IndentPosition(bs []byte, currentPos, width int) (pos, padding int) { - if width == 0 { - return 0, 0 - } - w := 0 - l := len(bs) - i := 0 - hasTab := false - for ; i < l; i++ { - if bs[i] == '\t' { - w += TabWidth(currentPos + w) - hasTab = true - } else if bs[i] == ' ' { - w++ - } else { - break - } - } - if w >= width { - if !hasTab { - return width, 0 - } - return i, w - width - } - return -1, -1 + return IndentPositionPadding(bs, currentPos, 0, width) } // IndentPositionPadding searches an indent position with the given width for the given line. @@ -188,9 +165,9 @@ func IndentPositionPadding(bs []byte, currentPos, paddingv, width int) (pos, pad i := 0 l := len(bs) for ; i < l; i++ { - if bs[i] == '\t' { + if bs[i] == '\t' && w < width { w += TabWidth(currentPos + w) - } else if bs[i] == ' ' { + } else if bs[i] == ' ' && w < width { w++ } else { break @@ -202,55 +179,6 @@ func IndentPositionPadding(bs []byte, currentPos, paddingv, width int) (pos, pad return -1, -1 } -// DedentPosition dedents lines by the given width. -func DedentPosition(bs []byte, currentPos, width int) (pos, padding int) { - if width == 0 { - return 0, 0 - } - w := 0 - l := len(bs) - i := 0 - for ; i < l; i++ { - if bs[i] == '\t' { - w += TabWidth(currentPos + w) - } else if bs[i] == ' ' { - w++ - } else { - break - } - } - if w >= width { - return i, w - width - } - return i, 0 -} - -// DedentPositionPadding dedents lines by the given width. -// This function is mostly same as DedentPosition except this function -// takes account into additional paddings. -func DedentPositionPadding(bs []byte, currentPos, paddingv, width int) (pos, padding int) { - if width == 0 { - return 0, paddingv - } - - w := 0 - i := 0 - l := len(bs) - for ; i < l; i++ { - if bs[i] == '\t' { - w += TabWidth(currentPos + w) - } else if bs[i] == ' ' { - w++ - } else { - break - } - } - if w >= width { - return i - paddingv, w - width - } - return i - paddingv, 0 -} - // IndentWidth calculate an indent width for the given line. func IndentWidth(bs []byte, currentPos int) (width, pos int) { l := len(bs)