docgen2.nim 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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, packages
  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.doc.conf.belongsToProjectPackage(g.module)) 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. finishGenerateDoc(g.doc)
  29. body
  30. try:
  31. generateIndex(g.doc)
  32. except IOError:
  33. discard
  34. proc close(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  35. closeImpl:
  36. writeOutput(g.doc, useWarning, groupedToc)
  37. proc closeJson(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  38. closeImpl:
  39. writeOutputJson(g.doc, useWarning)
  40. proc processNode(c: PPassContext, n: PNode): PNode =
  41. result = n
  42. var g = PGen(c)
  43. if shouldProcess(g):
  44. generateDoc(g.doc, n, n)
  45. proc processNodeJson(c: PPassContext, n: PNode): PNode =
  46. result = n
  47. var g = PGen(c)
  48. if shouldProcess(g):
  49. generateJson(g.doc, n, false)
  50. template myOpenImpl(ext: untyped) {.dirty.} =
  51. var g: PGen
  52. new(g)
  53. g.module = module
  54. g.config = graph.config
  55. var d = newDocumentor(AbsoluteFile toFullPath(graph.config, FileIndex module.position),
  56. graph.cache, graph.config, ext, module, hasToc = true)
  57. g.doc = d
  58. result = g
  59. proc myOpen(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  60. myOpenImpl(HtmlExt)
  61. proc myOpenTex(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  62. myOpenImpl(TexExt)
  63. proc myOpenJson(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  64. myOpenImpl(JsonExt)
  65. const docgen2Pass* = makePass(open = myOpen, process = processNode, close = close)
  66. const docgen2TexPass* = makePass(open = myOpenTex, process = processNode,
  67. close = close)
  68. const docgen2JsonPass* = makePass(open = myOpenJson, process = processNodeJson,
  69. close = closeJson)
  70. proc finishDoc2Pass*(project: string) =
  71. discard