docgen2.nim 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module implements a new documentation generator that runs after
  10. # semantic checking.
  11. import
  12. os, options, ast, astalgo, msgs, ropes, idents, passes, docgen, lineinfos,
  13. pathutils
  14. from modulegraphs import ModuleGraph
  15. type
  16. TGen = object of TPassContext
  17. doc: PDoc
  18. module: PSym
  19. PGen = ref TGen
  20. template shouldProcess(g): bool =
  21. (g.module.owner.id == g.doc.conf.mainPackageId and optWholeProject in g.doc.conf.globalOptions) or
  22. sfMainModule in g.module.flags
  23. template closeImpl(body: untyped) {.dirty.} =
  24. var g = PGen(p)
  25. let useWarning = sfMainModule notin g.module.flags
  26. #echo g.module.name.s, " ", g.module.owner.id, " ", gMainPackageId
  27. if shouldProcess(g):
  28. body
  29. try:
  30. generateIndex(g.doc)
  31. except IOError:
  32. discard
  33. proc close(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  34. closeImpl:
  35. writeOutput(g.doc, useWarning)
  36. proc closeJson(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  37. closeImpl:
  38. writeOutputJson(g.doc, useWarning)
  39. proc processNode(c: PPassContext, n: PNode): PNode =
  40. result = n
  41. var g = PGen(c)
  42. if shouldProcess(g):
  43. generateDoc(g.doc, n, n)
  44. proc processNodeJson(c: PPassContext, n: PNode): PNode =
  45. result = n
  46. var g = PGen(c)
  47. if shouldProcess(g):
  48. generateJson(g.doc, n)
  49. proc myOpen(graph: ModuleGraph; module: PSym): PPassContext =
  50. var g: PGen
  51. new(g)
  52. g.module = module
  53. var d = newDocumentor(AbsoluteFile toFullPath(graph.config, FileIndex module.position),
  54. graph.cache, graph.config)
  55. d.hasToc = true
  56. g.doc = d
  57. result = g
  58. const docgen2Pass* = makePass(open = myOpen, process = processNode, close = close)
  59. const docgen2JsonPass* = makePass(open = myOpen, process = processNodeJson,
  60. close = closeJson)
  61. proc finishDoc2Pass*(project: string) =
  62. discard