Skip to content

Commit

Permalink
Fixes #36
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Nov 26, 2019
1 parent 4536e57 commit 6c55ba5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
9 changes: 6 additions & 3 deletions parser/atx_heading.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func (b *atxHeadingParser) Open(parent ast.Node, reader text.Reader, pc Context)
return nil, NoChildren
}
start := i + l
if start >= len(line) {
start = len(line) - 1
}
origstart := start
stop := len(line) - util.TrimRightSpaceLength(line)

Expand Down Expand Up @@ -128,15 +131,15 @@ func (b *atxHeadingParser) Open(parent ast.Node, reader text.Reader, pc Context)
for _, attr := range attrs {
node.SetAttribute(attr.Name, attr.Value)
}
node.Lines().Append(text.NewSegment(segment.Start+start+1, segment.Start+closureOpen))
node.Lines().Append(text.NewSegment(segment.Start+start+1-segment.Padding, segment.Start+closureOpen-segment.Padding))
}
}
}
if !parsed {
start = origstart
stop := len(line) - util.TrimRightSpaceLength(line)
if stop <= start { // empty headings like '##[space]'
stop = start + 1
stop = start
} else {
i = stop - 1
for ; line[i] == '#' && i >= start; i-- {
Expand All @@ -149,7 +152,7 @@ func (b *atxHeadingParser) Open(parent ast.Node, reader text.Reader, pc Context)
}

if len(util.TrimRight(line[start:stop], []byte{'#'})) != 0 { // empty heading like '### ###'
node.Lines().Append(text.NewSegment(segment.Start+start, segment.Start+stop))
node.Lines().Append(text.NewSegment(segment.Start+start-segment.Padding, segment.Start+stop-segment.Padding))
}
}
return node, NoChildren
Expand Down
35 changes: 18 additions & 17 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ func (p *parser) Parse(reader text.Reader, opts ...ParseOption) ast.Node {
for _, at := range p.astTransformers {
at.Transform(root, reader, pc)
}
//root.Dump(reader.Source(), 0)
// root.Dump(reader.Source(), 0)
return root
}

Expand Down Expand Up @@ -1086,13 +1086,28 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context)
break
}
lineLength := len(line)
hardlineBreak := false
softLinebreak := line[lineLength-1] == '\n'
if lineLength > 2 && line[lineLength-2] == '\\' && softLinebreak { // ends with \\n
lineLength -= 2
hardlineBreak = true

} else if lineLength > 3 && line[lineLength-3] == '\\' && line[lineLength-2] == '\r' && softLinebreak { // ends with \\r\n
lineLength -= 3
hardlineBreak = true
} else if lineLength > 3 && line[lineLength-3] == ' ' && line[lineLength-2] == ' ' && softLinebreak { // ends with [space][space]\n
lineLength -= 3
hardlineBreak = true
} else if lineLength > 4 && line[lineLength-4] == ' ' && line[lineLength-3] == ' ' && line[lineLength-2] == '\r' && softLinebreak { // ends with [space][space]\r\n
lineLength -= 4
hardlineBreak = true
}

l, startPosition := block.Position()
n := 0
softLinebreak := false
for i := 0; i < lineLength; i++ {
c := line[i]
if c == '\n' {
softLinebreak = true
break
}
isSpace := util.IsSpace(c)
Expand Down Expand Up @@ -1150,20 +1165,6 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context)
}
diff := startPosition.Between(currentPosition)
stop := diff.Stop
hardlineBreak := false
if lineLength > 2 && line[lineLength-2] == '\\' && softLinebreak { // ends with \\n
stop--
hardlineBreak = true

} else if lineLength > 3 && line[lineLength-3] == '\\' && line[lineLength-2] == '\r' && softLinebreak { // ends with \\r\n
stop -= 2
hardlineBreak = true
} else if lineLength > 3 && line[lineLength-3] == ' ' && line[lineLength-2] == ' ' && softLinebreak { // ends with [space][space]\n
stop--
hardlineBreak = true
} else if lineLength > 4 && line[lineLength-4] == ' ' && line[lineLength-3] == ' ' && line[lineLength-2] == '\r' && softLinebreak { // ends with [space][space]\r\n
hardlineBreak = true
}
rest := diff.WithStop(stop)
text := ast.NewTextSegment(rest.TrimRightSpace(source))
text.SetSoftLineBreak(softLinebreak)
Expand Down
18 changes: 16 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func VisualizeSpaces(bs []byte) []byte {
bs = bytes.Replace(bs, []byte(" "), []byte("[SPACE]"), -1)
bs = bytes.Replace(bs, []byte("\t"), []byte("[TAB]"), -1)
bs = bytes.Replace(bs, []byte("\n"), []byte("[NEWLINE]\n"), -1)
bs = bytes.Replace(bs, []byte("\r"), []byte("[CR]\n"), -1)
bs = bytes.Replace(bs, []byte("\r"), []byte("[CR]"), -1)
return bs
}

Expand Down Expand Up @@ -620,8 +620,22 @@ func URLEscape(v []byte, resolveReference bool) []byte {
n = i
continue
}
if int(u8len) >= len(v) {
u8len = int8(len(v) - 1)
}
if u8len == 0 {
i++
n = i
continue
}
cob.Write(v[n:i])
cob.Write(StringToReadOnlyBytes(url.QueryEscape(string(v[i : i+int(u8len)]))))
stop := i + int(u8len)
if stop > len(v) {
i += 1
n = i
continue
}
cob.Write(StringToReadOnlyBytes(url.QueryEscape(string(v[i:stop]))))
i += int(u8len)
n = i
}
Expand Down

0 comments on commit 6c55ba5

Please sign in to comment.