httpserver supports a fixed port number

This commit is contained in:
Andreas Rumpf 2010-02-06 12:24:54 +01:00
commit fd522aa406
9 changed files with 278 additions and 201 deletions

View file

@ -1,5 +1,4 @@
Advanced commands:: Advanced commands::
pas convert a Pascal file to Nimrod syntax
pretty pretty print the inputfile pretty pretty print the inputfile
genDepend generate a DOT file containing the genDepend generate a DOT file containing the
module dependency graph module dependency graph

View file

@ -7,6 +7,10 @@
.. contents:: .. contents::
**Note:** ENDB has not been maintained/tested since several versions. Help if
you want this debugger to survive.
Nimrod comes with a platform independant debugger - Nimrod comes with a platform independant debugger -
the `Embedded Nimrod Debugger`:idx: (`ENDB`:idx:). The debugger is the `Embedded Nimrod Debugger`:idx: (`ENDB`:idx:). The debugger is
*embedded* into your executable if it has been *embedded* into your executable if it has been

View file

@ -50,22 +50,22 @@ proc dbQuote(s: string): string =
else: add(result, c) else: add(result, c)
add(result, '\'') add(result, '\'')
proc dbFormat(formatstr: string, args: openarray[string]): string = proc dbFormat(formatstr: TSqlQuery, args: openarray[string]): string =
result = "" result = ""
var a = 0 var a = 0
for c in items(formatstr): for c in items(string(formatstr)):
if c == '?': if c == '?':
add(result, dbQuote(args[a])) add(result, dbQuote(args[a]))
inc(a) inc(a)
else: else:
add(result, c) add(result, c)
proc TryQuery*(db: TDbConn, query: string, args: openarray[string]): bool = proc TryQuery*(db: TDbConn, query: TSqlQuery, args: openarray[string]): bool =
## tries to execute the query and returns true if successful, false otherwise. ## tries to execute the query and returns true if successful, false otherwise.
var q = dbFormat(query, args) var q = dbFormat(query, args)
return mysqlRealQuery(db, q, q.len) == 0'i32 return mysqlRealQuery(db, q, q.len) == 0'i32
proc Query*(db: TDbConn, query: string, args: openarray[string]) = proc Query*(db: TDbConn, query: TSqlQuery, args: openarray[string]) =
## executes the query and raises EDB if not successful. ## executes the query and raises EDB if not successful.
var q = dbFormat(query, args) var q = dbFormat(query, args)
if mysqlRealQuery(db, q, q.len) != 0'i32: dbError(db) if mysqlRealQuery(db, q, q.len) != 0'i32: dbError(db)
@ -79,7 +79,7 @@ proc properFreeResult(sqlres: PMYSQL_RES, row: cstringArray) =
while mysqlFetchRow(sqlres) != nil: nil while mysqlFetchRow(sqlres) != nil: nil
mysqlFreeResult(sqlres) mysqlFreeResult(sqlres)
iterator FastRows*(db: TDbConn, query: string, iterator FastRows*(db: TDbConn, query: TSqlQuery,
args: openarray[string]): TRow = args: openarray[string]): TRow =
## executes the query and iterates over the result dataset. This is very ## executes the query and iterates over the result dataset. This is very
## fast, but potenially dangerous: If the for-loop-body executes another ## fast, but potenially dangerous: If the for-loop-body executes another
@ -99,7 +99,7 @@ iterator FastRows*(db: TDbConn, query: string,
yield result yield result
properFreeResult(sqlres, row) properFreeResult(sqlres, row)
proc GetAllRows*(db: TDbConn, query: string, proc GetAllRows*(db: TDbConn, query: TSqlQuery,
args: openarray[string]): seq[TRow] = args: openarray[string]): seq[TRow] =
## executes the query and returns the whole result dataset. ## executes the query and returns the whole result dataset.
result = @[] result = @[]
@ -118,12 +118,12 @@ proc GetAllRows*(db: TDbConn, query: string,
inc(j) inc(j)
mysqlFreeResult(sqlres) mysqlFreeResult(sqlres)
iterator Rows*(db: TDbConn, query: string, iterator Rows*(db: TDbConn, query: TSqlQuery,
args: openarray[string]): TRow = args: openarray[string]): TRow =
## same as `FastRows`, but slower and safe. ## same as `FastRows`, but slower and safe.
for r in items(GetAllRows(db, query, args)): yield r for r in items(GetAllRows(db, query, args)): yield r
proc GetValue*(db: TDbConn, query: string, proc GetValue*(db: TDbConn, query: TSqlQuery,
args: openarray[string]): string = args: openarray[string]): string =
## executes the query and returns the result dataset's the first column ## executes the query and returns the result dataset's the first column
## of the first row. Returns "" if the dataset contains no rows. This uses ## of the first row. Returns "" if the dataset contains no rows. This uses
@ -133,7 +133,7 @@ proc GetValue*(db: TDbConn, query: string,
result = row[0] result = row[0]
break break
proc TryInsertID*(db: TDbConn, query: string, proc TryInsertID*(db: TDbConn, query: TSqlQuery,
args: openarray[string]): int64 = args: openarray[string]): int64 =
## executes the query (typically "INSERT") and returns the ## executes the query (typically "INSERT") and returns the
## generated ID for the row or -1 in case of an error. ## generated ID for the row or -1 in case of an error.
@ -143,13 +143,13 @@ proc TryInsertID*(db: TDbConn, query: string,
else: else:
result = mysql_insert_id(db) result = mysql_insert_id(db)
proc InsertID*(db: TDbConn, query: string, args: openArray[string]): int64 = proc InsertID*(db: TDbConn, query: TSqlQuery, args: openArray[string]): int64 =
## executes the query (typically "INSERT") and returns the ## executes the query (typically "INSERT") and returns the
## generated ID for the row. ## generated ID for the row.
result = TryInsertID(db, query, args) result = TryInsertID(db, query, args)
if result < 0: dbError(db) if result < 0: dbError(db)
proc QueryAffectedRows*(db: TDbConn, query: string, proc QueryAffectedRows*(db: TDbConn, query: TSqlQuery,
args: openArray[string]): int64 = args: openArray[string]): int64 =
## runs the query (typically "UPDATE") and returns the ## runs the query (typically "UPDATE") and returns the
## number of affected rows ## number of affected rows

39
lib/devel/httpserver.nim → lib/pure/httpserver.nim Executable file → Normal file
View file

@ -8,6 +8,20 @@
# #
## This module implements a simple HTTP-Server. ## This module implements a simple HTTP-Server.
##
## Example:
##
## .. code-block:: nimrod
## import strutils, sockets, httpserver
##
## var counter = 0
## proc handleRequest(client: TSocket, path, query: string): bool {.procvar.} =
## inc(counter)
## client.send("Hallo for the $#th time." % $counter & wwwNL)
## return false # do not stop processing
##
## run(handleRequest, TPort(80))
##
import strutils, os, osproc, strtabs, streams, sockets import strutils, os, osproc, strtabs, streams, sockets
@ -180,15 +194,18 @@ type
client*: TSocket ## the socket to write the file data to client*: TSocket ## the socket to write the file data to
path*, query*: string ## path and query the client requested path*, query*: string ## path and query the client requested
proc open*(s: var TServer, port = TPort(0)) = proc open*(s: var TServer, port = TPort(80)) =
## creates a new server at port `port`. If ``port == 0`` a free port is ## creates a new server at port `port`. If ``port == 0`` a free port is
## aquired that can be accessed later by the ``port`` proc. ## aquired that can be accessed later by the ``port`` proc.
s.socket = socket(AF_INET) s.socket = socket(AF_INET)
if s.socket == InvalidSocket: OSError() if s.socket == InvalidSocket: OSError()
bindAddr(s.socket) bindAddr(s.socket, port)
listen(s.socket) listen(s.socket)
s.port = getSockName(s.socket) if port == TPort(0):
s.port = getSockName(s.socket)
else:
s.port = port
s.client = InvalidSocket s.client = InvalidSocket
s.path = "" s.path = ""
s.query = "" s.query = ""
@ -220,7 +237,7 @@ proc close*(s: TServer) =
close(s.socket) close(s.socket)
proc run*(handleRequest: proc (client: TSocket, path, query: string): bool, proc run*(handleRequest: proc (client: TSocket, path, query: string): bool,
port = TPort(0)) = port = TPort(80)) =
## encapsulates the server object and main loop ## encapsulates the server object and main loop
var s: TServer var s: TServer
open(s, port) open(s, port)
@ -231,18 +248,6 @@ proc run*(handleRequest: proc (client: TSocket, path, query: string): bool,
close(s.client) close(s.client)
close(s) close(s)
when false:
proc main =
var (server, port) = startup()
echo("httpserver running on port ", int16(port))
while true:
var client = accept(server)
if client == InvalidSocket: OSError()
acceptRequest(client)
close(client)
close(server)
when isMainModule: when isMainModule:
var counter = 0 var counter = 0
proc handleRequest(client: TSocket, path, query: string): bool {.procvar.} = proc handleRequest(client: TSocket, path, query: string): bool {.procvar.} =
@ -250,5 +255,5 @@ when isMainModule:
client.send("Hallo, Andreas for the $#th time." % $counter & wwwNL) client.send("Hallo, Andreas for the $#th time." % $counter & wwwNL)
return false # do not stop processing return false # do not stop processing
run(handleRequest) run(handleRequest, TPort(80))

View file

@ -1,7 +1,7 @@
# #
# #
# Nimrod's Runtime Library # Nimrod's Runtime Library
# (c) Copyright 2009 Andreas Rumpf # (c) Copyright 2010 Andreas Rumpf
# #
# See the file "copying.txt", included in this # See the file "copying.txt", included in this
# distribution, for details about the copyright. # distribution, for details about the copyright.

View file

@ -9,28 +9,171 @@
## Regular expression support for Nimrod. Consider using the pegs module ## Regular expression support for Nimrod. Consider using the pegs module
## instead. ## instead.
## Currently this module is implemented by providing a wrapper around the
## `PRCE (Perl-Compatible Regular Expressions) <http://www.pcre.org>`_
## C library. This means that your application will depend on the PRCE
## library's licence when using this module, which should not be a problem
## though.
## PRCE's licence follows:
##
## .. include:: ../doc/regexprs.txt
##
{.compile: "tre/tre_all.c".} import
pcre, strutils
from strutils import addf
type
TRegExDesc {.pure, final.} = object
re_nsub: int # Number of parenthesized subexpressions.
value: pointer # For internal use only.
TRegEx* = ref TRegExDesc ## a compiled regular expression
EInvalidRegEx* = object of EInvalidValue
## is raised if the pattern is no valid regular expression.
TRegMatch {.pure.} = object
so, eo: cint
const const
MaxSubpatterns* = 10 MaxSubpatterns* = 10
## defines the maximum number of subpatterns that can be captured. ## defines the maximum number of subpatterns that can be captured.
## More subpatterns cannot be captured! ## More subpatterns cannot be captured!
type
TRegExOptions* = enum ## options for regular expressions
reIgnoreCase = 0, ## do caseless matching
reMultiLine = 1, ## ``^`` and ``$`` match newlines within data
reDotAll = 2, ## ``.`` matches anything including NL
reExtended = 3, ## ignore whitespace and ``#`` comments
PCRE_ANCHORED* = 0x00000010
PCRE_DOLLAR_ENDONLY* = 0x00000020
PCRE_EXTRA* = 0x00000040
PCRE_NOTBOL* = 0x00000080
PCRE_NOTEOL* = 0x00000100
PCRE_UNGREEDY* = 0x00000200
PCRE_NOTEMPTY* = 0x00000400
PCRE_UTF8* = 0x00000800
PCRE_NO_AUTO_CAPTURE* = 0x00001000
TRegExDesc {.pure, final.} = object
h: PPcre
TRegEx* = ref TRegExDesc ## a compiled regular expression
EInvalidRegEx* = object of EInvalidValue
## is raised if the pattern is no valid regular expression.
proc rawCompile(pattern: string, flags: cint): PPcre =
var
msg: CString
offset: int
com = pcreCompile(pattern, flags, addr(msg), addr(offset), nil)
if com == nil:
var e: ref EInvalidRegEx
new(e)
e.msg = $msg & "\n" & pattern & "\n" & repeatChar(offset) & "^\n"
raise e
return com
proc finalizeRegEx(x: TRegEx) = dealloc(x.h)
proc re*(s: string): TRegEx =
## Constructor of regular expressions. Note that Nimrod's
## extended raw string literals supports this syntax ``re"[abc]"`` as
## a short form for ``re(r"[abc]")``.
new(result, finalizeRegEx)
result.h = rawCompile(s,
var err = int(regncomp(addr(result^), s, s.len,
cint(REG_EXTENDED or REG_NEWLINE)))
if err != 0:
var e: ref EInvalidRegEx
new(e)
e.msg = ErrorMessages[err]
raise e
proc xre*(pattern: string): TRegEx =
## deletes whitespace from a pattern that is not escaped or in a character
## class. Then it constructs a regular expresion object via `re`.
## This is modelled after Perl's ``/x`` modifier.
var p = ""
var i = 0
while i < pattern.len:
case pattern[i]
of ' ', '\t':
inc i
of '\\':
add p, '\\'
add p, pattern[i+1]
inc i, 2
of '[':
while pattern[i] != ']' and pattern[i] != '\0':
add p, pattern[i]
inc i
else:
add p, pattern[i]
inc i
result = re(p)
proc matchOrFind(s: string, pattern: PPcre, matches: var openarray[string],
start: cint): cint =
var
rawMatches: array [0..maxSubpatterns * 3 - 1, cint]
res = int(pcreExec(pattern, nil, s, len(s), start, 0,
cast[ptr cint](addr(rawMatches)), maxSubpatterns * 3))
dealloc(pattern)
if res < 0: return res
for i in 0..res-1:
var
a = rawMatches[i * 2]
b = rawMatches[i * 2 + 1]
if a >= 0'i32: matches[i] = copy(s, a, int(b)-1)
else: matches[i] = ""
return res
proc matchOrFind(s: string, pattern: PPcre, start: cint): cint =
var
rawMatches: array [0..maxSubpatterns * 3 - 1, cint]
res = pcreExec(pattern, nil, s, len(s), start, 0,
cast[ptr cint](addr(rawMatches)), maxSubpatterns * 3)
dealloc(pattern)
return res
proc match(s, pattern: string, matches: var openarray[string],
start: int = 0): bool =
return matchOrFind(s, rawCompile(pattern, PCRE_ANCHORED),
matches, start) >= 0'i32
proc matchLen(s, pattern: string, matches: var openarray[string],
start: int = 0): int =
return matchOrFind(s, rawCompile(pattern, PCRE_ANCHORED), matches, start)
proc find(s, pattern: string, matches: var openarray[string],
start: int = 0): bool =
return matchOrFind(s, rawCompile(pattern, PCRE_MULTILINE),
matches, start) >= 0'i32
proc match(s, pattern: string, start: int = 0): bool =
return matchOrFind(s, rawCompile(pattern, PCRE_ANCHORED), start) >= 0'i32
proc find(s, pattern: string, start: int = 0): bool =
return matchOrFind(s, rawCompile(pattern, PCRE_MULTILINE), start) >= 0'i32
template `=~` *(s, pattern: expr): expr =
## This calls ``match`` with an implicit declared ``matches`` array that
## can be used in the scope of the ``=~`` call:
##
## .. code-block:: nimrod
##
## if line =~ r"\s*(\w+)\s*\=\s*(\w+)":
## # matches a key=value pair:
## echo("Key: ", matches[1])
## echo("Value: ", matches[2])
## elif line =~ r"\s*(\#.*)":
## # matches a comment
## # note that the implicit ``matches`` array is different from the
## # ``matches`` array of the first branch
## echo("comment: ", matches[1])
## else:
## echo("syntax error")
##
when not definedInScope(matches):
var matches: array[0..maxSubPatterns-1, string]
match(s, pattern, matches)
proc regnexec(preg: ptr TRegExDesc, s: cstring, len, nmatch: int, proc regnexec(preg: ptr TRegExDesc, s: cstring, len, nmatch: int,
pmatch: ptr array [0..maxSubpatterns-1, TRegMatch], pmatch: ptr array [0..maxSubpatterns-1, TRegMatch],
eflags: cint): cint {.importc.} eflags: cint): cint {.importc.}
@ -75,44 +218,6 @@ const
"Invalid use of repetition operators" "Invalid use of repetition operators"
] ]
proc finalizeRegEx(x: TRegEx) = regfree(addr(x^))
proc re*(s: string): TRegEx =
## Constructor of regular expressions. Note that Nimrod's
## extended raw string literals supports this syntax ``re"[abc]"`` as
## a short form for ``re(r"[abc]")``.
new(result, finalizeRegEx)
var err = int(regncomp(addr(result^), s, s.len,
cint(REG_EXTENDED or REG_NEWLINE)))
if err != 0:
var e: ref EInvalidRegEx
new(e)
e.msg = ErrorMessages[err]
raise e
proc xre*(pattern: string): TRegEx =
## deletes whitespace from a pattern that is not escaped or in a character
## class. Then it constructs a regular expresion object via `re`.
## This is modelled after Perl's ``/x`` modifier.
var p = ""
var i = 0
while i < pattern.len:
case pattern[i]
of ' ', '\t':
inc i
of '\\':
add p, '\\'
add p, pattern[i+1]
inc i, 2
of '[':
while pattern[i] != ']' and pattern[i] != '\0':
add p, pattern[i]
inc i
else:
add p, pattern[i]
inc i
result = re(p)
proc rawmatch(s: string, pattern: TRegEx, matches: var openarray[string], proc rawmatch(s: string, pattern: TRegEx, matches: var openarray[string],
start: int): tuple[first, last: int] = start: int): tuple[first, last: int] =
var var

View file

@ -223,6 +223,8 @@ proc allocParam(p: BProc, s: PSym) =
proc localDebugInfo(p: BProc, s: PSym) = proc localDebugInfo(p: BProc, s: PSym) =
var name, a: PRope var name, a: PRope
if {optStackTrace, optEndb} * p.options != {optStackTrace, optEndb}: return if {optStackTrace, optEndb} * p.options != {optStackTrace, optEndb}: return
# XXX work around a bug: No type information for open arrays possible:
if skipTypes(s.typ, abstractVar).kind == tyOpenArray: return
if gCmd == cmdCompileToLLVM: if gCmd == cmdCompileToLLVM:
# "address" is the 0th field # "address" is the 0th field
# "typ" is the 1rst field # "typ" is the 1rst field

View file

@ -1,7 +1,7 @@
# #
# #
# The Nimrod Compiler # The Nimrod Compiler
# (c) Copyright 2009 Andreas Rumpf # (c) Copyright 2010 Andreas Rumpf
# #
# See the file "copying.txt", included in this # See the file "copying.txt", included in this
# distribution, for details about the copyright. # distribution, for details about the copyright.
@ -14,36 +14,30 @@ import
proc genDependPass*(): TPass proc genDependPass*(): TPass
proc generateDot*(project: string) proc generateDot*(project: string)
# implementation
type type
TGen = object of TPassContext TGen = object of TPassContext
module*: PSym module*: PSym
filename*: string filename*: string
PGen = ref TGen PGen = ref TGen
var gDotGraph: PRope var gDotGraph: PRope # the generated DOT file; we need a global variable
proc addDependencyAux(importing, imported: string) = proc addDependencyAux(importing, imported: string) =
# the generated DOT file; we need a global variable appf(gDotGraph, "$1 -> $2;$n", [toRope(importing), toRope(imported)])
appf(gDotGraph, "$1 -> $2;$n", [toRope(importing), toRope(imported)]) # s1 -> s2_4 # s1 -> s2_4[label="[0-9]"];
# [label="[0-9]"];
proc addDotDependency(c: PPassContext, n: PNode): PNode = proc addDotDependency(c: PPassContext, n: PNode): PNode =
var
g: PGen
imported: string
result = n result = n
if n == nil: return if n == nil: return
g = PGen(c) var g = PGen(c)
case n.kind case n.kind
of nkImportStmt: of nkImportStmt:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
imported = splitFile(getModuleFile(n.sons[i])).name var imported = splitFile(getModuleFile(n.sons[i])).name
addDependencyAux(g.module.name.s, imported) addDependencyAux(g.module.name.s, imported)
of nkFromStmt: of nkFromStmt:
imported = splitFile(getModuleFile(n.sons[0])).name var imported = splitFile(getModuleFile(n.sons[0])).name
addDependencyAux(g.module.name.s, imported) addDependencyAux(g.module.name.s, imported)
of nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr: of nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr:
for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i]) for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i])

View file

@ -1,7 +1,7 @@
# #
# #
# The Nimrod Compiler # The Nimrod Compiler
# (c) Copyright 2009 Andreas Rumpf # (c) Copyright 2010 Andreas Rumpf
# #
# See the file "copying.txt", included in this # See the file "copying.txt", included in this
# distribution, for details about the copyright. # distribution, for details about the copyright.
@ -92,7 +92,7 @@ proc initIndexFile(d: PDoc) =
proc newDocumentor(filename: string): PDoc = proc newDocumentor(filename: string): PDoc =
new(result) new(result)
result.tocPart = @ [] result.tocPart = @[]
result.filename = filename result.filename = filename
result.id = 100 result.id = 100
result.splitAfter = 20 result.splitAfter = 20
@ -203,24 +203,21 @@ proc nextSplitPoint(s: string, start: int): int =
result = start result = start
while result < len(s) + 0: while result < len(s) + 0:
case s[result] case s[result]
of '_': of '_': return
return
of 'a'..'z': of 'a'..'z':
if result + 1 < len(s) + 0: if result + 1 < len(s) + 0:
if s[result + 1] in {'A'..'Z'}: return if s[result + 1] in {'A'..'Z'}: return
else: else: nil
nil
inc(result) inc(result)
dec(result) # last valid index dec(result) # last valid index
proc esc(s: string, splitAfter: int = - 1): string = proc esc(s: string, splitAfter: int = - 1): string =
var j, k, partLen: int
result = "" result = ""
if splitAfter >= 0: if splitAfter >= 0:
partLen = 0 var partLen = 0
j = 0 var j = 0
while j < len(s) + 0: while j < len(s):
k = nextSplitPoint(s, j) var k = nextSplitPoint(s, j)
if (splitter != " ") or (partLen + k - j + 1 > splitAfter): if (splitter != " ") or (partLen + k - j + 1 > splitAfter):
partLen = 0 partLen = 0
add(result, splitter) add(result, splitter)
@ -249,10 +246,9 @@ proc renderAux(d: PDoc, n: PRstNode, outer: string = "$1"): PRope =
result = ropef(outer, [result]) result = ropef(outer, [result])
proc setIndexForSourceTerm(d: PDoc, name: PRstNode, id: int) = proc setIndexForSourceTerm(d: PDoc, name: PRstNode, id: int) =
var a, h: PRstNode
if d.theIndex == nil: return if d.theIndex == nil: return
h = newRstNode(rnHyperlink) var h = newRstNode(rnHyperlink)
a = newRstNode(rnLeaf, d.indexValFilename & disp("#", "") & $(id)) var a = newRstNode(rnLeaf, d.indexValFilename & disp("#", "") & $id)
addSon(h, a) addSon(h, a)
addSon(h, a) addSon(h, a)
a = newRstNode(rnIdx) a = newRstNode(rnIdx)
@ -260,12 +256,11 @@ proc setIndexForSourceTerm(d: PDoc, name: PRstNode, id: int) =
setIndexPair(d.theIndex, a, h) setIndexPair(d.theIndex, a, h)
proc renderIndexTerm(d: PDoc, n: PRstNode): PRope = proc renderIndexTerm(d: PDoc, n: PRstNode): PRope =
var a, h: PRstNode
inc(d.id) inc(d.id)
result = dispF("<em id=\"$1\">$2</em>", "$2\\label{$1}", result = dispF("<em id=\"$1\">$2</em>", "$2\\label{$1}",
[toRope(d.id), renderAux(d, n)]) [toRope(d.id), renderAux(d, n)])
h = newRstNode(rnHyperlink) var h = newRstNode(rnHyperlink)
a = newRstNode(rnLeaf, d.indexValFilename & disp("#", "") & $(d.id)) var a = newRstNode(rnLeaf, d.indexValFilename & disp("#", "") & $d.id)
addSon(h, a) addSon(h, a)
addSon(h, a) addSon(h, a)
setIndexPair(d.theIndex, n, h) setIndexPair(d.theIndex, n, h)
@ -530,12 +525,13 @@ proc renderRstToRst(d: PDoc, n: PRstNode): PRope =
else: rawMessage(errCannotRenderX, $n.kind) else: rawMessage(errCannotRenderX, $n.kind)
proc renderTocEntry(d: PDoc, e: TTocEntry): PRope = proc renderTocEntry(d: PDoc, e: TTocEntry): PRope =
result = dispF("<li><a class=\"reference\" id=\"$1_toc\" href=\"#$1\">$2</a></li>$n", result = dispF(
"\\item\\label{$1_toc} $2\\ref{$1}$n", [e.refname, e.header]) "<li><a class=\"reference\" id=\"$1_toc\" href=\"#$1\">$2</a></li>$n",
"\\item\\label{$1_toc} $2\\ref{$1}$n", [e.refname, e.header])
proc renderTocEntries(d: PDoc, j: var int, lvl: int): PRope = proc renderTocEntries(d: PDoc, j: var int, lvl: int): PRope =
result = nil result = nil
while (j <= high(d.tocPart)): while j <= high(d.tocPart):
var a = abs(d.tocPart[j].n.level) var a = abs(d.tocPart[j].n.level)
if (a == lvl): if (a == lvl):
app(result, renderTocEntry(d, d.tocPart[j])) app(result, renderTocEntry(d, d.tocPart[j]))
@ -552,12 +548,9 @@ proc fieldAux(s: string): PRope =
result = toRope(strip(s)) result = toRope(strip(s))
proc renderImage(d: PDoc, n: PRstNode): PRope = proc renderImage(d: PDoc, n: PRstNode): PRope =
var var options: PRope = nil
s, scale: string var s = getFieldValue(n, "scale")
options: PRope if s != "": dispA(options, " scale=\"$1\"", " scale=$1", [fieldAux(s)])
options = nil
s = getFieldValue(n, "scale")
if s != "": dispA(options, " scale=\"$1\"", " scale=$1", [fieldAux(scale)])
s = getFieldValue(n, "height") s = getFieldValue(n, "height")
if s != "": dispA(options, " height=\"$1\"", " height=$1", [fieldAux(s)]) if s != "": dispA(options, " height=\"$1\"", " height=$1", [fieldAux(s)])
s = getFieldValue(n, "width") s = getFieldValue(n, "width")
@ -614,14 +607,10 @@ proc texColumns(n: PRstNode): string =
for i in countup(1, rsonsLen(n)): add(result, "|X") for i in countup(1, rsonsLen(n)): add(result, "|X")
proc renderField(d: PDoc, n: PRstNode): PRope = proc renderField(d: PDoc, n: PRstNode): PRope =
var var b = false
fieldname: string
fieldval: PRope
b: bool
b = false
if gCmd == cmdRst2Tex: if gCmd == cmdRst2Tex:
fieldname = addNodes(n.sons[0]) var fieldname = addNodes(n.sons[0])
fieldval = toRope(esc(strip(addNodes(n.sons[1])))) var fieldval = toRope(esc(strip(addNodes(n.sons[1]))))
if cmpIgnoreStyle(fieldname, "author") == 0: if cmpIgnoreStyle(fieldname, "author") == 0:
if d.meta[metaAuthor] == nil: if d.meta[metaAuthor] == nil:
d.meta[metaAuthor] = fieldval d.meta[metaAuthor] = fieldval
@ -637,45 +626,39 @@ proc renderRstToOut(d: PDoc, n: PRstNode): PRope =
if n == nil: if n == nil:
return nil return nil
case n.kind case n.kind
of rnInner: of rnInner: result = renderAux(d, n)
result = renderAux(d, n) of rnHeadline: result = renderHeadline(d, n)
of rnHeadline: of rnOverline: result = renderOverline(d, n)
result = renderHeadline(d, n) of rnTransition: result = renderAux(d, n, disp("<hr />\n", "\\hrule\n"))
of rnOverline: of rnParagraph: result = renderAux(d, n, disp("<p>$1</p>\n", "$1$n$n"))
result = renderOverline(d, n)
of rnTransition:
result = renderAux(d, n, disp("<hr />" & "\n", "\\hrule" & "\n"))
of rnParagraph:
result = renderAux(d, n, disp("<p>$1</p>" & "\n", "$1$n$n"))
of rnBulletList: of rnBulletList:
result = renderAux(d, n, disp("<ul class=\"simple\">$1</ul>" & "\n", result = renderAux(d, n, disp("<ul class=\"simple\">$1</ul>\n",
"\\begin{itemize}$1\\end{itemize}" & "\n")) "\\begin{itemize}$1\\end{itemize}\n"))
of rnBulletItem, rnEnumItem: of rnBulletItem, rnEnumItem:
result = renderAux(d, n, disp("<li>$1</li>" & "\n", "\\item $1" & "\n")) result = renderAux(d, n, disp("<li>$1</li>" & "\n", "\\item $1" & "\n"))
of rnEnumList: of rnEnumList:
result = renderAux(d, n, disp("<ol class=\"simple\">$1</ol>" & "\n", result = renderAux(d, n, disp("<ol class=\"simple\">$1</ol>" & "\n",
"\\begin{enumerate}$1\\end{enumerate}" & "\n")) "\\begin{enumerate}$1\\end{enumerate}" & "\n"))
of rnDefList: of rnDefList:
result = renderAux(d, n, disp("<dl class=\"docutils\">$1</dl>" & "\n", "\\begin{description}$1\\end{description}" & result = renderAux(d, n, disp("<dl class=\"docutils\">$1</dl>\n",
"\n")) "\\begin{description}$1\\end{description}\n"))
of rnDefItem: of rnDefItem: result = renderAux(d, n)
result = renderAux(d, n) of rnDefName: result = renderAux(d, n, disp("<dt>$1</dt>\n", "\\item[$1] "))
of rnDefName: of rnDefBody: result = renderAux(d, n, disp("<dd>$1</dd>\n", "$1\n"))
result = renderAux(d, n, disp("<dt>$1</dt>" & "\n", "\\item[$1] "))
of rnDefBody:
result = renderAux(d, n, disp("<dd>$1</dd>" & "\n", "$1" & "\n"))
of rnFieldList: of rnFieldList:
result = nil result = nil
for i in countup(0, rsonsLen(n) - 1): for i in countup(0, rsonsLen(n) - 1):
app(result, renderRstToOut(d, n.sons[i])) app(result, renderRstToOut(d, n.sons[i]))
if result != nil: if result != nil:
result = dispf("<table class=\"docinfo\" frame=\"void\" rules=\"none\">" & result = dispf(
"<table class=\"docinfo\" frame=\"void\" rules=\"none\">" &
"<col class=\"docinfo-name\" />" & "<col class=\"docinfo-name\" />" &
"<col class=\"docinfo-content\" />" & "<tbody valign=\"top\">$1" & "<col class=\"docinfo-content\" />" &
"</tbody></table>", "\\begin{description}$1\\end{description}" & "\n", "<tbody valign=\"top\">$1" &
[result]) "</tbody></table>",
of rnField: "\\begin{description}$1\\end{description}\n",
result = renderField(d, n) [result])
of rnField: result = renderField(d, n)
of rnFieldName: of rnFieldName:
result = renderAux(d, n, disp("<th class=\"docinfo-name\">$1:</th>", result = renderAux(d, n, disp("<th class=\"docinfo-name\">$1:</th>",
"\\item[$1:]")) "\\item[$1:]"))
@ -684,8 +667,8 @@ proc renderRstToOut(d: PDoc, n: PRstNode): PRope =
of rnIndex: of rnIndex:
result = renderRstToOut(d, n.sons[2]) result = renderRstToOut(d, n.sons[2])
of rnOptionList: of rnOptionList:
result = renderAux(d, n, disp("<table frame=\"void\">$1</table>", "\\begin{description}$n$1\\end{description}" & result = renderAux(d, n, disp("<table frame=\"void\">$1</table>",
"\n")) "\\begin{description}$n$1\\end{description}\n"))
of rnOptionListItem: of rnOptionListItem:
result = renderAux(d, n, disp("<tr>$1</tr>$n", "$1")) result = renderAux(d, n, disp("<tr>$1</tr>$n", "$1"))
of rnOptionGroup: of rnOptionGroup:
@ -707,7 +690,9 @@ proc renderRstToOut(d: PDoc, n: PRstNode): PRope =
result = renderAux(d, n, disp("<blockquote><p>$1</p></blockquote>$n", result = renderAux(d, n, disp("<blockquote><p>$1</p></blockquote>$n",
"\\begin{quote}$1\\end{quote}$n")) "\\begin{quote}$1\\end{quote}$n"))
of rnTable, rnGridTable: of rnTable, rnGridTable:
result = renderAux(d, n, disp("<table border=\"1\" class=\"docutils\">$1</table>", "\\begin{table}\\begin{rsttab}{" & result = renderAux(d, n, disp(
"<table border=\"1\" class=\"docutils\">$1</table>",
"\\begin{table}\\begin{rsttab}{" &
texColumns(n) & "|}$n\\hline$n$1\\end{rsttab}\\end{table}")) texColumns(n) & "|}$n\\hline$n$1\\end{rsttab}\\end{table}"))
of rnTableRow: of rnTableRow:
if rsonsLen(n) >= 1: if rsonsLen(n) >= 1:
@ -717,8 +702,7 @@ proc renderRstToOut(d: PDoc, n: PRstNode): PRope =
result = dispf("<tr>$1</tr>$n", "$1\\\\$n\\hline$n", [result]) result = dispf("<tr>$1</tr>$n", "$1\\\\$n\\hline$n", [result])
else: else:
result = nil result = nil
of rnTableDataCell: of rnTableDataCell: result = renderAux(d, n, disp("<td>$1</td>", "$1"))
result = renderAux(d, n, disp("<td>$1</td>", "$1"))
of rnTableHeaderCell: of rnTableHeaderCell:
result = renderAux(d, n, disp("<th>$1</th>", "\\textbf{$1}")) result = renderAux(d, n, disp("<th>$1</th>", "\\textbf{$1}"))
of rnLabel: of rnLabel:
@ -731,20 +715,17 @@ proc renderRstToOut(d: PDoc, n: PRstNode): PRope =
result = dispF("<a class=\"reference external\" href=\"#$2\">$1</a>", result = dispF("<a class=\"reference external\" href=\"#$2\">$1</a>",
"$1\\ref{$2}", [renderAux(d, n), toRope(rstnodeToRefname(n))]) "$1\\ref{$2}", [renderAux(d, n), toRope(rstnodeToRefname(n))])
of rnStandaloneHyperlink: of rnStandaloneHyperlink:
result = renderAux(d, n, disp("<a class=\"reference external\" href=\"$1\">$1</a>", result = renderAux(d, n, disp(
"\\href{$1}{$1}")) "<a class=\"reference external\" href=\"$1\">$1</a>",
"\\href{$1}{$1}"))
of rnHyperlink: of rnHyperlink:
result = dispF("<a class=\"reference external\" href=\"$2\">$1</a>", result = dispF("<a class=\"reference external\" href=\"$2\">$1</a>",
"\\href{$2}{$1}", "\\href{$2}{$1}",
[renderRstToOut(d, n.sons[0]), renderRstToOut(d, n.sons[1])]) [renderRstToOut(d, n.sons[0]), renderRstToOut(d, n.sons[1])])
of rnDirArg, rnRaw: of rnDirArg, rnRaw: result = renderAux(d, n)
result = renderAux(d, n) of rnImage, rnFigure: result = renderImage(d, n)
of rnImage, rnFigure: of rnCodeBlock: result = renderCodeBlock(d, n)
result = renderImage(d, n) of rnContainer: result = renderContainer(d, n)
of rnCodeBlock:
result = renderCodeBlock(d, n)
of rnContainer:
result = renderContainer(d, n)
of rnSubstitutionReferences, rnSubstitutionDef: of rnSubstitutionReferences, rnSubstitutionDef:
result = renderAux(d, n, disp("|$1|", "|$1|")) result = renderAux(d, n, disp("|$1|", "|$1|"))
of rnDirective: of rnDirective:
@ -752,12 +733,9 @@ proc renderRstToOut(d: PDoc, n: PRstNode): PRope =
of rnGeneralRole: of rnGeneralRole:
result = dispF("<span class=\"$2\">$1</span>", "\\span$2{$1}", result = dispF("<span class=\"$2\">$1</span>", "\\span$2{$1}",
[renderRstToOut(d, n.sons[0]), renderRstToOut(d, n.sons[1])]) [renderRstToOut(d, n.sons[0]), renderRstToOut(d, n.sons[1])])
of rnSub: of rnSub: result = renderAux(d, n, disp("<sub>$1</sub>", "\\rstsub{$1}"))
result = renderAux(d, n, disp("<sub>$1</sub>", "\\rstsub{$1}")) of rnSup: result = renderAux(d, n, disp("<sup>$1</sup>", "\\rstsup{$1}"))
of rnSup: of rnEmphasis: result = renderAux(d, n, disp("<em>$1</em>", "\\emph{$1}"))
result = renderAux(d, n, disp("<sup>$1</sup>", "\\rstsup{$1}"))
of rnEmphasis:
result = renderAux(d, n, disp("<em>$1</em>", "\\emph{$1}"))
of rnStrongEmphasis: of rnStrongEmphasis:
result = renderAux(d, n, disp("<strong>$1</strong>", "\\textbf{$1}")) result = renderAux(d, n, disp("<strong>$1</strong>", "\\textbf{$1}"))
of rnInterpretedText: of rnInterpretedText:
@ -768,33 +746,24 @@ proc renderRstToOut(d: PDoc, n: PRstNode): PRope =
else: else:
result = renderIndexTerm(d, n) result = renderIndexTerm(d, n)
of rnInlineLiteral: of rnInlineLiteral:
result = renderAux(d, n, disp("<tt class=\"docutils literal\"><span class=\"pre\">$1</span></tt>", result = renderAux(d, n, disp(
"\\texttt{$1}")) "<tt class=\"docutils literal\"><span class=\"pre\">$1</span></tt>",
of rnLeaf: "\\texttt{$1}"))
result = toRope(esc(n.text)) of rnLeaf: result = toRope(esc(n.text))
of rnContents: of rnContents: d.hasToc = true
d.hasToc = true of rnTitle: d.meta[metaTitle] = renderRstToOut(d, n.sons[0])
of rnTitle:
d.meta[metaTitle] = renderRstToOut(d, n.sons[0])
else: InternalError("renderRstToOut") else: InternalError("renderRstToOut")
proc generateDoc(d: PDoc, n: PNode) = proc generateDoc(d: PDoc, n: PNode) =
if n == nil: return if n == nil: return
case n.kind case n.kind
of nkCommentStmt: of nkCommentStmt: app(d.modDesc, genComment(d, n))
app(d.modDesc, genComment(d, n)) of nkProcDef: genItem(d, n, n.sons[namePos], skProc)
of nkProcDef: of nkMethodDef: genItem(d, n, n.sons[namePos], skMethod)
genItem(d, n, n.sons[namePos], skProc) of nkIteratorDef: genItem(d, n, n.sons[namePos], skIterator)
of nkMethodDef: of nkMacroDef: genItem(d, n, n.sons[namePos], skMacro)
genItem(d, n, n.sons[namePos], skMethod) of nkTemplateDef: genItem(d, n, n.sons[namePos], skTemplate)
of nkIteratorDef: of nkConverterDef: genItem(d, n, n.sons[namePos], skConverter)
genItem(d, n, n.sons[namePos], skIterator)
of nkMacroDef:
genItem(d, n, n.sons[namePos], skMacro)
of nkTemplateDef:
genItem(d, n, n.sons[namePos], skTemplate)
of nkConverterDef:
genItem(d, n, n.sons[namePos], skConverter)
of nkVarSection: of nkVarSection:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
if n.sons[i].kind != nkCommentStmt: if n.sons[i].kind != nkCommentStmt:
@ -812,8 +781,7 @@ proc generateDoc(d: PDoc, n: PNode) =
of nkWhenStmt: of nkWhenStmt:
# generate documentation for the first branch only: # generate documentation for the first branch only:
generateDoc(d, lastSon(n.sons[0])) generateDoc(d, lastSon(n.sons[0]))
else: else: nil
nil
proc genSection(d: PDoc, kind: TSymKind) = proc genSection(d: PDoc, kind: TSymKind) =
if d.section[kind] == nil: return if d.section[kind] == nil: return