hugolib: Extract replaceDivider logic
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 14 Aug 2017 06:42:30 +0000 (08:42 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 14 Aug 2017 07:42:55 +0000 (09:42 +0200)
To its own function and add a test and a benchmark for it.

hugolib/page.go
hugolib/page_test.go

index 0120d6ef02e52a17c08aaf937e837ee4fbd81fb5..b5d13d4db2cfeeebfff4938307e8a6c3789c684f 100644 (file)
@@ -484,26 +484,38 @@ var (
        internalSummaryDivider = []byte("HUGOMORE42")
 )
 
+// replaceDivider replaces the <!--more--> with an internal value and returns
+// whether the contentis truncated or not.
+// Note: The content slice will be modified if needed.
+func replaceDivider(content, from, to []byte) ([]byte, bool) {
+       sections := bytes.Split(content, from)
+
+       // If the raw content has nothing but whitespace after the summary
+       // marker then the page shouldn't be marked as truncated.  This check
+       // is simplest against the raw content because different markup engines
+       // (rst and asciidoc in particular) add div and p elements after the
+       // summary marker.
+       truncated := (len(sections) == 2 &&
+               len(bytes.Trim(sections[1], " \n\r")) > 0)
+
+       return bytes.Join(sections, to), truncated
+
+}
+
 // We have to replace the <!--more--> with something that survives all the
 // rendering engines.
-// TODO(bep) inline replace
 func (p *Page) replaceDivider(content []byte) []byte {
        summaryDivider := helpers.SummaryDivider
        // TODO(bep) handle better.
        if p.Ext() == "org" || p.Markup == "org" {
                summaryDivider = []byte("# more")
        }
-       sections := bytes.Split(content, summaryDivider)
 
-       // If the raw content has nothing but whitespace after the summary
-       // marker then the page shouldn't be marked as truncated.  This check
-       // is simplest against the raw content because different markup engines
-       // (rst and asciidoc in particular) add div and p elements after the
-       // summary marker.
-       p.Truncated = (len(sections) == 2 &&
-               len(bytes.Trim(sections[1], " \n\r")) > 0)
+       replaced, truncated := replaceDivider(content, summaryDivider, internalSummaryDivider)
+
+       p.Truncated = truncated
 
-       return bytes.Join(sections, internalSummaryDivider)
+       return replaced
 }
 
 // Returns the page as summary and main if a user defined split is provided.
index 5c5c06b07a5f19fddace5cdbd847c8ea899dc035..cc2ab9662ca0847758eea584be4de8826ce2cb02 100644 (file)
@@ -1096,6 +1096,60 @@ func TestSliceToLower(t *testing.T) {
        }
 }
 
+func TestReplaceDivider(t *testing.T) {
+       t.Parallel()
+
+       tests := []struct {
+               content           string
+               from              string
+               to                string
+               expectedContent   string
+               expectedTruncated bool
+       }{
+               {"none", "a", "b", "none", false},
+               {"summary divider content", "divider", "HUGO", "summary HUGO content", true},
+               {"summary\n\ndivider", "divider", "HUGO", "summary\n\nHUGO", false},
+               {"summary\n\ndivider\n\r", "divider", "HUGO", "summary\n\nHUGO\n\r", false},
+       }
+
+       for i, test := range tests {
+               replaced, truncated := replaceDivider([]byte(test.content), []byte(test.from), []byte(test.to))
+
+               if truncated != test.expectedTruncated {
+                       t.Fatalf("[%d] Expected truncated to be %t, was %t", i, test.expectedTruncated, truncated)
+               }
+
+               if string(replaced) != test.expectedContent {
+                       t.Fatalf("[%d] Expected content to be %q, was %q", i, test.expectedContent, replaced)
+               }
+       }
+}
+
+func BenchmarkReplaceDivider(b *testing.B) {
+       divider := "HUGO_DIVIDER"
+       from, to := []byte(divider), []byte("HUGO_REPLACED")
+
+       withDivider := make([][]byte, b.N)
+       noDivider := make([][]byte, b.N)
+
+       for i := 0; i < b.N; i++ {
+               withDivider[i] = []byte(strings.Repeat("Summary ", 5) + "\n" + divider + "\n" + strings.Repeat("Word ", 300))
+               noDivider[i] = []byte(strings.Repeat("Word ", 300))
+       }
+
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               _, t1 := replaceDivider(withDivider[i], from, to)
+               _, t2 := replaceDivider(noDivider[i], from, to)
+               if !t1 {
+                       b.Fatal("Should be truncated")
+               }
+               if t2 {
+                       b.Fatal("Should not be truncated")
+               }
+       }
+}
+
 func TestPagePaths(t *testing.T) {
        t.Parallel()