docgen2.nim 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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, PPassContext
  15. type
  16. TGen = object of PPassContext
  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, false)
  49. template myOpenImpl(ext: untyped) {.dirty.} =
  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, ext)
  55. d.hasToc = true
  56. g.doc = d
  57. result = g
  58. proc myOpen(graph: ModuleGraph; module: PSym): PPassContext =
  59. myOpenImpl(HtmlExt)
  60. proc myOpenJson(graph: ModuleGraph; module: PSym): PPassContext =
  61. myOpenImpl(JsonExt)
  62. const docgen2Pass* = makePass(open = myOpen, process = processNode, close = close)
  63. const docgen2JsonPass* = makePass(open = myOpenJson, process = processNodeJson,
  64. close = closeJson)
  65. proc finishDoc2Pass*(project: string) =
  66. discard