validate rst field for :number-lines:, :status: (#18304)
This commit is contained in:
parent
7d5e6b0169
commit
1b3c0f142d
5 changed files with 38 additions and 8 deletions
|
|
@ -164,6 +164,7 @@ template declareClosures =
|
||||||
of meNewSectionExpected: k = errNewSectionExpected
|
of meNewSectionExpected: k = errNewSectionExpected
|
||||||
of meGeneralParseError: k = errGeneralParseError
|
of meGeneralParseError: k = errGeneralParseError
|
||||||
of meInvalidDirective: k = errInvalidDirectiveX
|
of meInvalidDirective: k = errInvalidDirectiveX
|
||||||
|
of meInvalidRstField: k = errInvalidRstField
|
||||||
of meFootnoteMismatch: k = errFootnoteMismatch
|
of meFootnoteMismatch: k = errFootnoteMismatch
|
||||||
of mwRedefinitionOfLabel: k = warnRedefinitionOfLabel
|
of mwRedefinitionOfLabel: k = warnRedefinitionOfLabel
|
||||||
of mwUnknownSubstitution: k = warnUnknownSubstitutionX
|
of mwUnknownSubstitution: k = warnUnknownSubstitutionX
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ type
|
||||||
errGeneralParseError,
|
errGeneralParseError,
|
||||||
errNewSectionExpected,
|
errNewSectionExpected,
|
||||||
errInvalidDirectiveX,
|
errInvalidDirectiveX,
|
||||||
|
errInvalidRstField,
|
||||||
errFootnoteMismatch,
|
errFootnoteMismatch,
|
||||||
errProveInit, # deadcode
|
errProveInit, # deadcode
|
||||||
errGenerated,
|
errGenerated,
|
||||||
|
|
@ -96,6 +97,7 @@ const
|
||||||
errGeneralParseError: "general parse error",
|
errGeneralParseError: "general parse error",
|
||||||
errNewSectionExpected: "new section expected $1",
|
errNewSectionExpected: "new section expected $1",
|
||||||
errInvalidDirectiveX: "invalid directive: '$1'",
|
errInvalidDirectiveX: "invalid directive: '$1'",
|
||||||
|
errInvalidRstField: "invalid field: $1",
|
||||||
errFootnoteMismatch: "number of footnotes and their references don't match: $1",
|
errFootnoteMismatch: "number of footnotes and their references don't match: $1",
|
||||||
errProveInit: "Cannot prove that '$1' is initialized.", # deadcode
|
errProveInit: "Cannot prove that '$1' is initialized.", # deadcode
|
||||||
errGenerated: "$1",
|
errGenerated: "$1",
|
||||||
|
|
|
||||||
|
|
@ -225,6 +225,7 @@ type
|
||||||
meNewSectionExpected = "new section expected $1",
|
meNewSectionExpected = "new section expected $1",
|
||||||
meGeneralParseError = "general parse error",
|
meGeneralParseError = "general parse error",
|
||||||
meInvalidDirective = "invalid directive: '$1'",
|
meInvalidDirective = "invalid directive: '$1'",
|
||||||
|
meInvalidRstField = "invalid field: $1",
|
||||||
meFootnoteMismatch = "mismatch in number of footnotes and their refs: $1",
|
meFootnoteMismatch = "mismatch in number of footnotes and their refs: $1",
|
||||||
mwRedefinitionOfLabel = "redefinition of label '$1'",
|
mwRedefinitionOfLabel = "redefinition of label '$1'",
|
||||||
mwUnknownSubstitution = "unknown substitution '$1'",
|
mwUnknownSubstitution = "unknown substitution '$1'",
|
||||||
|
|
|
||||||
|
|
@ -907,6 +907,25 @@ proc renderSmiley(d: PDoc, n: PRstNode, result: var string) =
|
||||||
"\\includegraphics{$1}",
|
"\\includegraphics{$1}",
|
||||||
[d.config.getOrDefault"doc.smiley_format" % n.text])
|
[d.config.getOrDefault"doc.smiley_format" % n.text])
|
||||||
|
|
||||||
|
proc getField1Int(d: PDoc, n: PRstNode, fieldName: string): int =
|
||||||
|
# TODO: proper column/line info
|
||||||
|
template err(msg: string) =
|
||||||
|
d.msgHandler(d.filename, 1, 0, meInvalidRstField, msg)
|
||||||
|
let value = n.getFieldValue
|
||||||
|
var number: int
|
||||||
|
let nChars = parseInt(value, number)
|
||||||
|
if nChars == 0:
|
||||||
|
if value.len == 0:
|
||||||
|
err("field $1 requires an argument" % [fieldName])
|
||||||
|
else:
|
||||||
|
err("field $1 requires an integer, but '$2' was given" %
|
||||||
|
[fieldName, value])
|
||||||
|
elif nChars < value.len:
|
||||||
|
err("extra arguments were given to $1: '$2'" %
|
||||||
|
[fieldName, value[nChars..^1]])
|
||||||
|
else:
|
||||||
|
result = number
|
||||||
|
|
||||||
proc parseCodeBlockField(d: PDoc, n: PRstNode, params: var CodeBlockParams) =
|
proc parseCodeBlockField(d: PDoc, n: PRstNode, params: var CodeBlockParams) =
|
||||||
## Parses useful fields which can appear before a code block.
|
## Parses useful fields which can appear before a code block.
|
||||||
##
|
##
|
||||||
|
|
@ -916,9 +935,7 @@ proc parseCodeBlockField(d: PDoc, n: PRstNode, params: var CodeBlockParams) =
|
||||||
of "number-lines":
|
of "number-lines":
|
||||||
params.numberLines = true
|
params.numberLines = true
|
||||||
# See if the field has a parameter specifying a different line than 1.
|
# See if the field has a parameter specifying a different line than 1.
|
||||||
var number: int
|
params.startLine = getField1Int(d, n, "number-lines")
|
||||||
if parseInt(n.getFieldValue, number) > 0:
|
|
||||||
params.startLine = number
|
|
||||||
of "file", "filename":
|
of "file", "filename":
|
||||||
# The ``file`` option is a Nim extension to the official spec, it acts
|
# The ``file`` option is a Nim extension to the official spec, it acts
|
||||||
# like it would for other directives like ``raw`` or ``cvs-table``. This
|
# like it would for other directives like ``raw`` or ``cvs-table``. This
|
||||||
|
|
@ -936,9 +953,7 @@ proc parseCodeBlockField(d: PDoc, n: PRstNode, params: var CodeBlockParams) =
|
||||||
# consider whether `$docCmd` should be appended here too
|
# consider whether `$docCmd` should be appended here too
|
||||||
params.testCmd = unescape(params.testCmd)
|
params.testCmd = unescape(params.testCmd)
|
||||||
of "status", "exitcode":
|
of "status", "exitcode":
|
||||||
var status: int
|
params.status = getField1Int(d, n, n.getArgument)
|
||||||
if parseInt(n.getFieldValue, status) > 0:
|
|
||||||
params.status = status
|
|
||||||
of "default-language":
|
of "default-language":
|
||||||
params.langStr = n.getFieldValue.strip
|
params.langStr = n.getFieldValue.strip
|
||||||
params.lang = params.langStr.getSourceLanguage
|
params.lang = params.langStr.getSourceLanguage
|
||||||
|
|
@ -954,7 +969,6 @@ proc parseCodeBlockParams(d: PDoc, n: PRstNode): CodeBlockParams =
|
||||||
if n.isNil:
|
if n.isNil:
|
||||||
return
|
return
|
||||||
assert n.kind in {rnCodeBlock, rnInlineCode}
|
assert n.kind in {rnCodeBlock, rnInlineCode}
|
||||||
assert(not n.sons[2].isNil)
|
|
||||||
|
|
||||||
# Parse the field list for rendering parameters if there are any.
|
# Parse the field list for rendering parameters if there are any.
|
||||||
if not n.sons[1].isNil:
|
if not n.sons[1].isNil:
|
||||||
|
|
@ -1028,8 +1042,8 @@ proc renderCode(d: PDoc, n: PRstNode, result: var string) =
|
||||||
## option to differentiate between a plain code block and Nim's code block
|
## option to differentiate between a plain code block and Nim's code block
|
||||||
## extension.
|
## extension.
|
||||||
assert n.kind in {rnCodeBlock, rnInlineCode}
|
assert n.kind in {rnCodeBlock, rnInlineCode}
|
||||||
if n.sons[2] == nil: return
|
|
||||||
var params = d.parseCodeBlockParams(n)
|
var params = d.parseCodeBlockParams(n)
|
||||||
|
if n.sons[2] == nil: return
|
||||||
var m = n.sons[2].sons[0]
|
var m = n.sons[2].sons[0]
|
||||||
assert m.kind == rnLeaf
|
assert m.kind == rnLeaf
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1136,6 +1136,18 @@ Test1
|
||||||
check "<pre class=\"line-nums\">55\n</pre>" in output
|
check "<pre class=\"line-nums\">55\n</pre>" in output
|
||||||
check "<span class=\"Identifier\">x</span>" in output
|
check "<span class=\"Identifier\">x</span>" in output
|
||||||
|
|
||||||
|
test "Nim code-block indentation":
|
||||||
|
let input = dedent """
|
||||||
|
.. code-block:: nim
|
||||||
|
:number-lines: 55
|
||||||
|
let a = 1
|
||||||
|
"""
|
||||||
|
var error = new string
|
||||||
|
let output = input.toHtml(error=error)
|
||||||
|
check(error[] == "input(1, 1) Error: invalid field: " &
|
||||||
|
"extra arguments were given to number-lines: ' let a = 1'")
|
||||||
|
check "" == output
|
||||||
|
|
||||||
test "RST admonitions":
|
test "RST admonitions":
|
||||||
# check that all admonitions are implemented
|
# check that all admonitions are implemented
|
||||||
let input0 = dedent """
|
let input0 = dedent """
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue