|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +type Cell interface { |
| 10 | + Execute() (Cell, error) |
| 11 | + Render() (string, error) |
| 12 | +} |
| 13 | + |
| 14 | +type StaticCell struct { |
| 15 | + content string |
| 16 | +} |
| 17 | + |
| 18 | +func MakeStaticCellFromRaw(raw string) StaticCell { |
| 19 | + return StaticCell{content: raw} |
| 20 | +} |
| 21 | + |
| 22 | +func (t StaticCell) Execute() (Cell, error) { |
| 23 | + return t, nil |
| 24 | +} |
| 25 | + |
| 26 | +func (t StaticCell) Render() (string, error) { |
| 27 | + return t.content, nil |
| 28 | +} |
| 29 | + |
| 30 | +type BashCell struct { |
| 31 | + fencedCode string |
| 32 | + output string |
| 33 | +} |
| 34 | + |
| 35 | +func MakeBashCellFromRaw(fencedCode, output string) BashCell { |
| 36 | + return BashCell{fencedCode: fencedCode, output: output} |
| 37 | +} |
| 38 | + |
| 39 | +func (c BashCell) Execute() (Cell, error) { |
| 40 | + return c, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (c BashCell) Render() (string, error) { |
| 44 | + if c.output == "" { |
| 45 | + return c.fencedCode, nil |
| 46 | + } |
| 47 | + return c.fencedCode + "\n" + c.output, nil |
| 48 | +} |
| 49 | + |
| 50 | +type InfoString struct { |
| 51 | + Lang string |
| 52 | + IsLitdoc bool |
| 53 | +} |
| 54 | + |
| 55 | +func ParseInfoString(b Block) InfoString { |
| 56 | + firstLine := b.content |
| 57 | + if i := bytes.IndexByte(b.content, '\n'); i >= 0 { |
| 58 | + firstLine = b.content[:i] |
| 59 | + } |
| 60 | + var raw []byte |
| 61 | + switch b.kind { |
| 62 | + case BlockKindFencedCode: |
| 63 | + raw = bytes.TrimLeft(firstLine, "`~") |
| 64 | + case BlockKindHTMLComment: |
| 65 | + raw = bytes.TrimSpace(bytes.TrimPrefix(firstLine, []byte("<!--"))) |
| 66 | + default: |
| 67 | + return InfoString{} |
| 68 | + } |
| 69 | + parts := bytes.SplitN(raw, []byte(" | "), 2) |
| 70 | + lang := string(bytes.TrimSpace(parts[0])) |
| 71 | + isLitdoc := len(parts) > 1 && bytes.HasPrefix(bytes.TrimSpace(parts[1]), []byte("litdoc")) |
| 72 | + return InfoString{Lang: lang, IsLitdoc: isLitdoc} |
| 73 | +} |
| 74 | + |
| 75 | +func Classify(blocks []Block) ([]Cell, error) { |
| 76 | + var cells []Cell |
| 77 | + for _, b := range blocks { |
| 78 | + info := ParseInfoString(b) |
| 79 | + switch { |
| 80 | + case info.IsLitdoc && info.Lang == "bash": |
| 81 | + cell := MakeBashCellFromRaw(string(b.content), "") |
| 82 | + cells = append(cells, cell) |
| 83 | + case info.IsLitdoc: |
| 84 | + return nil, fmt.Errorf("unsupported language: %q", info.Lang) |
| 85 | + default: |
| 86 | + cells = append(cells, MakeStaticCellFromRaw(string(b.content))) |
| 87 | + } |
| 88 | + } |
| 89 | + return cells, nil |
| 90 | +} |
| 91 | + |
| 92 | +func Execute(cells []Cell) ([]Cell, error) { |
| 93 | + var executedCells []Cell |
| 94 | + for _, c := range cells { |
| 95 | + executed, err := c.Execute() |
| 96 | + if err != nil { |
| 97 | + return nil, fmt.Errorf("executing cell: %w", err) |
| 98 | + } |
| 99 | + executedCells = append(executedCells, executed) |
| 100 | + } |
| 101 | + return executedCells, nil |
| 102 | +} |
| 103 | + |
| 104 | +func Compose(cells []Cell) (string, error) { |
| 105 | + var dst strings.Builder |
| 106 | + for _, c := range cells { |
| 107 | + rendered, err := c.Render() |
| 108 | + if err != nil { |
| 109 | + return "", fmt.Errorf("rendering cell: %w", err) |
| 110 | + } |
| 111 | + dst.WriteString(rendered) |
| 112 | + } |
| 113 | + return dst.String(), nil |
| 114 | +} |
0 commit comments