2019-08-25 10:30:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-08-25 13:40:56 +00:00
|
|
|
"io"
|
2019-08-25 10:30:00 +00:00
|
|
|
)
|
|
|
|
|
2019-08-25 13:40:56 +00:00
|
|
|
type Renderer interface {
|
|
|
|
Render(w io.Writer) error
|
|
|
|
}
|
|
|
|
|
2019-08-25 10:30:00 +00:00
|
|
|
type Block struct {
|
|
|
|
Type string
|
|
|
|
Data json.RawMessage
|
2019-08-25 13:40:56 +00:00
|
|
|
|
|
|
|
renderer Renderer
|
2019-08-25 10:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Paragraph struct {
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Code struct {
|
|
|
|
Code string
|
|
|
|
}
|
|
|
|
|
|
|
|
type List struct {
|
|
|
|
Style string
|
|
|
|
Items []string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Header struct {
|
|
|
|
Level int
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChecklistItem struct {
|
|
|
|
Text string
|
|
|
|
Checked bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Checklist struct {
|
|
|
|
Style string
|
|
|
|
Items []ChecklistItem
|
|
|
|
}
|
|
|
|
|
|
|
|
type Link struct {
|
|
|
|
Link string
|
|
|
|
Meta LinkResponseMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
type Table struct {
|
|
|
|
Content [][]string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Document struct {
|
|
|
|
Time int64
|
|
|
|
Version string
|
|
|
|
Blocks []Block
|
|
|
|
}
|
|
|
|
|
2019-08-25 13:40:56 +00:00
|
|
|
func (block Block) getType() (Renderer, error) {
|
|
|
|
switch block.Type {
|
|
|
|
case "table":
|
|
|
|
return &Table{}, nil
|
|
|
|
case "link":
|
|
|
|
return &Link{}, nil
|
|
|
|
case "list":
|
|
|
|
return &List{}, nil
|
|
|
|
case "header":
|
|
|
|
return &Header{}, nil
|
|
|
|
case "paragraph":
|
|
|
|
return &Paragraph{}, nil
|
|
|
|
case "code":
|
|
|
|
return &Code{}, nil
|
|
|
|
case "checklist":
|
|
|
|
return &Checklist{}, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown type: %s", block.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (block Block) load() (Renderer, error) {
|
|
|
|
if block.renderer != nil {
|
|
|
|
return block.renderer, nil
|
|
|
|
}
|
|
|
|
renderer, err := block.getType()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(block.Data, renderer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
block.renderer = renderer
|
|
|
|
|
|
|
|
return renderer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (document *Document) Render(w io.Writer) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, block := range document.Blocks {
|
|
|
|
renderer, err := block.load()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = renderer.Render(&buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err := buf.WriteTo(w)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-25 10:30:00 +00:00
|
|
|
func renderJSON(text string) (string, error) {
|
2019-08-25 13:40:56 +00:00
|
|
|
var document Document
|
|
|
|
err := json.Unmarshal([]byte(text), &document)
|
2019-08-25 10:30:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-08-25 13:40:56 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
err = document.Render(&buf)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (checklist *Checklist) Render(w io.Writer) error {
|
2019-08-25 10:30:00 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2019-08-25 13:40:56 +00:00
|
|
|
buf.WriteString(`<div class="checklist">`)
|
|
|
|
for _, item := range checklist.Items {
|
|
|
|
buf.WriteString(`<div class="checklist--item">`)
|
|
|
|
|
|
|
|
buf.WriteString(`<span class="icon is-medium">`)
|
|
|
|
if item.Checked {
|
|
|
|
buf.WriteString(`<i class="fa fa-check-circle has-text-success"></i>`)
|
2020-05-07 12:15:36 +00:00
|
|
|
} else {
|
|
|
|
buf.WriteString(`<i class="fa fa-circle-thin"></i>`)
|
2019-08-25 10:30:00 +00:00
|
|
|
}
|
2019-08-25 13:40:56 +00:00
|
|
|
buf.WriteString(`</span>`)
|
|
|
|
buf.WriteString(`<div class="checklist--item-text">`)
|
|
|
|
buf.WriteString(item.Text)
|
|
|
|
buf.WriteString("</div>")
|
|
|
|
buf.WriteString("</div>")
|
|
|
|
}
|
|
|
|
buf.WriteString("</div>")
|
|
|
|
_, err := buf.WriteTo(w)
|
|
|
|
return err
|
|
|
|
}
|
2019-08-25 10:30:00 +00:00
|
|
|
|
2019-08-25 13:40:56 +00:00
|
|
|
func (code *Code) Render(w io.Writer) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("<pre>")
|
|
|
|
buf.WriteString(code.Code)
|
|
|
|
buf.WriteString("</pre>")
|
|
|
|
_, err := buf.WriteTo(w)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (link *Link) Render(w io.Writer) error {
|
|
|
|
// TODO(peter): improve link rendering
|
|
|
|
_, err := fmt.Fprintf(w, `<a href=%q>%s</a>`, link.Link, link.Meta.Title)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (table *Table) Render(w io.Writer) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("<table class='table'>")
|
|
|
|
for _, row := range table.Content {
|
|
|
|
buf.WriteString("<tr>")
|
|
|
|
for _, col := range row {
|
|
|
|
buf.WriteString("<td>")
|
|
|
|
buf.WriteString(col)
|
|
|
|
buf.WriteString("</td>")
|
|
|
|
}
|
|
|
|
buf.WriteString("</tr>")
|
2019-08-25 10:30:00 +00:00
|
|
|
}
|
2019-08-25 13:40:56 +00:00
|
|
|
buf.WriteString("</table>")
|
|
|
|
_, err := buf.WriteTo(w)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
func (list *List) Render(w io.Writer) error {
|
|
|
|
var buf bytes.Buffer
|
2019-08-25 10:30:00 +00:00
|
|
|
|
2019-08-25 13:40:56 +00:00
|
|
|
var tag string
|
|
|
|
if list.Style == "ordered" {
|
|
|
|
tag = "ol"
|
|
|
|
} else {
|
|
|
|
tag = "ul"
|
|
|
|
}
|
|
|
|
buf.WriteString("<")
|
|
|
|
buf.WriteString(tag)
|
|
|
|
buf.WriteString(">")
|
|
|
|
for _, item := range list.Items {
|
|
|
|
buf.WriteString("<li>")
|
|
|
|
buf.WriteString(item)
|
|
|
|
buf.WriteString("</li>")
|
|
|
|
}
|
|
|
|
buf.WriteString("</")
|
|
|
|
buf.WriteString(tag)
|
|
|
|
buf.WriteString(">")
|
|
|
|
|
|
|
|
_, err := buf.WriteTo(w)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (header *Header) Render(w io.Writer) error {
|
|
|
|
_, err := fmt.Fprintf(w, "<h%d>%s</h%d>", header.Level, header.Text, header.Level)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (para Paragraph) Render(w io.Writer) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("<p>")
|
|
|
|
buf.WriteString(para.Text)
|
|
|
|
buf.WriteString("</p>")
|
|
|
|
_, err := buf.WriteTo(w)
|
|
|
|
return err
|
2019-08-25 10:30:00 +00:00
|
|
|
}
|