pull/262/merge
emilkm 2016-10-04 04:03:06 +00:00 committed by GitHub
commit 55174bf651
5 changed files with 67 additions and 9 deletions

View File

@ -482,8 +482,12 @@ func (options *Html) AutoLink(out *bytes.Buffer, link []byte, kind int) {
out.WriteString("</a>")
}
func (options *Html) CodeSpan(out *bytes.Buffer, text []byte) {
out.WriteString("<code>")
func (options *Html) CodeSpan(out *bytes.Buffer, text []byte, lang string) {
if lang != "" {
out.WriteString("<code class=\"language-" + lang + "\">")
} else {
out.WriteString("<code>")
}
attrEscape(out, text)
out.WriteString("</code>")
}

View File

@ -111,17 +111,17 @@ func emphasis(p *parser, out *bytes.Buffer, data []byte, offset int) int {
func codeSpan(p *parser, out *bytes.Buffer, data []byte, offset int) int {
data = data[offset:]
dataLen := len(data)
nb := 0
// count the number of backticks in the delimiter
for nb < len(data) && data[nb] == '`' {
for nb < dataLen && data[nb] == '`' {
nb++
}
// find the next delimiter
i, end := 0, 0
for end = nb; end < len(data) && i < nb; end++ {
for end = nb; end < dataLen && i < nb; end++ {
if data[end] == '`' {
i++
} else {
@ -130,7 +130,7 @@ func codeSpan(p *parser, out *bytes.Buffer, data []byte, offset int) int {
}
// no matching delimiter?
if i < nb && end >= len(data) {
if i < nb && end >= dataLen {
return 0
}
@ -147,7 +147,37 @@ func codeSpan(p *parser, out *bytes.Buffer, data []byte, offset int) int {
// render the code span
if fBegin != fEnd {
p.r.CodeSpan(out, data[fBegin:fEnd])
langStart := 0
langEnd := 0
lang := ""
if p.flags&EXTENSION_INLINE_CODE_LANG != 0 {
// look for (lang) following the closing delimiter
idx := fEnd
for idx < dataLen-2 {
idx++
if data[idx] == '(' {
langStart = idx + 1
break
} else if data[idx] != ' ' {
break
}
}
if langStart > 0 {
for idx < dataLen && data[idx] != ')' {
idx++
}
if idx < dataLen {
langEnd = idx
end = idx + 1
}
}
}
if langStart < langEnd {
lang = string(data[langStart:langEnd])
}
p.r.CodeSpan(out, data[fBegin:fEnd], lang)
}
return end

View File

@ -407,8 +407,31 @@ func TestCodeSpan(t *testing.T) {
"```multiple ticks `with` ticks inside```\n",
"<p><code>multiple ticks `with` ticks inside</code></p>\n",
"`source code followed by lang`(go)\n",
"<p><code>source code followed by lang</code>(go)</p>\n",
"`source code followed by lang with spaces between` (go)\n",
"<p><code>source code followed by lang with spaces between</code> (go)</p>\n",
"`source code followed by lang`_(go)\n",
"<p><code>source code followed by lang</code>_(go)</p>\n",
}
doTestsInline(t, tests)
tests = []string{
"`source code followed by lang`(go)\n",
"<p><code class=\"language-go\">source code followed by lang</code></p>\n",
"`source code followed by lang with spaces between` (go)\n",
"<p><code class=\"language-go\">source code followed by lang with spaces between</code></p>\n",
"`source code followed by lang`_(go)\n",
"<p><code>source code followed by lang</code>_(go)</p>\n",
}
doTestsInlineParam(t, tests, Options{
Extensions: EXTENSION_INLINE_CODE_LANG},
0, HtmlRendererParameters{})
}
func TestLineBreak(t *testing.T) {

View File

@ -193,7 +193,7 @@ func (options *Latex) AutoLink(out *bytes.Buffer, link []byte, kind int) {
out.WriteString("}")
}
func (options *Latex) CodeSpan(out *bytes.Buffer, text []byte) {
func (options *Latex) CodeSpan(out *bytes.Buffer, text []byte, lang string) {
out.WriteString("\\texttt{")
escapeSpecialChars(out, text)
out.WriteString("}")

View File

@ -46,6 +46,7 @@ const (
EXTENSION_AUTO_HEADER_IDS // Create the header ID from the text
EXTENSION_BACKSLASH_LINE_BREAK // translate trailing backslashes into line breaks
EXTENSION_DEFINITION_LISTS // render definition lists
EXTENSION_INLINE_CODE_LANG // render inline code with language
commonHtmlFlags = 0 |
HTML_USE_XHTML |
@ -179,7 +180,7 @@ type Renderer interface {
// Span-level callbacks
AutoLink(out *bytes.Buffer, link []byte, kind int)
CodeSpan(out *bytes.Buffer, text []byte)
CodeSpan(out *bytes.Buffer, text []byte, lang string)
DoubleEmphasis(out *bytes.Buffer, text []byte)
Emphasis(out *bytes.Buffer, text []byte)
Image(out *bytes.Buffer, link []byte, title []byte, alt []byte)