kochdocs.nim 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. ## Part of 'koch' responsible for the documentation generation.
  2. import os, strutils, osproc
  3. const
  4. gaCode* = " --doc.googleAnalytics:UA-48159761-1"
  5. nimArgs = "--hint[Conf]:off --hint[Path]:off --hint[Processing]:off -d:boot --putenv:nimversion=$#" % system.NimVersion
  6. gitUrl = "https://github.com/nim-lang/Nim"
  7. docHtmlOutput = "doc/html"
  8. webUploadOutput = "web/upload"
  9. docHackDir = "tools/dochack"
  10. proc exe*(f: string): string =
  11. result = addFileExt(f, ExeExt)
  12. when defined(windows):
  13. result = result.replace('/','\\')
  14. proc findNim*(): string =
  15. var nim = "nim".exe
  16. result = "bin" / nim
  17. if existsFile(result): return
  18. for dir in split(getEnv("PATH"), PathSep):
  19. if existsFile(dir / nim): return dir / nim
  20. # assume there is a symlink to the exe or something:
  21. return nim
  22. proc exec*(cmd: string, errorcode: int = QuitFailure, additionalPath = "") =
  23. let prevPath = getEnv("PATH")
  24. if additionalPath.len > 0:
  25. var absolute = additionalPATH
  26. if not absolute.isAbsolute:
  27. absolute = getCurrentDir() / absolute
  28. echo("Adding to $PATH: ", absolute)
  29. putEnv("PATH", (if prevPath.len > 0: prevPath & PathSep else: "") & absolute)
  30. echo(cmd)
  31. if execShellCmd(cmd) != 0: quit("FAILURE", errorcode)
  32. putEnv("PATH", prevPath)
  33. proc execCleanPath*(cmd: string,
  34. additionalPath = ""; errorcode: int = QuitFailure) =
  35. # simulate a poor man's virtual environment
  36. let prevPath = getEnv("PATH")
  37. when defined(windows):
  38. let CleanPath = r"$1\system32;$1;$1\System32\Wbem" % getEnv"SYSTEMROOT"
  39. else:
  40. const CleanPath = r"/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin"
  41. putEnv("PATH", CleanPath & PathSep & additionalPath)
  42. echo(cmd)
  43. if execShellCmd(cmd) != 0: quit("FAILURE", errorcode)
  44. putEnv("PATH", prevPath)
  45. proc nimexec*(cmd: string) =
  46. exec findNim() & " " & cmd
  47. const
  48. pdf = """
  49. doc/manual.rst
  50. doc/lib.rst
  51. doc/tut1.rst
  52. doc/tut2.rst
  53. doc/nimc.rst
  54. doc/niminst.rst
  55. doc/gc.rst
  56. """.splitWhitespace()
  57. rst2html = """
  58. doc/intern.rst
  59. doc/apis.rst
  60. doc/lib.rst
  61. doc/manual.rst
  62. doc/tut1.rst
  63. doc/tut2.rst
  64. doc/nimc.rst
  65. doc/overview.rst
  66. doc/filters.rst
  67. doc/tools.rst
  68. doc/niminst.rst
  69. doc/nimgrep.rst
  70. doc/gc.rst
  71. doc/estp.rst
  72. doc/idetools.rst
  73. doc/docgen.rst
  74. doc/koch.rst
  75. doc/backends.rst
  76. doc/nimsuggest.rst
  77. doc/nep1.rst
  78. doc/nims.rst
  79. doc/contributing.rst
  80. doc/codeowners.rst
  81. doc/manual/var_t_return.rst
  82. """.splitWhitespace()
  83. doc = """
  84. lib/system.nim
  85. lib/system/nimscript.nim
  86. lib/pure/ospaths.nim
  87. lib/pure/parsejson.nim
  88. lib/pure/cstrutils.nim
  89. lib/core/macros.nim
  90. lib/pure/marshal.nim
  91. lib/core/typeinfo.nim
  92. lib/impure/re.nim
  93. lib/pure/typetraits.nim
  94. nimsuggest/sexp.nim
  95. lib/pure/concurrency/threadpool.nim
  96. lib/pure/concurrency/cpuinfo.nim
  97. lib/pure/concurrency/cpuload.nim
  98. lib/js/dom.nim
  99. lib/js/jsffi.nim
  100. lib/js/jsconsole.nim
  101. lib/js/asyncjs.nim
  102. lib/pure/os.nim
  103. lib/pure/strutils.nim
  104. lib/pure/math.nim
  105. lib/pure/matchers.nim
  106. lib/pure/editdistance.nim
  107. lib/pure/algorithm.nim
  108. lib/pure/stats.nim
  109. lib/windows/winlean.nim
  110. lib/pure/random.nim
  111. lib/pure/complex.nim
  112. lib/pure/times.nim
  113. lib/pure/osproc.nim
  114. lib/pure/pegs.nim
  115. lib/pure/dynlib.nim
  116. lib/pure/strscans.nim
  117. lib/pure/parseopt.nim
  118. lib/pure/hashes.nim
  119. lib/pure/strtabs.nim
  120. lib/pure/lexbase.nim
  121. lib/pure/parsecfg.nim
  122. lib/pure/parsexml.nim
  123. lib/pure/parsecsv.nim
  124. lib/pure/parsesql.nim
  125. lib/pure/streams.nim
  126. lib/pure/terminal.nim
  127. lib/pure/cgi.nim
  128. lib/pure/unicode.nim
  129. lib/pure/strmisc.nim
  130. lib/pure/htmlgen.nim
  131. lib/pure/parseutils.nim
  132. lib/pure/browsers.nim
  133. lib/impure/db_postgres.nim
  134. lib/impure/db_mysql.nim
  135. lib/impure/db_sqlite.nim
  136. lib/impure/db_odbc.nim
  137. lib/pure/db_common.nim
  138. lib/pure/httpserver.nim
  139. lib/pure/httpclient.nim
  140. lib/pure/smtp.nim
  141. lib/impure/ssl.nim
  142. lib/pure/ropes.nim
  143. lib/pure/unidecode/unidecode.nim
  144. lib/pure/xmlparser.nim
  145. lib/pure/htmlparser.nim
  146. lib/pure/xmltree.nim
  147. lib/pure/colors.nim
  148. lib/pure/mimetypes.nim
  149. lib/pure/json.nim
  150. lib/pure/base64.nim
  151. lib/pure/scgi.nim
  152. lib/impure/nre.nim
  153. lib/impure/nre/private/util.nim
  154. lib/deprecated/pure/sockets.nim
  155. lib/deprecated/pure/asyncio.nim
  156. lib/pure/collections/tables.nim
  157. lib/pure/collections/sets.nim
  158. lib/pure/collections/lists.nim
  159. lib/pure/collections/sharedlist.nim
  160. lib/pure/collections/sharedtables.nim
  161. lib/pure/collections/intsets.nim
  162. lib/pure/collections/queues.nim
  163. lib/pure/collections/deques.nim
  164. lib/pure/encodings.nim
  165. lib/pure/collections/sequtils.nim
  166. lib/pure/collections/rtarrays.nim
  167. lib/pure/cookies.nim
  168. lib/pure/memfiles.nim
  169. lib/pure/subexes.nim
  170. lib/pure/collections/critbits.nim
  171. lib/core/locks.nim
  172. lib/core/rlocks.nim
  173. lib/pure/oids.nim
  174. lib/pure/endians.nim
  175. lib/pure/uri.nim
  176. lib/pure/nimprof.nim
  177. lib/pure/unittest.nim
  178. lib/packages/docutils/highlite.nim
  179. lib/packages/docutils/rst.nim
  180. lib/packages/docutils/rstast.nim
  181. lib/packages/docutils/rstgen.nim
  182. lib/pure/logging.nim
  183. lib/pure/options.nim
  184. lib/pure/asyncdispatch.nim
  185. lib/pure/asyncnet.nim
  186. lib/pure/asyncstreams.nim
  187. lib/pure/asyncfutures.nim
  188. lib/pure/nativesockets.nim
  189. lib/pure/asynchttpserver.nim
  190. lib/pure/net.nim
  191. lib/pure/selectors.nim
  192. lib/pure/sugar.nim
  193. lib/pure/collections/chains.nim
  194. lib/pure/asyncfile.nim
  195. lib/deprecated/pure/ftpclient.nim
  196. lib/pure/asyncftpclient.nim
  197. lib/pure/lenientops.nim
  198. lib/pure/md5.nim
  199. lib/pure/rationals.nim
  200. lib/pure/distros.nim
  201. lib/pure/oswalkdir.nim
  202. lib/pure/collections/heapqueue.nim
  203. lib/pure/fenv.nim
  204. lib/std/sha1.nim
  205. lib/std/varints.nim
  206. lib/impure/rdstdin.nim
  207. lib/wrappers/linenoise/linenoise.nim
  208. lib/pure/strformat.nim
  209. lib/pure/segfaults.nim
  210. lib/pure/mersenne.nim
  211. lib/pure/coro.nim
  212. lib/pure/httpcore.nim
  213. lib/pure/bitops.nim
  214. lib/pure/nimtracker.nim
  215. lib/pure/punycode.nim
  216. lib/pure/volatile.nim
  217. """.splitWhitespace()
  218. doc0 = """
  219. lib/system/threads.nim
  220. lib/system/channels.nim
  221. """.splitWhitespace()
  222. withoutIndex = """
  223. lib/wrappers/mysql.nim
  224. lib/wrappers/iup.nim
  225. lib/wrappers/sqlite3.nim
  226. lib/wrappers/postgres.nim
  227. lib/wrappers/tinyc.nim
  228. lib/wrappers/odbcsql.nim
  229. lib/wrappers/pcre.nim
  230. lib/wrappers/openssl.nim
  231. lib/posix/posix.nim
  232. lib/posix/linux.nim
  233. lib/posix/termios.nim
  234. lib/wrappers/odbcsql.nim
  235. lib/js/jscore.nim
  236. """.splitWhitespace()
  237. proc sexec(cmds: openarray[string]) =
  238. ## Serial queue wrapper around exec.
  239. for cmd in cmds:
  240. echo(cmd)
  241. let (outp, exitCode) = osproc.execCmdEx(cmd)
  242. if exitCode != 0: quit outp
  243. proc mexec(cmds: openarray[string]) =
  244. ## Multiprocessor version of exec
  245. let r = execProcesses(cmds, {poStdErrToStdOut, poParentStreams, poEchoCmd})
  246. if r != 0:
  247. echo "external program failed, retrying serial work queue for logs!"
  248. sexec(cmds)
  249. proc buildDocSamples(nimArgs, destPath: string) =
  250. ## Special case documentation sample proc.
  251. ##
  252. ## TODO: consider integrating into the existing generic documentation builders
  253. ## now that we have a single `doc` command.
  254. exec(findNim() & " doc $# -o:$# $#" %
  255. [nimArgs, destPath / "docgen_sample.html", "doc" / "docgen_sample.nim"])
  256. proc buildDoc(nimArgs, destPath: string) =
  257. # call nim for the documentation:
  258. var
  259. commands = newSeq[string](rst2html.len + len(doc0) + len(doc) + withoutIndex.len)
  260. i = 0
  261. let nim = findNim()
  262. for d in items(rst2html):
  263. commands[i] = nim & " rst2html $# --git.url:$# -o:$# --index:on $#" %
  264. [nimArgs, gitUrl,
  265. destPath / changeFileExt(splitFile(d).name, "html"), d]
  266. i.inc
  267. for d in items(doc0):
  268. commands[i] = nim & " doc0 $# --git.url:$# -o:$# --index:on $#" %
  269. [nimArgs, gitUrl,
  270. destPath / changeFileExt(splitFile(d).name, "html"), d]
  271. i.inc
  272. for d in items(doc):
  273. commands[i] = nim & " doc $# --git.url:$# -o:$# --index:on $#" %
  274. [nimArgs, gitUrl,
  275. destPath / changeFileExt(splitFile(d).name, "html"), d]
  276. i.inc
  277. for d in items(withoutIndex):
  278. commands[i] = nim & " doc2 $# --git.url:$# -o:$# $#" %
  279. [nimArgs, gitUrl,
  280. destPath / changeFileExt(splitFile(d).name, "html"), d]
  281. i.inc
  282. mexec(commands)
  283. exec(nim & " buildIndex -o:$1/theindex.html $1" % [destPath])
  284. proc buildPdfDoc*(nimArgs, destPath: string) =
  285. createDir(destPath)
  286. if os.execShellCmd("pdflatex -version") != 0:
  287. echo "pdflatex not found; no PDF documentation generated"
  288. else:
  289. const pdflatexcmd = "pdflatex -interaction=nonstopmode "
  290. for d in items(pdf):
  291. exec(findNim() & " rst2tex $# $#" % [nimArgs, d])
  292. # call LaTeX twice to get cross references right:
  293. exec(pdflatexcmd & changeFileExt(d, "tex"))
  294. exec(pdflatexcmd & changeFileExt(d, "tex"))
  295. # delete all the crappy temporary files:
  296. let pdf = splitFile(d).name & ".pdf"
  297. let dest = destPath / pdf
  298. removeFile(dest)
  299. moveFile(dest=dest, source=pdf)
  300. removeFile(changeFileExt(pdf, "aux"))
  301. if existsFile(changeFileExt(pdf, "toc")):
  302. removeFile(changeFileExt(pdf, "toc"))
  303. removeFile(changeFileExt(pdf, "log"))
  304. removeFile(changeFileExt(pdf, "out"))
  305. removeFile(changeFileExt(d, "tex"))
  306. proc buildJS() =
  307. exec(findNim() & " js -d:release --out:$1 tools/nimblepkglist.nim" %
  308. [webUploadOutput / "nimblepkglist.js"])
  309. exec(findNim() & " js " & (docHackDir / "dochack.nim"))
  310. proc buildDocs*(args: string) =
  311. let
  312. a = nimArgs & " " & args
  313. docHackJs = "dochack.js"
  314. docHackJsSource = docHackDir / "nimcache" / docHackJs
  315. docHackJsDest = docHtmlOutput / docHackJs
  316. buildJS() # This call generates docHackJsSource
  317. let docup = webUploadOutput / NimVersion
  318. createDir(docup)
  319. buildDocSamples(a, docup)
  320. buildDoc(a, docup)
  321. # 'nimArgs' instead of 'a' is correct here because we don't want
  322. # that the offline docs contain the 'gaCode'!
  323. createDir(docHtmlOutput)
  324. buildDocSamples(nimArgs, docHtmlOutput)
  325. buildDoc(nimArgs, docHtmlOutput)
  326. copyFile(docHackJsSource, docHackJsDest)
  327. copyFile(docHackJsSource, docup / docHackJs)