docgen2.nim 2.2 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. options, ast, msgs, passes, docgen, lineinfos, pathutils
  13. from modulegraphs import ModuleGraph, PPassContext
  14. type
  15. TGen = object of PPassContext
  16. doc: PDoc
  17. module: PSym
  18. config: ConfigRef
  19. PGen = ref TGen
  20. proc shouldProcess(g: PGen): bool =
  21. (optWholeProject in g.doc.conf.globalOptions and g.module.getnimblePkgId == g.doc.conf.mainPackageId) or
  22. sfMainModule in g.module.flags or g.config.projectMainIdx == g.module.info.fileIndex
  23. template closeImpl(body: untyped) {.dirty.} =
  24. var g = PGen(p)
  25. let useWarning = sfMainModule notin g.module.flags
  26. if shouldProcess(g):
  27. body
  28. try:
  29. generateIndex(g.doc)
  30. except IOError:
  31. discard
  32. proc close(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  33. closeImpl:
  34. writeOutput(g.doc, useWarning)
  35. proc closeJson(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  36. closeImpl:
  37. writeOutputJson(g.doc, useWarning)
  38. proc processNode(c: PPassContext, n: PNode): PNode =
  39. result = n
  40. var g = PGen(c)
  41. if shouldProcess(g):
  42. generateDoc(g.doc, n, n)
  43. proc processNodeJson(c: PPassContext, n: PNode): PNode =
  44. result = n
  45. var g = PGen(c)
  46. if shouldProcess(g):
  47. generateJson(g.doc, n, false)
  48. template myOpenImpl(ext: untyped) {.dirty.} =
  49. var g: PGen
  50. new(g)
  51. g.module = module
  52. g.config = graph.config
  53. var d = newDocumentor(AbsoluteFile toFullPath(graph.config, FileIndex module.position),
  54. graph.cache, graph.config, ext, module)
  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