Fix headers and unexport plaintext renderer.

This commit is contained in:
Tobias Weingartner 2016-05-01 16:45:24 -07:00
parent 2bccc1466e
commit 45e783c3f9
3 changed files with 41 additions and 37 deletions

View File

@ -6,6 +6,7 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"strconv"
"strings" "strings"
"text/template" "text/template"
"time" "time"
@ -142,6 +143,8 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Content-Length", strconv.FormatInt(int64(len(html)), 10))
middleware.SetLastModifiedHeader(w, lastModTime) middleware.SetLastModifiedHeader(w, lastModTime)
if r.Method == http.MethodGet { if r.Method == http.MethodGet {
w.Write(html) w.Write(html)

View File

@ -7,32 +7,33 @@ import (
) )
// Ensure we implement the Blackfriday Markdown Renderer interface // Ensure we implement the Blackfriday Markdown Renderer interface
var _ blackfriday.Renderer = (*Renderer)(nil) var _ blackfriday.Renderer = (*renderer)(nil)
// Renderer is a plain-text Markdown renderer that implements the // renderer renders Markdown to plain-text meant for listings and excerpts,
// blackfriday.Renderer interface. Many of the required methods are // and implements the blackfriday.Renderer interface.
// stubs with no output. //
type Renderer struct{} // Many of the methods are stubs with no output to prevent output of HTML markup.
type renderer struct{}
// Blocklevel callbacks // Blocklevel callbacks
// Stub BlockCode is the code tag callback. // Stub BlockCode is the code tag callback.
func (r Renderer) BlockCode(out *bytes.Buffer, text []byte, land string) {} func (r renderer) BlockCode(out *bytes.Buffer, text []byte, land string) {}
// Stub BlockQuote is teh quote tag callback. // Stub BlockQuote is teh quote tag callback.
func (r Renderer) BlockQuote(out *bytes.Buffer, text []byte) {} func (r renderer) BlockQuote(out *bytes.Buffer, text []byte) {}
// Stub BlockHtml is the HTML tag callback. // Stub BlockHtml is the HTML tag callback.
func (r Renderer) BlockHtml(out *bytes.Buffer, text []byte) {} func (r renderer) BlockHtml(out *bytes.Buffer, text []byte) {}
// Stub Header is the header tag callback. // Stub Header is the header tag callback.
func (r Renderer) Header(out *bytes.Buffer, text func() bool, level int, id string) {} func (r renderer) Header(out *bytes.Buffer, text func() bool, level int, id string) {}
// Stub HRule is the horizontal rule tag callback. // Stub HRule is the horizontal rule tag callback.
func (r Renderer) HRule(out *bytes.Buffer) {} func (r renderer) HRule(out *bytes.Buffer) {}
// List is the list tag callback. // List is the list tag callback.
func (r Renderer) List(out *bytes.Buffer, text func() bool, flags int) { func (r renderer) List(out *bytes.Buffer, text func() bool, flags int) {
// TODO: This is not desired (we'd rather not write lists as part of summary), // TODO: This is not desired (we'd rather not write lists as part of summary),
// but see this issue: https://github.com/russross/blackfriday/issues/189 // but see this issue: https://github.com/russross/blackfriday/issues/189
marker := out.Len() marker := out.Len()
@ -43,11 +44,11 @@ func (r Renderer) List(out *bytes.Buffer, text func() bool, flags int) {
} }
// Stub ListItem is the list item tag callback. // Stub ListItem is the list item tag callback.
func (r Renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {} func (r renderer) ListItem(out *bytes.Buffer, text []byte, flags int) {}
// Paragraph is the paragraph tag callback. This renders simple paragraph text // Paragraph is the paragraph tag callback. This renders simple paragraph text
// into plain text, such that summaries can be easily generated. // into plain text, such that summaries can be easily generated.
func (r Renderer) Paragraph(out *bytes.Buffer, text func() bool) { func (r renderer) Paragraph(out *bytes.Buffer, text func() bool) {
marker := out.Len() marker := out.Len()
if !text() { if !text() {
out.Truncate(marker) out.Truncate(marker)
@ -56,34 +57,34 @@ func (r Renderer) Paragraph(out *bytes.Buffer, text func() bool) {
} }
// Stub Table is the table tag callback. // Stub Table is the table tag callback.
func (r Renderer) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {} func (r renderer) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {}
// Stub TableRow is the table row tag callback. // Stub TableRow is the table row tag callback.
func (r Renderer) TableRow(out *bytes.Buffer, text []byte) {} func (r renderer) TableRow(out *bytes.Buffer, text []byte) {}
// Stub TableHeaderCell is the table header cell tag callback. // Stub TableHeaderCell is the table header cell tag callback.
func (r Renderer) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {} func (r renderer) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {}
// Stub TableCell is the table cell tag callback. // Stub TableCell is the table cell tag callback.
func (r Renderer) TableCell(out *bytes.Buffer, text []byte, flags int) {} func (r renderer) TableCell(out *bytes.Buffer, text []byte, flags int) {}
// Stub Footnotes is the foot notes tag callback. // Stub Footnotes is the foot notes tag callback.
func (r Renderer) Footnotes(out *bytes.Buffer, text func() bool) {} func (r renderer) Footnotes(out *bytes.Buffer, text func() bool) {}
// Stub FootnoteItem is the footnote item tag callback. // Stub FootnoteItem is the footnote item tag callback.
func (r Renderer) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {} func (r renderer) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {}
// Stub TitleBlock is the title tag callback. // Stub TitleBlock is the title tag callback.
func (r Renderer) TitleBlock(out *bytes.Buffer, text []byte) {} func (r renderer) TitleBlock(out *bytes.Buffer, text []byte) {}
// Spanlevel callbacks // Spanlevel callbacks
// Stub AutoLink is the autolink tag callback. // Stub AutoLink is the autolink tag callback.
func (r Renderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {} func (r renderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {}
// CodeSpan is the code span tag callback. Outputs a simple Markdown version // CodeSpan is the code span tag callback. Outputs a simple Markdown version
// of the code span. // of the code span.
func (r Renderer) CodeSpan(out *bytes.Buffer, text []byte) { func (r renderer) CodeSpan(out *bytes.Buffer, text []byte) {
out.Write([]byte("`")) out.Write([]byte("`"))
out.Write(text) out.Write(text)
out.Write([]byte("`")) out.Write([]byte("`"))
@ -91,62 +92,62 @@ func (r Renderer) CodeSpan(out *bytes.Buffer, text []byte) {
// DoubleEmphasis is the double emphasis tag callback. Outputs a simple // DoubleEmphasis is the double emphasis tag callback. Outputs a simple
// plain-text version of the input. // plain-text version of the input.
func (r Renderer) DoubleEmphasis(out *bytes.Buffer, text []byte) { func (r renderer) DoubleEmphasis(out *bytes.Buffer, text []byte) {
out.Write(text) out.Write(text)
} }
// Emphasis is the emphasis tag callback. Outputs a simple plain-text // Emphasis is the emphasis tag callback. Outputs a simple plain-text
// version of the input. // version of the input.
func (r Renderer) Emphasis(out *bytes.Buffer, text []byte) { func (r renderer) Emphasis(out *bytes.Buffer, text []byte) {
out.Write(text) out.Write(text)
} }
// Stub Image is the image tag callback. // Stub Image is the image tag callback.
func (r Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {} func (r renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {}
// Stub LineBreak is the line break tag callback. // Stub LineBreak is the line break tag callback.
func (r Renderer) LineBreak(out *bytes.Buffer) {} func (r renderer) LineBreak(out *bytes.Buffer) {}
// Link is the link tag callback. Outputs a sipmle plain-text version // Link is the link tag callback. Outputs a sipmle plain-text version
// of the input. // of the input.
func (r Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) { func (r renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
out.Write(content) out.Write(content)
} }
// Stub RawHtmlTag is the raw HTML tag callback. // Stub RawHtmlTag is the raw HTML tag callback.
func (r Renderer) RawHtmlTag(out *bytes.Buffer, tag []byte) {} func (r renderer) RawHtmlTag(out *bytes.Buffer, tag []byte) {}
// TripleEmphasis is the triple emphasis tag callback. Outputs a simple plain-text // TripleEmphasis is the triple emphasis tag callback. Outputs a simple plain-text
// version of the input. // version of the input.
func (r Renderer) TripleEmphasis(out *bytes.Buffer, text []byte) { func (r renderer) TripleEmphasis(out *bytes.Buffer, text []byte) {
out.Write(text) out.Write(text)
} }
// Stub StrikeThrough is the strikethrough tag callback. // Stub StrikeThrough is the strikethrough tag callback.
func (r Renderer) StrikeThrough(out *bytes.Buffer, text []byte) {} func (r renderer) StrikeThrough(out *bytes.Buffer, text []byte) {}
// Stub FootnoteRef is the footnote ref tag callback. // Stub FootnoteRef is the footnote ref tag callback.
func (r Renderer) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {} func (r renderer) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {}
// Lowlevel callbacks // Lowlevel callbacks
// Entity callback. Outputs a simple plain-text version of the input. // Entity callback. Outputs a simple plain-text version of the input.
func (r Renderer) Entity(out *bytes.Buffer, entity []byte) { func (r renderer) Entity(out *bytes.Buffer, entity []byte) {
out.Write(entity) out.Write(entity)
} }
// NormalText callback. Outputs a simple plain-text version of the input. // NormalText callback. Outputs a simple plain-text version of the input.
func (r Renderer) NormalText(out *bytes.Buffer, text []byte) { func (r renderer) NormalText(out *bytes.Buffer, text []byte) {
out.Write(text) out.Write(text)
} }
// Header and footer // Header and footer
// Stub DocumentHeader callback. // Stub DocumentHeader callback.
func (r Renderer) DocumentHeader(out *bytes.Buffer) {} func (r renderer) DocumentHeader(out *bytes.Buffer) {}
// Stub DocumentFooter callback. // Stub DocumentFooter callback.
func (r Renderer) DocumentFooter(out *bytes.Buffer) {} func (r renderer) DocumentFooter(out *bytes.Buffer) {}
// Stub GetFlags returns zero. // Stub GetFlags returns zero.
func (r Renderer) GetFlags() int { return 0 } func (r renderer) GetFlags() int { return 0 }

View File

@ -9,7 +9,7 @@ import (
// Markdown formats input using a plain-text renderer, and // Markdown formats input using a plain-text renderer, and
// then returns up to the first `wordcount` words as a summary. // then returns up to the first `wordcount` words as a summary.
func Markdown(input []byte, wordcount int) []byte { func Markdown(input []byte, wordcount int) []byte {
words := bytes.Fields(blackfriday.Markdown(input, Renderer{}, 0)) words := bytes.Fields(blackfriday.Markdown(input, renderer{}, 0))
if wordcount > len(words) { if wordcount > len(words) {
wordcount = len(words) wordcount = len(words)
} }