RST: improve line blocks (#16518)

This commit is contained in:
Andrey Makarov
2020-12-31 13:20:04 +03:00
committed by GitHub
parent 17992fca1d
commit 5984f7a7dd
5 changed files with 68 additions and 17 deletions

View File

@@ -1147,7 +1147,8 @@ proc isAdornmentHeadline(p: RstParser, adornmentIdx: int): bool =
proc isLineBlock(p: RstParser): bool =
var j = tokenAfterNewline(p)
result = currentTok(p).col == p.tok[j].col and p.tok[j].symbol == "|" or
p.tok[j].col > currentTok(p).col
p.tok[j].col > currentTok(p).col or
p.tok[j].symbol == "\n"
proc predNL(p: RstParser): bool =
result = true
@@ -1245,21 +1246,28 @@ proc whichSection(p: RstParser): RstNodeKind =
proc parseLineBlock(p: var RstParser): PRstNode =
result = nil
if nextTok(p).kind == tkWhite:
if nextTok(p).kind in {tkWhite, tkIndent}:
var col = currentTok(p).col
result = newRstNode(rnLineBlock)
pushInd(p, p.tok[p.idx + 2].col)
inc p.idx, 2
while true:
var item = newRstNode(rnLineBlockItem)
parseSection(p, item)
if nextTok(p).kind == tkWhite:
if nextTok(p).symbol.len > 1: # pass additional indentation after '| '
item.text = nextTok(p).symbol
inc p.idx, 2
pushInd(p, p.tok[p.idx].col)
parseSection(p, item)
popInd(p)
else: # tkIndent => add an empty line
item.text = "\n"
inc p.idx, 1
result.add(item)
if currentTok(p).kind == tkIndent and currentTok(p).ival == col and
nextTok(p).symbol == "|" and p.tok[p.idx + 2].kind == tkWhite:
inc p.idx, 3
nextTok(p).symbol == "|" and
p.tok[p.idx + 2].kind in {tkWhite, tkIndent}:
inc p.idx, 1
else:
break
popInd(p)
proc parseParagraph(p: var RstParser, result: PRstNode) =
while true:

View File

@@ -35,7 +35,8 @@ type
rnOptionList, rnOptionListItem, rnOptionGroup, rnOption, rnOptionString,
rnOptionArgument, rnDescription, rnLiteralBlock, rnQuotedLiteralBlock,
rnLineBlock, # the | thingie
rnLineBlockItem, # sons of the | thing
rnLineBlockItem, # a son of rnLineBlock - one line inside it.
# When `RstNode` text="\n" the line's empty
rnBlockQuote, # text just indented
rnTable, rnGridTable, rnMarkdownTable, rnTableRow, rnTableHeaderCell, rnTableDataCell,
rnLabel, # used for footnotes and other things
@@ -73,7 +74,7 @@ type
kind*: RstNodeKind ## the node's kind
text*: string ## valid for leafs in the AST; and the title of
## the document or the section; and rnEnumList
## and rnAdmonition
## and rnAdmonition; and rnLineBlockItem
level*: int ## valid for some node kinds
sons*: RstNodeSeq ## the node's sons

View File

@@ -1160,9 +1160,21 @@ proc renderRstToOut(d: PDoc, n: PRstNode, result: var string) =
of rnQuotedLiteralBlock:
doAssert false, "renderRstToOut"
of rnLineBlock:
renderAux(d, n, "<p>$1</p>", "$1\n\n", result)
if n.sons.len == 1 and n.sons[0].text == "\n":
# whole line block is one empty line, no need to add extra spacing
renderAux(d, n, "<p>$1</p> ", "\n\n$1", result)
else: # add extra spacing around the line block for Latex
renderAux(d, n, "<p>$1</p>", "\n\\vspace{0.5em}\n$1\\vspace{0.5em}\n", result)
of rnLineBlockItem:
renderAux(d, n, "$1<br />", "$1\\\\\n", result)
if n.text.len == 0: # normal case - no additional indentation
renderAux(d, n, "$1<br/>", "\\noindent $1\n\n", result)
elif n.text == "\n": # add one empty line
renderAux(d, n, "<br/>", "\\vspace{1em}\n", result)
else: # additional indentation w.r.t. '| '
let indent = $(0.5 * (n.text.len - 1).toFloat) & "em"
renderAux(d, n,
"<span style=\"margin-left: " & indent & "\">$1</span><br/>",
"\\noindent\\hspace{" & indent & "}$1\n\n", result)
of rnBlockQuote:
renderAux(d, n, "<blockquote><p>$1</p></blockquote>\n",
"\\begin{quote}$1\\end{quote}\n", result)