diff options
Diffstat (limited to 'vendor/github.com/yuin/goldmark/parser')
13 files changed, 185 insertions, 179 deletions
diff --git a/vendor/github.com/yuin/goldmark/parser/attribute.go b/vendor/github.com/yuin/goldmark/parser/attribute.go index 42985f4..5647a51 100644 --- a/vendor/github.com/yuin/goldmark/parser/attribute.go +++ b/vendor/github.com/yuin/goldmark/parser/attribute.go @@ -15,14 +15,14 @@ var attrNameClass = []byte("class") // An Attribute is an attribute of the markdown elements. type Attribute struct { Name []byte - Value interface{} + Value any } // An Attributes is a collection of attributes. type Attributes []Attribute // Find returns a (value, true) if an attribute correspond with given name is found, otherwise (nil, false). -func (as Attributes) Find(name []byte) (interface{}, bool) { +func (as Attributes) Find(name []byte) (any, bool) { for _, a := range as { if bytes.Equal(a.Name, name) { return a.Value, true @@ -31,7 +31,7 @@ func (as Attributes) Find(name []byte) (interface{}, bool) { return nil, false } -func (as Attributes) findUpdate(name []byte, cb func(v interface{}) interface{}) bool { +func (as Attributes) findUpdate(name []byte, cb func(v any) any) bool { for i, a := range as { if bytes.Equal(a.Name, name) { as[i].Value = cb(a.Value) @@ -64,7 +64,7 @@ func ParseAttributes(reader text.Reader) (Attributes, bool) { return nil, false } if bytes.Equal(attr.Name, attrNameClass) { - if !attrs.findUpdate(attrNameClass, func(v interface{}) interface{} { + if !attrs.findUpdate(attrNameClass, func(v any) any { ret := make([]byte, 0, len(v.([]byte))+1+len(attr.Value.([]byte))) ret = append(ret, v.([]byte)...) return append(append(ret, ' '), attr.Value.([]byte)...) @@ -142,10 +142,10 @@ func parseAttribute(reader text.Reader) (Attribute, bool) { return Attribute{Name: name, Value: value}, true } -func parseAttributeValue(reader text.Reader) (interface{}, bool) { +func parseAttributeValue(reader text.Reader) (any, bool) { reader.SkipSpaces() c := reader.Peek() - var value interface{} + var value any var ok bool switch c { case text.EOF: @@ -169,9 +169,9 @@ func parseAttributeValue(reader text.Reader) (interface{}, bool) { return value, true } -func parseAttributeArray(reader text.Reader) ([]interface{}, bool) { +func parseAttributeArray(reader text.Reader) ([]any, bool) { reader.Advance(1) // skip [ - ret := []interface{}{} + ret := []any{} for i := 0; ; i++ { c := reader.Peek() comma := false @@ -298,7 +298,7 @@ var bytesTrue = []byte("true") var bytesFalse = []byte("false") var bytesNull = []byte("null") -func parseAttributeOthers(reader text.Reader) (interface{}, bool) { +func parseAttributeOthers(reader text.Reader) (any, bool) { line, _ := reader.PeekLine() c := line[0] if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || diff --git a/vendor/github.com/yuin/goldmark/parser/atx_heading.go b/vendor/github.com/yuin/goldmark/parser/atx_heading.go index dae5e84..b5c6df0 100644 --- a/vendor/github.com/yuin/goldmark/parser/atx_heading.go +++ b/vendor/github.com/yuin/goldmark/parser/atx_heading.go @@ -13,7 +13,7 @@ type HeadingConfig struct { } // SetOption implements SetOptioner. -func (b *HeadingConfig) SetOption(name OptionName, _ interface{}) { +func (b *HeadingConfig) SetOption(name OptionName, _ any) { switch name { case optAutoHeadingID: b.AutoHeadingID = true @@ -98,69 +98,47 @@ func (b *atxHeadingParser) Open(parent ast.Node, reader text.Reader, pc Context) if l == 0 { return nil, NoChildren } - start := i + l - if start >= len(line) { - start = len(line) - 1 - } - origstart := start - stop := len(line) - util.TrimRightSpaceLength(line) + start := min(i+l, len(line)-1) node := ast.NewHeading(level) - parsed := false - if b.Attribute { // handles special case like ### heading ### {#id} - start-- - closureClose := -1 - closureOpen := -1 - for j := start; j < stop; { - c := line[j] - if util.IsEscapedPunctuation(line, j) { - j += 2 - } else if util.IsSpace(c) && j < stop-1 && line[j+1] == '#' { - closureOpen = j + 1 - k := j + 1 - for ; k < stop && line[k] == '#'; k++ { - } - closureClose = k - break - } else { - j++ - } + hl := text.NewSegment( + segment.Start+start-segment.Padding, + segment.Start+len(line)-segment.Padding) + hl = hl.TrimRightSpace(reader.Source()) + if hl.Len() == 0 { + reader.AdvanceToEOL() + return node, NoChildren + } + + if b.Attribute { + node.Lines().Append(hl) + parseLastLineAttributes(node, reader, pc) + hl = node.Lines().At(0) + node.Lines().Clear() + } + + // handle closing sequence of '#' characters + line = hl.Value(reader.Source()) + stop := len(line) + if stop == 0 { // empty headings like '##[space]' + stop = 0 + } else { + i = stop - 1 + for ; line[i] == '#' && i > 0; i-- { } - if closureClose > 0 { - reader.Advance(closureClose) - attrs, ok := ParseAttributes(reader) - rest, _ := reader.PeekLine() - parsed = ok && util.IsBlank(rest) - if parsed { - for _, attr := range attrs { - node.SetAttribute(attr.Name, attr.Value) - } - node.Lines().Append(text.NewSegment( - segment.Start+start+1-segment.Padding, - segment.Start+closureOpen-segment.Padding)) - } + if i == 0 && line[0] == '#' { // empty headings like '### ###' + reader.AdvanceToEOL() + return node, NoChildren } - } - if !parsed { - start = origstart - stop := len(line) - util.TrimRightSpaceLength(line) - if stop <= start { // empty headings like '##[space]' - stop = start - } else { - i = stop - 1 - for ; line[i] == '#' && i >= start; i-- { - } - if i != stop-1 && !util.IsSpace(line[i]) { - i = stop - 1 - } - i++ + if i != stop-1 && util.IsSpace(line[i]) { stop = i - } - - if len(util.TrimRight(line[start:stop], []byte{'#'})) != 0 { // empty heading like '### ###' - node.Lines().Append(text.NewSegment(segment.Start+start-segment.Padding, segment.Start+stop-segment.Padding)) + stop -= util.TrimRightSpaceLength(line[0:stop]) } } + hl.Stop = hl.Start + stop + node.Lines().Append(hl) + reader.AdvanceToEOL() + return node, NoChildren } @@ -169,13 +147,6 @@ func (b *atxHeadingParser) Continue(node ast.Node, reader text.Reader, pc Contex } func (b *atxHeadingParser) Close(node ast.Node, reader text.Reader, pc Context) { - if b.Attribute { - _, ok := node.AttributeString("id") - if !ok { - parseLastLineAttributes(node, reader, pc) - } - } - if b.AutoHeadingID { id, ok := node.AttributeString("id") if !ok { @@ -205,7 +176,7 @@ func generateAutoHeadingID(node *ast.Heading, reader text.Reader, pc Context) { node.SetAttribute(attrNameID, headingID) } -func parseLastLineAttributes(node ast.Node, reader text.Reader, pc Context) { +func parseLastLineAttributes(node ast.Node, reader text.Reader, _ Context) { lastIndex := node.Lines().Len() - 1 if lastIndex < 0 { // empty headings return @@ -213,36 +184,36 @@ func parseLastLineAttributes(node ast.Node, reader text.Reader, pc Context) { lastLine := node.Lines().At(lastIndex) line := lastLine.Value(reader.Source()) lr := text.NewReader(line) - var attrs Attributes - var ok bool var start text.Segment var sl int - var end text.Segment for { c := lr.Peek() - if c == text.EOF { + if c == text.EOF || c == '\n' { break } if c == '\\' { lr.Advance(1) - if lr.Peek() == '{' { + if util.IsPunct(lr.Peek()) { lr.Advance(1) } continue } if c == '{' { sl, start = lr.Position() - attrs, ok = ParseAttributes(lr) - _, end = lr.Position() + attrs, ok := ParseAttributes(lr) + if ok { + if nl, _ := lr.PeekLine(); nl == nil || util.IsBlank(nl) { + for _, attr := range attrs { + node.SetAttribute(attr.Name, attr.Value) + } + lastLine.Stop = lastLine.Start + start.Start + lastLine = lastLine.TrimRightSpace(reader.Source()) + node.Lines().Set(lastIndex, lastLine) + return + } + } lr.SetPosition(sl, start) } lr.Advance(1) } - if ok && util.IsBlank(line[end.Start:]) { - for _, attr := range attrs { - node.SetAttribute(attr.Name, attr.Value) - } - lastLine.Stop = lastLine.Start + start.Start - node.Lines().Set(lastIndex, lastLine) - } } diff --git a/vendor/github.com/yuin/goldmark/parser/delimiter.go b/vendor/github.com/yuin/goldmark/parser/delimiter.go index 93b32de..be58c2b 100644 --- a/vendor/github.com/yuin/goldmark/parser/delimiter.go +++ b/vendor/github.com/yuin/goldmark/parser/delimiter.go @@ -207,6 +207,7 @@ func ProcessDelimiters(bottom ast.Node, pc Context) { closer.ConsumeCharacters(consume) node := opener.Processor.OnMatch(consume) + node.(interface{ SetPos(int) }).SetPos(opener.Segment.Start) parent := opener.Parent() child := opener.NextSibling() diff --git a/vendor/github.com/yuin/goldmark/parser/fcode_block.go b/vendor/github.com/yuin/goldmark/parser/fcode_block.go index d0833fb..0c265c0 100644 --- a/vendor/github.com/yuin/goldmark/parser/fcode_block.go +++ b/vendor/github.com/yuin/goldmark/parser/fcode_block.go @@ -34,10 +34,7 @@ func (b *fencedCodeBlockParser) Trigger() []byte { func (b *fencedCodeBlockParser) Open(parent ast.Node, reader text.Reader, pc Context) (ast.Node, State) { line, segment := reader.PeekLine() - pos := pc.BlockOffset() - if pos < 0 || (line[pos] != '`' && line[pos] != '~') { - return nil, NoChildren - } + pos := pc.BlockIndent() findent := pos fenceChar := line[pos] i := pos @@ -79,11 +76,7 @@ func (b *fencedCodeBlockParser) Continue(node ast.Node, reader text.Reader, pc C } length := i - pos if length >= fdata.length && util.IsBlank(line[i:]) { - newline := 1 - if line[len(line)-1] != '\n' { - newline = 0 - } - reader.Advance(segment.Stop - segment.Start - newline + segment.Padding) + reader.AdvanceToEOL() return Close } } @@ -99,7 +92,7 @@ func (b *fencedCodeBlockParser) Continue(node ast.Node, reader text.Reader, pc C } seg.ForceNewline = true // EOF as newline node.Lines().Append(seg) - reader.AdvanceAndSetPadding(segment.Stop-segment.Start-pos-1, padding) + reader.AdvanceToEOL() return Continue | NoChildren } diff --git a/vendor/github.com/yuin/goldmark/parser/html_block.go b/vendor/github.com/yuin/goldmark/parser/html_block.go index 262ef93..53e2376 100644 --- a/vendor/github.com/yuin/goldmark/parser/html_block.go +++ b/vendor/github.com/yuin/goldmark/parser/html_block.go @@ -114,9 +114,6 @@ func (b *htmlBlockParser) Open(parent ast.Node, reader text.Reader, pc Context) var node *ast.HTMLBlock line, segment := reader.PeekLine() last := pc.LastOpenedBlock().Node - if pos := pc.BlockOffset(); pos < 0 || line[pos] != '<' { - return nil, NoChildren - } if m := htmlBlockType1OpenRegexp.FindSubmatchIndex(line); m != nil { node = ast.NewHTMLBlock(ast.HTMLBlockType1) diff --git a/vendor/github.com/yuin/goldmark/parser/link.go b/vendor/github.com/yuin/goldmark/parser/link.go index 7390d7b..bd9cf8d 100644 --- a/vendor/github.com/yuin/goldmark/parser/link.go +++ b/vendor/github.com/yuin/goldmark/parser/link.go @@ -166,9 +166,10 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N l, pos := block.Position() var link *ast.Link var hasValue bool - if c == '(' { // normal link + switch c { + case '(': link = s.parseLink(parent, last, block, pc) - } else if c == '[' { // reference link + case '[': link, hasValue = s.parseReferenceLink(parent, last, block, pc) if link == nil && hasValue { ast.MergeOrReplaceTextSegment(last.Parent(), last, last.Segment) @@ -200,13 +201,18 @@ func (s *linkParser) Parse(parent ast.Node, block text.Reader, pc Context) ast.N s.processLinkLabel(parent, link, last, pc) link.Title = ref.Title() link.Destination = ref.Destination() + link.Reference = ast.NewReferenceLink(ast.ReferenceLinkShortcut, maybeReference) } + var n ast.Node if last.IsImage { last.Parent().RemoveChild(last.Parent(), last) - return ast.NewImage(link) + n = ast.NewImage(link) + } else { + last.Parent().RemoveChild(last.Parent(), last) + n = link } - last.Parent().RemoveChild(last.Parent(), last) - return link + n.(interface{ SetPos(int) }).SetPos(last.Segment.Start) + return n } func (s *linkParser) containsLink(n ast.Node) bool { @@ -262,11 +268,12 @@ func (s *linkParser) parseReferenceLink(parent ast.Node, last *linkLabelState, } var maybeReference []byte + refType := ast.ReferenceLinkFull if segments.Len() == 1 { // avoid allocate a new byte slice maybeReference = block.Value(segments.At(0)) } else { maybeReference = []byte{} - for i := 0; i < segments.Len(); i++ { + for i := range segments.Len() { s := segments.At(i) maybeReference = append(maybeReference, block.Value(s)...) } @@ -274,6 +281,7 @@ func (s *linkParser) parseReferenceLink(parent ast.Node, last *linkLabelState, if util.IsBlank(maybeReference) { // collapsed reference link s := text.NewSegment(last.Segment.Stop, orgpos.Start-1) maybeReference = block.Value(s) + refType = ast.ReferenceLinkCollapsed } // CommonMark spec says: // > A link label can have at most 999 characters inside the square brackets. @@ -290,6 +298,7 @@ func (s *linkParser) parseReferenceLink(parent ast.Node, last *linkLabelState, s.processLinkLabel(parent, link, last, pc) link.Title = ref.Title() link.Destination = ref.Destination() + link.Reference = ast.NewReferenceLink(refType, maybeReference) return link, true } @@ -388,7 +397,7 @@ func parseLinkTitle(block text.Reader) ([]byte, bool) { return block.Value(segments.At(0)), true } var title []byte - for i := 0; i < segments.Len(); i++ { + for i := range segments.Len() { s := segments.At(i) title = append(title, block.Value(s)...) } diff --git a/vendor/github.com/yuin/goldmark/parser/link_ref.go b/vendor/github.com/yuin/goldmark/parser/link_ref.go index ea3f654..600024a 100644 --- a/vendor/github.com/yuin/goldmark/parser/link_ref.go +++ b/vendor/github.com/yuin/goldmark/parser/link_ref.go @@ -18,8 +18,17 @@ func (p *linkReferenceParagraphTransformer) Transform(node *ast.Paragraph, reade block := text.NewBlockReader(reader.Source(), lines) removes := [][2]int{} for { - start, end := parseLinkReferenceDefinition(block, pc) + ref, start, end := parseLinkReferenceDefinition(block, pc) if start > -1 { + if start == 0 { + ref.SetBlankPreviousLines(node.HasBlankPreviousLines()) + } + node.Parent().InsertBefore(node.Parent(), node, ref) + for i := start + 1; i < end; i++ { + ref.Lines().Append(lines.At(i)) + } + seg := ref.Lines().At(ref.Lines().Len() - 1) + ref.Lines().Set(ref.Lines().Len()-1, seg.TrimRightSpace(reader.Source())) if start == end { end++ } @@ -41,57 +50,56 @@ func (p *linkReferenceParagraphTransformer) Transform(node *ast.Paragraph, reade } if lines.Len() == 0 { - t := ast.NewTextBlock() - t.SetBlankPreviousLines(node.HasBlankPreviousLines()) - node.Parent().ReplaceChild(node.Parent(), node, t) + node.Parent().RemoveChild(node.Parent(), node) return } node.SetLines(lines) } -func parseLinkReferenceDefinition(block text.Reader, pc Context) (int, int) { +func parseLinkReferenceDefinition(block text.Reader, pc Context) (ast.Node, int, int) { block.SkipSpaces() line, _ := block.PeekLine() if line == nil { - return -1, -1 + return nil, -1, -1 } startLine, _ := block.Position() width, pos := util.IndentWidth(line, 0) if width > 3 { - return -1, -1 + return nil, -1, -1 } if width != 0 { pos++ } if line[pos] != '[' { - return -1, -1 + return nil, -1, -1 } + _, startPos := block.Position() block.Advance(pos + 1) segments, found := block.FindClosure('[', ']', linkFindClosureOptions) if !found { - return -1, -1 + return nil, -1, -1 } var label []byte if segments.Len() == 1 { label = block.Value(segments.At(0)) } else { - for i := 0; i < segments.Len(); i++ { + for i := range segments.Len() { s := segments.At(i) label = append(label, block.Value(s)...) } } if util.IsBlank(label) { - return -1, -1 + return nil, -1, -1 } if block.Peek() != ':' { - return -1, -1 + return nil, -1, -1 } block.Advance(1) block.SkipSpaces() destination, ok := parseLinkDestination(block) if !ok { - return -1, -1 + return nil, -1, -1 } line, _ = block.PeekLine() isNewLine := line == nil || util.IsBlank(line) @@ -101,14 +109,15 @@ func parseLinkReferenceDefinition(block text.Reader, pc Context) (int, int) { opener := block.Peek() if opener != '"' && opener != '\'' && opener != '(' { if !isNewLine { - return -1, -1 + return nil, -1, -1 } - ref := NewReference(label, destination, nil) - pc.AddReference(ref) - return startLine, endLine + 1 + ref := ast.NewLinkReferenceDefinition(label, destination, nil) + ref.Lines().Append(startPos) + pc.AddReference(newASTReference(ref)) + return ref, startLine, endLine + 1 } if spaces == 0 { - return -1, -1 + return nil, -1, -1 } block.Advance(1) closer := opener @@ -118,18 +127,19 @@ func parseLinkReferenceDefinition(block text.Reader, pc Context) (int, int) { segments, found = block.FindClosure(opener, closer, linkFindClosureOptions) if !found { if !isNewLine { - return -1, -1 + return nil, -1, -1 } - ref := NewReference(label, destination, nil) - pc.AddReference(ref) + ref := ast.NewLinkReferenceDefinition(label, destination, nil) + ref.Lines().Append(startPos) + pc.AddReference(newASTReference(ref)) block.AdvanceLine() - return startLine, endLine + 1 + return ref, startLine, endLine + 1 } var title []byte if segments.Len() == 1 { title = block.Value(segments.At(0)) } else { - for i := 0; i < segments.Len(); i++ { + for i := range segments.Len() { s := segments.At(i) title = append(title, block.Value(s)...) } @@ -138,15 +148,17 @@ func parseLinkReferenceDefinition(block text.Reader, pc Context) (int, int) { line, _ = block.PeekLine() if line != nil && !util.IsBlank(line) { if !isNewLine { - return -1, -1 + return nil, -1, -1 } - ref := NewReference(label, destination, title) - pc.AddReference(ref) - return startLine, endLine + ref := ast.NewLinkReferenceDefinition(label, destination, title) + ref.Lines().Append(startPos) + pc.AddReference(newASTReference(ref)) + return ref, startLine, endLine } endLine, _ = block.Position() - ref := NewReference(label, destination, title) - pc.AddReference(ref) - return startLine, endLine + 1 + ref := ast.NewLinkReferenceDefinition(label, destination, title) + ref.Lines().Append(startPos) + pc.AddReference(newASTReference(ref)) + return ref, startLine, endLine + 1 } diff --git a/vendor/github.com/yuin/goldmark/parser/list.go b/vendor/github.com/yuin/goldmark/parser/list.go index 3e0eea6..ca7040a 100644 --- a/vendor/github.com/yuin/goldmark/parser/list.go +++ b/vendor/github.com/yuin/goldmark/parser/list.go @@ -18,7 +18,7 @@ const ( var skipListParserKey = NewContextKey() var emptyListItemWithBlankLines = NewContextKey() -var listItemFlagValue interface{} = true +var listItemFlagValue any = true // Same as // `^(([ ]*)([\-\*\+]))(\s+.*)?\n?$`.FindSubmatchIndex or @@ -80,14 +80,6 @@ func parseListItem(line []byte) ([6]int, listItemType) { return ret, typ } -func matchesListItem(source []byte, strict bool) ([6]int, listItemType) { - m, typ := parseListItem(source) - if typ != notList && (!strict || strict && m[1] < 4) { - return m, typ - } - return m, notList -} - func calcListOffset(source []byte, match [6]int) int { var offset int if match[4] < 0 || util.IsBlank(source[match[4]:]) { // list item starts with a blank line @@ -132,7 +124,7 @@ func (b *listParser) Open(parent ast.Node, reader text.Reader, pc Context) (ast. return nil, NoChildren } line, _ := reader.PeekLine() - match, typ := matchesListItem(line, true) + match, typ := parseListItem(line) if typ == notList { return nil, NoChildren } @@ -198,7 +190,7 @@ func (b *listParser) Continue(node ast.Node, reader text.Reader, pc Context) Sta if indent < offset || lastIsEmpty { if indent < 4 { - match, typ := matchesListItem(line, false) // may have a leading spaces more than 3 + match, typ := parseListItem(line) if typ != notList && match[1]-offset < 4 { marker := line[match[3]-1] if !list.CanContinue(marker, typ == orderedList) { diff --git a/vendor/github.com/yuin/goldmark/parser/list_item.go b/vendor/github.com/yuin/goldmark/parser/list_item.go index f4d7da4..9bef62e 100644 --- a/vendor/github.com/yuin/goldmark/parser/list_item.go +++ b/vendor/github.com/yuin/goldmark/parser/list_item.go @@ -28,7 +28,7 @@ func (b *listItemParser) Open(parent ast.Node, reader text.Reader, pc Context) ( } offset := lastOffset(list) line, _ := reader.PeekLine() - match, typ := matchesListItem(line, false) + match, typ := parseListItem(line) if typ == notList { return nil, NoChildren } @@ -61,7 +61,7 @@ func (b *listItemParser) Continue(node ast.Node, reader text.Reader, pc Context) isEmpty := node.ChildCount() == 0 && pc.Get(emptyListItemWithBlankLines) != nil indent, _ := util.IndentWidth(line, reader.LineOffset()) if (isEmpty || indent < offset) && indent < 4 { - _, typ := matchesListItem(line, true) + _, typ := parseListItem(line) // new list item found if typ != notList { pc.Set(skipListParserKey, listItemFlagValue) diff --git a/vendor/github.com/yuin/goldmark/parser/paragraph.go b/vendor/github.com/yuin/goldmark/parser/paragraph.go index 801b0df..ace6042 100644 --- a/vendor/github.com/yuin/goldmark/parser/paragraph.go +++ b/vendor/github.com/yuin/goldmark/parser/paragraph.go @@ -22,9 +22,8 @@ func (b *paragraphParser) Trigger() []byte { } func (b *paragraphParser) Open(parent ast.Node, reader text.Reader, pc Context) (ast.Node, State) { - _, segment := reader.PeekLine() - segment = segment.TrimLeftSpace(reader.Source()) - if segment.IsEmpty() { + line, segment := reader.PeekLine() + if util.IsBlank(line) { return nil, NoChildren } node := ast.NewParagraph() @@ -47,7 +46,7 @@ func (b *paragraphParser) Close(node ast.Node, reader text.Reader, pc Context) { lines := node.Lines() if lines.Len() != 0 { // trim leading spaces - for i := 0; i < lines.Len(); i++ { + for i := range lines.Len() { l := lines.At(i) lines.Set(i, l.TrimLeftSpace(reader.Source())) } diff --git a/vendor/github.com/yuin/goldmark/parser/parser.go b/vendor/github.com/yuin/goldmark/parser/parser.go index 78a6b26..05d5649 100644 --- a/vendor/github.com/yuin/goldmark/parser/parser.go +++ b/vendor/github.com/yuin/goldmark/parser/parser.go @@ -37,6 +37,10 @@ func NewReference(label, destination, title []byte) Reference { return &reference{label, destination, title} } +func newASTReference(v *ast.LinkReferenceDefinition) Reference { + return &astReference{v} +} + func (r *reference) Label() []byte { return r.label } @@ -53,6 +57,26 @@ func (r *reference) String() string { return fmt.Sprintf("Reference{Label:%s, Destination:%s, Title:%s}", r.label, r.destination, r.title) } +type astReference struct { + v *ast.LinkReferenceDefinition +} + +func (r *astReference) Label() []byte { + return r.v.Label +} + +func (r *astReference) Destination() []byte { + return r.v.Destination +} + +func (r *astReference) Title() []byte { + return r.v.Title +} + +func (r *astReference) String() string { + return fmt.Sprintf("Reference{Label:%s, Destination:%s, Title:%s}", r.Label(), r.Destination(), r.Title()) +} + // An IDs interface is a collection of the element ids. type IDs interface { // Generate generates a new element id. @@ -136,13 +160,13 @@ type Context interface { String() string // Get returns a value associated with the given key. - Get(ContextKey) interface{} + Get(ContextKey) any // ComputeIfAbsent computes a value if a value associated with the given key is absent and returns the value. - ComputeIfAbsent(ContextKey, func() interface{}) interface{} + ComputeIfAbsent(ContextKey, func() any) any // Set sets the given value to the context. - Set(ContextKey, interface{}) + Set(ContextKey, any) // AddReference adds the given reference to this context. AddReference(Reference) @@ -220,7 +244,7 @@ func WithIDs(ids IDs) ContextOption { } type parseContext struct { - store []interface{} + store []any ids IDs refs map[string]Reference blockOffset int @@ -240,7 +264,7 @@ func NewContext(options ...ContextOption) Context { } return &parseContext{ - store: make([]interface{}, ContextKeyMax+1), + store: make([]any, ContextKeyMax+1), refs: map[string]Reference{}, ids: cfg.IDs, blockOffset: -1, @@ -251,11 +275,11 @@ func NewContext(options ...ContextOption) Context { } } -func (p *parseContext) Get(key ContextKey) interface{} { +func (p *parseContext) Get(key ContextKey) any { return p.store[key] } -func (p *parseContext) ComputeIfAbsent(key ContextKey, f func() interface{}) interface{} { +func (p *parseContext) ComputeIfAbsent(key ContextKey, f func() any) any { v := p.store[key] if v == nil { v = f() @@ -264,7 +288,7 @@ func (p *parseContext) ComputeIfAbsent(key ContextKey, f func() interface{}) int return v } -func (p *parseContext) Set(key ContextKey, value interface{}) { +func (p *parseContext) Set(key ContextKey, value any) { p.store[key] = value } @@ -426,7 +450,7 @@ const ( // A Config struct is a data structure that holds configuration of the Parser. type Config struct { - Options map[OptionName]interface{} + Options map[OptionName]any BlockParsers util.PrioritizedSlice /*<BlockParser>*/ InlineParsers util.PrioritizedSlice /*<InlineParser>*/ ParagraphTransformers util.PrioritizedSlice /*<ParagraphTransformer>*/ @@ -437,7 +461,7 @@ type Config struct { // NewConfig returns a new Config. func NewConfig() *Config { return &Config{ - Options: map[OptionName]interface{}{}, + Options: map[OptionName]any{}, BlockParsers: util.PrioritizedSlice{}, InlineParsers: util.PrioritizedSlice{}, ParagraphTransformers: util.PrioritizedSlice{}, @@ -482,7 +506,7 @@ type SetOptioner interface { // SetOption sets the given option to the object. // Unacceptable options may be passed. // Thus implementations must ignore unacceptable options. - SetOption(name OptionName, value interface{}) + SetOption(name OptionName, value any) } // A BlockParser interface parses a block level element like Paragraph, List, @@ -630,7 +654,7 @@ type Block struct { } type parser struct { - options map[OptionName]interface{} + options map[OptionName]any blockParsers [256][]BlockParser freeBlockParsers []BlockParser inlineParsers [256][]InlineParser @@ -712,7 +736,7 @@ func WithEscapedSpace() Option { type withOption struct { name OptionName - value interface{} + value any } func (o *withOption) SetParserOption(c *Config) { @@ -721,7 +745,7 @@ func (o *withOption) SetParserOption(c *Config) { // WithOption is a functional option that allow you to set // an arbitrary option to the parser. -func WithOption(name OptionName, value interface{}) Option { +func WithOption(name OptionName, value any) Option { return &withOption{name, value} } @@ -733,7 +757,7 @@ func NewParser(options ...Option) Parser { } p := &parser{ - options: map[OptionName]interface{}{}, + options: map[OptionName]any{}, config: config, } @@ -746,7 +770,7 @@ func (p *parser) AddOptions(opts ...Option) { } } -func (p *parser) addBlockParser(v util.PrioritizedValue, options map[OptionName]interface{}) { +func (p *parser) addBlockParser(v util.PrioritizedValue, options map[OptionName]any) { bp, ok := v.Value.(BlockParser) if !ok { panic(fmt.Sprintf("%v is not a BlockParser", v.Value)) @@ -770,7 +794,7 @@ func (p *parser) addBlockParser(v util.PrioritizedValue, options map[OptionName] } } -func (p *parser) addInlineParser(v util.PrioritizedValue, options map[OptionName]interface{}) { +func (p *parser) addInlineParser(v util.PrioritizedValue, options map[OptionName]any) { ip, ok := v.Value.(InlineParser) if !ok { panic(fmt.Sprintf("%v is not a InlineParser", v.Value)) @@ -793,7 +817,7 @@ func (p *parser) addInlineParser(v util.PrioritizedValue, options map[OptionName } } -func (p *parser) addParagraphTransformer(v util.PrioritizedValue, options map[OptionName]interface{}) { +func (p *parser) addParagraphTransformer(v util.PrioritizedValue, options map[OptionName]any) { pt, ok := v.Value.(ParagraphTransformer) if !ok { panic(fmt.Sprintf("%v is not a ParagraphTransformer", v.Value)) @@ -807,7 +831,7 @@ func (p *parser) addParagraphTransformer(v util.PrioritizedValue, options map[Op p.paragraphTransformers = append(p.paragraphTransformers, pt) } -func (p *parser) addASTTransformer(v util.PrioritizedValue, options map[OptionName]interface{}) { +func (p *parser) addASTTransformer(v util.PrioritizedValue, options map[OptionName]any) { at, ok := v.Value.(ASTTransformer) if !ok { panic(fmt.Sprintf("%v is not a ASTTransformer", v.Value)) @@ -961,13 +985,17 @@ retry: if continuable && result == noBlocksOpened && !bp.CanInterruptParagraph() { continue } + if w > 3 && !bp.CanAcceptIndentedLine() { continue } lastBlock = pc.LastOpenedBlock() last := lastBlock.Node + _, blockPos := reader.Position() node, state := bp.Open(parent, reader, pc) if node != nil { + node.SetPos(blockPos.Start + max(pc.BlockOffset(), 0)) + // Parser requires last node to be a paragraph. // With table extension: // @@ -1066,7 +1094,7 @@ func (p *parser) parseBlocks(parent ast.Node, reader text.Reader, pc Context) { break } lastIndex := l - 1 - for i := 0; i < l; i++ { + for i := range l { be := openedBlocks[i] line, _ := reader.PeekLine() if line == nil { @@ -1170,7 +1198,7 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context) l, startPosition := block.Position() n := 0 - for i := 0; i < lineLength; i++ { + for i := range lineLength { c := line[i] if c == '\n' { break @@ -1196,6 +1224,9 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context) for _, ip := range ips { inlineNode = ip.Parse(parent, block, pc) if inlineNode != nil { + if inlineNode.Pos() < 0 { + inlineNode.(interface{ SetPos(int) }).SetPos(startPosition.Start) + } break } block.SetPosition(savedLine, savedPosition) diff --git a/vendor/github.com/yuin/goldmark/parser/raw_html.go b/vendor/github.com/yuin/goldmark/parser/raw_html.go index 1d582a7..a374614 100644 --- a/vendor/github.com/yuin/goldmark/parser/raw_html.go +++ b/vendor/github.com/yuin/goldmark/parser/raw_html.go @@ -63,7 +63,7 @@ var emptyComment2 = []byte("<!--->") var openComment = []byte("<!--") var closeComment = []byte("-->") -func (s *rawHTMLParser) parseComment(block text.Reader, pc Context) ast.Node { +func (s *rawHTMLParser) parseComment(block text.Reader, _ Context) ast.Node { savedLine, savedSegment := block.Position() node := ast.NewRawHTML() line, segment := block.PeekLine() @@ -98,7 +98,7 @@ func (s *rawHTMLParser) parseComment(block text.Reader, pc Context) ast.Node { return nil } -func (s *rawHTMLParser) parseUntil(block text.Reader, closer []byte, pc Context) ast.Node { +func (s *rawHTMLParser) parseUntil(block text.Reader, closer []byte, _ Context) ast.Node { savedLine, savedSegment := block.Position() node := ast.NewRawHTML() for { @@ -119,7 +119,7 @@ func (s *rawHTMLParser) parseUntil(block text.Reader, closer []byte, pc Context) return nil } -func (s *rawHTMLParser) parseMultiLineRegexp(reg *regexp.Regexp, block text.Reader, pc Context) ast.Node { +func (s *rawHTMLParser) parseMultiLineRegexp(reg *regexp.Regexp, block text.Reader, _ Context) ast.Node { sline, ssegment := block.Position() if block.Match(reg) { node := ast.NewRawHTML() diff --git a/vendor/github.com/yuin/goldmark/parser/setext_headings.go b/vendor/github.com/yuin/goldmark/parser/setext_headings.go index 915bcc1..3558baa 100644 --- a/vendor/github.com/yuin/goldmark/parser/setext_headings.go +++ b/vendor/github.com/yuin/goldmark/parser/setext_headings.go @@ -95,6 +95,7 @@ func (b *setextHeadingParser) Close(node ast.Node, reader text.Reader, pc Contex } heading.Parent().RemoveChild(heading.Parent(), heading) } else { + heading.SetPos(tmp.Lines().At(0).Start) heading.SetLines(tmp.Lines()) heading.SetBlankPreviousLines(tmp.HasBlankPreviousLines()) tp := tmp.Parent() |
