-
Notifications
You must be signed in to change notification settings - Fork 0
[Feature] Copy markdown by cell #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
41ff320
Copy input.md to output.md through Cells
mwittie f7d3ae0
Use a string builder in Compose
mwittie 075c353
Use an idiomatic loop in Classify, since we don't need the index
mwittie ff4aaeb
Bring back fmt-check and vet targets
mwittie 4899752
Update dependencies
mwittie a880f04
Update test names
mwittie 0e1ea02
Make sure to clean and regenerate mocks as a part of PR
mwittie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
|
|
||
| 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")) | ||
|
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 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.