Add pygmentsstyle and pygmentsuseclasses options

Fixes #204

Conflicts:
	commands/hugo.go
This commit is contained in:
LK4D4 2014-05-07 20:38:14 +04:00 committed by spf13
parent 5df0cf7eca
commit 2194cc77de
3 changed files with 14 additions and 4 deletions

View file

@ -110,6 +110,8 @@ func InitializeConfig() {
viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"}) viper.SetDefault("Indexes", map[string]string{"tag": "tags", "category": "categories"})
viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0)) viper.SetDefault("Permalinks", make(hugolib.PermalinkOverrides, 0))
viper.SetDefault("Sitemap", hugolib.Sitemap{Priority: -1}) viper.SetDefault("Sitemap", hugolib.Sitemap{Priority: -1})
viper.SetDefault("PygmentsStyle", "monokai")
viper.SetDefault("PygmentsUseClasses", false)
if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed { if hugoCmdV.PersistentFlags().Lookup("build-drafts").Changed {
viper.Set("BuildDrafts", Draft) viper.Set("BuildDrafts", Draft)
@ -134,7 +136,6 @@ func InitializeConfig() {
if hugoCmdV.PersistentFlags().Lookup("logfile").Changed { if hugoCmdV.PersistentFlags().Lookup("logfile").Changed {
viper.Set("LogFile", LogFile) viper.Set("LogFile", LogFile)
} }
if BaseUrl != "" { if BaseUrl != "" {
if !strings.HasSuffix(BaseUrl, "/") { if !strings.HasSuffix(BaseUrl, "/") {
BaseUrl = BaseUrl + "/" BaseUrl = BaseUrl + "/"

View file

@ -27,13 +27,13 @@ silently simply pass the content along unhighlighted.
* **Warning** pygments is relatively slow and our integration isn't * **Warning** pygments is relatively slow and our integration isn't
as optimized as it could be. Expect much longer build times when using server side highlighting. as optimized as it could be. Expect much longer build times when using server side highlighting.
* Languages available depends on your pygments installation. * Languages available depends on your pygments installation.
* While pygments supports a few different output formats and options we currently
only support output=html, style=monokai, noclasses=true, and encoding=utf-8.
* Styles are inline in order to be supported in syndicated content when references * Styles are inline in order to be supported in syndicated content when references
to style sheets are not carried over. to style sheets are not carried over.
* We have sought to have the simplest interface possible, which consequently * We have sought to have the simplest interface possible, which consequently
limits configuration. An ambitious user is encouraged to extend the current limits configuration. An ambitious user is encouraged to extend the current
functionality to offer more customization. functionality to offer more customization.
* You can change appearance with config options `pygmentsstyle`(default
`"monokai"`) and `pygmentsuseclasses`(defaut `false`).
### Usage ### Usage
Highlight takes exactly one required parameter of language and requires a Highlight takes exactly one required parameter of language and requires a

View file

@ -15,10 +15,12 @@ package helpers
import ( import (
"bytes" "bytes"
"fmt"
"os/exec" "os/exec"
"strings" "strings"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
) )
func Highlight(code string, lexer string) string { func Highlight(code string, lexer string) string {
@ -32,8 +34,15 @@ func Highlight(code string, lexer string) string {
var out bytes.Buffer var out bytes.Buffer
var stderr bytes.Buffer var stderr bytes.Buffer
style := viper.GetString("PygmentsStyle")
cmd := exec.Command(pygmentsBin, "-l"+lexer, "-fhtml", "-O style=monokai,noclasses=true,encoding=utf-8") noclasses := "true"
if viper.GetBool("PygmentsUseClasses") {
noclasses = "false"
}
cmd := exec.Command(pygmentsBin, "-l"+lexer, "-fhtml", "-O",
fmt.Sprintf("style=%s,noclasses=%s,encoding=utf8", style, noclasses))
cmd.Stdin = strings.NewReader(code) cmd.Stdin = strings.NewReader(code)
cmd.Stdout = &out cmd.Stdout = &out
cmd.Stderr = &stderr cmd.Stderr = &stderr