goldmark_plaintext.go (3124B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package text 19 20 import ( 21 "github.com/yuin/goldmark/ast" 22 "github.com/yuin/goldmark/parser" 23 "github.com/yuin/goldmark/text" 24 ) 25 26 // plaintextParser implements goldmark.parser.BlockParser 27 type plaintextParser struct{} 28 29 var defaultPlaintextParser = &plaintextParser{} 30 31 func newPlaintextParser() parser.BlockParser { 32 return defaultPlaintextParser 33 } 34 35 func (b *plaintextParser) Trigger() []byte { 36 return nil 37 } 38 39 func (b *plaintextParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) { 40 _, segment := reader.PeekLine() 41 node := ast.NewParagraph() 42 node.Lines().Append(segment) 43 reader.Advance(segment.Len() - 1) 44 return node, parser.NoChildren 45 } 46 47 func (b *plaintextParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State { 48 _, segment := reader.PeekLine() 49 node.Lines().Append(segment) 50 reader.Advance(segment.Len() - 1) 51 return parser.Continue | parser.NoChildren 52 } 53 54 func (b *plaintextParser) Close(node ast.Node, reader text.Reader, pc parser.Context) {} 55 56 func (b *plaintextParser) CanInterruptParagraph() bool { 57 return false 58 } 59 60 func (b *plaintextParser) CanAcceptIndentedLine() bool { 61 return true 62 } 63 64 // plaintextParserNoParagraph implements goldmark.parser.BlockParser 65 type plaintextParserNoParagraph struct{} 66 67 var defaultPlaintextParserNoParagraph = &plaintextParserNoParagraph{} 68 69 func newPlaintextParserNoParagraph() parser.BlockParser { 70 return defaultPlaintextParserNoParagraph 71 } 72 73 func (b *plaintextParserNoParagraph) Trigger() []byte { 74 return nil 75 } 76 77 func (b *plaintextParserNoParagraph) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) { 78 _, segment := reader.PeekLine() 79 node := ast.NewDocument() 80 node.Lines().Append(segment) 81 reader.Advance(segment.Len() - 1) 82 return node, parser.NoChildren 83 } 84 85 func (b *plaintextParserNoParagraph) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State { 86 _, segment := reader.PeekLine() 87 node.Lines().Append(segment) 88 reader.Advance(segment.Len() - 1) 89 return parser.Continue | parser.NoChildren 90 } 91 92 func (b *plaintextParserNoParagraph) Close(node ast.Node, reader text.Reader, pc parser.Context) {} 93 94 func (b *plaintextParserNoParagraph) CanInterruptParagraph() bool { 95 return false 96 } 97 98 func (b *plaintextParserNoParagraph) CanAcceptIndentedLine() bool { 99 return true 100 }