docgen2.nim 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. let groupedToc = true
  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, groupedToc)
  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. g.config = graph.config
  54. var d = newDocumentor(AbsoluteFile toFullPath(graph.config, FileIndex module.position),
  55. graph.cache, graph.config, ext, module)
  56. d.hasToc = true
  57. g.doc = d
  58. result = g
  59. proc myOpen(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  60. myOpenImpl(HtmlExt)
  61. proc myOpenJson(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  62. myOpenImpl(JsonExt)
  63. const docgen2Pass* = makePass(open = myOpen, process = processNode, close = close)
  64. const docgen2JsonPass* = makePass(open = myOpenJson, process = processNodeJson,
  65. close = closeJson)
  66. proc finishDoc2Pass*(project: string) =
  67. discard