summaryrefslogtreecommitdiff
path: root/src/scaladoc/scala/tools/nsc/doc/html/HtmlFactory.scala
blob: 8313d842e5e02a086c2408d646631e9b16000112 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* NSC -- new Scala compiler
 * Copyright 2007-2013 LAMP/EPFL
 * @author  David Bernard, Manohar Jonnalagedda
 */

package scala.tools.nsc
package doc
package html

import model._
import java.io.{ File => JFile }
import io.{ Streamable, Directory }
import scala.collection._
import page.diagram._

/** A class that can generate Scaladoc sites to some fixed root folder.
  * @author David Bernard
  * @author Gilles Dubochet */
class HtmlFactory(val universe: doc.Universe, index: doc.Index) {

  /** The character encoding to be used for generated Scaladoc sites.
    * This value is currently always UTF-8. */
  def encoding: String = "UTF-8"

  def siteRoot: JFile = new JFile(universe.settings.outdir.value)

  def libResources = List(
    "index.js",
    "jquery-ui.js",
    "jquery.js",
    "jquery.layout.js",
    "scheduler.js",
    "diagrams.js",
    "template.js",
    "tools.tooltip.js",
    "modernizr.custom.js",

    "index.css",
    "ref-index.css",
    "template.css",
    "diagrams.css",

    "class.png",
    "class_big.png",
    "class_diagram.png",
    "object.png",
    "object_big.png",
    "object_diagram.png",
    "package.png",
    "package_big.png",
    "trait.png",
    "trait_big.png",
    "trait_diagram.png",
    "type.png",
    "type_big.png",
    "type_diagram.png",

    "class_to_object_big.png",
    "object_to_class_big.png",
    "trait_to_object_big.png",
    "object_to_trait_big.png",
    "type_to_object_big.png",
    "object_to_type_big.png",

    "arrow-down.png",
    "arrow-right.png",
    "filter_box_left.png",
    "filter_box_left2.gif",
    "filter_box_right.png",
    "filterbg.gif",
    "filterboxbarbg.gif",
    "filterboxbg.gif",

    "constructorsbg.gif",
    "defbg-blue.gif",
    "defbg-green.gif",
    "filterboxbarbg.png",
    "fullcommenttopbg.gif",
    "ownderbg2.gif",
    "ownerbg.gif",
    "ownerbg2.gif",
    "packagesbg.gif",
    "signaturebg.gif",
    "signaturebg2.gif",
    "typebg.gif",
    "conversionbg.gif",
    "valuemembersbg.gif",

    "navigation-li-a.png",
    "navigation-li.png",
    "remove.png",
    "selected-right.png",
    "selected.png",
    "selected2-right.png",
    "selected2.png",
    "selected-right-implicits.png",
    "selected-implicits.png",
    "unselected.png",

    "permalink.png"
  )

  /** Generates the Scaladoc site for a model into the site root.
    * A scaladoc site is a set of HTML and related files
    * that document a model extracted from a compiler run.
    */
  def generate() {

    def copyResource(subPath: String) {
      val bytes = new Streamable.Bytes {
        val p = "/scala/tools/nsc/doc/html/resource/" + subPath
        val inputStream = getClass.getResourceAsStream(p)
        assert(inputStream != null, p)
      }.toByteArray()
      val dest = Directory(siteRoot) / subPath
      dest.parent.createDirectory()
      val out = dest.toFile.bufferedOutput()
      try out.write(bytes, 0, bytes.length)
      finally out.close()
    }

    libResources foreach (s => copyResource("lib/" + s))

    new page.Index(universe, index) writeFor this
    new page.IndexScript(universe, index) writeFor this
    if (index.hasDeprecatedMembers)
      new page.DeprecatedIndex(universe, index) writeFor this
    try {
      writeTemplates(_ writeFor this)
      for (letter <- index.firstLetterIndex) {
        new html.page.ReferenceIndex(letter._1, index, universe) writeFor this
      }
    } finally {
      DiagramStats.printStats(universe.settings)
      universe.dotRunner.cleanup()
    }
  }

  def writeTemplates(writeForThis: HtmlPage => Unit) {
    val written = mutable.HashSet.empty[DocTemplateEntity]

    def writeTemplate(tpl: DocTemplateEntity) {
      if (!(written contains tpl)) {
        val diagramGenerator: DiagramGenerator = new DotDiagramGenerator(universe.settings, universe.dotRunner)
        writeForThis(new page.Template(universe, diagramGenerator, tpl))
        written += tpl
        tpl.templates collect { case d: DocTemplateEntity => d } map writeTemplate
      }
    }

    writeTemplate(universe.rootPackage)
  }
}