~poptart/x

29a20e3e3c5ac83b9390df661683285a56a18dc9 — terrorbyte 9 months ago 5d0ee4e
Updated renderer to accept codeblock hints over analysis mode
1 files changed, 33 insertions(+), 6 deletions(-)

M md/html/renderer.go
M md/html/renderer.go => md/html/renderer.go +33 -6
@@ 640,6 640,7 @@ func (r *Renderer) headingEnter(w io.Writer, nodeData *ast.Heading) {
		attrID := `id="` + id + `"`
		attrs = append(attrs, attrID)
	}

	attrs = append(attrs, BlockAttrs(nodeData)...)
	r.cr(w)
	r.outTag(w, headingOpenTagFromLevel(nodeData.Level), attrs)


@@ 652,6 653,26 @@ func (r *Renderer) headingExit(w io.Writer, heading *ast.Heading) {
	}
}

func (r *Renderer) metaHeader(w io.Writer, heading *ast.Heading) {
	metaMD := bytes.Split(heading.Literal, []byte(" "))
	if len(metaMD) < 1 {
		return
	}
	metaType := bytes.Split(metaMD[0], []byte("META:"))[1]
	switch string(metaType) {
	case "style":
		io.WriteString(w, "<link rel='stylesheet' href='"+string(metaMD[1][:])+"'>")
		r.cr(w)
	case "title":
		io.WriteString(w, "<title>"+string(metaMD[1][:])+"</title>")
	case "description":
		io.WriteString(w, "<link rel='stylesheet' href='"+string(metaMD[1][:])+"'>")
	default:
		return
	}

}

func (r *Renderer) heading(w io.Writer, node *ast.Heading, entering bool) {
	if entering {
		r.headingEnter(w, node)


@@ 792,19 813,25 @@ func (r *Renderer) codeBlock(w io.Writer, codeBlock *ast.CodeBlock) {
	attrs = appendLanguageAttr(attrs, codeBlock.Info)
	attrs = append(attrs, BlockAttrs(codeBlock)...)
	r.cr(w)

	r.outs(w, "<pre class='chroma'><span class='cp'>")
	//code := tagWithAttributes("<code", attrs)
	//r.outs(w, code)
	if r.opts.Comments != nil {
		r.EscapeHTMLCallouts(w, codeBlock.Literal)
	} else {
		options := []chromaHtml.Option{
			chromaHtml.Standalone(false),
		}
		lexer := lexers.Analyse(string(codeBlock.Literal))
		if lexer == nil {
			lexer = lexers.Fallback
		//Get the codeblock hint and if it succeeds mark that as the lexer. If not attempt to guess and fallback
		lexer := lexers.Fallback
		if len(codeBlock.Info) != 0 {
			lexer = lexers.Get(string(codeBlock.Info))
			if lexer == nil {
				lexer = lexers.Fallback
			}
		} else {
			lexer = lexers.Analyse(string(codeBlock.Literal))
			if lexer == nil {
				lexer = lexers.Fallback
			}
		}
		lexer = chroma.Coalesce(lexer)
		style := styles.Get("monokai")