// Copyright 2016 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.
package hugolib
import (
"encoding/json"
"fmt"
"html/template"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/tpl"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
)
const (
baseURL = "http://foo/bar"
)
func TestShortcodeCrossrefs(t *testing.T) {
for _, relative := range []bool{true, false} {
doTestShortcodeCrossrefs(t, relative)
}
}
func doTestShortcodeCrossrefs(t *testing.T, relative bool) {
testCommonResetState()
viper.Set("baseURL", baseURL)
var refShortcode string
var expectedBase string
if relative {
refShortcode = "relref"
expectedBase = "/bar"
} else {
refShortcode = "ref"
expectedBase = baseURL
}
path := filepath.FromSlash("blog/post.md")
in := fmt.Sprintf(`{{< %s "%s" >}}`, refShortcode, path)
writeSource(t, "content/"+path, simplePageWithURL+": "+in)
expected := fmt.Sprintf(`%s/simple/url/`, expectedBase)
sites, err := newHugoSitesDefaultLanguage()
require.NoError(t, err)
require.NoError(t, sites.Build(BuildCfg{}))
require.Len(t, sites.Sites[0].regularPages, 1)
output := string(sites.Sites[0].regularPages[0].Content)
if !strings.Contains(output, expected) {
t.Errorf("Got\n%q\nExpected\n%q", output, expected)
}
}
func TestShortcodeHighlight(t *testing.T) {
testCommonResetState()
if !helpers.HasPygments() {
t.Skip("Skip test as Pygments is not installed")
}
viper.Set("pygmentsStyle", "bw")
viper.Set("pygmentsUseClasses", false)
for i, this := range []struct {
in, expected string
}{
{`
{{< highlight java >}}
void do();
{{< /highlight >}}`,
"(?s)^\n