Skip to content
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

Removing leading NL, compatible with Python textwrap.dedent("""\. #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions dedent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
)

var (
leadingNewline = regexp.MustCompile("^[\n]")
whitespaceOnly = regexp.MustCompile("(?m)^[ \t]+$")
leadingWhitespace = regexp.MustCompile("(?m)(^[ \t]*)(?:[^ \t\n])")
)
Expand All @@ -18,6 +19,7 @@ var (
func Dedent(text string) string {
var margin string

text = leadingNewline.ReplaceAllString(text, "")
text = whitespaceOnly.ReplaceAllString(text, "")
indents := leadingWhitespace.FindAllStringSubmatch(text, -1)

Expand Down
5 changes: 3 additions & 2 deletions dedent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ func TestDedentUneven(t *testing.T) {
while 1:
return foo
`,
expect: `
def foo():
expect: `def foo():
while 1:
return foo
`,
Expand Down Expand Up @@ -153,10 +152,12 @@ func ExampleDedent() {
consectetur adipiscing elit.
Curabitur justo tellus, facilisis nec efficitur dictum,
fermentum vitae ligula. Sed eu convallis sapien.`
fmt.Println("-------------")
fmt.Println(Dedent(s))
fmt.Println("-------------")
fmt.Println(s)
// Output:
// -------------
// Lorem ipsum dolor sit amet,
// consectetur adipiscing elit.
// Curabitur justo tellus, facilisis nec efficitur dictum,
Expand Down