Esempio n. 1
0
func TestHighlight(t *testing.T) {
	viper.Reset()
	defer viper.Reset()

	if !helpers.HasPygments() {
		t.Skip("Skip test as Pygments is not installed")
	}
	viper.Set("PygmentsStyle", "bw")
	viper.Set("PygmentsUseClasses", false)

	templ := tpl.New()

	code := `
{{< highlight java >}}
void do();
{{< /highlight >}}`

	p, _ := pageFromString(simplePage, "simple.md")
	output, err := HandleShortcodes(code, p, templ)

	if err != nil {
		t.Fatal("Handle shortcode error", err)
	}
	matched, err := regexp.MatchString("(?s)^\n<div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\">.*?void</span> do().*?</pre></div>\n$", output)

	if err != nil {
		t.Fatal("Regexp error", err)
	}

	if !matched {
		t.Error("Hightlight mismatch, got\n", output)
	}
}
Esempio n. 2
0
func TestHighlight(t *testing.T) {
	if !helpers.HasPygments() {
		t.Skip("Skip test as Pygments is not installed")
	}
	defer viper.Set("PygmentsStyle", viper.Get("PygmentsStyle"))
	viper.Set("PygmentsStyle", "bw")

	tem := tpl.New()

	code := `
{{< highlight java >}}
void do();
{{< /highlight >}}`
	CheckShortCodeMatch(t, code, "\n<div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span style=\"font-weight: bold\">void</span> do();\n</pre></div>\n", tem)
}
func TestShortcodeHighlight(t *testing.T) {
	viper.Reset()
	defer viper.Reset()

	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<div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\">.*?void</span> do().*?</pre></div>\n$",
		},
		{`
{{< highlight java "style=friendly" >}}
void do();
{{< /highlight >}}`,
			"(?s)^\n<div class=\"highlight\" style=\"background: #f0f0f0\"><pre style=\"line-height: 125%\">.*?void</span>.*?do</span>.*?().*?</pre></div>\n$",
		},
	} {
		templ := tpl.New()
		p, _ := pageFromString(simplePage, "simple.md")
		output, err := HandleShortcodes(this.in, p, templ)

		if err != nil {
			t.Fatalf("[%d] Handle shortcode error", i)
		}

		matched, err := regexp.MatchString(this.expected, output)

		if err != nil {
			t.Fatalf("[%d] Regexp error", i)
		}

		if !matched {
			t.Errorf("[%d] Hightlight mismatch, got %s\n", i, output)
		}
	}
}
Esempio n. 4
0
func TestShortcodesInSite(t *testing.T) {
	testCommonResetState()

	baseURL := "http://foo/bar"
	viper.Set("DefaultExtension", "html")
	viper.Set("DefaultContentLanguage", "en")
	viper.Set("baseurl", baseURL)
	viper.Set("UglyURLs", false)
	viper.Set("verbose", true)

	viper.Set("pygmentsuseclasses", true)
	viper.Set("pygmentscodefences", true)

	tests := []struct {
		contentPath string
		content     string
		outFile     string
		expected    string
	}{
		{"sect/doc1.md", `a{{< b >}}c`,
			filepath.FromSlash("sect/doc1/index.html"), "<p>abc</p>\n"},
		// Issue #1642: Multiple shortcodes wrapped in P
		// Deliberately forced to pass even if they maybe shouldn't.
		{"sect/doc2.md", `a

{{< b >}}		
{{< c >}}
{{< d >}}

e`,
			filepath.FromSlash("sect/doc2/index.html"),
			"<p>a</p>\n\n<p>b<br />\nc\nd</p>\n\n<p>e</p>\n"},
		{"sect/doc3.md", `a

{{< b >}}		
{{< c >}}

{{< d >}}

e`,
			filepath.FromSlash("sect/doc3/index.html"),
			"<p>a</p>\n\n<p>b<br />\nc</p>\n\nd\n\n<p>e</p>\n"},
		{"sect/doc4.md", `a
{{< b >}}
{{< b >}}
{{< b >}}
{{< b >}}
{{< b >}}










`,
			filepath.FromSlash("sect/doc4/index.html"),
			"<p>a\nb\nb\nb\nb\nb</p>\n"},
		// #2192 #2209: Shortcodes in markdown headers
		{"sect/doc5.md", `# {{< b >}}	
## {{% c %}}`,
			filepath.FromSlash("sect/doc5/index.html"), "\n\n<h1 id=\"hahahugoshortcode-1hbhb\">b</h1>\n\n<h2 id=\"hahahugoshortcode-2hbhb\">c</h2>\n"},
		// #2223 pygments
		{"sect/doc6.md", "\n```bash\nb: {{< b >}} c: {{% c %}}\n```\n",
			filepath.FromSlash("sect/doc6/index.html"),
			"b: b c: c\n</code></pre></div>\n"},
		// #2249
		{"sect/doc7.ad", `_Shortcodes:_ *b: {{< b >}} c: {{% c %}}*`,
			filepath.FromSlash("sect/doc7/index.html"),
			"<div class=\"paragraph\">\n<p><em>Shortcodes:</em> <strong>b: b c: c</strong></p>\n</div>\n"},
		{"sect/doc8.rst", `**Shortcodes:** *b: {{< b >}} c: {{% c %}}*`,
			filepath.FromSlash("sect/doc8/index.html"),
			"<div class=\"document\">\n\n\n<p><strong>Shortcodes:</strong> <em>b: b c: c</em></p>\n</div>"},
		{"sect/doc9.mmark", `
---
menu:
  main:
    parent: 'parent'
---
**Shortcodes:** *b: {{< b >}} c: {{% c %}}*`,
			filepath.FromSlash("sect/doc9/index.html"),
			"<p><strong>Shortcodes:</strong> <em>b: b c: c</em></p>\n"},
		// Issue #1229: Menus not available in shortcode.
		{"sect/doc10.md", `---
menu:
  main:
    identifier: 'parent'
tags:
- Menu
---
**Menus:** {{< menu >}}`,
			filepath.FromSlash("sect/doc10/index.html"),
			"<p><strong>Menus:</strong> 1</p>\n"},
		// Issue #2323: Taxonomies not available in shortcode.
		{"sect/doc11.md", `---
tags:
- Bugs
---
**Tags:** {{< tags >}}`,
			filepath.FromSlash("sect/doc11/index.html"),
			"<p><strong>Tags:</strong> 2</p>\n"},
	}

	sources := make([]source.ByteSource, len(tests))

	for i, test := range tests {
		sources[i] = source.ByteSource{Name: filepath.FromSlash(test.contentPath), Content: []byte(test.content)}
	}

	s := &Site{
		Source:   &source.InMemorySource{ByteSource: sources},
		targets:  targetList{page: &target.PagePub{UglyURLs: false}},
		Language: helpers.NewDefaultLanguage(),
	}

	addTemplates := func(templ tpl.Template) error {
		templ.AddTemplate("_default/single.html", "{{.Content}}")

		templ.AddInternalShortcode("b.html", `b`)
		templ.AddInternalShortcode("c.html", `c`)
		templ.AddInternalShortcode("d.html", `d`)
		templ.AddInternalShortcode("menu.html", `{{ len (index .Page.Menus "main").Children }}`)
		templ.AddInternalShortcode("tags.html", `{{ len .Page.Site.Taxonomies.tags }}`)

		return nil

	}

	sites, err := newHugoSites(s)

	if err != nil {
		t.Fatalf("Failed to build site: %s", err)
	}

	if err = sites.Build(BuildCfg{withTemplate: addTemplates}); err != nil {
		t.Fatalf("Failed to build site: %s", err)
	}

	for _, test := range tests {
		if strings.HasSuffix(test.contentPath, ".ad") && !helpers.HasAsciidoc() {
			fmt.Println("Skip Asciidoc test case as no Asciidoc present.")
			continue
		} else if strings.HasSuffix(test.contentPath, ".rst") && !helpers.HasRst() {
			fmt.Println("Skip Rst test case as no rst2html present.")
			continue
		} else if strings.Contains(test.expected, "code") && !helpers.HasPygments() {
			fmt.Println("Skip Pygments test case as no pygments present.")
			continue
		}

		file, err := hugofs.Destination().Open(test.outFile)

		if err != nil {
			t.Fatalf("Did not find %s in target: %s", test.outFile, err)
		}

		content := helpers.ReaderToString(file)

		if !strings.Contains(content, test.expected) {
			t.Fatalf("%s content expected:\n%q\ngot:\n%q", test.outFile, test.expected, content)
		}
	}

}