Skip to content

Commit 625f50c

Browse files
authored
[Feature] Copy markdown by cell (#3)
1 parent 5147973 commit 625f50c

11 files changed

Lines changed: 728 additions & 71 deletions

File tree

.mockery.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
template: testify
2+
filename: "{{.InterfaceName}}_mock_test.go"
3+
packages:
4+
litdoc/internal:
5+
interfaces:
6+
Cell:
7+
config:
8+
dir: ./internal/
9+
pkgname: internal_test

Makefile

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
.PHONY: pre-pr
2-
pre-pr: fmt-check vet test
2+
pre-pr: clean mock fmt-check vet test
33

4-
.PHONY: vet
5-
vet:
6-
@go vet ./...
4+
.PHONY: fmt
5+
fmt:
6+
@gofmt -w .
77

88
.PHONY: fmt-check
99
fmt-check:
@@ -14,18 +14,31 @@ fmt-check:
1414
exit 1; \
1515
fi
1616

17+
.PHONY: vet
18+
vet:
19+
@go vet ./...
20+
21+
.PHONY: mock
22+
mock:
23+
@mockery
24+
25+
.PHONY: mock-clean
26+
mock-clean:
27+
@find . \( -name '*_mock_test.go' -o -name '*_mock.go' \) -not -path './vendor/*' -delete
28+
1729
GO_FILES := $(shell find . -name '*.go' -not -path './vendor/*')
30+
GOCACHE ?= /tmp/litdoc-go-build
1831

1932
bin/litdoc: $(GO_FILES)
20-
@go build -o bin/litdoc .
33+
@GOCACHE=$(GOCACHE) go build -o bin/litdoc .
2134

2235
.PHONY: build
2336
build: bin/litdoc
2437

2538
.PHONY: test
2639
test: build
27-
@go test ./... --count=1
40+
@GOCACHE=$(GOCACHE) go test ./... --count=1
2841

2942
.PHONY: clean
30-
clean:
31-
@rm -rf bin/
43+
clean: mock-clean
44+
@rm -rf bin/

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1515
github.com/pmezard/go-difflib v1.0.0 // indirect
1616
github.com/spf13/pflag v1.0.9 // indirect
17+
github.com/stretchr/objx v0.5.2 // indirect
1718
golang.org/x/sys v0.26.0 // indirect
1819
golang.org/x/tools v0.26.0 // indirect
1920
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
1414
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
1515
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
1616
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
17+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
18+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
1719
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
1820
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
1921
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=

internal/Cell_mock_test.go

Lines changed: 146 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cell.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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

Comments
 (0)