docgen2.nim 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. 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, g.config)
  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 openHtml*(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  60. myOpenImpl(HtmlExt)
  61. proc openTex*(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  62. myOpenImpl(TexExt)
  63. proc openJson*(graph: ModuleGraph; module: PSym; idgen: IdGenerator): PPassContext =
  64. myOpenImpl(JsonExt)