github.com/cozy/cozy-stack@v0.0.0-20240327093429-939e4a21320e/model/note/custom/decision.go (about)

     1  package custom
     2  
     3  import (
     4  	"github.com/yuin/goldmark/ast"
     5  	"github.com/yuin/goldmark/parser"
     6  	"github.com/yuin/goldmark/text"
     7  	"github.com/yuin/goldmark/util"
     8  )
     9  
    10  // A DecisionList struct represents a decisionList in atlaskit.
    11  type DecisionList struct {
    12  	ast.BaseBlock
    13  }
    14  
    15  // Dump implements Node.Dump.
    16  func (n *DecisionList) Dump(source []byte, level int) {
    17  	ast.DumpHelper(n, source, level, nil, nil)
    18  }
    19  
    20  // KindDecisionList is a NodeKind of the DecisionList node.
    21  var KindDecisionList = ast.NewNodeKind("DecisionList")
    22  
    23  // Kind implements Node.Kind.
    24  func (n *DecisionList) Kind() ast.NodeKind {
    25  	return KindDecisionList
    26  }
    27  
    28  // NewDecisionList returns a new DecisionList node.
    29  func NewDecisionList() *DecisionList {
    30  	return &DecisionList{
    31  		BaseBlock: ast.BaseBlock{},
    32  	}
    33  }
    34  
    35  // A DecisionItem struct represents a decisionItem in atlaskit.
    36  type DecisionItem struct {
    37  	ast.BaseBlock
    38  }
    39  
    40  // Dump implements Node.Dump.
    41  func (n *DecisionItem) Dump(source []byte, level int) {
    42  	ast.DumpHelper(n, source, level, nil, nil)
    43  }
    44  
    45  // KindDecisionItem is a NodeKind of the DecisionItem node.
    46  var KindDecisionItem = ast.NewNodeKind("DecisionItem")
    47  
    48  // Kind implements Node.Kind.
    49  func (n *DecisionItem) Kind() ast.NodeKind {
    50  	return KindDecisionItem
    51  }
    52  
    53  // NewDecisionItem returns a new DecisionItem node.
    54  func NewDecisionItem() *DecisionItem {
    55  	return &DecisionItem{
    56  		BaseBlock: ast.BaseBlock{},
    57  	}
    58  }
    59  
    60  type decisionListParser struct{}
    61  
    62  var defaultDecisionListParser = &decisionListParser{}
    63  
    64  // NewDecisionListParser returns a new BlockParser that
    65  // parses decisionLists.
    66  func NewDecisionListParser() parser.BlockParser {
    67  	return defaultDecisionListParser
    68  }
    69  
    70  func (b *decisionListParser) Trigger() []byte {
    71  	return []byte{0xe2}
    72  }
    73  
    74  func (b *decisionListParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) {
    75  	if _, ok := parent.(*DecisionList); ok {
    76  		return nil, parser.NoChildren
    77  	}
    78  	line, _ := reader.PeekLine()
    79  	w, pos := util.IndentWidth(line, reader.LineOffset())
    80  	if w > 3 || pos+4 > len(line) || line[pos+1] != 0x9c || line[pos+2] != 0x8d || line[pos+3] != ' ' {
    81  		return nil, parser.NoChildren
    82  	}
    83  	return NewDecisionList(), parser.HasChildren
    84  }
    85  
    86  func (b *decisionListParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State {
    87  	line, _ := reader.PeekLine()
    88  	if util.IsBlank(line) {
    89  		return parser.Close
    90  	}
    91  	return parser.Continue | parser.HasChildren
    92  }
    93  
    94  func (b *decisionListParser) Close(node ast.Node, reader text.Reader, pc parser.Context) {
    95  	// nothing to do
    96  }
    97  
    98  func (b *decisionListParser) CanInterruptParagraph() bool {
    99  	return true
   100  }
   101  
   102  func (b *decisionListParser) CanAcceptIndentedLine() bool {
   103  	return false
   104  }
   105  
   106  type decisionItemParser struct{}
   107  
   108  var defaultDecisionItemParser = &decisionItemParser{}
   109  
   110  // NewDecisionItemParser returns a new BlockParser that
   111  // parses decisionItems.
   112  func NewDecisionItemParser() parser.BlockParser {
   113  	return defaultDecisionItemParser
   114  }
   115  
   116  func (b *decisionItemParser) Trigger() []byte {
   117  	return []byte{0xe2} // 0xe2 is the first byte of ✍
   118  }
   119  
   120  func (b *decisionItemParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) {
   121  	if _, ok := parent.(*DecisionList); !ok {
   122  		return nil, parser.NoChildren
   123  	}
   124  	line, _ := reader.PeekLine()
   125  	w, pos := util.IndentWidth(line, reader.LineOffset())
   126  	// 0x9c and 0x8d are the second and third bytes of ✍
   127  	if w > 3 || pos+4 > len(line) || line[pos+1] != 0x9c || line[pos+2] != 0x8d || line[pos+3] != ' ' {
   128  		return nil, parser.NoChildren
   129  	}
   130  	reader.Advance(pos + 4)
   131  	return NewDecisionItem(), parser.HasChildren
   132  }
   133  
   134  func (b *decisionItemParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State {
   135  	return parser.Close
   136  }
   137  
   138  func (b *decisionItemParser) Close(node ast.Node, reader text.Reader, pc parser.Context) {
   139  	// nothing to do
   140  }
   141  
   142  func (b *decisionItemParser) CanInterruptParagraph() bool {
   143  	return true
   144  }
   145  
   146  func (b *decisionItemParser) CanAcceptIndentedLine() bool {
   147  	return false
   148  }