|
| 1 | +// Copyright 2022 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package math |
| 6 | + |
| 7 | +import ( |
| 8 | + "github.com/yuin/goldmark/ast" |
| 9 | + "github.com/yuin/goldmark/parser" |
| 10 | + "github.com/yuin/goldmark/text" |
| 11 | + "github.com/yuin/goldmark/util" |
| 12 | +) |
| 13 | + |
| 14 | +type blockParser struct { |
| 15 | + parseDollars bool |
| 16 | +} |
| 17 | + |
| 18 | +type blockData struct { |
| 19 | + dollars bool |
| 20 | + indent int |
| 21 | +} |
| 22 | + |
| 23 | +var blockInfoKey = parser.NewContextKey() |
| 24 | + |
| 25 | +// NewBlockParser creates a new math BlockParser |
| 26 | +func NewBlockParser(parseDollarBlocks bool) parser.BlockParser { |
| 27 | + return &blockParser{ |
| 28 | + parseDollars: parseDollarBlocks, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Open parses the current line and returns a result of parsing. |
| 33 | +func (b *blockParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) { |
| 34 | + line, _ := reader.PeekLine() |
| 35 | + pos := pc.BlockOffset() |
| 36 | + if pos == -1 || len(line[pos:]) < 2 { |
| 37 | + return nil, parser.NoChildren |
| 38 | + } |
| 39 | + |
| 40 | + dollars := false |
| 41 | + if b.parseDollars && line[pos] == '$' && line[pos+1] == '$' { |
| 42 | + dollars = true |
| 43 | + } else if line[pos] != '\\' || line[pos+1] != '[' { |
| 44 | + return nil, parser.NoChildren |
| 45 | + } |
| 46 | + |
| 47 | + pc.Set(blockInfoKey, &blockData{dollars: dollars, indent: pos}) |
| 48 | + node := NewBlock() |
| 49 | + return node, parser.NoChildren |
| 50 | +} |
| 51 | + |
| 52 | +// Continue parses the current line and returns a result of parsing. |
| 53 | +func (b *blockParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State { |
| 54 | + line, segment := reader.PeekLine() |
| 55 | + data := pc.Get(blockInfoKey).(*blockData) |
| 56 | + w, pos := util.IndentWidth(line, 0) |
| 57 | + if w < 4 { |
| 58 | + if data.dollars { |
| 59 | + i := pos |
| 60 | + for ; i < len(line) && line[i] == '$'; i++ { |
| 61 | + } |
| 62 | + length := i - pos |
| 63 | + if length >= 2 && util.IsBlank(line[i:]) { |
| 64 | + reader.Advance(segment.Stop - segment.Start - segment.Padding) |
| 65 | + return parser.Close |
| 66 | + } |
| 67 | + } else if len(line[pos:]) > 1 && line[pos] == '\\' && line[pos+1] == ']' && util.IsBlank(line[pos+2:]) { |
| 68 | + reader.Advance(segment.Stop - segment.Start - segment.Padding) |
| 69 | + return parser.Close |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + pos, padding := util.IndentPosition(line, 0, data.indent) |
| 74 | + seg := text.NewSegmentPadding(segment.Start+pos, segment.Stop, padding) |
| 75 | + node.Lines().Append(seg) |
| 76 | + reader.AdvanceAndSetPadding(segment.Stop-segment.Start-pos-1, padding) |
| 77 | + return parser.Continue | parser.NoChildren |
| 78 | +} |
| 79 | + |
| 80 | +// Close will be called when the parser returns Close. |
| 81 | +func (b *blockParser) Close(node ast.Node, reader text.Reader, pc parser.Context) { |
| 82 | + pc.Set(blockInfoKey, nil) |
| 83 | +} |
| 84 | + |
| 85 | +// CanInterruptParagraph returns true if the parser can interrupt paragraphs, |
| 86 | +// otherwise false. |
| 87 | +func (b *blockParser) CanInterruptParagraph() bool { |
| 88 | + return true |
| 89 | +} |
| 90 | + |
| 91 | +// CanAcceptIndentedLine returns true if the parser can open new node when |
| 92 | +// the given line is being indented more than 3 spaces. |
| 93 | +func (b *blockParser) CanAcceptIndentedLine() bool { |
| 94 | + return false |
| 95 | +} |
| 96 | + |
| 97 | +// Trigger returns a list of characters that triggers Parse method of |
| 98 | +// this parser. |
| 99 | +// If Trigger returns a nil, Open will be called with any lines. |
| 100 | +// |
| 101 | +// We leave this as nil as our parse method is quick enough |
| 102 | +func (b *blockParser) Trigger() []byte { |
| 103 | + return nil |
| 104 | +} |
0 commit comments