Merge 724433aa05
into 5f33e7b787
commit
55174bf651
8
html.go
8
html.go
|
@ -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>")
|
||||
}
|
||||
|
|
40
inline.go
40
inline.go
|
@ -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
|
||||
|
|
|
@ -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) {
|
||||
|
|
2
latex.go
2
latex.go
|
@ -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("}")
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue