summary refs log tree commit diff stats
path: root/compiler/docgen2.nim
blob: 2d63220deda5855d15df10218d1cddd0d3d64724 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#
#
#           The Nim Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This module implements a new documentation generator that runs after
# semantic checking.

import
  os, options, ast, astalgo, msgs, ropes, idents, passes, docgen, lineinfos

from modulegraphs import ModuleGraph

type
  TGen = object of TPassContext
    doc: PDoc
    module: PSym
  PGen = ref TGen

template closeImpl(body: untyped) {.dirty.} =
  var g = PGen(p)
  let useWarning = sfMainModule notin g.module.flags
  #echo g.module.name.s, " ", g.module.owner.id, " ", gMainPackageId
  if (g.module.owner.id == g.doc.conf.mainPackageId and optWholeProject in g.doc.conf.globalOptions) or
      sfMainModule in g.module.flags:
    body
    try:
      generateIndex(g.doc)
    except IOError:
      discard

proc close(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  closeImpl:
    writeOutput(g.doc, toFilename(graph.config, FileIndex g.module.position), HtmlExt, useWarning)

proc closeJson(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  closeImpl:
    writeOutputJson(g.doc, toFilename(graph.config, FileIndex g.module.position), ".json", useWarning)

proc processNode(c: PPassContext, n: PNode): PNode =
  result = n
  var g = PGen(c)
  generateDoc(g.doc, n)

proc processNodeJson(c: PPassContext, n: PNode): PNode =
  result = n
  var g = PGen(c)
  generateJson(g.doc, n)

proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
  var g: PGen
  new(g)
  g.module = module
  var d = newDocumentor(toFilename(graph.config, FileIndex module.position), graph.config)
  d.hasToc = true
  g.doc = d
  result = g

const docgen2Pass* = makePass(open = myOpen, process = processNode, close = close)
const docgen2JsonPass* = makePass(open = myOpen, process = processNodeJson,
                                  close = closeJson)

proc finishDoc2Pass*(project: string) =
  discard