summaryrefslogtreecommitdiffstats
path: root/common/text
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-05-28 13:18:50 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-05-30 11:32:55 +0200
commitd2cfaede5be420c7d8b701d97b98bc61b87e46d5 (patch)
treedb0926a42c3b53df10b87671d1733391b831b517 /common/text
parent322d19a81fedbf423a047bdf286499d2e25d14be (diff)
Improve shortcode indentation handling
* Record the leading whitespace (tabs, spaces) before the shortcode when parsing the page. * Apply that indentation to the rendered result of shortcodes without inner content (where the user will apply indentation). Fixes #9946
Diffstat (limited to 'common/text')
-rw-r--r--common/text/transform.go14
-rw-r--r--common/text/transform_test.go18
2 files changed, 32 insertions, 0 deletions
diff --git a/common/text/transform.go b/common/text/transform.go
index b324b54c1..2b05b9b4f 100644
--- a/common/text/transform.go
+++ b/common/text/transform.go
@@ -61,3 +61,17 @@ func Puts(s string) string {
}
return s + "\n"
}
+
+// VisitLinesAfter calls the given function for each line, including newlines, in the given string.
+func VisitLinesAfter(s string, fn func(line string)) {
+ high := strings.Index(s, "\n")
+ for high != -1 {
+ fn(s[:high+1])
+ s = s[high+1:]
+ high = strings.Index(s, "\n")
+ }
+
+ if s != "" {
+ fn(s)
+ }
+}
diff --git a/common/text/transform_test.go b/common/text/transform_test.go
index 992dd524c..10738aee7 100644
--- a/common/text/transform_test.go
+++ b/common/text/transform_test.go
@@ -41,3 +41,21 @@ func TestPuts(t *testing.T) {
c.Assert(Puts("\nA\n"), qt.Equals, "\nA\n")
c.Assert(Puts(""), qt.Equals, "")
}
+
+func TestVisitLinesAfter(t *testing.T) {
+ const lines = `line 1
+line 2
+
+line 3`
+
+ var collected []string
+
+ VisitLinesAfter(lines, func(s string) {
+ collected = append(collected, s)
+ })
+
+ c := qt.New(t)
+
+ c.Assert(collected, qt.DeepEquals, []string{"line 1\n", "line 2\n", "\n", "line 3"})
+
+}