2015-12-10 17:19:38 -05:00
// Copyright 2015 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-08-05 10:53:58 -04:00
package hugolib
import (
2017-01-02 05:44:17 -05:00
"bytes"
2015-07-26 09:28:56 -04:00
"fmt"
2014-01-29 17:50:31 -05:00
"html/template"
2017-01-02 05:44:17 -05:00
"os"
2014-11-06 11:52:01 -05:00
"path/filepath"
2015-01-24 06:44:35 -05:00
"reflect"
2015-07-26 09:28:56 -04:00
"sort"
2014-01-29 17:50:31 -05:00
"strings"
"testing"
"time"
2014-10-16 20:20:09 -04:00
2015-01-24 06:44:35 -05:00
"github.com/spf13/cast"
2017-01-10 04:55:03 -05:00
"github.com/spf13/hugo/deps"
2014-10-16 20:20:09 -04:00
"github.com/spf13/hugo/helpers"
2015-05-27 21:19:59 -04:00
"github.com/stretchr/testify/assert"
2016-08-01 17:04:44 -04:00
"github.com/stretchr/testify/require"
2013-08-05 10:53:58 -04:00
)
2016-03-24 09:42:03 -04:00
var emptyPage = ""
2013-08-05 10:53:58 -04:00
2013-09-18 13:17:43 -04:00
const (
2016-03-24 09:42:03 -04:00
simplePage = "---\ntitle: Simple\n---\nSimple Page\n"
invalidFrontMatterMissing = "This is a test"
renderNoFrontmatter = "<!doctype><html><head></head><body>This is a test</body></html>"
contentWithCommentedFrontmatter = "<!--\n+++\ntitle = \"Network configuration\"\ndescription = \"Docker networking\"\nkeywords = [\"network\"]\n[menu.main]\nparent= \"smn_administrate\"\n+++\n-->\n\n# Network configuration\n\n##\nSummary"
contentWithCommentedTextFrontmatter = "<!--[metaData]>\n+++\ntitle = \"Network configuration\"\ndescription = \"Docker networking\"\nkeywords = [\"network\"]\n[menu.main]\nparent= \"smn_administrate\"\n+++\n<![end-metadata]-->\n\n# Network configuration\n\n##\nSummary"
contentWithCommentedLongFrontmatter = "<!--[metaData123456789012345678901234567890]>\n+++\ntitle = \"Network configuration\"\ndescription = \"Docker networking\"\nkeywords = [\"network\"]\n[menu.main]\nparent= \"smn_administrate\"\n+++\n<![end-metadata]-->\n\n# Network configuration\n\n##\nSummary"
contentWithCommentedLong2Frontmatter = "<!--[metaData]>\n+++\ntitle = \"Network configuration\"\ndescription = \"Docker networking\"\nkeywords = [\"network\"]\n[menu.main]\nparent= \"smn_administrate\"\n+++\n<![end-metadata123456789012345678901234567890]-->\n\n# Network configuration\n\n##\nSummary"
invalidFrontmatterShortDelim = `
2013-08-05 10:53:58 -04:00
--
title : Short delim start
-- -
Short Delim
`
2016-03-24 09:42:03 -04:00
invalidFrontmatterShortDelimEnding = `
2013-08-05 10:53:58 -04:00
-- -
title : Short delim ending
--
Short Delim
`
2016-03-24 09:42:03 -04:00
invalidFrontmatterLadingWs = `
2013-08-14 08:57:14 -04:00
2013-08-05 10:53:58 -04:00
-- -
title : Leading WS
-- -
Leading
`
2016-03-24 09:42:03 -04:00
simplePageJSON = `
2013-08-07 02:04:49 -04:00
{
"title" : "spf13-vim 3.0 release and new website" ,
"description" : "spf13-vim is a cross platform distribution of vim plugins and resources for Vim." ,
2014-09-09 16:58:02 -04:00
"tags" : [ ".vimrc" , "plugins" , "spf13-vim" , "VIm" ] ,
2013-08-07 02:04:49 -04:00
"date" : "2012-04-06" ,
"categories" : [
"Development" ,
"VIM"
] ,
2014-10-02 18:25:52 -04:00
"slug" : "-spf13-vim-3-0-release-and-new-website-"
2013-08-07 02:04:49 -04:00
}
2013-10-01 22:45:24 -04:00
Content of the file goes Here
`
2016-03-24 09:42:03 -04:00
simplePageJSONLoose = `
2013-10-01 22:45:24 -04:00
{
"title" : "spf13-vim 3.0 release and new website"
"description" : "spf13-vim is a cross platform distribution of vim plugins and resources for Vim."
2014-09-09 16:58:02 -04:00
"tags" : [ ".vimrc" , "plugins" , "spf13-vim" , "VIm" ]
2013-10-01 22:45:24 -04:00
"date" : "2012-04-06"
"categories" : [
"Development"
"VIM"
] ,
"slug" : "spf13-vim-3-0-release-and-new-website"
}
2013-08-14 08:57:14 -04:00
Content of the file goes Here
2013-08-07 02:04:49 -04:00
`
2016-03-24 09:42:03 -04:00
simplePageRFC3339Date = "---\ntitle: RFC3339 Date\ndate: \"2013-05-17T16:59:30Z\"\n---\nrfc3339 content"
simplePageJSONMultiple = `
2013-08-07 02:04:49 -04:00
{
"title" : "foobar" ,
"customData" : { "foo" : "bar" } ,
"date" : "2012-08-06"
}
Some text
`
2016-03-24 09:42:03 -04:00
simplePageNoLayout = ` -- -
2013-08-14 18:22:57 -04:00
title : simple_no_layout
-- -
No Layout called out `
2016-03-24 09:42:03 -04:00
simplePageLayoutFoobar = ` -- -
2013-08-14 18:22:57 -04:00
title : simple layout foobar
layout : foobar
-- -
Layout foobar `
2016-03-24 09:42:03 -04:00
simplePageTypeFoobar = ` -- -
2013-08-14 18:22:57 -04:00
type : foobar
-- -
type foobar `
2016-03-24 09:42:03 -04:00
simplePageTypeLayout = ` -- -
2013-08-14 18:22:57 -04:00
type : barfoo
layout : buzfoo
-- -
type and layout set `
2016-03-24 09:42:03 -04:00
simplePageWithSummaryDelimiter = ` -- -
2013-08-21 05:37:14 -04:00
title : Simple
-- -
2013-09-18 12:15:46 -04:00
Summary Next Line
2013-08-21 05:37:14 -04:00
< ! -- more -- >
2013-10-04 12:28:28 -04:00
Some more text
2016-08-01 17:04:44 -04:00
`
simplePageWithSummaryDelimiterAndMarkdownThatCrossesBorder = ` -- -
title : Simple
-- -
The [ best static site generator ] [ hugo ] . [ ^ 1 ]
< ! -- more -- >
[ hugo ] : http : //gohugo.io/
[ ^ 1 ] : Many people say so .
2013-10-04 12:28:28 -04:00
`
2016-03-24 09:42:03 -04:00
simplePageWithShortcodeInSummary = ` -- -
2013-10-04 12:28:28 -04:00
title : Simple
-- -
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
Summary Next Line . { { < figure src = "/not/real" > } } .
2013-10-04 12:28:28 -04:00
More text here .
2013-08-21 05:37:14 -04:00
Some more text
2014-05-29 23:42:12 -04:00
`
2016-03-24 09:42:03 -04:00
simplePageWithEmbeddedScript = ` -- -
2014-05-29 23:42:12 -04:00
title : Simple
-- -
< script type = ' text / javascript ' > alert ( ' the script tags are still there , right ? ' ) ; < / script >
2013-08-21 05:37:14 -04:00
`
2016-03-24 09:42:03 -04:00
simplePageWithSummaryDelimiterSameLine = ` -- -
2013-09-01 00:07:22 -04:00
title : Simple
-- -
2013-09-18 12:15:46 -04:00
Summary Same Line < ! -- more -- >
2013-09-01 00:07:22 -04:00
Some more text
2017-01-26 14:58:25 -05:00
`
simplePageWithSummaryDelimiterOnlySummary = ` -- -
title : Simple
-- -
Summary text
< ! -- more -- >
2015-07-12 05:05:37 -04:00
`
2016-03-24 09:42:03 -04:00
simplePageWithAllCJKRunes = ` -- -
2015-07-12 05:05:37 -04:00
title : Simple
-- -
€ € € € €
2015-09-03 06:22:20 -04:00
你好
도형이
カテゴリー
2015-07-12 05:05:37 -04:00
2013-10-15 09:15:52 -04:00
`
2016-03-24 09:42:03 -04:00
simplePageWithMainEnglishWithCJKRunes = ` -- -
2015-09-03 06:22:20 -04:00
title : Simple
-- -
In Chinese , 好 means good . In Chinese , 好 means good .
In Chinese , 好 means good . In Chinese , 好 means good .
In Chinese , 好 means good . In Chinese , 好 means good .
In Chinese , 好 means good . In Chinese , 好 means good .
In Chinese , 好 means good . In Chinese , 好 means good .
In Chinese , 好 means good . In Chinese , 好 means good .
In Chinese , 好 means good . In Chinese , 好 means good .
More then 70 words .
`
2016-03-24 09:42:03 -04:00
simplePageWithMainEnglishWithCJKRunesSummary = "In Chinese, 好 means good. In Chinese, 好 means good. " +
2015-09-03 06:22:20 -04:00
"In Chinese, 好 means good. In Chinese, 好 means good. " +
"In Chinese, 好 means good. In Chinese, 好 means good. " +
"In Chinese, 好 means good. In Chinese, 好 means good. " +
"In Chinese, 好 means good. In Chinese, 好 means good. " +
"In Chinese, 好 means good. In Chinese, 好 means good. " +
"In Chinese, 好 means good. In Chinese, 好 means good."
2016-03-24 09:42:03 -04:00
simplePageWithIsCJKLanguageFalse = ` -- -
2015-09-03 06:22:20 -04:00
title : Simple
isCJKLanguage : false
-- -
In Chinese , 好的啊 means good . In Chinese , 好的呀 means good .
In Chinese , 好的啊 means good . In Chinese , 好的呀 means good .
In Chinese , 好的啊 means good . In Chinese , 好的呀 means good .
In Chinese , 好的啊 means good . In Chinese , 好的呀 means good .
In Chinese , 好的啊 means good . In Chinese , 好的呀 means good .
In Chinese , 好的啊 means good . In Chinese , 好的呀 means good .
In Chinese , 好的啊 means good . In Chinese , 好的呀呀 means good enough .
More then 70 words .
`
2016-03-24 09:42:03 -04:00
simplePageWithIsCJKLanguageFalseSummary = "In Chinese, 好的啊 means good. In Chinese, 好的呀 means good. " +
2015-09-03 06:22:20 -04:00
"In Chinese, 好的啊 means good. In Chinese, 好的呀 means good. " +
"In Chinese, 好的啊 means good. In Chinese, 好的呀 means good. " +
"In Chinese, 好的啊 means good. In Chinese, 好的呀 means good. " +
"In Chinese, 好的啊 means good. In Chinese, 好的呀 means good. " +
"In Chinese, 好的啊 means good. In Chinese, 好的呀 means good. " +
"In Chinese, 好的啊 means good. In Chinese, 好的呀呀 means good enough."
2016-03-24 09:42:03 -04:00
simplePageWithLongContent = ` -- -
2013-10-15 09:15:52 -04:00
title : Simple
-- -
2014-01-28 23:09:24 -05:00
Lorem ipsum dolor sit amet , consectetur adipisicing elit , sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua . Ut enim ad minim veniam , quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat .
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur . Excepteur sint occaecat cupidatat non proident , sunt in
culpa qui officia deserunt mollit anim id est laborum . Lorem ipsum dolor sit
amet , consectetur adipisicing elit , sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua . Ut enim ad minim veniam , quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat . Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur . Excepteur sint occaecat cupidatat non proident , sunt in culpa qui
officia deserunt mollit anim id est laborum . Lorem ipsum dolor sit amet ,
consectetur adipisicing elit , sed do eiusmod tempor incididunt ut labore et
dolore magna aliqua . Ut enim ad minim veniam , quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat . Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur .
Excepteur sint occaecat cupidatat non proident , sunt in culpa qui officia
deserunt mollit anim id est laborum . Lorem ipsum dolor sit amet , consectetur
adipisicing elit , sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua . Ut enim ad minim veniam , quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat . Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur . Excepteur sint
occaecat cupidatat non proident , sunt in culpa qui officia deserunt mollit anim
id est laborum . Lorem ipsum dolor sit amet , consectetur adipisicing elit , sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua . Ut enim ad minim
veniam , quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat . Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur . Excepteur sint occaecat cupidatat non
proident , sunt in culpa qui officia deserunt mollit anim id est laborum . Lorem
ipsum dolor sit amet , consectetur adipisicing elit , sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua . Ut enim ad minim veniam , quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat .
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur . Excepteur sint occaecat cupidatat non proident , sunt in
culpa qui officia deserunt mollit anim id est laborum . Lorem ipsum dolor sit
amet , consectetur adipisicing elit , sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua . Ut enim ad minim veniam , quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat . Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur . Excepteur sint occaecat cupidatat non proident , sunt in culpa qui
officia deserunt mollit anim id est laborum . `
2016-03-24 09:42:03 -04:00
pageWithToC = ` -- -
2014-01-28 23:09:24 -05:00
title : TOC
-- -
For some moments the old man did not reply . He stood with bowed head , buried in deep thought . But at last he spoke .
# # AA
I have no idea , of course , how long it took me to reach the limit of the plain ,
but at last I entered the foothills , following a pretty little canyon upward
toward the mountains . Beside me frolicked a laughing brooklet , hurrying upon
its noisy way down to the silent sea . In its quieter pools I discovered many
small fish , of four - or five - pound weight I should imagine . In appearance ,
except as to size and color , they were not unlike the whale of our own seas . As
I watched them playing about I discovered , not only that they suckled their
young , but that at intervals they rose to the surface to breathe as well as to
feed upon certain grasses and a strange , scarlet lichen which grew upon the
rocks just above the water line .
# # # AAA
I remember I felt an extraordinary persuasion that I was being played with ,
that presently , when I was upon the very verge of safety , this mysterious
death -- as swift as the passage of light -- would leap after me from the pit about
the cylinder and strike me down . # # BB
# # # BBB
"You're a great Granser," he cried delightedly , "always making believe them little marks mean something."
2015-01-25 06:08:02 -05:00
`
2016-03-24 09:42:03 -04:00
simplePageWithAdditionalExtension = ` ++ +
2015-01-25 06:08:02 -05:00
[ blackfriday ]
extensions = [ "hardLineBreak" ]
++ +
first line .
second line .
fourth line .
2013-09-01 00:07:22 -04:00
`
2015-05-09 06:12:30 -04:00
2016-03-24 09:42:03 -04:00
simplePageWithURL = ` -- -
2015-05-09 06:12:30 -04:00
title : Simple
url : simple / url /
-- -
Simple Page With URL `
2016-03-24 09:42:03 -04:00
simplePageWithSlug = ` -- -
2015-05-09 06:12:30 -04:00
title : Simple
slug : simple - slug
-- -
Simple Page With Slug `
2016-03-24 09:42:03 -04:00
simplePageWithDate = ` -- -
2015-05-09 06:12:30 -04:00
title : Simple
date : ' 2013 - 10 - 15 T06 : 16 : 13 '
-- -
Simple Page With Date `
2016-03-24 09:42:03 -04:00
UTF8Page = ` -- -
2015-05-09 06:12:30 -04:00
title : ラーメン
-- -
UTF8 Page `
2016-03-24 09:42:03 -04:00
UTF8PageWithURL = ` -- -
2015-05-09 06:12:30 -04:00
title : ラーメン
url : ラーメン / url /
-- -
UTF8 Page With URL `
2016-03-24 09:42:03 -04:00
UTF8PageWithSlug = ` -- -
2015-05-09 06:12:30 -04:00
title : ラーメン
slug : ラーメン - slug
-- -
UTF8 Page With Slug `
2016-03-24 09:42:03 -04:00
UTF8PageWithDate = ` -- -
2015-05-09 06:12:30 -04:00
title : ラーメン
date : ' 2013 - 10 - 15 T06 : 16 : 13 '
-- -
UTF8 Page With Date `
2013-09-18 13:17:43 -04:00
)
2013-09-01 00:07:22 -04:00
2016-03-24 09:42:03 -04:00
var pageWithVariousFrontmatterTypes = ` ++ +
2013-10-24 17:31:04 -04:00
a_string = "bar"
an_integer = 1
a_float = 1.3
2014-01-28 23:24:59 -05:00
a_bool = false
2013-10-24 17:31:04 -04:00
a_date = 1979 - 05 - 27 T07 : 32 : 00 Z
2015-01-24 06:44:35 -05:00
[ a_table ]
a_key = "a_value"
2013-10-24 17:31:04 -04:00
++ +
Front Matter with various frontmatter types `
2016-03-24 09:42:03 -04:00
var pageWithCalendarYAMLFrontmatter = ` -- -
2015-07-26 09:28:56 -04:00
type : calendar
weeks :
-
start : "Jan 5"
days :
- activity : class
room : EN1000
- activity : lab
- activity : class
- activity : lab
- activity : class
-
start : "Jan 12"
days :
- activity : class
- activity : lab
- activity : class
- activity : lab
- activity : exam
-- -
Hi .
`
2016-03-24 09:42:03 -04:00
var pageWithCalendarJSONFrontmatter = ` {
2015-07-26 09:28:56 -04:00
"type" : "calendar" ,
"weeks" : [
{
"start" : "Jan 5" ,
"days" : [
{ "activity" : "class" , "room" : "EN1000" } ,
{ "activity" : "lab" } ,
{ "activity" : "class" } ,
{ "activity" : "lab" } ,
{ "activity" : "class" }
]
} ,
{
"start" : "Jan 12" ,
"days" : [
{ "activity" : "class" } ,
{ "activity" : "lab" } ,
{ "activity" : "class" } ,
{ "activity" : "lab" } ,
{ "activity" : "exam" }
]
}
]
}
Hi .
`
2016-03-24 09:42:03 -04:00
var pageWithCalendarTOMLFrontmatter = ` ++ +
2015-07-26 09:28:56 -04:00
type = "calendar"
[ [ weeks ] ]
start = "Jan 5"
[ [ weeks . days ] ]
activity = "class"
room = "EN1000"
[ [ weeks . days ] ]
activity = "lab"
[ [ weeks . days ] ]
activity = "class"
[ [ weeks . days ] ]
activity = "lab"
[ [ weeks . days ] ]
activity = "class"
[ [ weeks ] ]
start = "Jan 12"
[ [ weeks . days ] ]
activity = "class"
[ [ weeks . days ] ]
activity = "lab"
[ [ weeks . days ] ]
activity = "class"
[ [ weeks . days ] ]
activity = "lab"
[ [ weeks . days ] ]
activity = "exam"
++ +
Hi .
`
2013-08-05 10:53:58 -04:00
func checkError ( t * testing . T , err error , expected string ) {
2014-01-29 17:50:31 -05:00
if err == nil {
t . Fatalf ( "err is nil. Expected: %s" , expected )
}
if err . Error ( ) != expected {
t . Errorf ( "err.Error() returned: '%s'. Expected: '%s'" , err . Error ( ) , expected )
}
2013-08-05 10:53:58 -04:00
}
func TestDegenerateEmptyPageZeroLengthName ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
s := newTestSite ( t )
_ , err := s . NewPage ( "" )
2014-01-29 17:50:31 -05:00
if err == nil {
t . Fatalf ( "A zero length page name must return an error" )
}
2013-08-05 10:53:58 -04:00
2014-01-29 17:50:31 -05:00
checkError ( t , err , "Zero length page name" )
2013-08-05 10:53:58 -04:00
}
func TestDegenerateEmptyPage ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
s := newTestSite ( t )
_ , err := s . NewPageFrom ( strings . NewReader ( emptyPage ) , "test" )
2014-01-29 17:50:31 -05:00
if err != nil {
t . Fatalf ( "Empty files should not trigger an error. Should be able to touch a file while watching without erroring out." )
}
2013-08-05 10:53:58 -04:00
}
func checkPageTitle ( t * testing . T , page * Page , title string ) {
2014-01-29 17:50:31 -05:00
if page . Title != title {
t . Fatalf ( "Page title is: %s. Expected %s" , page . Title , title )
}
2013-08-05 10:53:58 -04:00
}
2016-08-01 17:04:44 -04:00
func checkPageContent ( t * testing . T , page * Page , content string , msg ... interface { } ) {
a := normalizeContent ( content )
b := normalizeContent ( string ( page . Content ) )
if a != b {
t . Fatalf ( "Page content is:\n%q\nExpected:\n%q (%q)" , b , a , msg )
2014-01-29 17:50:31 -05:00
}
2014-01-28 23:09:24 -05:00
}
2016-08-01 17:04:44 -04:00
func normalizeContent ( c string ) string {
2016-08-17 07:41:48 -04:00
norm := c
norm = strings . Replace ( norm , "\n" , " " , - 1 )
2016-08-01 17:04:44 -04:00
norm = strings . Replace ( norm , " " , " " , - 1 )
norm = strings . Replace ( norm , " " , " " , - 1 )
norm = strings . Replace ( norm , " " , " " , - 1 )
2016-08-17 07:41:48 -04:00
norm = strings . Replace ( norm , "p> " , "p>" , - 1 )
norm = strings . Replace ( norm , "> <" , "> <" , - 1 )
2016-08-01 17:04:44 -04:00
return strings . TrimSpace ( norm )
}
2014-01-28 23:09:24 -05:00
func checkPageTOC ( t * testing . T , page * Page , toc string ) {
2014-01-29 17:50:31 -05:00
if page . TableOfContents != template . HTML ( toc ) {
t . Fatalf ( "Page TableOfContents is: %q.\nExpected %q" , page . TableOfContents , toc )
}
2013-08-05 10:53:58 -04:00
}
2016-08-01 17:04:44 -04:00
func checkPageSummary ( t * testing . T , page * Page , summary string , msg ... interface { } ) {
a := normalizeContent ( string ( page . Summary ) )
b := normalizeContent ( summary )
if a != b {
t . Fatalf ( "Page summary is:\n%q.\nExpected\n%q (%q)" , a , b , msg )
2014-01-29 17:50:31 -05:00
}
2013-08-21 05:37:14 -04:00
}
2013-08-05 10:53:58 -04:00
func checkPageType ( t * testing . T , page * Page , pageType string ) {
2014-01-29 17:50:31 -05:00
if page . Type ( ) != pageType {
t . Fatalf ( "Page type is: %s. Expected: %s" , page . Type ( ) , pageType )
}
2013-08-05 10:53:58 -04:00
}
2013-10-07 00:57:45 -04:00
func checkPageLayout ( t * testing . T , page * Page , layout ... string ) {
2015-11-02 11:24:50 -05:00
if ! listEqual ( page . layouts ( ) , layout ) {
2016-10-28 03:34:47 -04:00
t . Fatalf ( "Page layout is:\n%s. Expected:\n%s" , page . layouts ( ) , layout )
2014-01-29 17:50:31 -05:00
}
2013-08-05 10:53:58 -04:00
}
2013-09-03 17:51:06 -04:00
func checkPageDate ( t * testing . T , page * Page , time time . Time ) {
2014-01-29 17:50:31 -05:00
if page . Date != time {
t . Fatalf ( "Page date is: %s. Expected: %s" , page . Date , time )
}
2013-09-03 17:51:06 -04:00
}
2013-11-10 15:04:51 -05:00
func checkTruncation ( t * testing . T , page * Page , shouldBe bool , msg string ) {
2014-01-29 17:50:31 -05:00
if page . Summary == "" {
t . Fatal ( "page has no summary, can not check truncation" )
}
if page . Truncated != shouldBe {
if shouldBe {
t . Fatalf ( "page wasn't truncated: %s" , msg )
} else {
t . Fatalf ( "page was truncated: %s" , msg )
}
}
2013-11-10 15:04:51 -05:00
}
2016-08-01 17:04:44 -04:00
func normalizeExpected ( ext , str string ) string {
str = normalizeContent ( str )
switch ext {
default :
return str
case "html" :
return strings . Trim ( helpers . StripHTML ( str ) , " " )
case "ad" :
paragraphs := strings . Split ( str , "</p>" )
expected := ""
for _ , para := range paragraphs {
if para == "" {
continue
}
expected += fmt . Sprintf ( "<div class=\"paragraph\">\n%s</p></div>\n" , para )
}
return expected
case "rst" :
return fmt . Sprintf ( "<div class=\"document\">\n\n\n%s</div>" , str )
}
}
2016-10-18 02:43:44 -04:00
func testAllMarkdownEnginesForPages ( t * testing . T ,
2016-10-28 03:34:47 -04:00
assertFunc func ( t * testing . T , ext string , pages Pages ) , settings map [ string ] interface { } , pageSources ... string ) {
2016-08-01 17:04:44 -04:00
engines := [ ] struct {
ext string
shouldExecute func ( ) bool
} {
{ "md" , func ( ) bool { return true } } ,
{ "mmark" , func ( ) bool { return true } } ,
2016-10-18 02:43:44 -04:00
{ "ad" , func ( ) bool { return helpers . HasAsciidoc ( ) } } ,
2016-08-01 17:04:44 -04:00
// TODO(bep) figure a way to include this without too much work.{"html", func() bool { return true }},
{ "rst" , func ( ) bool { return helpers . HasRst ( ) } } ,
}
for _ , e := range engines {
if ! e . shouldExecute ( ) {
continue
}
2017-02-04 22:20:06 -05:00
cfg , fs := newTestCfg ( )
2017-01-10 04:55:03 -05:00
2016-10-28 03:34:47 -04:00
if settings != nil {
for k , v := range settings {
2017-02-04 22:20:06 -05:00
cfg . Set ( k , v )
2016-10-28 03:34:47 -04:00
}
}
contentDir := "content"
2017-02-04 22:20:06 -05:00
if s := cfg . GetString ( "contentDir" ) ; s != "" {
2016-10-28 03:34:47 -04:00
contentDir = s
}
var fileSourcePairs [ ] string
2016-10-18 02:43:44 -04:00
for i , source := range pageSources {
2016-10-28 03:34:47 -04:00
fileSourcePairs = append ( fileSourcePairs , fmt . Sprintf ( "p%d.%s" , i , e . ext ) , source )
2016-10-18 02:43:44 -04:00
}
2016-08-01 17:04:44 -04:00
2016-10-28 03:34:47 -04:00
for i := 0 ; i < len ( fileSourcePairs ) ; i += 2 {
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( contentDir , fileSourcePairs [ i ] ) , fileSourcePairs [ i + 1 ] )
2016-10-28 03:34:47 -04:00
}
2017-02-04 22:20:06 -05:00
s := buildSingleSite ( t , deps . DepsCfg { Fs : fs , Cfg : cfg } , BuildCfg { SkipRender : true } )
2016-08-01 17:04:44 -04:00
2016-11-13 08:27:10 -05:00
require . Len ( t , s . RegularPages , len ( pageSources ) )
2016-08-01 17:04:44 -04:00
2016-11-13 08:27:10 -05:00
assertFunc ( t , e . ext , s . RegularPages )
2016-08-01 17:04:44 -04:00
}
}
2013-08-05 10:53:58 -04:00
func TestCreateNewPage ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-10-18 02:43:44 -04:00
assertFunc := func ( t * testing . T , ext string , pages Pages ) {
p := pages [ 0 ]
2016-10-28 03:34:47 -04:00
2016-10-28 04:35:19 -04:00
// issue #2290: Path is relative to the content dir and will continue to be so.
require . Equal ( t , filepath . FromSlash ( fmt . Sprintf ( "p0.%s" , ext ) ) , p . Path ( ) )
2016-10-30 12:59:24 -04:00
assert . False ( t , p . IsHome ( ) )
2016-08-01 17:04:44 -04:00
checkPageTitle ( t , p , "Simple" )
checkPageContent ( t , p , normalizeExpected ( ext , "<p>Simple Page</p>\n" ) )
checkPageSummary ( t , p , "Simple Page" )
checkPageType ( t , p , "page" )
checkPageLayout ( t , p , "page/single.html" , "_default/single.html" , "theme/page/single.html" , "theme/_default/single.html" )
checkTruncation ( t , p , false , "simple short page" )
2014-01-29 17:50:31 -05:00
}
2015-05-27 21:19:59 -04:00
2016-10-28 04:35:19 -04:00
settings := map [ string ] interface { } {
"contentDir" : "mycontent" ,
}
testAllMarkdownEnginesForPages ( t , assertFunc , settings , simplePage )
2016-08-01 17:04:44 -04:00
}
func TestSplitSummaryAndContent ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-08-01 17:04:44 -04:00
for i , this := range [ ] struct {
2017-01-26 14:58:25 -05:00
markup string
content string
expectedSummary string
expectedContent string
2016-08-01 17:04:44 -04:00
} {
{ "markdown" , ` < p > Summary Same LineHUGOMORE42 < / p >
2017-01-26 14:58:25 -05:00
< p > Some more text < / p > ` , "<p>Summary Same Line</p>" , "<p>Summary Same Line</p>\n\n<p>Some more text</p>" } ,
2016-08-01 17:04:44 -04:00
{ "asciidoc" , ` <div class="paragraph"><p>sn</p></div><div class="paragraph"><p>HUGOMORE42Some more text</p></div> ` ,
"<div class=\"paragraph\"><p>sn</p></div>" ,
2017-01-26 14:58:25 -05:00
"<div class=\"paragraph\"><p>sn</p></div><div class=\"paragraph\"><p>Some more text</p></div>" } ,
2016-08-01 17:04:44 -04:00
{ "rst" ,
"<div class=\"document\"><p>Summary Next Line</p><p>HUGOMORE42Some more text</p></div>" ,
"<div class=\"document\"><p>Summary Next Line</p></div>" ,
2017-01-26 14:58:25 -05:00
"<div class=\"document\"><p>Summary Next Line</p><p>Some more text</p></div>" } ,
{ "markdown" , "<p>a</p><p>b</p><p>HUGOMORE42c</p>" , "<p>a</p><p>b</p>" , "<p>a</p><p>b</p><p>c</p>" } ,
{ "markdown" , "<p>a</p><p>b</p><p>cHUGOMORE42</p>" , "<p>a</p><p>b</p><p>c</p>" , "<p>a</p><p>b</p><p>c</p>" } ,
{ "markdown" , "<p>a</p><p>bHUGOMORE42</p><p>c</p>" , "<p>a</p><p>b</p>" , "<p>a</p><p>b</p><p>c</p>" } ,
{ "markdown" , "<p>aHUGOMORE42</p><p>b</p><p>c</p>" , "<p>a</p>" , "<p>a</p><p>b</p><p>c</p>" } ,
{ "markdown" , " HUGOMORE42 " , "" , "" } ,
{ "markdown" , "HUGOMORE42" , "" , "" } ,
{ "markdown" , "<p>HUGOMORE42" , "<p>" , "<p>" } ,
{ "markdown" , "HUGOMORE42<p>" , "" , "<p>" } ,
{ "markdown" , "\n\n<p>HUGOMORE42</p>\n" , "<p></p>" , "<p></p>" } ,
2016-10-18 02:43:44 -04:00
// Issue #2586
// Note: Hugo will not split mid-sentence but will look for the closest
// paragraph end marker. This may be a change from Hugo 0.16, but it makes sense.
{ "markdown" , ` <p>this is an example HUGOMORE42of the issue.</p> ` ,
"<p>this is an example of the issue.</p>" ,
2017-01-26 14:58:25 -05:00
"<p>this is an example of the issue.</p>" } ,
2016-10-18 02:43:44 -04:00
// Issue: #2538
{ "markdown" , fmt . Sprintf ( ` < p class = "lead" > % s < / p > HUGOMORE42 < p > % s < / p >
` ,
strings . Repeat ( "A" , 10 ) , strings . Repeat ( "B" , 31 ) ) ,
fmt . Sprintf ( ` <p class="lead">%s</p> ` , strings . Repeat ( "A" , 10 ) ) ,
fmt . Sprintf ( ` <p class="lead">%s</p><p>%s</p> ` , strings . Repeat ( "A" , 10 ) , strings . Repeat ( "B" , 31 ) ) ,
} ,
2016-08-01 17:04:44 -04:00
} {
2016-10-18 02:43:44 -04:00
sc , err := splitUserDefinedSummaryAndContent ( this . markup , [ ] byte ( this . content ) )
2016-08-01 17:04:44 -04:00
2016-10-18 02:43:44 -04:00
require . NoError ( t , err )
2016-08-01 17:04:44 -04:00
require . NotNil ( t , sc , fmt . Sprintf ( "[%d] Nil %s" , i , this . markup ) )
require . Equal ( t , this . expectedSummary , string ( sc . summary ) , fmt . Sprintf ( "[%d] Summary markup %s" , i , this . markup ) )
require . Equal ( t , this . expectedContent , string ( sc . content ) , fmt . Sprintf ( "[%d] Content markup %s" , i , this . markup ) )
}
2013-08-21 05:37:14 -04:00
}
func TestPageWithDelimiter ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-10-18 02:43:44 -04:00
assertFunc := func ( t * testing . T , ext string , pages Pages ) {
p := pages [ 0 ]
2016-08-01 17:04:44 -04:00
checkPageTitle ( t , p , "Simple" )
checkPageContent ( t , p , normalizeExpected ( ext , "<p>Summary Next Line</p>\n\n<p>Some more text</p>\n" ) , ext )
checkPageSummary ( t , p , normalizeExpected ( ext , "<p>Summary Next Line</p>" ) , ext )
checkPageType ( t , p , "page" )
checkPageLayout ( t , p , "page/single.html" , "_default/single.html" , "theme/page/single.html" , "theme/_default/single.html" )
checkTruncation ( t , p , true , "page with summary delimiter" )
}
2016-10-28 03:34:47 -04:00
testAllMarkdownEnginesForPages ( t , assertFunc , nil , simplePageWithSummaryDelimiter )
2016-08-01 17:04:44 -04:00
}
// Issue #1076
func TestPageWithDelimiterForMarkdownThatCrossesBorder ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
cfg , fs := newTestCfg ( )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "simple.md" ) , simplePageWithSummaryDelimiterAndMarkdownThatCrossesBorder )
2017-02-04 22:20:06 -05:00
s := buildSingleSite ( t , deps . DepsCfg { Fs : fs , Cfg : cfg } , BuildCfg { SkipRender : true } )
2016-08-01 17:04:44 -04:00
2016-11-13 08:27:10 -05:00
require . Len ( t , s . RegularPages , 1 )
2016-08-01 17:04:44 -04:00
2016-11-13 08:27:10 -05:00
p := s . RegularPages [ 0 ]
2016-08-01 17:04:44 -04:00
if p . Summary != template . HTML ( "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup class=\"footnote-ref\" id=\"fnref:1\"><a rel=\"footnote\" href=\"#fn:1\">1</a></sup>\n</p>" ) {
t . Fatalf ( "Got summary:\n%q" , p . Summary )
}
if p . Content != template . HTML ( "<p>The <a href=\"http://gohugo.io/\">best static site generator</a>.<sup class=\"footnote-ref\" id=\"fnref:1\"><a rel=\"footnote\" href=\"#fn:1\">1</a></sup>\n</p>\n<div class=\"footnotes\">\n\n<hr />\n\n<ol>\n<li id=\"fn:1\">Many people say so.\n <a class=\"footnote-return\" href=\"#fnref:1\"><sup>[return]</sup></a></li>\n</ol>\n</div>" ) {
t . Fatalf ( "Got content:\n%q" , p . Content )
2014-01-29 17:50:31 -05:00
}
2013-10-04 12:28:28 -04:00
}
2013-09-01 00:07:22 -04:00
2016-12-01 04:21:49 -05:00
// Issue #2601
func TestPageRawContent ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
cfg , fs := newTestCfg ( )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "raw.md" ) , ` -- -
2016-12-01 04:21:49 -05:00
title : Raw
-- -
* * Raw * * ` )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "layouts" , "_default" , "single.html" ) , ` {{ .RawContent }} ` )
2016-12-01 04:21:49 -05:00
2017-02-04 22:20:06 -05:00
s := buildSingleSite ( t , deps . DepsCfg { Fs : fs , Cfg : cfg } , BuildCfg { SkipRender : true } )
2016-12-01 04:21:49 -05:00
require . Len ( t , s . RegularPages , 1 )
p := s . RegularPages [ 0 ]
require . Contains ( t , p . RawContent ( ) , "**Raw**" )
}
2013-10-04 12:28:28 -04:00
func TestPageWithShortCodeInSummary ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-10-18 02:43:44 -04:00
assertFunc := func ( t * testing . T , ext string , pages Pages ) {
p := pages [ 0 ]
2016-08-01 17:04:44 -04:00
checkPageTitle ( t , p , "Simple" )
2016-08-17 07:41:48 -04:00
checkPageContent ( t , p , normalizeExpected ( ext , "<p>Summary Next Line. \n<figure >\n \n <img src=\"/not/real\" />\n \n \n</figure>\n.\nMore text here.</p>\n\n<p>Some more text</p>\n" ) )
checkPageSummary ( t , p , "Summary Next Line. . More text here. Some more text" )
2016-08-01 17:04:44 -04:00
checkPageType ( t , p , "page" )
checkPageLayout ( t , p , "page/single.html" , "_default/single.html" , "theme/page/single.html" , "theme/_default/single.html" )
2014-01-29 17:50:31 -05:00
}
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
2016-10-28 03:34:47 -04:00
testAllMarkdownEnginesForPages ( t , assertFunc , nil , simplePageWithShortcodeInSummary )
2013-09-01 00:07:22 -04:00
}
2014-05-29 23:42:12 -04:00
func TestPageWithEmbeddedScriptTag ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-10-18 02:43:44 -04:00
assertFunc := func ( t * testing . T , ext string , pages Pages ) {
p := pages [ 0 ]
2016-08-01 17:04:44 -04:00
if ext == "ad" || ext == "rst" {
// TOD(bep)
return
}
checkPageContent ( t , p , "<script type='text/javascript'>alert('the script tags are still there, right?');</script>\n" , ext )
2014-05-29 23:42:12 -04:00
}
2016-08-01 17:04:44 -04:00
2016-10-28 03:34:47 -04:00
testAllMarkdownEnginesForPages ( t , assertFunc , nil , simplePageWithEmbeddedScript )
2014-05-29 23:42:12 -04:00
}
2015-01-25 06:08:02 -05:00
func TestPageWithAdditionalExtension ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
cfg , fs := newTestCfg ( )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "simple.md" ) , simplePageWithAdditionalExtension )
2017-02-04 22:20:06 -05:00
s := buildSingleSite ( t , deps . DepsCfg { Fs : fs , Cfg : cfg } , BuildCfg { SkipRender : true } )
2016-08-01 17:04:44 -04:00
2016-11-13 08:27:10 -05:00
require . Len ( t , s . RegularPages , 1 )
2016-08-01 17:04:44 -04:00
2016-11-13 08:27:10 -05:00
p := s . RegularPages [ 0 ]
2016-08-01 17:04:44 -04:00
2015-01-25 06:08:02 -05:00
checkPageContent ( t , p , "<p>first line.<br />\nsecond line.</p>\n\n<p>fourth line.</p>\n" )
}
2014-01-28 23:09:24 -05:00
func TestTableOfContents ( t * testing . T ) {
2016-08-01 17:04:44 -04:00
2017-02-04 22:20:06 -05:00
cfg , fs := newTestCfg ( )
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "tocpage.md" ) , pageWithToC )
2017-02-04 22:20:06 -05:00
s := buildSingleSite ( t , deps . DepsCfg { Fs : fs , Cfg : cfg } , BuildCfg { SkipRender : true } )
2016-08-01 17:04:44 -04:00
2016-11-13 08:27:10 -05:00
require . Len ( t , s . RegularPages , 1 )
2016-08-01 17:04:44 -04:00
2016-11-13 08:27:10 -05:00
p := s . RegularPages [ 0 ]
2016-08-01 17:04:44 -04:00
2016-04-12 13:18:02 -04:00
checkPageContent ( t , p , "\n\n<p>For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.</p>\n\n<h2 id=\"aa\">AA</h2>\n\n<p>I have no idea, of course, how long it took me to reach the limit of the plain,\nbut at last I entered the foothills, following a pretty little canyon upward\ntoward the mountains. Beside me frolicked a laughing brooklet, hurrying upon\nits noisy way down to the silent sea. In its quieter pools I discovered many\nsmall fish, of four-or five-pound weight I should imagine. In appearance,\nexcept as to size and color, they were not unlike the whale of our own seas. As\nI watched them playing about I discovered, not only that they suckled their\nyoung, but that at intervals they rose to the surface to breathe as well as to\nfeed upon certain grasses and a strange, scarlet lichen which grew upon the\nrocks just above the water line.</p>\n\n<h3 id=\"aaa\">AAA</h3>\n\n<p>I remember I felt an extraordinary persuasion that I was being played with,\nthat presently, when I was upon the very verge of safety, this mysterious\ndeath–as swift as the passage of light–would leap after me from the pit about\nthe cylinder and strike me down. ## BB</p>\n\n<h3 id=\"bbb\">BBB</h3>\n\n<p>“You’re a great Granser,” he cried delightedly, “always making believe them little marks mean something.”</p>\n" )
checkPageTOC ( t , p , "<nav id=\"TableOfContents\">\n<ul>\n<li>\n<ul>\n<li><a href=\"#aa\">AA</a>\n<ul>\n<li><a href=\"#aaa\">AAA</a></li>\n<li><a href=\"#bbb\">BBB</a></li>\n</ul></li>\n</ul></li>\n</ul>\n</nav>" )
2014-01-28 23:09:24 -05:00
}
2013-09-01 00:07:22 -04:00
func TestPageWithMoreTag ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-10-18 02:43:44 -04:00
assertFunc := func ( t * testing . T , ext string , pages Pages ) {
p := pages [ 0 ]
2016-08-01 17:04:44 -04:00
checkPageTitle ( t , p , "Simple" )
checkPageContent ( t , p , normalizeExpected ( ext , "<p>Summary Same Line</p>\n\n<p>Some more text</p>\n" ) )
checkPageSummary ( t , p , normalizeExpected ( ext , "<p>Summary Same Line</p>" ) )
checkPageType ( t , p , "page" )
checkPageLayout ( t , p , "page/single.html" , "_default/single.html" , "theme/page/single.html" , "theme/_default/single.html" )
2016-10-18 02:43:44 -04:00
2014-01-29 17:50:31 -05:00
}
2016-08-01 17:04:44 -04:00
2016-10-28 03:34:47 -04:00
testAllMarkdownEnginesForPages ( t , assertFunc , nil , simplePageWithSummaryDelimiterSameLine )
2013-08-05 10:53:58 -04:00
}
2017-01-26 14:58:25 -05:00
func TestPageWithMoreTagOnlySummary ( t * testing . T ) {
assertFunc := func ( t * testing . T , ext string , pages Pages ) {
p := pages [ 0 ]
checkTruncation ( t , p , false , "page with summary delimiter at end" )
}
testAllMarkdownEnginesForPages ( t , assertFunc , nil , simplePageWithSummaryDelimiterOnlySummary )
}
2013-09-03 17:51:06 -04:00
func TestPageWithDate ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
cfg , fs := newTestCfg ( )
2016-08-01 17:04:44 -04:00
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "simple.md" ) , simplePageRFC3339Date )
2017-02-04 22:20:06 -05:00
s := buildSingleSite ( t , deps . DepsCfg { Fs : fs , Cfg : cfg } , BuildCfg { SkipRender : true } )
2016-08-01 17:04:44 -04:00
2016-11-13 08:27:10 -05:00
require . Len ( t , s . RegularPages , 1 )
2016-08-01 17:04:44 -04:00
2016-11-13 08:27:10 -05:00
p := s . RegularPages [ 0 ]
2016-08-01 17:04:44 -04:00
d , _ := time . Parse ( time . RFC3339 , "2013-05-17T16:59:30Z" )
2014-01-29 17:50:31 -05:00
checkPageDate ( t , p , d )
2013-09-03 17:51:06 -04:00
}
2015-09-03 06:22:20 -04:00
func TestWordCountWithAllCJKRunesWithoutHasCJKLanguage ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-10-18 02:43:44 -04:00
assertFunc := func ( t * testing . T , ext string , pages Pages ) {
p := pages [ 0 ]
2016-08-17 07:41:48 -04:00
if p . WordCount ( ) != 8 {
t . Fatalf ( "[%s] incorrect word count for content '%s'. expected %v, got %v" , ext , p . plain , 8 , p . WordCount ( ) )
2016-08-01 17:04:44 -04:00
}
2015-09-03 06:22:20 -04:00
}
2016-10-28 03:34:47 -04:00
testAllMarkdownEnginesForPages ( t , assertFunc , nil , simplePageWithAllCJKRunes )
2015-09-03 06:22:20 -04:00
}
func TestWordCountWithAllCJKRunesHasCJKLanguage ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-10-28 03:34:47 -04:00
settings := map [ string ] interface { } { "hasCJKLanguage" : true }
2015-09-03 06:22:20 -04:00
2016-10-18 02:43:44 -04:00
assertFunc := func ( t * testing . T , ext string , pages Pages ) {
p := pages [ 0 ]
2016-08-17 07:41:48 -04:00
if p . WordCount ( ) != 15 {
t . Fatalf ( "[%s] incorrect word count for content '%s'. expected %v, got %v" , ext , p . plain , 15 , p . WordCount ( ) )
2016-08-01 17:04:44 -04:00
}
2015-09-03 06:22:20 -04:00
}
2016-10-28 03:34:47 -04:00
testAllMarkdownEnginesForPages ( t , assertFunc , settings , simplePageWithAllCJKRunes )
2015-09-03 06:22:20 -04:00
}
func TestWordCountWithMainEnglishWithCJKRunes ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-10-28 03:34:47 -04:00
settings := map [ string ] interface { } { "hasCJKLanguage" : true }
2015-09-03 06:22:20 -04:00
2016-10-18 02:43:44 -04:00
assertFunc := func ( t * testing . T , ext string , pages Pages ) {
p := pages [ 0 ]
2016-08-17 07:41:48 -04:00
if p . WordCount ( ) != 74 {
t . Fatalf ( "[%s] incorrect word count for content '%s'. expected %v, got %v" , ext , p . plain , 74 , p . WordCount ( ) )
2016-08-01 17:04:44 -04:00
}
2015-07-12 05:05:37 -04:00
2016-08-01 17:04:44 -04:00
if p . Summary != simplePageWithMainEnglishWithCJKRunesSummary {
t . Fatalf ( "[%s] incorrect Summary for content '%s'. expected %v, got %v" , ext , p . plain ,
simplePageWithMainEnglishWithCJKRunesSummary , p . Summary )
}
2015-09-03 06:22:20 -04:00
}
2016-08-01 17:04:44 -04:00
2016-10-28 03:34:47 -04:00
testAllMarkdownEnginesForPages ( t , assertFunc , settings , simplePageWithMainEnglishWithCJKRunes )
2015-09-03 06:22:20 -04:00
}
func TestWordCountWithIsCJKLanguageFalse ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
settings := map [ string ] interface { } {
"hasCJKLanguage" : true ,
}
2015-09-03 06:22:20 -04:00
2016-10-18 02:43:44 -04:00
assertFunc := func ( t * testing . T , ext string , pages Pages ) {
p := pages [ 0 ]
2016-08-17 07:41:48 -04:00
if p . WordCount ( ) != 75 {
t . Fatalf ( "[%s] incorrect word count for content '%s'. expected %v, got %v" , ext , p . plain , 74 , p . WordCount ( ) )
2016-08-01 17:04:44 -04:00
}
2015-09-03 06:22:20 -04:00
2016-08-01 17:04:44 -04:00
if p . Summary != simplePageWithIsCJKLanguageFalseSummary {
t . Fatalf ( "[%s] incorrect Summary for content '%s'. expected %v, got %v" , ext , p . plain ,
simplePageWithIsCJKLanguageFalseSummary , p . Summary )
}
2015-07-12 05:05:37 -04:00
}
2016-08-01 17:04:44 -04:00
2017-02-04 22:20:06 -05:00
testAllMarkdownEnginesForPages ( t , assertFunc , settings , simplePageWithIsCJKLanguageFalse )
2016-08-01 17:04:44 -04:00
2015-07-12 05:05:37 -04:00
}
2013-10-15 09:15:52 -04:00
func TestWordCount ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-10-18 02:43:44 -04:00
assertFunc := func ( t * testing . T , ext string , pages Pages ) {
p := pages [ 0 ]
2016-08-17 07:41:48 -04:00
if p . WordCount ( ) != 483 {
t . Fatalf ( "[%s] incorrect word count. expected %v, got %v" , ext , 483 , p . WordCount ( ) )
2016-08-01 17:04:44 -04:00
}
2014-01-29 17:50:31 -05:00
2016-08-17 07:41:48 -04:00
if p . FuzzyWordCount ( ) != 500 {
t . Fatalf ( "[%s] incorrect word count. expected %v, got %v" , ext , 500 , p . WordCount ( ) )
2016-08-01 17:04:44 -04:00
}
2016-08-17 07:41:48 -04:00
if p . ReadingTime ( ) != 3 {
t . Fatalf ( "[%s] incorrect min read. expected %v, got %v" , ext , 3 , p . ReadingTime ( ) )
2016-08-01 17:04:44 -04:00
}
2014-01-29 17:50:31 -05:00
2016-08-01 17:04:44 -04:00
checkTruncation ( t , p , true , "long page" )
2014-01-29 17:50:31 -05:00
}
2016-10-28 03:34:47 -04:00
testAllMarkdownEnginesForPages ( t , assertFunc , nil , simplePageWithLongContent )
2013-10-15 09:15:52 -04:00
}
2013-08-07 02:04:49 -04:00
func TestCreatePage ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2014-01-29 17:50:31 -05:00
var tests = [ ] struct {
r string
} {
2016-03-24 09:42:03 -04:00
{ simplePageJSON } ,
{ simplePageJSONLoose } ,
{ simplePageJSONMultiple } ,
2014-01-29 17:50:31 -05:00
//{strings.NewReader(SIMPLE_PAGE_JSON_COMPACT)},
}
for _ , test := range tests {
2017-02-04 22:20:06 -05:00
s := newTestSite ( t )
p , _ := s . NewPage ( "page" )
2015-04-03 15:41:12 -04:00
if _ , err := p . ReadFrom ( strings . NewReader ( test . r ) ) ; err != nil {
2014-01-29 17:50:31 -05:00
t . Errorf ( "Unable to parse page: %s" , err )
}
}
2013-08-07 02:04:49 -04:00
}
2013-08-05 10:53:58 -04:00
func TestDegenerateInvalidFrontMatterShortDelim ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2014-01-29 17:50:31 -05:00
var tests = [ ] struct {
r string
err string
} {
2016-03-24 09:42:03 -04:00
{ invalidFrontmatterShortDelimEnding , "unable to read frontmatter at filepos 45: EOF" } ,
2014-01-29 17:50:31 -05:00
}
for _ , test := range tests {
2017-02-04 22:20:06 -05:00
s := newTestSite ( t )
p , _ := s . NewPage ( "invalid/front/matter/short/delim" )
2015-04-03 15:41:12 -04:00
_ , err := p . ReadFrom ( strings . NewReader ( test . r ) )
2014-01-29 17:50:31 -05:00
checkError ( t , err , test . err )
}
2013-08-05 10:53:58 -04:00
}
2013-09-18 13:17:43 -04:00
func TestShouldRenderContent ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2014-01-29 17:50:31 -05:00
var tests = [ ] struct {
text string
render bool
} {
2016-03-24 09:42:03 -04:00
{ invalidFrontMatterMissing , true } ,
2014-01-29 17:50:31 -05:00
// TODO how to deal with malformed frontmatter. In this case it'll be rendered as markdown.
2016-03-24 09:42:03 -04:00
{ invalidFrontmatterShortDelim , true } ,
{ renderNoFrontmatter , false } ,
{ contentWithCommentedFrontmatter , true } ,
{ contentWithCommentedTextFrontmatter , true } ,
{ contentWithCommentedLongFrontmatter , false } ,
{ contentWithCommentedLong2Frontmatter , true } ,
2014-01-29 17:50:31 -05:00
}
for _ , test := range tests {
2017-02-04 22:20:06 -05:00
s := newTestSite ( t )
p , _ := s . NewPage ( "render/front/matter" )
2015-04-03 15:41:12 -04:00
_ , err := p . ReadFrom ( strings . NewReader ( test . text ) )
2014-05-01 14:11:56 -04:00
p = pageMust ( p , err )
2014-01-29 17:50:31 -05:00
if p . IsRenderable ( ) != test . render {
t . Errorf ( "expected p.IsRenderable() == %t, got %t" , test . render , p . IsRenderable ( ) )
}
}
2013-09-18 13:17:43 -04:00
}
2015-07-26 09:28:56 -04:00
// Issue #768
func TestCalendarParamsVariants ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
s := newTestSite ( t )
pageJSON , _ := s . NewPage ( "test/fileJSON.md" )
2016-03-24 09:42:03 -04:00
_ , _ = pageJSON . ReadFrom ( strings . NewReader ( pageWithCalendarJSONFrontmatter ) )
2015-07-26 09:28:56 -04:00
2017-02-04 22:20:06 -05:00
pageYAML , _ := s . NewPage ( "test/fileYAML.md" )
2016-03-24 09:42:03 -04:00
_ , _ = pageYAML . ReadFrom ( strings . NewReader ( pageWithCalendarYAMLFrontmatter ) )
2015-07-26 09:28:56 -04:00
2017-02-04 22:20:06 -05:00
pageTOML , _ := s . NewPage ( "test/fileTOML.md" )
2016-03-24 09:42:03 -04:00
_ , _ = pageTOML . ReadFrom ( strings . NewReader ( pageWithCalendarTOMLFrontmatter ) )
2015-07-26 09:28:56 -04:00
assert . True ( t , compareObjects ( pageJSON . Params , pageYAML . Params ) )
assert . True ( t , compareObjects ( pageJSON . Params , pageTOML . Params ) )
}
2013-10-24 17:31:04 -04:00
func TestDifferentFrontMatterVarTypes ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
s := newTestSite ( t )
page , _ := s . NewPage ( "test/file1.md" )
2016-03-24 09:42:03 -04:00
_ , _ = page . ReadFrom ( strings . NewReader ( pageWithVariousFrontmatterTypes ) )
2014-01-29 17:50:31 -05:00
dateval , _ := time . Parse ( time . RFC3339 , "1979-05-27T07:32:00Z" )
if page . GetParam ( "a_string" ) != "bar" {
t . Errorf ( "frontmatter not handling strings correctly should be %s, got: %s" , "bar" , page . GetParam ( "a_string" ) )
}
if page . GetParam ( "an_integer" ) != 1 {
t . Errorf ( "frontmatter not handling ints correctly should be %s, got: %s" , "1" , page . GetParam ( "an_integer" ) )
}
if page . GetParam ( "a_float" ) != 1.3 {
2014-03-01 00:31:21 -05:00
t . Errorf ( "frontmatter not handling floats correctly should be %f, got: %s" , 1.3 , page . GetParam ( "a_float" ) )
2014-01-29 17:50:31 -05:00
}
if page . GetParam ( "a_bool" ) != false {
2014-03-01 00:31:21 -05:00
t . Errorf ( "frontmatter not handling bools correctly should be %t, got: %s" , false , page . GetParam ( "a_bool" ) )
2014-01-29 17:50:31 -05:00
}
if page . GetParam ( "a_date" ) != dateval {
t . Errorf ( "frontmatter not handling dates correctly should be %s, got: %s" , dateval , page . GetParam ( "a_date" ) )
}
2015-01-24 06:44:35 -05:00
param := page . GetParam ( "a_table" )
if param == nil {
t . Errorf ( "frontmatter not handling tables correctly should be type of %v, got: type of %v" , reflect . TypeOf ( page . Params [ "a_table" ] ) , reflect . TypeOf ( param ) )
}
if cast . ToStringMap ( param ) [ "a_key" ] != "a_value" {
t . Errorf ( "frontmatter not handling values inside a table correctly should be %s, got: %s" , "a_value" , cast . ToStringMap ( page . Params [ "a_table" ] ) [ "a_key" ] )
}
2013-10-24 17:31:04 -04:00
}
2013-08-05 10:53:58 -04:00
func TestDegenerateInvalidFrontMatterLeadingWhitespace ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
s := newTestSite ( t )
p , _ := s . NewPage ( "invalid/front/matter/leading/ws" )
2016-03-24 09:42:03 -04:00
_ , err := p . ReadFrom ( strings . NewReader ( invalidFrontmatterLadingWs ) )
2014-01-29 17:50:31 -05:00
if err != nil {
t . Fatalf ( "Unable to parse front matter given leading whitespace: %s" , err )
}
2013-08-05 10:53:58 -04:00
}
2013-08-14 18:22:57 -04:00
2013-08-30 20:14:36 -04:00
func TestSectionEvaluation ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
s := newTestSite ( t )
page , _ := s . NewPage ( filepath . FromSlash ( "blue/file1.md" ) )
2016-03-24 09:42:03 -04:00
page . ReadFrom ( strings . NewReader ( simplePage ) )
2014-10-16 20:20:09 -04:00
if page . Section ( ) != "blue" {
2014-12-07 13:48:00 -05:00
t . Errorf ( "Section should be %s, got: %s" , "blue" , page . Section ( ) )
2014-01-29 17:50:31 -05:00
}
2013-08-30 20:14:36 -04:00
}
2013-10-07 00:57:45 -04:00
func L ( s ... string ) [ ] string {
2014-01-29 17:50:31 -05:00
return s
2013-10-07 00:57:45 -04:00
}
2013-08-14 18:22:57 -04:00
func TestLayoutOverride ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2014-01-29 17:50:31 -05:00
var (
2016-03-24 09:42:03 -04:00
pathContentTwoDir = filepath . Join ( "content" , "dub" , "sub" , "file1.md" )
pathContentOneDir = filepath . Join ( "content" , "gub" , "file1.md" )
pathContentNoDir = filepath . Join ( "content" , "file1" )
pathOneDirectory = filepath . Join ( "fub" , "file1.md" )
pathNoDirectory = filepath . Join ( "file1.md" )
2014-01-29 17:50:31 -05:00
)
tests := [ ] struct {
content string
path string
expectedLayout [ ] string
} {
2016-03-24 09:42:03 -04:00
{ simplePageNoLayout , pathContentTwoDir , L ( "dub/single.html" , "_default/single.html" ) } ,
{ simplePageNoLayout , pathContentOneDir , L ( "gub/single.html" , "_default/single.html" ) } ,
{ simplePageNoLayout , pathContentNoDir , L ( "page/single.html" , "_default/single.html" ) } ,
{ simplePageNoLayout , pathOneDirectory , L ( "fub/single.html" , "_default/single.html" ) } ,
{ simplePageNoLayout , pathNoDirectory , L ( "page/single.html" , "_default/single.html" ) } ,
{ simplePageLayoutFoobar , pathContentTwoDir , L ( "dub/foobar.html" , "_default/foobar.html" ) } ,
{ simplePageLayoutFoobar , pathContentOneDir , L ( "gub/foobar.html" , "_default/foobar.html" ) } ,
{ simplePageLayoutFoobar , pathOneDirectory , L ( "fub/foobar.html" , "_default/foobar.html" ) } ,
{ simplePageLayoutFoobar , pathNoDirectory , L ( "page/foobar.html" , "_default/foobar.html" ) } ,
{ simplePageTypeFoobar , pathContentTwoDir , L ( "foobar/single.html" , "_default/single.html" ) } ,
{ simplePageTypeFoobar , pathContentOneDir , L ( "foobar/single.html" , "_default/single.html" ) } ,
{ simplePageTypeFoobar , pathContentNoDir , L ( "foobar/single.html" , "_default/single.html" ) } ,
{ simplePageTypeFoobar , pathOneDirectory , L ( "foobar/single.html" , "_default/single.html" ) } ,
{ simplePageTypeFoobar , pathNoDirectory , L ( "foobar/single.html" , "_default/single.html" ) } ,
{ simplePageTypeLayout , pathContentTwoDir , L ( "barfoo/buzfoo.html" , "_default/buzfoo.html" ) } ,
{ simplePageTypeLayout , pathContentOneDir , L ( "barfoo/buzfoo.html" , "_default/buzfoo.html" ) } ,
{ simplePageTypeLayout , pathContentNoDir , L ( "barfoo/buzfoo.html" , "_default/buzfoo.html" ) } ,
{ simplePageTypeLayout , pathOneDirectory , L ( "barfoo/buzfoo.html" , "_default/buzfoo.html" ) } ,
{ simplePageTypeLayout , pathNoDirectory , L ( "barfoo/buzfoo.html" , "_default/buzfoo.html" ) } ,
2014-01-29 17:50:31 -05:00
}
for _ , test := range tests {
2017-02-04 22:20:06 -05:00
s := newTestSite ( t )
p , _ := s . NewPage ( test . path )
2015-04-03 15:41:12 -04:00
_ , err := p . ReadFrom ( strings . NewReader ( test . content ) )
2014-01-29 17:50:31 -05:00
if err != nil {
t . Fatalf ( "Unable to parse content:\n%s\n" , test . content )
}
2014-06-03 17:06:32 -04:00
for _ , y := range test . expectedLayout {
test . expectedLayout = append ( test . expectedLayout , "theme/" + y )
}
2015-11-02 11:24:50 -05:00
if ! listEqual ( p . layouts ( ) , test . expectedLayout ) {
t . Errorf ( "Layout mismatch. Expected: %s, got: %s" , test . expectedLayout , p . layouts ( ) )
2014-01-29 17:50:31 -05:00
}
}
2013-08-14 18:22:57 -04:00
}
2013-10-07 00:57:45 -04:00
2014-09-09 16:58:02 -04:00
func TestSliceToLower ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2014-10-02 18:25:52 -04:00
tests := [ ] struct {
value [ ] string
2014-09-09 16:58:02 -04:00
expected [ ] string
} {
2014-10-02 18:25:52 -04:00
{ [ ] string { "a" , "b" , "c" } , [ ] string { "a" , "b" , "c" } } ,
{ [ ] string { "a" , "B" , "c" } , [ ] string { "a" , "b" , "c" } } ,
{ [ ] string { "A" , "B" , "C" } , [ ] string { "a" , "b" , "c" } } ,
2014-09-09 16:58:02 -04:00
}
for _ , test := range tests {
2014-10-16 20:20:09 -04:00
res := helpers . SliceToLower ( test . value )
2014-09-09 16:58:02 -04:00
for i , val := range res {
if val != test . expected [ i ] {
t . Errorf ( "Case mismatch. Expected %s, got %s" , test . expected [ i ] , res [ i ] )
}
}
}
}
2015-05-31 12:54:50 -04:00
func TestPagePaths ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2015-05-20 02:21:21 -04:00
2017-02-04 22:20:06 -05:00
siteParmalinksSetting := map [ string ] string {
2015-05-09 06:12:30 -04:00
"post" : ":year/:month/:day/:title/" ,
}
tests := [ ] struct {
2015-05-20 02:21:21 -04:00
content string
path string
hasPermalink bool
expected string
2015-05-09 06:12:30 -04:00
} {
2017-02-04 22:20:06 -05:00
{ simplePage , "post/x.md" , false , "post/x.html" } ,
{ simplePageWithURL , "post/x.md" , false , "simple/url/index.html" } ,
{ simplePageWithSlug , "post/x.md" , false , "post/simple-slug.html" } ,
{ simplePageWithDate , "post/x.md" , true , "2013/10/15/simple/index.html" } ,
{ UTF8Page , "post/x.md" , false , "post/x.html" } ,
{ UTF8PageWithURL , "post/x.md" , false , "ラーメン/url/index.html" } ,
{ UTF8PageWithSlug , "post/x.md" , false , "post/ラーメン-slug.html" } ,
{ UTF8PageWithDate , "post/x.md" , true , "2013/10/15/ラーメン/index.html" } ,
2015-05-09 06:12:30 -04:00
}
2017-02-04 22:20:06 -05:00
for i , test := range tests {
cfg , fs := newTestCfg ( )
cfg . Set ( "defaultExtension" , "html" )
2015-05-09 06:12:30 -04:00
if test . hasPermalink {
2017-02-04 22:20:06 -05:00
cfg . Set ( "permalinks" , siteParmalinksSetting )
2015-05-09 06:12:30 -04:00
}
2017-02-04 22:20:06 -05:00
writeSource ( t , fs , filepath . Join ( "content" , filepath . FromSlash ( test . path ) ) , test . content )
s := buildSingleSite ( t , deps . DepsCfg { Fs : fs , Cfg : cfg } , BuildCfg { SkipRender : true } )
require . Len ( t , s . RegularPages , 1 )
p := s . RegularPages [ 0 ]
2015-05-31 12:54:50 -04:00
expectedTargetPath := filepath . FromSlash ( test . expected )
expectedFullFilePath := filepath . FromSlash ( test . path )
2015-05-09 06:12:30 -04:00
2015-05-31 12:54:50 -04:00
if p . TargetPath ( ) != expectedTargetPath {
2017-02-04 22:20:06 -05:00
t . Fatalf ( "[%d] %s => TargetPath expected: '%s', got: '%s'" , i , test . content , expectedTargetPath , p . TargetPath ( ) )
2015-05-31 12:54:50 -04:00
}
if p . FullFilePath ( ) != expectedFullFilePath {
2017-02-04 22:20:06 -05:00
t . Fatalf ( "[%d] %s => FullFilePath expected: '%s', got: '%s'" , i , test . content , expectedFullFilePath , p . FullFilePath ( ) )
2015-05-09 06:12:30 -04:00
}
}
}
2016-03-24 09:42:03 -04:00
var pageWithDraftAndPublished = ` -- -
2015-08-02 02:02:20 -04:00
title : broken
published : false
draft : true
-- -
some content
`
func TestDraftAndPublishedFrontMatterError ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
s := newTestSite ( t )
_ , err := s . NewPageFrom ( strings . NewReader ( pageWithDraftAndPublished ) , "content/post/broken.md" )
2015-08-02 02:02:20 -04:00
if err != ErrHasDraftAndPublished {
t . Errorf ( "expected ErrHasDraftAndPublished, was %#v" , err )
}
}
2016-10-11 12:12:28 -04:00
var pagesWithPublishedFalse = ` -- -
2015-08-30 18:51:25 -04:00
title : okay
published : false
-- -
some content
`
2016-03-24 09:42:03 -04:00
var pageWithPublishedTrue = ` -- -
2015-08-30 18:51:25 -04:00
title : okay
published : true
-- -
some content
`
func TestPublishedFrontMatter ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
s := newTestSite ( t )
p , err := s . NewPageFrom ( strings . NewReader ( pagesWithPublishedFalse ) , "content/post/broken.md" )
2015-08-30 18:51:25 -04:00
if err != nil {
t . Fatalf ( "err during parse: %s" , err )
}
if ! p . Draft {
t . Errorf ( "expected true, got %t" , p . Draft )
}
2017-02-04 22:20:06 -05:00
p , err = s . NewPageFrom ( strings . NewReader ( pageWithPublishedTrue ) , "content/post/broken.md" )
2015-08-30 18:51:25 -04:00
if err != nil {
t . Fatalf ( "err during parse: %s" , err )
}
if p . Draft {
t . Errorf ( "expected false, got %t" , p . Draft )
}
}
2016-10-11 12:12:28 -04:00
var pagesDraftTemplate = [ ] string { ` -- -
title : "okay"
draft : % t
-- -
some content
` ,
` ++ +
title = "okay"
draft = % t
++ +
some content
` ,
}
func TestDraft ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
s := newTestSite ( t )
2016-10-11 12:12:28 -04:00
for _ , draft := range [ ] bool { true , false } {
for i , templ := range pagesDraftTemplate {
pageContent := fmt . Sprintf ( templ , draft )
2017-02-04 22:20:06 -05:00
p , err := s . NewPageFrom ( strings . NewReader ( pageContent ) , "content/post/broken.md" )
2016-10-11 12:12:28 -04:00
if err != nil {
t . Fatalf ( "err during parse: %s" , err )
}
if p . Draft != draft {
t . Errorf ( "[%d] expected %t, got %t" , i , draft , p . Draft )
}
}
}
}
2016-12-27 21:08:24 -05:00
var pagesParamsTemplate = [ ] string { ` ++ +
title = "okay"
draft = false
tags = [ "hugo" , "web" ]
social = [
[ "a" , "#" ] ,
[ "b" , "#" ] ,
]
++ +
some content
` ,
` -- -
title : "okay"
draft : false
tags :
- hugo
- web
social :
- - a
- "#"
- - b
- "#"
-- -
some content
` ,
` {
"title" : "okay" ,
"draft" : false ,
"tags" : [ "hugo" , "web" ] ,
"social" : [
[ "a" , "#" ] ,
[ "b" , "#" ]
]
}
some content
` ,
}
func TestPageParams ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
s := newTestSite ( t )
2017-02-19 02:50:08 -05:00
wantedMap := map [ string ] interface { } {
2016-12-27 21:08:24 -05:00
"tags" : [ ] string { "hugo" , "web" } ,
// Issue #2752
"social" : [ ] interface { } {
[ ] interface { } { "a" , "#" } ,
[ ] interface { } { "b" , "#" } ,
} ,
}
for i , c := range pagesParamsTemplate {
2017-02-04 22:20:06 -05:00
p , err := s . NewPageFrom ( strings . NewReader ( c ) , "content/post/params.md" )
2016-12-27 21:08:24 -05:00
require . NoError ( t , err , "err during parse" , "#%d" , i )
2017-02-19 02:50:08 -05:00
for key , _ := range wantedMap {
assert . Equal ( t , wantedMap [ key ] , p . Params [ key ] , "#%d" , key )
}
2016-12-27 21:08:24 -05:00
}
}
2017-02-19 02:50:08 -05:00
func TestTraverse ( t * testing . T ) {
exampleParams := ` -- -
rating : "5 stars"
tags :
- hugo
- web
social :
twitter : "@jxxf"
facebook : "https://example.com"
-- - `
t . Parallel ( )
s := newTestSite ( t )
p , _ := s . NewPageFrom ( strings . NewReader ( exampleParams ) , "content/post/params.md" )
topLevelKeyValue , _ := p . Param ( "rating" )
assert . Equal ( t , "5 stars" , topLevelKeyValue )
nestedStringKeyValue , _ := p . Param ( "social.twitter" )
assert . Equal ( t , "@jxxf" , nestedStringKeyValue )
nonexistentKeyValue , _ := p . Param ( "doesn't.exist" )
assert . Nil ( t , nonexistentKeyValue )
}
2016-02-07 07:32:08 -05:00
func TestPageSimpleMethods ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
s := newTestSite ( t )
2016-02-07 07:32:08 -05:00
for i , this := range [ ] struct {
assertFunc func ( p * Page ) bool
} {
{ func ( p * Page ) bool { return ! p . IsNode ( ) } } ,
{ func ( p * Page ) bool { return p . IsPage ( ) } } ,
{ func ( p * Page ) bool { return p . Plain ( ) == "Do Be Do Be Do" } } ,
{ func ( p * Page ) bool { return strings . Join ( p . PlainWords ( ) , " " ) == "Do Be Do Be Do" } } ,
} {
2017-02-04 22:20:06 -05:00
p , _ := s . NewPage ( "Test" )
2016-02-07 07:32:08 -05:00
p . Content = "<h1>Do Be Do Be Do</h1>"
if ! this . assertFunc ( p ) {
t . Errorf ( "[%d] Page method error" , i )
}
}
}
2016-11-11 03:19:16 -05:00
func TestIndexPageSimpleMethods ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-11-11 03:19:16 -05:00
for i , this := range [ ] struct {
assertFunc func ( n * Page ) bool
} {
{ func ( n * Page ) bool { return n . IsNode ( ) } } ,
{ func ( n * Page ) bool { return ! n . IsPage ( ) } } ,
2016-11-13 08:27:10 -05:00
{ func ( n * Page ) bool { return n . RSSLink == "rssLink" } } ,
2016-11-11 03:19:16 -05:00
{ func ( n * Page ) bool { return n . Scratch ( ) != nil } } ,
{ func ( n * Page ) bool { return n . Hugo ( ) != nil } } ,
{ func ( n * Page ) bool { return n . Now ( ) . Unix ( ) == time . Now ( ) . Unix ( ) } } ,
} {
2016-11-16 14:32:38 -05:00
n := & Page { pageInit : & pageInit { } , Kind : KindHome }
2016-11-23 11:26:13 -05:00
2016-11-11 03:19:16 -05:00
n . RSSLink = "rssLink"
if ! this . assertFunc ( n ) {
t . Errorf ( "[%d] Node method error" , i )
}
}
}
2016-11-13 06:33:11 -05:00
func TestKind ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-11-11 03:19:16 -05:00
// Add tests for these constants to make sure they don't change
2016-11-13 06:33:11 -05:00
require . Equal ( t , "page" , KindPage )
require . Equal ( t , "home" , KindHome )
require . Equal ( t , "section" , KindSection )
require . Equal ( t , "taxonomy" , KindTaxonomy )
require . Equal ( t , "taxonomyTerm" , KindTaxonomyTerm )
2016-11-11 03:19:16 -05:00
}
2016-04-12 18:14:00 -04:00
func TestChompBOM ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-04-12 18:14:00 -04:00
const utf8BOM = "\xef\xbb\xbf"
2017-02-04 22:20:06 -05:00
cfg , fs := newTestCfg ( )
2016-08-01 17:04:44 -04:00
2017-01-10 04:55:03 -05:00
writeSource ( t , fs , filepath . Join ( "content" , "simple.md" ) , utf8BOM + simplePage )
2017-02-04 22:20:06 -05:00
s := buildSingleSite ( t , deps . DepsCfg { Fs : fs , Cfg : cfg } , BuildCfg { SkipRender : true } )
2016-04-12 18:14:00 -04:00
2016-11-13 08:27:10 -05:00
require . Len ( t , s . RegularPages , 1 )
2016-08-01 17:04:44 -04:00
2016-11-13 08:27:10 -05:00
p := s . RegularPages [ 0 ]
2016-08-01 17:04:44 -04:00
2016-04-12 18:14:00 -04:00
checkPageTitle ( t , p , "Simple" )
}
2013-10-07 00:57:45 -04:00
func listEqual ( left , right [ ] string ) bool {
2014-01-29 17:50:31 -05:00
if len ( left ) != len ( right ) {
return false
}
2013-10-07 00:57:45 -04:00
2014-01-29 17:50:31 -05:00
for i := range left {
if left [ i ] != right [ i ] {
return false
}
}
2013-10-07 00:57:45 -04:00
2014-01-29 17:50:31 -05:00
return true
2013-10-07 00:57:45 -04:00
}
2015-07-26 09:28:56 -04:00
// TODO(bep) this may be useful for other tests.
func compareObjects ( a interface { } , b interface { } ) bool {
aStr := strings . Split ( fmt . Sprintf ( "%v" , a ) , "" )
sort . Strings ( aStr )
bStr := strings . Split ( fmt . Sprintf ( "%v" , b ) , "" )
sort . Strings ( bStr )
return strings . Join ( aStr , "" ) == strings . Join ( bStr , "" )
}
2016-05-11 11:45:09 -04:00
2016-06-14 09:53:49 -04:00
func TestShouldBuild ( t * testing . T ) {
2017-02-04 22:20:06 -05:00
t . Parallel ( )
2016-05-11 11:45:09 -04:00
var past = time . Date ( 2009 , 11 , 17 , 20 , 34 , 58 , 651387237 , time . UTC )
var future = time . Date ( 2037 , 11 , 17 , 20 , 34 , 58 , 651387237 , time . UTC )
var zero = time . Time { }
var publishSettings = [ ] struct {
buildFuture bool
buildExpired bool
buildDrafts bool
draft bool
publishDate time . Time
expiryDate time . Time
out bool
} {
// publishDate and expiryDate
{ false , false , false , false , zero , zero , true } ,
{ false , false , false , false , zero , future , true } ,
{ false , false , false , false , past , zero , true } ,
{ false , false , false , false , past , future , true } ,
{ false , false , false , false , past , past , false } ,
{ false , false , false , false , future , future , false } ,
{ false , false , false , false , future , past , false } ,
// buildFuture and buildExpired
{ false , true , false , false , past , past , true } ,
{ true , true , false , false , past , past , true } ,
{ true , false , false , false , past , past , false } ,
{ true , false , false , false , future , future , true } ,
{ true , true , false , false , future , future , true } ,
{ false , true , false , false , future , past , false } ,
// buildDrafts and draft
{ true , true , false , true , past , future , false } ,
{ true , true , true , true , past , future , true } ,
{ true , true , true , true , past , future , true } ,
}
for _ , ps := range publishSettings {
2016-06-14 09:53:49 -04:00
s := shouldBuild ( ps . buildFuture , ps . buildExpired , ps . buildDrafts , ps . draft ,
2016-05-11 11:45:09 -04:00
ps . publishDate , ps . expiryDate )
if s != ps . out {
t . Errorf ( "AssertShouldBuild unexpected output with params: %+v" , ps )
}
}
}
2017-01-02 05:44:17 -05:00
func BenchmarkParsePage ( b * testing . B ) {
2017-02-04 22:20:06 -05:00
s := newTestSite ( b )
2017-01-02 05:44:17 -05:00
f , _ := os . Open ( "testdata/redis.cn.md" )
var buf bytes . Buffer
buf . ReadFrom ( f )
b . ResetTimer ( )
for i := 0 ; i < b . N ; i ++ {
2017-02-04 22:20:06 -05:00
page , _ := s . NewPage ( "bench" )
2017-01-02 05:44:17 -05:00
page . ReadFrom ( bytes . NewReader ( buf . Bytes ( ) ) )
}
}