Skip to content

Commit

Permalink
allow upper-case letters + more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
a-mr committed Dec 10, 2020
1 parent d753acc commit 555af98
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 34 deletions.
58 changes: 50 additions & 8 deletions lib/packages/docutils/rst.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,38 @@
## subset is implemented. Some features of the `markdown`:idx: wiki syntax are
## also supported.
##
## Supported RST features:
##
## * body elements
## + sections
## + transitions
## + paragraphs
## + bullet lists using \+, \*, \-
## + enumerated lists using arabic numerals or alphabet
## characters: 1. ... 2. ... *or* a. ... b. ... *or* A. ... B. ...
## + definition lists
## + field lists
## + option lists
## + indented literal blocks
## + simple tables
## + directives
## - image, figure
## - code-block
## - substitution definitions: replace and image
## - ... a few more
## + comments
## * inline markup
## + *emphasis*, **strong emphasis**, `interpreted text`,
## ``inline literals``, hyperlink references, substitution references,
## standalone hyperlinks
##
## Additional features:
##
## * ***triple emphasis*** (bold and italic) using \*\*\*
##
## Optional additional features, turned on by ``options: RstParseOption`` in
## `rstParse proc <#rstParse,string,string,int,int,bool,RstParseOptions,FindFileHandler,MsgHandler>`_:
##
## * emoji / smiley symbols
## * markdown tables
## * markdown code blocks
Expand All @@ -21,10 +51,18 @@
##
## Limitations:
##
## * no footnotes & citations support
## * no grid tables
## * no roman numerals in enumerated lists
## * no simple-inline-markup
## * no Unicode support in character width calculations
## * body elements
## - no roman numerals in enumerated lists
## - no quoted literal blocks
## - no doctest blocks
## - no grid tables
## - directives: no support for admonitions (notes, caution)
## - no footnotes & citations support
## - no inline internal targets
## * inline markup
## - no simple-inline-markup
## - no embedded URI and aliases
##
## **Note:** Import ``packages/docutils/rst`` to use this module

Expand Down Expand Up @@ -1486,7 +1524,8 @@ proc parseDefinitionList(p: var RstParser): PRstNode =

proc parseEnumList(p: var RstParser): PRstNode =
const
wildcards: array[0..5, string] = ["(n)", "n) ", "n. ", "(x) ", "x) ", "x. "]
wildcards: array[0..5, string] = ["(n) ", "n) ", "n. ",
"(x) ", "x) ", "x. "]
# enumerator patterns, where 'x' means letter and 'n' means number
wildToken: array[0..5, int] = [4, 3, 3, 4, 3, 3] # number of tokens
wildIndex: array[0..5, int] = [1, 0, 0, 1, 0, 0]
Expand All @@ -1498,9 +1537,12 @@ proc parseEnumList(p: var RstParser): PRstNode =
if match(p, p.idx, wildcards[w]): break
inc w
assert w < wildcards.len
result.text = p.tok[p.idx + wildIndex[w]].symbol
if result.text == "#": result.text = "1"
var prevEnum = result.text
for i in 0 ..< wildToken[w]-1: # add first enumerator with (, ), and .
if p.tok[p.idx + i].symbol == "#":
result.text.add "1"
else:
result.text.add p.tok[p.idx + i].symbol
var prevEnum = p.tok[p.idx + wildIndex[w]].symbol
inc p.idx, wildToken[w]
while true:
var item = newRstNode(rnEnumItem)
Expand Down
77 changes: 51 additions & 26 deletions lib/packages/docutils/rstgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,56 @@ proc renderField(d: PDoc, n: PRstNode, result: var string) =
if not b:
renderAux(d, n, "<tr>$1</tr>\n", "$1", result)

proc renderEnumList(d: PDoc, n: PRstNode, result: var string) =
var
specifier = ""
specStart = ""
i1 = 0
pre = ""
i2 = n.text.len-1
post = ""
if n.text[0] == '(':
i1 = 1
pre = "("
if n.text[^1] == ')' or n.text[^1] == '.':
i2 = n.text.len-2
post = $n.text[^1]
let enumR = i1 .. i2 # enumerator range without surrounding (, ), .
if d.target == outLatex:
result.add ("\n%"&n.text&"\n")
# use enumerate parameters from package enumitem
if n.text[i1].isDigit:
var labelDef = ""
if pre != "" or post != "":
labelDef = "label=" & pre & "\\arabic*" & post & ","
if n.text[enumR] != "1":
specStart = "start=$1" % [n.text[enumR]]
if labelDef != "" or specStart != "":
specifier = "[$1$2]" % [labelDef, specStart]
else:
let (first, labelDef) =
if n.text[i1].isUpperAscii: ('A', "label=" & pre & "\\Alph*" & post)
else: ('a', "label=" & pre & "\\alph*" & post)
if n.text[i1] != first:
specStart = ",start=" & $(ord(n.text[i1]) - ord(first) + 1)
specifier = "[$1$2]" % [labelDef, specStart]
else: # HTML
# TODO: implement enumerator formatting using pre and post ( and ) for HTML
if n.text[i1].isDigit:
if n.text[enumR] != "1":
specStart = " start=\"$1\"" % [n.text[enumR]]
specifier = "class=\"simple\"" & specStart
else:
let (first, labelDef) =
if n.text[i1].isUpperAscii: ('A', "class=\"upperalpha simple\"")
else: ('a', "class=\"loweralpha simple\"")
if n.text[i1] != first:
specStart = " start=\"$1\"" % [ $(ord(n.text[i1]) - ord(first) + 1) ]
specifier = labelDef & specStart
renderAux(d, n, "<ol " & specifier & ">$1</ol>\n",
"\\begin{enumerate}" & specifier & "$1\\end{enumerate}\n",
result)

proc renderRstToOut(d: PDoc, n: PRstNode, result: var string) =
if n == nil: return
case n.kind
Expand All @@ -1042,32 +1092,7 @@ proc renderRstToOut(d: PDoc, n: PRstNode, result: var string) =
"\\begin{itemize}$1\\end{itemize}\n", result)
of rnBulletItem, rnEnumItem:
renderAux(d, n, "<li>$1</li>\n", "\\item $1\n", result)
of rnEnumList:
var
specifier = ""
specStart = ""
if d.target == outLatex:
# use enumerate parameters from package enumitem
if n.text[0].isDigit:
if n.text != "1":
specStart = "[start=$1]" % [n.text]
specifier = specStart
else:
if n.text != "a":
specStart = ",start=" & $(ord(n.text[0]) - ord('a') + 1)
specifier = "[label=(\alph*)$1]" % [specStart]
else:
if n.text[0].isDigit:
if n.text != "1":
specStart = " start=\"$1\"" % [n.text]
specifier = "class=\"simple\"" & specStart
else:
if n.text != "a":
specStart = " start=\"$1\"" % [ $(ord(n.text[0]) - ord('a') + 1) ]
specifier = "class=\"loweralpha simple\"" & specStart
renderAux(d, n, "<ol " & specifier & ">$1</ol>\n",
"\\begin{enumerate}" & specifier & "$1\\end{enumerate}\n",
result)
of rnEnumList: renderEnumList(d, n, result)
of rnDefList:
renderAux(d, n, "<dl class=\"docutils\">$1</dl>\n",
"\\begin{description}$1\\end{description}\n", result)
Expand Down
13 changes: 13 additions & 0 deletions tests/stdlib/trstgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,19 @@ Test1
assert count(output6, "<li>") == 3
assert "start=\"2\"" in output6 and "class=\"loweralpha simple\"" in output6

let input7 = dedent """
... And for uppercase alphabetic enumerators.
C. string1
#. string2
#. string3
"""
let output7 = rstToHtml(input7, {roSupportMarkdown}, defaultConfig())
assert count(output7, "<ol ") == 1
assert count(output7, "</ol>") == 1
assert count(output7, "<li>") == 3
assert "start=\"3\"" in output7 and "class=\"upperalpha simple\"" in output7

test "RST bullet lists":
let input1 = dedent """
* line1
Expand Down

0 comments on commit 555af98

Please sign in to comment.