github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/cmd/check-markdown/doc.go (about)

     1  //
     2  // Copyright (c) 2019 Intel Corporation
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  //
     6  
     7  package main
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  
    13  	"github.com/sirupsen/logrus"
    14  )
    15  
    16  // Details of the main document, and all other documents it references.
    17  // Key: document name.
    18  var docs map[string]*Doc
    19  
    20  func init() {
    21  	docs = make(map[string]*Doc)
    22  }
    23  
    24  // newDoc creates a new document.
    25  func newDoc(name string, logger *logrus.Entry) *Doc {
    26  	d := &Doc{
    27  		Name:     name,
    28  		Headings: make(map[string]Heading),
    29  		Links:    make(map[string][]Link),
    30  		Parsed:   false,
    31  		ShowTOC:  false,
    32  		Logger:   logger,
    33  	}
    34  
    35  	d.Logger = logger.WithField("file", d.Name)
    36  
    37  	// add to the hash
    38  	docs[name] = d
    39  
    40  	return d
    41  }
    42  
    43  // getDoc returns the Doc structure represented by the specified name,
    44  // creating it and adding to the docs map if necessary.
    45  func getDoc(name string, logger *logrus.Entry) (*Doc, error) {
    46  	if name == "" {
    47  		return &Doc{}, errors.New("need doc name")
    48  	}
    49  
    50  	doc, ok := docs[name]
    51  	if ok {
    52  		return doc, nil
    53  	}
    54  
    55  	return newDoc(name, logger), nil
    56  }
    57  
    58  // hasHeading returns true if the specified heading exists for the document.
    59  func (d *Doc) hasHeading(name string) bool {
    60  	return d.heading(name) != nil
    61  }
    62  
    63  // Errorf is a convenience function to generate an error for this particular
    64  // document.
    65  func (d *Doc) Errorf(format string, args ...interface{}) error {
    66  	s := fmt.Sprintf(format, args...)
    67  
    68  	return fmt.Errorf("file=%q: %s", d.Name, s)
    69  }
    70  
    71  // String "pretty-prints" the specified document
    72  //
    73  // Just display the name as that is enough in text output.
    74  func (d *Doc) String() string {
    75  	return d.Name
    76  }