-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to create a .md file including the entire contents (#156)
- Loading branch information
1 parent
32ff2cd
commit f179129
Showing
19 changed files
with
234 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
File renamed without changes.
This file contains 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,44 @@ | ||
package problems | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
) | ||
|
||
func ParseSection(dir, section string) (string, error) { | ||
content, err := os.ReadFile(filepath.Join(dir, section, "README.md")) | ||
if err != nil { | ||
return "", fmt.Errorf("error reading file: %w", err) | ||
} | ||
|
||
preparedContent, err := replaceRehearsal(string(content), dir, section) | ||
if err != nil { | ||
return "", fmt.Errorf("error replacing the rehearsal section in file: %w", err) | ||
} | ||
|
||
return preparedContent, nil | ||
} | ||
|
||
func replaceRehearsal(input, dir, section string) (string, error) { | ||
rehearsalRegex := regexp.MustCompile(`(?s)(## Rehearsal\n.*?)(\n##|\z)`) | ||
|
||
match := rehearsalRegex.FindStringSubmatch(input) | ||
if match == nil { | ||
return "", errors.New("no rehearsal section found") | ||
} | ||
|
||
rehearsalContent := match[1] | ||
newRehearsals, err := newRehearsalEntry(rehearsalContent) | ||
if err != nil { | ||
return "", fmt.Errorf("error parsing rehearsal entry: %w", err) | ||
} | ||
if len(newRehearsals) == 0 { | ||
log.Printf("no rehearsals found %s, %s", section, rehearsalContent) | ||
} | ||
replacement := fmt.Sprintf("## Rehearsal\n%s", stringRehearsalEntries(dir, section, newRehearsals)) | ||
return rehearsalRegex.ReplaceAllString(input, replacement), nil | ||
} |
This file contains 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,82 @@ | ||
package problems | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type rehearsalEntry struct { | ||
Name string | ||
TestFileName string | ||
SolutionFileName string | ||
} | ||
|
||
func (r *rehearsalEntry) String() string { | ||
return fmt.Sprintf("### %s\n", r.Name) | ||
} | ||
|
||
/* | ||
newRehearsalEntry parses the rehearsal section of the README.md file that looks like: | ||
## Rehearsal | ||
* [Some Name 1](./problem_test1.go), [Solution](./solution1.go) | ||
* [Some Name 2](./problem_test2.go), [Solution](./solution2.go). | ||
*/ | ||
func newRehearsalEntry(input string) ([]rehearsalEntry, error) { | ||
lines := strings.Split(input, "\n") | ||
entries := []rehearsalEntry{} | ||
re := regexp.MustCompile(`\* \[([^\]]+)\]\(\.\/([^\)]+)\), \[Solution\]\(\.\/([^\)]+)\)`) | ||
|
||
for _, line := range lines { | ||
line = strings.TrimSpace(line) | ||
if line == "" || strings.HasPrefix(line, "##") { | ||
continue // Skip empty lines and heading lines | ||
} | ||
matches := re.FindStringSubmatch(line) | ||
if len(matches) != 4 { | ||
return nil, fmt.Errorf("invalid line format: %s, %d", line, len(matches)) | ||
} | ||
|
||
entry := rehearsalEntry{ | ||
Name: matches[1], | ||
TestFileName: matches[2], | ||
SolutionFileName: matches[3], | ||
} | ||
entries = append(entries, entry) | ||
} | ||
|
||
return entries, nil | ||
} | ||
|
||
func stringRehearsalEntries(dir, section string, entries []rehearsalEntry) string { | ||
output := "" | ||
for _, entry := range entries { | ||
output += fmt.Sprintf("\n### %s\n", entry.Name) | ||
|
||
testFileContent, err := os.ReadFile(filepath.Join(dir, section, entry.TestFileName)) | ||
if err != nil { | ||
output += fmt.Sprintf("Error reading test file: %s\n", err) | ||
continue | ||
} | ||
output += "```GO\n" + string(testFileContent) + "\n```\n" | ||
} | ||
|
||
output += "\n## Rehearsal Solutions\n" | ||
|
||
for _, entry := range entries { | ||
output += fmt.Sprintf("\n### %s\n", entry.Name) | ||
|
||
testFileContent, err := os.ReadFile(filepath.Join(dir, section, entry.SolutionFileName)) | ||
if err != nil { | ||
output += fmt.Sprintf("Error reading test file: %s\n", err) | ||
continue | ||
} | ||
output += "```GO\n" + string(testFileContent) + "\n```\n" | ||
} | ||
|
||
return output | ||
} |
This file contains 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,28 @@ | ||
package problems | ||
|
||
import "testing" | ||
|
||
func TestNewRehearsalEntry(t *testing.T) { | ||
input := `## Rehearsal | ||
* [Bubble Sort](./bubble_sort_test.go), [Solution](./bubble_sort.go) | ||
* [Reverse Array In-place](./reverse_inplace_test.go), [Solution](./reverse_inplace.go) | ||
* [Add Two Numbers](./add_two_numbers_test.go), [Solution](./add_two_numbers.go) | ||
* [Find Duplicate in Array](./find_duplicate_in_array_test.go), [Solution](./find_duplicate_in_array.go) | ||
* [Zero Sum Triplets](./zero_sum_triplets_test.go), [Solution](./zero_sum_triplets.go) | ||
* [Product of All Other Elements](./product_of_all_other_elements_test.go), [Solution](./product_of_all_other_elements.go)` | ||
|
||
entries, err := newRehearsalEntry(input) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
if len(entries) != 6 { | ||
t.Fatalf("expected 5 entries, got %d", len(entries)) | ||
} | ||
|
||
strOutput := stringRehearsalEntries(entries) | ||
if strOutput == "" { | ||
t.Fatal("expected non-empty string, got empty string") | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
.github/cmd/cmd/sections.go → .github/cmd/pkg/problems/sections.go
This file contains 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 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 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 |
---|---|---|
|
@@ -3,3 +3,4 @@ profile.cov | |
.pre-commit-config.yaml | ||
.vscode | ||
.idea | ||
bin |
This file contains 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,27 @@ | ||
all: build | ||
|
||
build: | ||
mkdir -p ./bin | ||
go run ./.github/cmd/main.go export-md > ./bin/go-dsa.md | ||
|
||
test: | ||
go test --race -v -coverprofile=profile.cov ./... | ||
|
||
clean: | ||
go clean | ||
rm -f ./bin/* | ||
|
||
fmt: | ||
go fmt ./... | ||
gofmt -w -s -r 'interface{} -> any' . | ||
|
||
deps: | ||
go mod tidy | ||
go mod download | ||
|
||
vet: | ||
go vet ./... | ||
|
||
ci: fmt vet test build clean | ||
|
||
.PHONY: all build run test bench clean fmt deps update |
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
module github.com/spring1843/go-dsa | ||
|
||
go 1.22 | ||
|
||
require ( | ||
github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
github.com/spf13/cobra v1.8.1 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
) |
This file contains 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,10 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= | ||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= | ||
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains 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