From 527e5bd1b29c1087d4b5375beac519d0b74294eb Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 10 Jun 2022 15:45:28 +0200 Subject: Fix copy/paste of empty lines (#19798) * Fix copy/paste of empty newlines again Fixes: https://github.com/go-gitea/gitea/issues/19331 Regressed by: https://github.com/go-gitea/gitea/pull/18270 Needed to do another newline addition to the Chroma output HTML to get copy/paste work again. The previous replacement conditions are probably obsolete, but as I'm not 100% sure, I opted to keep them. Specifically, the Chroma HTML change mentioned in https://github.com/go-gitea/gitea/pull/18270#issuecomment-1013350246 broke our previous newline replacement for such empty lines. Also included are a few changes to make the test more pleasant to work with. * run go mod tidy * add util.Dedent * copy in the code Co-authored-by: techknowlogick Co-authored-by: Lunny Xiao Co-authored-by: Lauris BH --- modules/util/util.go | 33 +++++++++++++++++++++++++++++++++ modules/util/util_test.go | 7 +++++++ 2 files changed, 40 insertions(+) (limited to 'modules/util') diff --git a/modules/util/util.go b/modules/util/util.go index 351a345473..1017117874 100644 --- a/modules/util/util.go +++ b/modules/util/util.go @@ -9,6 +9,7 @@ import ( "crypto/rand" "errors" "math/big" + "regexp" "strconv" "strings" @@ -191,3 +192,35 @@ var titleCaser = cases.Title(language.English) func ToTitleCase(s string) string { return titleCaser.String(s) } + +var ( + whitespaceOnly = regexp.MustCompile("(?m)^[ \t]+$") + leadingWhitespace = regexp.MustCompile("(?m)(^[ \t]*)(?:[^ \t\n])") +) + +// Dedent removes common indentation of a multi-line string along with whitespace around it +// Based on https://github.com/lithammer/dedent +func Dedent(s string) string { + var margin string + + s = whitespaceOnly.ReplaceAllString(s, "") + indents := leadingWhitespace.FindAllStringSubmatch(s, -1) + + for i, indent := range indents { + if i == 0 { + margin = indent[1] + } else if strings.HasPrefix(indent[1], margin) { + continue + } else if strings.HasPrefix(margin, indent[1]) { + margin = indent[1] + } else { + margin = "" + break + } + } + + if margin != "" { + s = regexp.MustCompile("(?m)^"+margin).ReplaceAllString(s, "") + } + return strings.TrimSpace(s) +} diff --git a/modules/util/util_test.go b/modules/util/util_test.go index ca5bd87eae..91b0ef9455 100644 --- a/modules/util/util_test.go +++ b/modules/util/util_test.go @@ -225,3 +225,10 @@ func TestToTitleCase(t *testing.T) { assert.Equal(t, ToTitleCase(`foo bar baz`), `Foo Bar Baz`) assert.Equal(t, ToTitleCase(`FOO BAR BAZ`), `Foo Bar Baz`) } + +func TestDedent(t *testing.T) { + assert.Equal(t, Dedent(` + foo + bar + `), "foo\n\tbar") +} -- cgit v1.2.3