github.com/anthonyme00/gomarkdoc@v1.0.0/lang/list.go (about)

     1  package lang
     2  
     3  import (
     4  	"go/doc/comment"
     5  	"strconv"
     6  )
     7  
     8  // List defines a list block element in the documentation for a symbol or
     9  // package.
    10  type List struct {
    11  	blankBetween bool
    12  	items        []*Item
    13  }
    14  
    15  // NewList initializes a list from the equivalent type from the comment package.
    16  func NewList(cfg *Config, docList *comment.List) *List {
    17  	var l List
    18  	l.items = make([]*Item, len(docList.Items))
    19  	for i, item := range docList.Items {
    20  		l.items[i] = NewItem(cfg.Inc(0), item)
    21  	}
    22  
    23  	l.blankBetween = docList.BlankBetween()
    24  
    25  	return &l
    26  }
    27  
    28  // BlankBetween returns true if there should be a blank line between list items.
    29  func (l *List) BlankBetween() bool {
    30  	return l.blankBetween
    31  }
    32  
    33  // Items returns the slice of items in the list.
    34  func (l *List) Items() []*Item {
    35  	return l.items
    36  }
    37  
    38  // ItemKind identifies the kind of item
    39  type ItemKind string
    40  
    41  const (
    42  	// OrderedItem identifies an ordered (i.e. numbered) item.
    43  	OrderedItem ItemKind = "ordered"
    44  
    45  	// UnorderedItem identifies an unordered (i.e. bulletted) item.
    46  	UnorderedItem ItemKind = "unordered"
    47  )
    48  
    49  // Item defines a single item in a list in the documentation for a symbol or
    50  // package.
    51  type Item struct {
    52  	blocks []*Block
    53  	kind   ItemKind
    54  	number int
    55  }
    56  
    57  // NewItem initializes a list item from the equivalent type from the comment
    58  // package.
    59  func NewItem(cfg *Config, docItem *comment.ListItem) *Item {
    60  	var (
    61  		num  int
    62  		kind ItemKind
    63  	)
    64  	if n, err := strconv.Atoi(docItem.Number); err == nil {
    65  		num = n
    66  		kind = OrderedItem
    67  	} else {
    68  		kind = UnorderedItem
    69  	}
    70  
    71  	return &Item{
    72  		blocks: ParseBlocks(cfg, docItem.Content, true),
    73  		kind:   kind,
    74  		number: num,
    75  	}
    76  }
    77  
    78  // Blocks returns the blocks of documentation in a list item.
    79  func (i *Item) Blocks() []*Block {
    80  	return i.blocks
    81  }
    82  
    83  // Kind returns the kind of the list item.
    84  func (i *Item) Kind() ItemKind {
    85  	return i.kind
    86  }
    87  
    88  // Number returns the number of the list item. Only populated if the item is of
    89  // the OrderedItem kind.
    90  func (i *Item) Number() int {
    91  	return i.number
    92  }