Highlight Nim default in Markdown code in .nim (#20110)
Highlight Nim by default in Markdown code in .nim
This commit is contained in:
parent
99dd588d6b
commit
40e0048a50
4 changed files with 28 additions and 13 deletions
|
|
@ -1688,6 +1688,18 @@ proc mayLoadFile(p: RstParser, result: var PRstNode) =
|
||||||
n.add newLeaf(readFile(path))
|
n.add newLeaf(readFile(path))
|
||||||
result.sons[2] = n
|
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 =
|
proc parseMarkdownCodeblock(p: var RstParser): PRstNode =
|
||||||
result = newRstNodeA(p, rnCodeBlock)
|
result = newRstNodeA(p, rnCodeBlock)
|
||||||
result.sons.setLen(3)
|
result.sons.setLen(3)
|
||||||
|
|
@ -1731,6 +1743,8 @@ proc parseMarkdownCodeblock(p: var RstParser): PRstNode =
|
||||||
var lb = newRstNode(rnLiteralBlock)
|
var lb = newRstNode(rnLiteralBlock)
|
||||||
lb.add(n)
|
lb.add(n)
|
||||||
result.sons[2] = lb
|
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 =
|
proc parseMarkdownLink(p: var RstParser; father: PRstNode): bool =
|
||||||
var desc, link = ""
|
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.
|
# Extend the field block if we are using our custom Nim extension.
|
||||||
if nimExtension:
|
if nimExtension:
|
||||||
# Create a field block if the input block didn't have any.
|
defaultCodeLangNim(p, result)
|
||||||
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 dirContainer(p: var RstParser): PRstNode =
|
proc dirContainer(p: var RstParser): PRstNode =
|
||||||
result = parseDirective(p, rnContainer, {hasArg}, parseSectionWrapper)
|
result = parseDirective(p, rnContainer, {hasArg}, parseSectionWrapper)
|
||||||
|
|
|
||||||
|
|
@ -1074,7 +1074,7 @@ proc renderCode(d: PDoc, n: PRstNode, result: var string) =
|
||||||
blockEnd = "}"
|
blockEnd = "}"
|
||||||
dispA(d.target, result, blockStart, blockStart, [])
|
dispA(d.target, result, blockStart, blockStart, [])
|
||||||
if params.lang == langNone:
|
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,
|
rstMessage(d.filenames, d.msgHandler, n.info, mwUnsupportedLanguage,
|
||||||
params.langStr)
|
params.langStr)
|
||||||
for letter in m.text: escChar(d.target, result, letter, emText)
|
for letter in m.text: escChar(d.target, result, letter, emText)
|
||||||
|
|
|
||||||
|
|
@ -474,7 +474,12 @@ suite "RST parsing":
|
||||||
let expectCodeBlock = dedent"""
|
let expectCodeBlock = dedent"""
|
||||||
rnCodeBlock
|
rnCodeBlock
|
||||||
[nil]
|
[nil]
|
||||||
[nil]
|
rnFieldList
|
||||||
|
rnField
|
||||||
|
rnFieldName
|
||||||
|
rnLeaf 'default-language'
|
||||||
|
rnFieldBody
|
||||||
|
rnLeaf 'Nim'
|
||||||
rnLiteralBlock
|
rnLiteralBlock
|
||||||
rnLeaf '
|
rnLeaf '
|
||||||
let a = 1
|
let a = 1
|
||||||
|
|
|
||||||
|
|
@ -582,9 +582,14 @@ Test literal block
|
||||||
```
|
```
|
||||||
let x = 1
|
let x = 1
|
||||||
``` """
|
``` """
|
||||||
let output1 = input1.toHtml
|
let output1 = input1.toHtml({roSupportMarkdown, roPreferMarkdown})
|
||||||
doAssert "<pre" in output1 and "class=\"Keyword\"" notin output1
|
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 = """
|
let input2 = """
|
||||||
Parse the block with language specifier:
|
Parse the block with language specifier:
|
||||||
```Nim
|
```Nim
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue