-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquote.go
61 lines (51 loc) · 1.29 KB
/
quote.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package markdown
// A Quote is a [Block] representing a [block quote].
//
// [block quote]: https://spec.commonmark.org/0.31.2/#block-quotes
type Quote struct {
Position
Blocks []Block // content of quote
}
func (*Quote) Block() {}
func (b *Quote) printHTML(p *printer) {
p.html("<blockquote>\n")
for _, c := range b.Blocks {
c.printHTML(p)
}
p.html("</blockquote>\n")
}
func (b *Quote) printMarkdown(p *printer) {
p.maybeQuoteNL('>')
p.WriteString("> ")
defer p.pop(p.push("> "))
printMarkdownBlocks(b.Blocks, p)
}
// A quoteBuildier is a [blockBuilder] for a block quote.
type quoteBuilder struct{}
// startBlockQuote is a [starter] for a [Quote].
func startBlockQuote(p *parser, s line) (line, bool) {
line, ok := trimQuote(s)
if !ok {
return s, false
}
p.addBlock(new(quoteBuilder))
return line, true
}
func trimQuote(s line) (line, bool) {
t := s
t.trimSpace(0, 3, false)
if !t.trim('>') {
return s, false
}
t.trimSpace(0, 1, true)
return t, true
}
func (b *quoteBuilder) extend(p *parser, s line) (line, bool) {
return trimQuote(s)
}
func (b *quoteBuilder) build(p *parser) Block {
return &Quote{p.pos(), p.blocks()}
}