Markdown code blocks migration part 7 (#20547)
This commit is contained in:
parent
13b3ea71da
commit
19ff746916
17 changed files with 426 additions and 411 deletions
|
|
@ -379,22 +379,22 @@ func sort*[T](a: var openArray[T],
|
|||
## `cmp`, you may use `system.cmp` or instead call the overloaded
|
||||
## version of `sort`, which uses `system.cmp`.
|
||||
##
|
||||
## .. code-block:: nim
|
||||
##
|
||||
## sort(myIntArray, system.cmp[int])
|
||||
## # do not use cmp[string] here as we want to use the specialized
|
||||
## # overload:
|
||||
## sort(myStrArray, system.cmp)
|
||||
## ```nim
|
||||
## sort(myIntArray, system.cmp[int])
|
||||
## # do not use cmp[string] here as we want to use the specialized
|
||||
## # overload:
|
||||
## sort(myStrArray, system.cmp)
|
||||
## ```
|
||||
##
|
||||
## You can inline adhoc comparison procs with the `do notation
|
||||
## <manual_experimental.html#do-notation>`_. Example:
|
||||
##
|
||||
## .. code-block:: nim
|
||||
##
|
||||
## ```nim
|
||||
## people.sort do (x, y: Person) -> int:
|
||||
## result = cmp(x.surname, y.surname)
|
||||
## if result == 0:
|
||||
## result = cmp(x.name, y.name)
|
||||
## ```
|
||||
##
|
||||
## **See also:**
|
||||
## * `sort proc<#sort,openArray[T]>`_
|
||||
|
|
|
|||
|
|
@ -41,13 +41,13 @@
|
|||
## requested amount of data is read **or** an exception occurs.
|
||||
##
|
||||
## Code to read some data from a socket may look something like this:
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## var future = socket.recv(100)
|
||||
## future.addCallback(
|
||||
## proc () =
|
||||
## echo(future.read)
|
||||
## )
|
||||
## ```Nim
|
||||
## var future = socket.recv(100)
|
||||
## future.addCallback(
|
||||
## proc () =
|
||||
## echo(future.read)
|
||||
## )
|
||||
## ```
|
||||
##
|
||||
## All asynchronous functions returning a `Future` will not block. They
|
||||
## will not however return immediately. An asynchronous function will have
|
||||
|
|
@ -108,24 +108,24 @@
|
|||
## You can handle exceptions in the same way as in ordinary Nim code;
|
||||
## by using the try statement:
|
||||
##
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## ```Nim
|
||||
## try:
|
||||
## let data = await sock.recv(100)
|
||||
## echo("Received ", data)
|
||||
## except:
|
||||
## # Handle exception
|
||||
##
|
||||
##
|
||||
## ```
|
||||
##
|
||||
## An alternative approach to handling exceptions is to use `yield` on a future
|
||||
## then check the future's `failed` property. For example:
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## ```Nim
|
||||
## var future = sock.recv(100)
|
||||
## yield future
|
||||
## if future.failed:
|
||||
## # Handle exception
|
||||
## ```
|
||||
##
|
||||
##
|
||||
## Discarding futures
|
||||
## ==================
|
||||
|
|
|
|||
|
|
@ -9,18 +9,19 @@
|
|||
|
||||
## This module implements asynchronous file reading and writing.
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## import std/[asyncfile, asyncdispatch, os]
|
||||
## ```Nim
|
||||
## import std/[asyncfile, asyncdispatch, os]
|
||||
##
|
||||
## proc main() {.async.} =
|
||||
## var file = openAsync(getTempDir() / "foobar.txt", fmReadWrite)
|
||||
## await file.write("test")
|
||||
## file.setFilePos(0)
|
||||
## let data = await file.readAll()
|
||||
## doAssert data == "test"
|
||||
## file.close()
|
||||
## proc main() {.async.} =
|
||||
## var file = openAsync(getTempDir() / "foobar.txt", fmReadWrite)
|
||||
## await file.write("test")
|
||||
## file.setFilePos(0)
|
||||
## let data = await file.readAll()
|
||||
## doAssert data == "test"
|
||||
## file.close()
|
||||
##
|
||||
## waitFor main()
|
||||
## waitFor main()
|
||||
## ```
|
||||
|
||||
import asyncdispatch, os
|
||||
|
||||
|
|
|
|||
|
|
@ -21,13 +21,14 @@
|
|||
## In order to begin any sort of transfer of files you must first
|
||||
## connect to an FTP server. You can do so with the `connect` procedure.
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## import std/[asyncdispatch, asyncftpclient]
|
||||
## proc main() {.async.} =
|
||||
## var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test")
|
||||
## await ftp.connect()
|
||||
## echo("Connected")
|
||||
## waitFor(main())
|
||||
## ```Nim
|
||||
## import std/[asyncdispatch, asyncftpclient]
|
||||
## proc main() {.async.} =
|
||||
## var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test")
|
||||
## await ftp.connect()
|
||||
## echo("Connected")
|
||||
## waitFor(main())
|
||||
## ```
|
||||
##
|
||||
## A new `main` async procedure must be declared to allow the use of the
|
||||
## `await` keyword. The connection will complete asynchronously and the
|
||||
|
|
@ -41,16 +42,17 @@
|
|||
## working directory before you do so with the `pwd` procedure, you can also
|
||||
## instead specify an absolute path.
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## import std/[asyncdispatch, asyncftpclient]
|
||||
## proc main() {.async.} =
|
||||
## var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test")
|
||||
## await ftp.connect()
|
||||
## let currentDir = await ftp.pwd()
|
||||
## assert currentDir == "/home/user/"
|
||||
## await ftp.store("file.txt", "file.txt")
|
||||
## echo("File finished uploading")
|
||||
## waitFor(main())
|
||||
## ```Nim
|
||||
## import std/[asyncdispatch, asyncftpclient]
|
||||
## proc main() {.async.} =
|
||||
## var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test")
|
||||
## await ftp.connect()
|
||||
## let currentDir = await ftp.pwd()
|
||||
## assert currentDir == "/home/user/"
|
||||
## await ftp.store("file.txt", "file.txt")
|
||||
## echo("File finished uploading")
|
||||
## waitFor(main())
|
||||
## ```
|
||||
##
|
||||
## Checking the progress of a file transfer
|
||||
## ========================================
|
||||
|
|
@ -62,20 +64,21 @@
|
|||
## Procs that take an `onProgressChanged` callback will call this every
|
||||
## `progressInterval` milliseconds.
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## import std/[asyncdispatch, asyncftpclient]
|
||||
## ```Nim
|
||||
## import std/[asyncdispatch, asyncftpclient]
|
||||
##
|
||||
## proc onProgressChanged(total, progress: BiggestInt,
|
||||
## speed: float) {.async.} =
|
||||
## echo("Uploaded ", progress, " of ", total, " bytes")
|
||||
## echo("Current speed: ", speed, " kb/s")
|
||||
## proc onProgressChanged(total, progress: BiggestInt,
|
||||
## speed: float) {.async.} =
|
||||
## echo("Uploaded ", progress, " of ", total, " bytes")
|
||||
## echo("Current speed: ", speed, " kb/s")
|
||||
##
|
||||
## proc main() {.async.} =
|
||||
## var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test", progressInterval = 500)
|
||||
## await ftp.connect()
|
||||
## await ftp.store("file.txt", "/home/user/file.txt", onProgressChanged)
|
||||
## echo("File finished uploading")
|
||||
## waitFor(main())
|
||||
## proc main() {.async.} =
|
||||
## var ftp = newAsyncFtpClient("example.com", user = "test", pass = "test", progressInterval = 500)
|
||||
## await ftp.connect()
|
||||
## await ftp.store("file.txt", "/home/user/file.txt", onProgressChanged)
|
||||
## echo("File finished uploading")
|
||||
## waitFor(main())
|
||||
## ```
|
||||
|
||||
|
||||
import asyncdispatch, asyncnet, nativesockets, strutils, parseutils, os, times
|
||||
|
|
|
|||
|
|
@ -65,8 +65,7 @@
|
|||
##
|
||||
## The following example demonstrates a simple chat server.
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
##
|
||||
## ```Nim
|
||||
## import std/[asyncnet, asyncdispatch]
|
||||
##
|
||||
## var clients {.threadvar.}: seq[AsyncSocket]
|
||||
|
|
@ -93,7 +92,7 @@
|
|||
##
|
||||
## asyncCheck serve()
|
||||
## runForever()
|
||||
##
|
||||
## ```
|
||||
|
||||
import std/private/since
|
||||
|
||||
|
|
|
|||
|
|
@ -69,8 +69,9 @@ proc openDefaultBrowser*(url: string) =
|
|||
##
|
||||
## This proc doesn't raise an exception on error, beware.
|
||||
##
|
||||
## .. code-block:: nim
|
||||
## ```nim
|
||||
## block: openDefaultBrowser("https://nim-lang.org")
|
||||
## ```
|
||||
doAssert url.len > 0, "URL must not be empty string"
|
||||
openDefaultBrowserImpl(url)
|
||||
|
||||
|
|
@ -85,10 +86,11 @@ proc openDefaultBrowser*() {.since: (1, 1).} =
|
|||
##
|
||||
## This proc doesn't raise an exception on error, beware.
|
||||
##
|
||||
## ```nim
|
||||
## block: openDefaultBrowser()
|
||||
## ```
|
||||
##
|
||||
## **See also:**
|
||||
##
|
||||
## * https://tools.ietf.org/html/rfc6694#section-3
|
||||
##
|
||||
## .. code-block:: nim
|
||||
## block: openDefaultBrowser()
|
||||
openDefaultBrowserImpl("http:about:blank") # See IETF RFC-6694 Section 3.
|
||||
|
|
|
|||
|
|
@ -9,25 +9,25 @@
|
|||
|
||||
## This module implements helper procs for CGI applications. Example:
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## ```Nim
|
||||
## import std/[strtabs, cgi]
|
||||
##
|
||||
## import std/[strtabs, cgi]
|
||||
##
|
||||
## # Fill the values when debugging:
|
||||
## when debug:
|
||||
## setTestData("name", "Klaus", "password", "123456")
|
||||
## # read the data into `myData`
|
||||
## var myData = readData()
|
||||
## # check that the data's variable names are "name" or "password"
|
||||
## validateData(myData, "name", "password")
|
||||
## # start generating content:
|
||||
## writeContentType()
|
||||
## # generate content:
|
||||
## write(stdout, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n")
|
||||
## write(stdout, "<html><head><title>Test</title></head><body>\n")
|
||||
## writeLine(stdout, "your name: " & myData["name"])
|
||||
## writeLine(stdout, "your password: " & myData["password"])
|
||||
## writeLine(stdout, "</body></html>")
|
||||
## # Fill the values when debugging:
|
||||
## when debug:
|
||||
## setTestData("name", "Klaus", "password", "123456")
|
||||
## # read the data into `myData`
|
||||
## var myData = readData()
|
||||
## # check that the data's variable names are "name" or "password"
|
||||
## validateData(myData, "name", "password")
|
||||
## # start generating content:
|
||||
## writeContentType()
|
||||
## # generate content:
|
||||
## write(stdout, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n")
|
||||
## write(stdout, "<html><head><title>Test</title></head><body>\n")
|
||||
## writeLine(stdout, "your name: " & myData["name"])
|
||||
## writeLine(stdout, "your password: " & myData["password"])
|
||||
## writeLine(stdout, "</body></html>")
|
||||
## ```
|
||||
|
||||
import strutils, os, strtabs, cookies, uri
|
||||
export uri.encodeUrl, uri.decodeUrl
|
||||
|
|
@ -252,9 +252,9 @@ proc setTestData*(keysvalues: varargs[string]) =
|
|||
## Fills the appropriate environment variables to test your CGI application.
|
||||
## This can only simulate the 'GET' request method. `keysvalues` should
|
||||
## provide embedded (name, value)-pairs. Example:
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## setTestData("name", "Hanz", "password", "12345")
|
||||
## ```Nim
|
||||
## setTestData("name", "Hanz", "password", "12345")
|
||||
## ```
|
||||
putEnv("REQUEST_METHOD", "GET")
|
||||
var i = 0
|
||||
var query = ""
|
||||
|
|
@ -269,9 +269,9 @@ proc setTestData*(keysvalues: varargs[string]) =
|
|||
proc writeContentType*() =
|
||||
## Calls this before starting to send your HTML data to `stdout`. This
|
||||
## implements this part of the CGI protocol:
|
||||
##
|
||||
## .. code-block:: Nim
|
||||
## write(stdout, "Content-type: text/html\n\n")
|
||||
## ```Nim
|
||||
## write(stdout, "Content-type: text/html\n\n")
|
||||
## ```
|
||||
write(stdout, "Content-type: text/html\n\n")
|
||||
|
||||
proc resetForStacktrace() =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue