Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
template: testify
filename: "{{.InterfaceName}}_mock_test.go"
packages:
litdoc/internal:
interfaces:
Cell:
config:
dir: ./internal/
pkgname: internal_test
29 changes: 21 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.PHONY: pre-pr
pre-pr: fmt-check vet test
pre-pr: clean mock fmt-check vet test

.PHONY: vet
vet:
@go vet ./...
.PHONY: fmt
fmt:
@gofmt -w .

.PHONY: fmt-check
fmt-check:
Expand All @@ -14,18 +14,31 @@ fmt-check:
exit 1; \
fi

.PHONY: vet
vet:
@go vet ./...

.PHONY: mock
mock:
@mockery

.PHONY: mock-clean
mock-clean:
@find . \( -name '*_mock_test.go' -o -name '*_mock.go' \) -not -path './vendor/*' -delete

GO_FILES := $(shell find . -name '*.go' -not -path './vendor/*')
GOCACHE ?= /tmp/litdoc-go-build

bin/litdoc: $(GO_FILES)
@go build -o bin/litdoc .
@GOCACHE=$(GOCACHE) go build -o bin/litdoc .

.PHONY: build
build: bin/litdoc

.PHONY: test
test: build
@go test ./... --count=1
@GOCACHE=$(GOCACHE) go test ./... --count=1

.PHONY: clean
clean:
@rm -rf bin/
clean: mock-clean
@rm -rf bin/
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/stretchr/objx v0.5.2 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/tools v0.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
Expand Down
146 changes: 146 additions & 0 deletions internal/Cell_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions internal/cell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package internal

import (
"bytes"
"fmt"
"strings"
)

type Cell interface {
Execute() (Cell, error)
Render() (string, error)
}

type StaticCell struct {
content string
}

func MakeStaticCellFromRaw(raw string) StaticCell {
return StaticCell{content: raw}
}

func (t StaticCell) Execute() (Cell, error) {
return t, nil
}

func (t StaticCell) Render() (string, error) {
return t.content, nil
}

type BashCell struct {
fencedCode string
output string
}

func MakeBashCellFromRaw(fencedCode, output string) BashCell {
return BashCell{fencedCode: fencedCode, output: output}
}

func (c BashCell) Execute() (Cell, error) {
return c, nil
Comment thread
mwittie marked this conversation as resolved.
}

func (c BashCell) Render() (string, error) {
if c.output == "" {
return c.fencedCode, nil
}
return c.fencedCode + "\n" + c.output, nil
}

type InfoString struct {
Lang string
IsLitdoc bool
}

func ParseInfoString(b Block) InfoString {
firstLine := b.content
if i := bytes.IndexByte(b.content, '\n'); i >= 0 {
firstLine = b.content[:i]
}
var raw []byte
switch b.kind {
case BlockKindFencedCode:
raw = bytes.TrimLeft(firstLine, "`~")
case BlockKindHTMLComment:
raw = bytes.TrimSpace(bytes.TrimPrefix(firstLine, []byte("<!--")))
default:
return InfoString{}
}
parts := bytes.SplitN(raw, []byte(" | "), 2)
lang := string(bytes.TrimSpace(parts[0]))
isLitdoc := len(parts) > 1 && bytes.HasPrefix(bytes.TrimSpace(parts[1]), []byte("litdoc"))
Comment thread
mwittie marked this conversation as resolved.
return InfoString{Lang: lang, IsLitdoc: isLitdoc}
}

func Classify(blocks []Block) ([]Cell, error) {
var cells []Cell
for _, b := range blocks {
info := ParseInfoString(b)
switch {
case info.IsLitdoc && info.Lang == "bash":
cell := MakeBashCellFromRaw(string(b.content), "")
cells = append(cells, cell)
case info.IsLitdoc:
return nil, fmt.Errorf("unsupported language: %q", info.Lang)
default:
cells = append(cells, MakeStaticCellFromRaw(string(b.content)))
}
}
return cells, nil
}

func Execute(cells []Cell) ([]Cell, error) {
var executedCells []Cell
for _, c := range cells {
executed, err := c.Execute()
if err != nil {
return nil, fmt.Errorf("executing cell: %w", err)
}
executedCells = append(executedCells, executed)
}
return executedCells, nil
}

func Compose(cells []Cell) (string, error) {
var dst strings.Builder
for _, c := range cells {
rendered, err := c.Render()
if err != nil {
return "", fmt.Errorf("rendering cell: %w", err)
}
dst.WriteString(rendered)
}
return dst.String(), nil
}
Loading
Loading