| 1 | // gomuks - A terminal Matrix client written in Go. |
| 2 | // Copyright (C) 2020 Tulir Asokan |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU Affero General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU Affero General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU Affero General Public License |
| 15 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | |
| 17 | package html |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "strings" |
| 22 | |
| 23 | "go.mau.fi/mauview" |
| 24 | ) |
| 25 | |
| 26 | type BlockquoteEntity struct { |
| 27 | *ContainerEntity |
| 28 | } |
| 29 | |
| 30 | const BlockQuoteChar = '>' |
| 31 | |
| 32 | func NewBlockquoteEntity(children []Entity) *BlockquoteEntity { |
| 33 | return &BlockquoteEntity{&ContainerEntity{ |
| 34 | BaseEntity: &BaseEntity{ |
| 35 | Tag: "blockquote", |
| 36 | Block: true, |
| 37 | }, |
| 38 | Children: children, |
| 39 | Indent: 2, |
| 40 | }} |
| 41 | } |
| 42 | |
| 43 | func (be *BlockquoteEntity) AdjustStyle(fn AdjustStyleFunc, reason AdjustStyleReason) Entity { |
| 44 | be.BaseEntity = be.BaseEntity.AdjustStyle(fn, reason).(*BaseEntity) |
| 45 | return be |
| 46 | } |
| 47 | |
| 48 | func (be *BlockquoteEntity) Clone() Entity { |
| 49 | return &BlockquoteEntity{ContainerEntity: be.ContainerEntity.Clone().(*ContainerEntity)} |
| 50 | } |
| 51 | |
| 52 | func (be *BlockquoteEntity) Draw(screen mauview.Screen, ctx DrawContext) { |
| 53 | be.ContainerEntity.Draw(screen, ctx) |
| 54 | for y := 0; y < be.height; y++ { |
| 55 | screen.SetContent(0, y, BlockQuoteChar, nil, be.Style) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func (be *BlockquoteEntity) PlainText() string { |
| 60 | if len(be.Children) == 0 { |
| 61 | return "" |
| 62 | } |
| 63 | var buf strings.Builder |
| 64 | newlined := false |
| 65 | for i, child := range be.Children { |
| 66 | if i != 0 && child.IsBlock() && !newlined { |
| 67 | buf.WriteRune('\n') |
| 68 | } |
| 69 | newlined = false |
| 70 | for i, row := range strings.Split(child.PlainText(), "\n") { |
| 71 | if i != 0 { |
| 72 | buf.WriteRune('\n') |
| 73 | } |
| 74 | buf.WriteRune('>') |
| 75 | buf.WriteRune(' ') |
| 76 | buf.WriteString(row) |
| 77 | } |
| 78 | if child.IsBlock() { |
| 79 | buf.WriteRune('\n') |
| 80 | newlined = true |
| 81 | } |
| 82 | } |
| 83 | return strings.TrimSpace(buf.String()) |
| 84 | } |
| 85 | |
| 86 | func (be *BlockquoteEntity) String() string { |
| 87 | return fmt.Sprintf("&html.BlockquoteEntity{%s},\n", be.BaseEntity) |
| 88 | } |