Implemented quotes.

This commit is contained in:
Dominik Picheta 2016-01-30 15:05:28 +00:00
commit f6f4e5c888
2 changed files with 66 additions and 1 deletions

View file

@ -9,7 +9,7 @@
import
os, strutils, times, md5, strtabs, cgi, math, db_sqlite, matchers,
rst, rstgen, captchas, scgi, jester, asyncdispatch, asyncnet, cache, sequtils,
parseutils, utils
parseutils, utils, htmlparser, xmltree, streams
when not defined(windows):
import bcrypt # TODO
@ -419,9 +419,69 @@ proc isPreview(c: TForumData): bool =
proc isDelete(c: TForumData): bool =
result = c.req.params.hasKey("delete")
proc processGT(n: XmlNode, tag: string): (int, XmlNode, string) =
result = (0, newElement(tag), tag)
if n.kind == xnElement and len(n) == 1 and n[0].kind == xnElement:
return processGT(n[0], if n[0].kind == xnElement: n[0].tag else: tag)
var countGT = true
for c in items(n):
case c.kind
of xnText:
if c.text == ">" and countGT:
result[0].inc()
else:
countGT = false
result[1].add(newText(c.text))
else:
result[1].add(c)
proc blockquoteFinish(currentBlockquote, newNode: var XmlNode, n: XmlNode) =
if currentBlockquote.len > 0:
#echo(currentBlockquote.repr)
newNode.add(currentBlockquote)
currentBlockquote = newElement("blockquote")
newNode.add(n)
proc rstToHtml(content: string): string =
result = rstgen.rstToHtml(content, {roSupportSmilies, roSupportMarkdown},
docConfig)
# Bolt on quotes.
# TODO: Yes, this is ugly. I wrote it quickly. PRs welcome ;)
try:
var node = parseHtml(newStringStream(result))
var newNode = newElement("document")
if node.kind == xnElement:
var currentBlockquote = newElement("blockquote")
for n in items(node):
case n.kind
of xnElement:
case n.tag
of "p":
let (nesting, contentNode, tag) = processGT(n, "p")
if nesting > 0:
var bq = currentBlockquote
for i in 1 .. <nesting:
var newBq = bq.child("blockquote")
if newBq.isNil:
newBq = newElement("blockquote")
bq.add(newBq)
bq = newBq
bq.insert(contentNode, if bq.len == 0: 0 else: bq.len)
else:
blockquoteFinish(currentBlockquote, newNode, n)
else:
blockquoteFinish(currentBlockquote, newNode, n)
of xnText:
if n.text[0] == '\10':
newNode.add(n)
else:
blockquoteFinish(currentBlockquote, newNode, n)
else:
blockquoteFinish(currentBlockquote, newNode, n)
result = $newNode
except:
echo("[WARNING] Could not parse rst html.")
proc validateRst(c: var TForumData, content: string): bool =
result = true

View file

@ -705,4 +705,9 @@ blockquote {
margin: 10px 0px;
border-left: 2px solid rgb(61, 61, 61);
color: rgb(109, 109, 109);
}
blockquote p {
color: rgb(109, 109, 109) !important;
}