docgen2.nim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. os, options, ast, astalgo, msgs, ropes, idents, passes, docgen
  13. from modulegraphs import ModuleGraph
  14. type
  15. TGen = object of TPassContext
  16. doc: PDoc
  17. module: PSym
  18. PGen = ref TGen
  19. template closeImpl(body: untyped) {.dirty.} =
  20. var g = PGen(p)
  21. let useWarning = sfMainModule notin g.module.flags
  22. #echo g.module.name.s, " ", g.module.owner.id, " ", gMainPackageId
  23. if (g.module.owner.id == gMainPackageId and gWholeProject) or
  24. sfMainModule in g.module.flags:
  25. body
  26. try:
  27. generateIndex(g.doc)
  28. except IOError:
  29. discard
  30. proc close(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  31. closeImpl:
  32. writeOutput(g.doc, g.module.filename, HtmlExt, useWarning)
  33. proc closeJson(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  34. closeImpl:
  35. writeOutputJson(g.doc, g.module.filename, ".json", useWarning)
  36. proc processNode(c: PPassContext, n: PNode): PNode =
  37. result = n
  38. var g = PGen(c)
  39. generateDoc(g.doc, n)
  40. proc processNodeJson(c: PPassContext, n: PNode): PNode =
  41. result = n
  42. var g = PGen(c)
  43. generateJson(g.doc, n)
  44. proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
  45. var g: PGen
  46. new(g)
  47. g.module = module
  48. var d = newDocumentor(module.filename, options.gConfigVars)
  49. d.hasToc = true
  50. g.doc = d
  51. result = g
  52. const docgen2Pass* = makePass(open = myOpen, process = processNode, close = close)
  53. const docgen2JsonPass* = makePass(open = myOpen, process = processNodeJson,
  54. close = closeJson)
  55. proc finishDoc2Pass*(project: string) =
  56. discard