2016-03-01 12:51:46 +00:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2015-12-10 22:19:38 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2015-07-03 21:53:50 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-09-25 06:59:02 +00:00
|
|
|
"strings"
|
2015-07-03 21:53:50 +00:00
|
|
|
|
2017-06-13 17:07:35 +00:00
|
|
|
"github.com/gohugoio/hugo/config"
|
2015-10-12 18:47:06 +00:00
|
|
|
"github.com/miekg/mmark"
|
2015-07-03 21:53:50 +00:00
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
)
|
|
|
|
|
2016-03-24 22:16:18 +00:00
|
|
|
// HugoHTMLRenderer wraps a blackfriday.Renderer, typically a blackfriday.Html
|
2015-09-07 18:41:02 +00:00
|
|
|
// Enabling Hugo to customise the rendering experience
|
2016-03-24 22:16:18 +00:00
|
|
|
type HugoHTMLRenderer struct {
|
2017-09-25 06:59:02 +00:00
|
|
|
cs *ContentSpec
|
2016-07-22 09:00:52 +00:00
|
|
|
*RenderingContext
|
2015-07-03 21:53:50 +00:00
|
|
|
blackfriday.Renderer
|
|
|
|
}
|
|
|
|
|
2017-08-02 12:25:05 +00:00
|
|
|
// BlockCode renders a given text as a block of code.
|
|
|
|
// Pygments is used if it is setup to handle code fences.
|
2017-02-05 03:20:06 +00:00
|
|
|
func (r *HugoHTMLRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string) {
|
|
|
|
if r.Cfg.GetBool("pygmentsCodeFences") && (lang != "" || r.Cfg.GetBool("pygmentsCodeFencesGuessSyntax")) {
|
|
|
|
opts := r.Cfg.GetString("pygmentsOptions")
|
2018-01-23 08:48:04 +00:00
|
|
|
str := strings.Trim(string(text), "\n\r")
|
2017-09-25 06:59:02 +00:00
|
|
|
highlighted, _ := r.cs.Highlight(str, lang, opts)
|
|
|
|
out.WriteString(highlighted)
|
2015-07-03 21:53:50 +00:00
|
|
|
} else {
|
2017-02-05 03:20:06 +00:00
|
|
|
r.Renderer.BlockCode(out, text, lang)
|
2015-07-03 21:53:50 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-07 18:41:02 +00:00
|
|
|
|
2016-09-09 11:08:20 +00:00
|
|
|
// ListItem adds task list support to the Blackfriday renderer.
|
2017-02-05 03:20:06 +00:00
|
|
|
func (r *HugoHTMLRenderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
|
|
|
if !r.Config.TaskLists {
|
|
|
|
r.Renderer.ListItem(out, text, flags)
|
2016-09-09 11:08:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case bytes.HasPrefix(text, []byte("[ ] ")):
|
2017-08-04 15:08:27 +00:00
|
|
|
text = append([]byte(`<label><input type="checkbox" disabled class="task-list-item">`), text[3:]...)
|
|
|
|
text = append(text, []byte(`</label>`)...)
|
2016-09-09 11:08:20 +00:00
|
|
|
|
|
|
|
case bytes.HasPrefix(text, []byte("[x] ")) || bytes.HasPrefix(text, []byte("[X] ")):
|
2017-08-04 15:08:27 +00:00
|
|
|
text = append([]byte(`<label><input type="checkbox" checked disabled class="task-list-item">`), text[3:]...)
|
|
|
|
text = append(text, []byte(`</label>`)...)
|
2016-09-09 11:08:20 +00:00
|
|
|
}
|
|
|
|
|
2017-02-05 03:20:06 +00:00
|
|
|
r.Renderer.ListItem(out, text, flags)
|
2016-09-09 11:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List adds task list support to the Blackfriday renderer.
|
2017-02-05 03:20:06 +00:00
|
|
|
func (r *HugoHTMLRenderer) List(out *bytes.Buffer, text func() bool, flags int) {
|
|
|
|
if !r.Config.TaskLists {
|
|
|
|
r.Renderer.List(out, text, flags)
|
2016-09-09 11:08:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
marker := out.Len()
|
2017-02-05 03:20:06 +00:00
|
|
|
r.Renderer.List(out, text, flags)
|
2016-09-09 11:08:20 +00:00
|
|
|
if out.Len() > marker {
|
|
|
|
list := out.Bytes()[marker:]
|
|
|
|
if bytes.Contains(list, []byte("task-list-item")) {
|
2017-07-29 18:52:45 +00:00
|
|
|
// Find the index of the first >, it might be 3 or 4 depending on whether
|
|
|
|
// there is a new line at the start, but this is safer than just hardcoding it.
|
|
|
|
closingBracketIndex := bytes.Index(list, []byte(">"))
|
2016-09-09 11:08:20 +00:00
|
|
|
// Rewrite the buffer from the marker
|
|
|
|
out.Truncate(marker)
|
2017-07-29 18:52:45 +00:00
|
|
|
// Safely assuming closingBracketIndex won't be -1 since there is a list
|
2016-09-09 11:08:20 +00:00
|
|
|
// May be either dl, ul or ol
|
2017-07-29 18:52:45 +00:00
|
|
|
list := append(list[:closingBracketIndex], append([]byte(` class="task-list"`), list[closingBracketIndex:]...)...)
|
2016-09-09 11:08:20 +00:00
|
|
|
out.Write(list)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 12:25:05 +00:00
|
|
|
// HugoMmarkHTMLRenderer wraps a mmark.Renderer, typically a mmark.html,
|
|
|
|
// enabling Hugo to customise the rendering experience.
|
2016-03-24 22:16:18 +00:00
|
|
|
type HugoMmarkHTMLRenderer struct {
|
2017-09-25 06:59:02 +00:00
|
|
|
cs *ContentSpec
|
2015-09-07 18:41:02 +00:00
|
|
|
mmark.Renderer
|
2017-02-05 03:20:06 +00:00
|
|
|
Cfg config.Provider
|
2015-09-07 18:41:02 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 12:25:05 +00:00
|
|
|
// BlockCode renders a given text as a block of code.
|
|
|
|
// Pygments is used if it is setup to handle code fences.
|
2017-02-05 03:20:06 +00:00
|
|
|
func (r *HugoMmarkHTMLRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string, caption []byte, subfigure bool, callouts bool) {
|
|
|
|
if r.Cfg.GetBool("pygmentsCodeFences") && (lang != "" || r.Cfg.GetBool("pygmentsCodeFencesGuessSyntax")) {
|
2018-01-23 08:48:04 +00:00
|
|
|
str := strings.Trim(string(text), "\n\r")
|
2017-09-25 06:59:02 +00:00
|
|
|
highlighted, _ := r.cs.Highlight(str, lang, "")
|
|
|
|
out.WriteString(highlighted)
|
2015-09-07 18:41:02 +00:00
|
|
|
} else {
|
2017-02-05 03:20:06 +00:00
|
|
|
r.Renderer.BlockCode(out, text, lang, caption, subfigure, callouts)
|
2015-09-07 18:41:02 +00:00
|
|
|
}
|
|
|
|
}
|