...

Source file src/github.com/yuin/goldmark/parser/paragraph.go

Documentation: github.com/yuin/goldmark/parser

     1  package parser
     2  
     3  import (
     4  	"github.com/yuin/goldmark/ast"
     5  	"github.com/yuin/goldmark/text"
     6  	"github.com/yuin/goldmark/util"
     7  )
     8  
     9  type paragraphParser struct {
    10  }
    11  
    12  var defaultParagraphParser = &paragraphParser{}
    13  
    14  // NewParagraphParser returns a new BlockParser that
    15  // parses paragraphs.
    16  func NewParagraphParser() BlockParser {
    17  	return defaultParagraphParser
    18  }
    19  
    20  func (b *paragraphParser) Trigger() []byte {
    21  	return nil
    22  }
    23  
    24  func (b *paragraphParser) Open(parent ast.Node, reader text.Reader, pc Context) (ast.Node, State) {
    25  	_, segment := reader.PeekLine()
    26  	segment = segment.TrimLeftSpace(reader.Source())
    27  	if segment.IsEmpty() {
    28  		return nil, NoChildren
    29  	}
    30  	node := ast.NewParagraph()
    31  	node.Lines().Append(segment)
    32  	reader.Advance(segment.Len() - 1)
    33  	return node, NoChildren
    34  }
    35  
    36  func (b *paragraphParser) Continue(node ast.Node, reader text.Reader, pc Context) State {
    37  	line, segment := reader.PeekLine()
    38  	if util.IsBlank(line) {
    39  		return Close
    40  	}
    41  	node.Lines().Append(segment)
    42  	reader.Advance(segment.Len() - 1)
    43  	return Continue | NoChildren
    44  }
    45  
    46  func (b *paragraphParser) Close(node ast.Node, reader text.Reader, pc Context) {
    47  	lines := node.Lines()
    48  	if lines.Len() != 0 {
    49  		// trim leading spaces
    50  		for i := 0; i < lines.Len(); i++ {
    51  			l := lines.At(i)
    52  			lines.Set(i, l.TrimLeftSpace(reader.Source()))
    53  		}
    54  
    55  		// trim trailing spaces
    56  		length := lines.Len()
    57  		lastLine := node.Lines().At(length - 1)
    58  		node.Lines().Set(length-1, lastLine.TrimRightSpace(reader.Source()))
    59  	}
    60  	if lines.Len() == 0 {
    61  		node.Parent().RemoveChild(node.Parent(), node)
    62  		return
    63  	}
    64  }
    65  
    66  func (b *paragraphParser) CanInterruptParagraph() bool {
    67  	return false
    68  }
    69  
    70  func (b *paragraphParser) CanAcceptIndentedLine() bool {
    71  	return false
    72  }
    73  

View as plain text