docgen2.nim 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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, 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 closeDoc*(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  35. result = nil
  36. closeImpl:
  37. writeOutput(g.doc, useWarning, groupedToc)
  38. proc closeJson*(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  39. result = nil
  40. closeImpl:
  41. writeOutputJson(g.doc, useWarning)
  42. proc processNode*(c: PPassContext, n: PNode): PNode =
  43. result = n
  44. var g = PGen(c)
  45. if shouldProcess(g):
  46. generateDoc(g.doc, n, n, g.config)
  47. proc processNodeJson*(c: PPassContext, n: PNode): PNode =
  48. result = n
  49. var g = PGen(c)
  50. if shouldProcess(g):
  51. generateJson(g.doc, n, g.config, false)
  52. template myOpenImpl(ext: untyped) {.dirty.} =
  53. var g: PGen
  54. new(g)
  55. g.module = module
  56. g.config = graph.config
  57. var d = newDocumentor(AbsoluteFile toFullPath(graph.config, FileIndex module.position),
  58. graph.cache, graph.config, ext, module, hasToc = true)
  59. g.doc = d
  60. result = g
  61. proc openHtml*(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  62. myOpenImpl(HtmlExt)
  63. proc openTex*(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  64. myOpenImpl(TexExt)
  65. proc openJson*(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  66. myOpenImpl(JsonExt)