2017-11-20 05:49:19 +00:00
|
|
|
package core
|
|
|
|
|
2017-11-24 19:56:32 +00:00
|
|
|
import (
|
2017-12-02 18:47:21 +00:00
|
|
|
"bytes"
|
2017-12-23 21:22:51 +00:00
|
|
|
"crypto/md5"
|
2017-12-02 18:47:21 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-12-23 21:22:51 +00:00
|
|
|
"io"
|
2017-12-02 18:47:21 +00:00
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2017-11-24 19:56:32 +00:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
|
|
"github.com/shurcooL/github_flavored_markdown"
|
|
|
|
)
|
2017-11-20 05:49:19 +00:00
|
|
|
|
2017-12-02 18:47:21 +00:00
|
|
|
// Regexps for Markdown use cases.
|
|
|
|
var (
|
|
|
|
// Match title from the first `# h1` heading.
|
|
|
|
reMarkdownTitle = regexp.MustCompile(`(?m:^#([^#\r\n]+)$)`)
|
|
|
|
|
|
|
|
// Match fenced code blocks with languages defined.
|
2017-12-23 02:34:58 +00:00
|
|
|
reFencedCode = regexp.MustCompile("```" + `([a-z]*)[\r\n]([\s\S]*?)[\r\n]\s*` + "```")
|
2017-12-02 18:47:21 +00:00
|
|
|
|
|
|
|
// Regexp to match fenced code blocks in rendered Markdown HTML.
|
|
|
|
// Tweak this if you change Markdown engines later.
|
|
|
|
reCodeBlock = regexp.MustCompile(`<div class="highlight highlight-(.+?)"><pre>(.+?)</pre></div>`)
|
|
|
|
reDecodeBlock = regexp.MustCompile(`\[?FENCED_CODE_%d_BLOCK?\]`)
|
|
|
|
)
|
|
|
|
|
|
|
|
// A container for parsed code blocks.
|
|
|
|
type codeBlock struct {
|
|
|
|
placeholder int
|
|
|
|
language string
|
|
|
|
source string
|
|
|
|
}
|
|
|
|
|
|
|
|
// TitleFromMarkdown tries to find a title from the source of a Markdown file.
|
|
|
|
//
|
|
|
|
// On error, returns "Untitled" along with the error. So if you're lazy and
|
|
|
|
// want a suitable default, you can safely ignore the error.
|
|
|
|
func TitleFromMarkdown(body string) (string, error) {
|
|
|
|
m := reMarkdownTitle.FindStringSubmatch(body)
|
|
|
|
if len(m) > 0 {
|
|
|
|
return m[1], nil
|
|
|
|
}
|
|
|
|
return "Untitled", errors.New(
|
|
|
|
"did not find a single h1 (denoted by # prefix) for Markdown title",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-11-24 19:56:32 +00:00
|
|
|
// RenderMarkdown renders markdown to HTML, safely. It uses blackfriday to
|
|
|
|
// render Markdown to HTML and then Bluemonday to sanitize the resulting HTML.
|
2017-11-20 05:49:19 +00:00
|
|
|
func (b *Blog) RenderMarkdown(input string) string {
|
2017-11-24 19:56:32 +00:00
|
|
|
unsafe := []byte(b.RenderTrustedMarkdown(input))
|
|
|
|
|
|
|
|
// Sanitize HTML, but allow fenced code blocks to not get mangled in user
|
|
|
|
// submitted comments.
|
|
|
|
p := bluemonday.UGCPolicy()
|
|
|
|
p.AllowAttrs("class").Matching(reFencedCodeClass).OnElements("code")
|
|
|
|
html := p.SanitizeBytes(unsafe)
|
|
|
|
return string(html)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderTrustedMarkdown renders markdown to HTML, but without applying
|
|
|
|
// bluemonday filtering afterward. This is for blog posts and website
|
|
|
|
// Markdown pages, not for user-submitted comments or things.
|
|
|
|
func (b *Blog) RenderTrustedMarkdown(input string) string {
|
2017-12-02 18:47:21 +00:00
|
|
|
// Find and hang on to fenced code blocks.
|
|
|
|
codeBlocks := []codeBlock{}
|
|
|
|
matches := reFencedCode.FindAllStringSubmatch(input, -1)
|
|
|
|
for i, m := range matches {
|
|
|
|
language, source := m[1], m[2]
|
|
|
|
if language == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
codeBlocks = append(codeBlocks, codeBlock{i, language, source})
|
|
|
|
|
|
|
|
input = strings.Replace(input, m[0], fmt.Sprintf(
|
|
|
|
"[?FENCED_CODE_%d_BLOCK?]",
|
|
|
|
i,
|
|
|
|
), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render the HTML out.
|
|
|
|
html := string(github_flavored_markdown.Markdown([]byte(input)))
|
|
|
|
|
|
|
|
// Substitute fenced codes back in.
|
|
|
|
for _, block := range codeBlocks {
|
2017-12-23 21:22:51 +00:00
|
|
|
highlighted, _ := b.Pygmentize(block.language, block.source)
|
2017-12-02 18:47:21 +00:00
|
|
|
html = strings.Replace(html,
|
|
|
|
fmt.Sprintf("[?FENCED_CODE_%d_BLOCK?]", block.placeholder),
|
|
|
|
highlighted,
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-11-24 19:56:32 +00:00
|
|
|
return string(html)
|
2017-11-20 05:49:19 +00:00
|
|
|
}
|
2017-12-02 18:47:21 +00:00
|
|
|
|
|
|
|
// Pygmentize searches for fenced code blocks in rendered Markdown HTML
|
|
|
|
// and runs Pygments to syntax highlight it.
|
|
|
|
//
|
|
|
|
// On error the original given source is returned back.
|
|
|
|
//
|
2017-12-23 21:22:51 +00:00
|
|
|
// The rendered result is cached in Redis if available, because the CLI
|
|
|
|
// call takes ~0.6s which is slow if you're rendering a lot of code blocks.
|
|
|
|
func (b *Blog) Pygmentize(language, source string) (string, error) {
|
|
|
|
var result string
|
|
|
|
|
|
|
|
// Hash the source for the cache key.
|
|
|
|
h := md5.New()
|
|
|
|
io.WriteString(h, language+source)
|
|
|
|
hash := fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
cacheKey := "pygmentize:" + hash
|
|
|
|
|
|
|
|
// Do we have it cached?
|
2017-12-23 21:32:26 +00:00
|
|
|
if cached, err := b.Cache.Get(cacheKey); err == nil && len(cached) > 0 {
|
2017-12-23 21:22:51 +00:00
|
|
|
return string(cached), nil
|
|
|
|
}
|
2017-12-02 18:47:21 +00:00
|
|
|
|
2017-12-23 21:22:51 +00:00
|
|
|
// Defer to the `pygmentize` command
|
|
|
|
bin := "pygmentize"
|
2017-12-02 18:47:21 +00:00
|
|
|
if _, err := exec.LookPath(bin); err != nil {
|
|
|
|
return source, errors.New("pygmentize not installed")
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(bin, "-l"+language, "-f"+"html", "-O encoding=utf-8")
|
|
|
|
cmd.Stdin = strings.NewReader(source)
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
log.Error("Error running pygments: %s", stderr.String())
|
|
|
|
return source, err
|
|
|
|
}
|
|
|
|
|
2017-12-23 21:22:51 +00:00
|
|
|
result = out.String()
|
|
|
|
err := b.Cache.Set(cacheKey, []byte(result), 60*60*24) // cool md5's don't change
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Couldn't cache Pygmentize output: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2017-12-02 18:47:21 +00:00
|
|
|
}
|