|
| 1 | +using Markdown |
| 2 | + |
| 3 | +Base.@kwdef struct MarkdownBlock |
| 4 | + block::Block |
| 5 | + text::Markdown.MD |
| 6 | +end |
| 7 | + |
| 8 | +function draw(d::MarkdownBlock, area::Rect, buf::Buffer) |
| 9 | + draw(d.block, area, buf); |
| 10 | + text_area = inner(d.block, area) |
| 11 | + |
| 12 | + title_area = Rect(left(area), top(area), width(area), height(area) ÷ 5) |
| 13 | + # Assume first block is always the header |
| 14 | + draw(d.text.content[1], title_area, buf) |
| 15 | + |
| 16 | + body_area = Rect(left(area), bottom(title_area), width(area), height(area) - height(title_area)) |
| 17 | + body_area = Rect(body_area, Margin(5, 5)) |
| 18 | + items = d.text.content[2:end] |
| 19 | + inner_area = Rect(left(body_area), top(body_area) - height(body_area) ÷ length(items), width(body_area), height(body_area) ÷ length(items)) |
| 20 | + for (i, c) in enumerate(d.text.content[2:end]) |
| 21 | + inner_area = Rect(left(inner_area), bottom(inner_area), width(inner_area), height(inner_area)) |
| 22 | + draw(c, inner_area, buf) |
| 23 | + end |
| 24 | +end |
| 25 | + |
| 26 | +function draw(h::Markdown.Header, area::Rect, buf::Buffer) |
| 27 | + b = Block(border_type = BorderTypeLight) |
| 28 | + draw(b, area, buf); |
| 29 | + |
| 30 | + title = h.text[1] |
| 31 | + |
| 32 | + row = top(area) + height(area) ÷ 2 |
| 33 | + col = width(area) ÷ 2 - ( length(title) ÷ 2 ) |
| 34 | + set(buf, col, row, title, Crayon(bold = true)) |
| 35 | +end |
| 36 | + |
| 37 | +function draw(l::Markdown.List, area::Rect, buf::Buffer) |
| 38 | + |
| 39 | + for (i, item) in enumerate(l.items) |
| 40 | + item_area = Rect( |
| 41 | + left(area), |
| 42 | + top(area) + (i - 1) * (height(area) ÷ length(l.items)), |
| 43 | + width(area), |
| 44 | + height(area) ÷ length(l.items) |
| 45 | + ) |
| 46 | + row = top(item_area) |
| 47 | + col = left(item_area) |
| 48 | + set(buf, col, row, " * ", Crayon(bold = true, foreground = :red)) |
| 49 | + inner_area = Rect(item_area, Margin(0, 5)) |
| 50 | + draw(item[1], inner_area, buf) |
| 51 | + end |
| 52 | + |
| 53 | +end |
| 54 | + |
| 55 | +function draw(p::Markdown.Paragraph, area::Rect, buf::Buffer) |
| 56 | + |
| 57 | + row = top(area) |
| 58 | + col = left(area) |
| 59 | + for c in p.content |
| 60 | + if typeof(c) == String |
| 61 | + l = length(c) |
| 62 | + set(buf, col, row, c) |
| 63 | + col += l |
| 64 | + elseif typeof(c) == Markdown.Bold |
| 65 | + l = length(c.text[1]) |
| 66 | + set(buf, col, row, c.text[1], Crayon(bold = true)) |
| 67 | + col += l |
| 68 | + elseif typeof(c) == Markdown.Italic |
| 69 | + l = length(c.text[1]) |
| 70 | + set(buf, col, row, c.text[1], Crayon(italics = true)) |
| 71 | + col += l |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | +end |
0 commit comments