Change generate.go to use chroma
This commit is contained in:
parent
0fab6e00d6
commit
96da444e47
@ -7,15 +7,16 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/russross/blackfriday"
|
|
||||||
"github.com/alecthomas/chroma"
|
"github.com/alecthomas/chroma"
|
||||||
|
"github.com/alecthomas/chroma/formatters/html"
|
||||||
"github.com/alecthomas/chroma/lexers"
|
"github.com/alecthomas/chroma/lexers"
|
||||||
|
"github.com/alecthomas/chroma/styles"
|
||||||
|
"github.com/russross/blackfriday"
|
||||||
)
|
)
|
||||||
|
|
||||||
// siteDir is the target directory into which the HTML gets generated. Its
|
// siteDir is the target directory into which the HTML gets generated. Its
|
||||||
@ -48,25 +49,6 @@ func copyFile(src, dst string) {
|
|||||||
check(err)
|
check(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pipe(bin string, arg []string, src string) []byte {
|
|
||||||
cmd := exec.Command(bin, arg...)
|
|
||||||
in, err := cmd.StdinPipe()
|
|
||||||
check(err)
|
|
||||||
out, err := cmd.StdoutPipe()
|
|
||||||
check(err)
|
|
||||||
err = cmd.Start()
|
|
||||||
check(err)
|
|
||||||
_, err = in.Write([]byte(src))
|
|
||||||
check(err)
|
|
||||||
err = in.Close()
|
|
||||||
check(err)
|
|
||||||
bytes, err := ioutil.ReadAll(out)
|
|
||||||
check(err)
|
|
||||||
err = cmd.Wait()
|
|
||||||
check(err)
|
|
||||||
return bytes
|
|
||||||
}
|
|
||||||
|
|
||||||
func sha1Sum(s string) string {
|
func sha1Sum(s string) string {
|
||||||
h := sha1.New()
|
h := sha1.New()
|
||||||
h.Write([]byte(s))
|
h.Write([]byte(s))
|
||||||
@ -80,24 +62,20 @@ func mustReadFile(path string) string {
|
|||||||
return string(bytes)
|
return string(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cachedPygmentize(lex string, src string) string {
|
func cachedPygmentize(sourcePath string, src string) string {
|
||||||
ensureDir(cacheDir)
|
ensureDir(cacheDir)
|
||||||
arg := []string{"-l", lex, "-f", "html"}
|
cachePath := cacheDir + "/pygmentize-" + sha1Sum(src)
|
||||||
cachePath := cacheDir + "/pygmentize-" + strings.Join(arg, "-") + "-" + sha1Sum(src)
|
|
||||||
cacheBytes, cacheErr := ioutil.ReadFile(cachePath)
|
cacheBytes, cacheErr := ioutil.ReadFile(cachePath)
|
||||||
if cacheErr == nil {
|
if cacheErr == nil {
|
||||||
return string(cacheBytes)
|
return string(cacheBytes)
|
||||||
}
|
}
|
||||||
renderBytes := pipe(pygmentizeBin, arg, src)
|
renderBytes := chromaFormat(src, sourcePath)
|
||||||
// Newer versions of Pygments add silly empty spans.
|
// Newer versions of Pygments add silly empty spans.
|
||||||
renderCleanString := strings.Replace(string(renderBytes), "<span></span>", "", -1)
|
return renderBytes
|
||||||
writeErr := ioutil.WriteFile(cachePath, []byte(renderCleanString), 0600)
|
|
||||||
check(writeErr)
|
|
||||||
return renderCleanString
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func markdown(src string) string {
|
func markdown(src string) string {
|
||||||
return string(blackfriday.MarkdownCommon([]byte(src)))
|
return string(blackfriday.Run([]byte(src)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func readLines(path string) []string {
|
func readLines(path string) []string {
|
||||||
@ -111,15 +89,6 @@ func mustGlob(glob string) []string {
|
|||||||
return paths
|
return paths
|
||||||
}
|
}
|
||||||
|
|
||||||
func whichLexer(path string) string {
|
|
||||||
if strings.HasSuffix(path, ".go") {
|
|
||||||
return "go"
|
|
||||||
} else if strings.HasSuffix(path, ".sh") {
|
|
||||||
return "console"
|
|
||||||
}
|
|
||||||
panic("No lexer for " + path)
|
|
||||||
}
|
|
||||||
|
|
||||||
func debug(msg string) {
|
func debug(msg string) {
|
||||||
if os.Getenv("DEBUG") == "1" {
|
if os.Getenv("DEBUG") == "1" {
|
||||||
fmt.Fprintln(os.Stderr, msg)
|
fmt.Fprintln(os.Stderr, msg)
|
||||||
@ -219,30 +188,30 @@ func parseSegs(sourcePath string) ([]*Seg, string) {
|
|||||||
|
|
||||||
func parseAndRenderSegs(sourcePath string) ([]*Seg, string) {
|
func parseAndRenderSegs(sourcePath string) ([]*Seg, string) {
|
||||||
segs, filecontent := parseSegs(sourcePath)
|
segs, filecontent := parseSegs(sourcePath)
|
||||||
lexer := whichLexer(sourcePath)
|
var goFile bool
|
||||||
for _, seg := range segs {
|
for _, seg := range segs {
|
||||||
if seg.Docs != "" {
|
if seg.Docs != "" {
|
||||||
seg.DocsRendered = markdown(seg.Docs)
|
seg.DocsRendered = markdown(seg.Docs)
|
||||||
}
|
}
|
||||||
if seg.Code != "" {
|
if seg.Code != "" {
|
||||||
seg.CodeRendered = cachedPygmentize(lexer, seg.Code)
|
seg.CodeRendered = cachedPygmentize(sourcePath, seg.Code)
|
||||||
// adding the content to the js code for copying to the clipboard
|
// adding the content to the js code for copying to the clipboard
|
||||||
if strings.HasSuffix(sourcePath, ".go") {
|
if strings.HasSuffix(sourcePath, ".go") {
|
||||||
|
goFile = true
|
||||||
seg.CodeForJs = strings.Trim(seg.Code, "\n") + "\n"
|
seg.CodeForJs = strings.Trim(seg.Code, "\n") + "\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// we are only interested in the 'go' code to pass to play.golang.org
|
// we are only interested in the 'go' code to pass to play.golang.org
|
||||||
if lexer != "go" {
|
if !goFile {
|
||||||
filecontent = ""
|
filecontent = ""
|
||||||
}
|
}
|
||||||
return segs, filecontent
|
return segs, filecontent
|
||||||
}
|
}
|
||||||
|
|
||||||
func chromaFormat(code string) string {
|
func chromaFormat(code, fileExtension string) string {
|
||||||
lexer := chroma.Go
|
lexer := lexers.Get(fileExtension)
|
||||||
if lexer == nil {
|
if lexer == nil {
|
||||||
panic("")
|
|
||||||
lexer = lexers.Fallback
|
lexer = lexers.Fallback
|
||||||
}
|
}
|
||||||
lexer = chroma.Coalesce(lexer)
|
lexer = chroma.Coalesce(lexer)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user