mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 22:10:33 +00:00
Highlight Nim default in Markdown code in .nim (#20110)
Highlight Nim by default in Markdown code in .nim
This commit is contained in:
@@ -1688,6 +1688,18 @@ proc mayLoadFile(p: RstParser, result: var PRstNode) =
|
||||
n.add newLeaf(readFile(path))
|
||||
result.sons[2] = n
|
||||
|
||||
proc defaultCodeLangNim(p: RstParser, result: var PRstNode) =
|
||||
# Create a field block if the input block didn't have any.
|
||||
if result.sons[1].isNil: result.sons[1] = newRstNode(rnFieldList)
|
||||
assert result.sons[1].kind == rnFieldList
|
||||
# Hook the extra field and specify the Nim language as value.
|
||||
var extraNode = newRstNode(rnField, info=lineInfo(p))
|
||||
extraNode.add(newRstNode(rnFieldName))
|
||||
extraNode.add(newRstNode(rnFieldBody))
|
||||
extraNode.sons[0].add newLeaf("default-language")
|
||||
extraNode.sons[1].add newLeaf("Nim")
|
||||
result.sons[1].add(extraNode)
|
||||
|
||||
proc parseMarkdownCodeblock(p: var RstParser): PRstNode =
|
||||
result = newRstNodeA(p, rnCodeBlock)
|
||||
result.sons.setLen(3)
|
||||
@@ -1731,6 +1743,8 @@ proc parseMarkdownCodeblock(p: var RstParser): PRstNode =
|
||||
var lb = newRstNode(rnLiteralBlock)
|
||||
lb.add(n)
|
||||
result.sons[2] = lb
|
||||
if result.sons[0].isNil and roNimFile in p.s.options:
|
||||
defaultCodeLangNim(p, result)
|
||||
|
||||
proc parseMarkdownLink(p: var RstParser; father: PRstNode): bool =
|
||||
var desc, link = ""
|
||||
@@ -3227,16 +3241,7 @@ proc dirCodeBlock(p: var RstParser, nimExtension = false): PRstNode =
|
||||
|
||||
# Extend the field block if we are using our custom Nim extension.
|
||||
if nimExtension:
|
||||
# Create a field block if the input block didn't have any.
|
||||
if result.sons[1].isNil: result.sons[1] = newRstNode(rnFieldList)
|
||||
assert result.sons[1].kind == rnFieldList
|
||||
# Hook the extra field and specify the Nim language as value.
|
||||
var extraNode = newRstNode(rnField, info=lineInfo(p))
|
||||
extraNode.add(newRstNode(rnFieldName))
|
||||
extraNode.add(newRstNode(rnFieldBody))
|
||||
extraNode.sons[0].add newLeaf("default-language")
|
||||
extraNode.sons[1].add newLeaf("Nim")
|
||||
result.sons[1].add(extraNode)
|
||||
defaultCodeLangNim(p, result)
|
||||
|
||||
proc dirContainer(p: var RstParser): PRstNode =
|
||||
result = parseDirective(p, rnContainer, {hasArg}, parseSectionWrapper)
|
||||
|
||||
@@ -1074,7 +1074,7 @@ proc renderCode(d: PDoc, n: PRstNode, result: var string) =
|
||||
blockEnd = "}"
|
||||
dispA(d.target, result, blockStart, blockStart, [])
|
||||
if params.lang == langNone:
|
||||
if len(params.langStr) > 0:
|
||||
if len(params.langStr) > 0 and params.langStr.toLowerAscii != "none":
|
||||
rstMessage(d.filenames, d.msgHandler, n.info, mwUnsupportedLanguage,
|
||||
params.langStr)
|
||||
for letter in m.text: escChar(d.target, result, letter, emText)
|
||||
|
||||
@@ -474,7 +474,12 @@ suite "RST parsing":
|
||||
let expectCodeBlock = dedent"""
|
||||
rnCodeBlock
|
||||
[nil]
|
||||
[nil]
|
||||
rnFieldList
|
||||
rnField
|
||||
rnFieldName
|
||||
rnLeaf 'default-language'
|
||||
rnFieldBody
|
||||
rnLeaf 'Nim'
|
||||
rnLiteralBlock
|
||||
rnLeaf '
|
||||
let a = 1
|
||||
|
||||
@@ -582,9 +582,14 @@ Test literal block
|
||||
```
|
||||
let x = 1
|
||||
``` """
|
||||
let output1 = input1.toHtml
|
||||
let output1 = input1.toHtml({roSupportMarkdown, roPreferMarkdown})
|
||||
doAssert "<pre" in output1 and "class=\"Keyword\"" notin output1
|
||||
|
||||
# Check Nim highlighting by default in .nim files:
|
||||
let output1nim = input1.toHtml({roSupportMarkdown, roPreferMarkdown,
|
||||
roNimFile})
|
||||
doAssert "<pre" in output1nim and "class=\"Keyword\"" in output1nim
|
||||
|
||||
let input2 = """
|
||||
Parse the block with language specifier:
|
||||
```Nim
|
||||
|
||||
Reference in New Issue
Block a user