Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
taylormonacelli committed Jul 24, 2024
1 parent 2be5a1a commit 2c7da79
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 48 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ fmt: .timestamps/.fmt.time
@mkdir -p .timestamps
@touch $@

.PHONY: test # run all tests
test:
go test -count=1 ./core

.PHONY: lint # lint
lint: .timestamps/.lint.time

Expand Down
43 changes: 35 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,56 @@

Purpose:

littlewill is a tool for cleaning up Markdown links by removing extra spaces within the link text.

## example usage
## Example Usage

```bash
littlewill input.md > output.md
```

## Install littlewill

On macOS/Linux:
```bash
brew install gkwa/homebrew-tools/littlewill
```

## install littlewill
On Windows:
```powershell
TBD
```

## Running Tests

on macos/linux:
```bash
To run the tests for littlewill, follow these steps:

brew install gkwa/homebrew-tools/littlewill
1. Ensure you have Go installed on your system.
2. Navigate to the project root directory.
3. Run the following command:

```bash
go test ./...
```

This will run all tests in the project, including the ones in the `core` package.

on windows:
To run tests with verbose output:

```powershell
```bash
go test -v ./...
```

TBD
To run a specific test:

```bash
go test -v ./core -run TestCleanupMarkdownLinks
```

Replace `TestCleanupMarkdownLinks` with the name of the specific test you want to run.

## Development

The main functionality is implemented in the `core/cleanup.go` file. Tests are located in `core/cleanup_test.go`.

To contribute or modify the code, make sure to run the tests after any changes to ensure everything is working as expected.
27 changes: 27 additions & 0 deletions core/cleanup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package core

import (
"fmt"
"io"
"regexp"
)

var markdownLinkRegex = regexp.MustCompile(`\[\s*(\S.*?)\s*\]\(`)

func CleanupMarkdownLinks(r io.Reader, w io.Writer) error {
buf, err := io.ReadAll(r)
if err != nil {
return fmt.Errorf("CleanupMarkdownLinks: failed to read input: %w", err)
}

cleaned := markdownLinkRegex.ReplaceAllFunc(buf, func(match []byte) []byte {
return markdownLinkRegex.ReplaceAll(match, []byte("[$1]("))
})

_, err = w.Write(cleaned)
if err != nil {
return fmt.Errorf("CleanupMarkdownLinks: failed to write output: %w", err)
}

return nil
}
131 changes: 131 additions & 0 deletions core/cleanup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package core

import (
"bytes"
"errors"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestCleanupMarkdownLinks(t *testing.T) {
testCases := []struct {
name string
input string
expected string
}{
{
name: "No links",
input: `
This is some text.
This more text.
Here is a link https://google.com.
`,
expected: `
This is some text.
This more text.
Here is a link https://google.com.
`,
},
{
name: "Single link with extra space",
input: `
[ Google](https://google.com)
This is some text
[
Managing 100s of Kubernetes Clusters using Cluster API](https://techblog.citystoragesystems.com/p/managing-100s-of-kubernetes-clusters "Managing 100s of Kubernetes Clusters using Cluster API")
This more text.
Here is a link https://google.com.
`,
expected: `
[Google](https://google.com)
This is some text
[Managing 100s of Kubernetes Clusters using Cluster API](https://techblog.citystoragesystems.com/p/managing-100s-of-kubernetes-clusters "Managing 100s of Kubernetes Clusters using Cluster API")
This more text.
Here is a link https://google.com.
`,
},
{
name: "Multiple links with extra space",
input: `
[Google](https://google.com)
This is some text
[
Managing 100s of Kubernetes Clusters using Cluster API](https://techblog.citystoragesystems.com/p/managing-100s-of-kubernetes-clusters "Managing 100s of Kubernetes Clusters using Cluster API")
This more text.
[
Another link with space](https://example.com)
Here is a link https://google.com.
`,
expected: `
[Google](https://google.com)
This is some text
[Managing 100s of Kubernetes Clusters using Cluster API](https://techblog.citystoragesystems.com/p/managing-100s-of-kubernetes-clusters "Managing 100s of Kubernetes Clusters using Cluster API")
This more text.
[Another link with space](https://example.com)
Here is a link https://google.com.
`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
input := strings.NewReader(tc.input)
var output bytes.Buffer
err := CleanupMarkdownLinks(input, &output)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
result := output.String()
if diff := cmp.Diff(tc.expected, result); diff != "" {
t.Errorf("Unexpected result (-want +got):\n%s", diff)
}
})
}
}

func TestCleanupMarkdownLinksErrors(t *testing.T) {
t.Run("Read error", func(t *testing.T) {
errReader := &errorReader{err: errors.New("read error")}
var output bytes.Buffer
err := CleanupMarkdownLinks(errReader, &output)
if err == nil {
t.Fatal("Expected an error, got nil")
}
if !strings.Contains(err.Error(), "CleanupMarkdownLinks: failed to read input: read error") {
t.Errorf("Unexpected error message: %v", err)
}
})

t.Run("Write error", func(t *testing.T) {
input := strings.NewReader("some input")
errWriter := &errorWriter{err: errors.New("write error")}
err := CleanupMarkdownLinks(input, errWriter)
if err == nil {
t.Fatal("Expected an error, got nil")
}
if !strings.Contains(err.Error(), "CleanupMarkdownLinks: failed to write output: write error") {
t.Errorf("Unexpected error message: %v", err)
}
})
}

type errorReader struct {
err error
}

func (e *errorReader) Read(p []byte) (n int, err error) {
return 0, e.err
}

type errorWriter struct {
err error
}

func (e *errorWriter) Write(p []byte) (n int, err error) {
return 0, e.err
}
23 changes: 11 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-logr/logr v1.4.2
github.com/go-logr/zapr v1.3.0
github.com/go-logr/zerologr v1.2.3
github.com/google/go-cmp v0.6.0
github.com/google/go-containerregistry v0.20.1
github.com/magefile/mage v1.15.0
github.com/rs/zerolog v1.33.0
Expand All @@ -19,10 +20,10 @@ require (

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/cloudflare/circl v1.3.9 // indirect
github.com/cyphar/filepath-securejoin v0.3.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
Expand All @@ -38,24 +39,22 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/skeema/knownhosts v1.3.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.18.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit 2c7da79

Please sign in to comment.