-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.scala
251 lines (218 loc) · 7.75 KB
/
generator.scala
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import net.htmlparser.jericho._
import scala.collection.mutable._
import scala.collection.jcl.Conversions._
import scala.util.matching._
import java.io._
class Dir(pathname: String) {
val path = new File(pathname)
def glob(pattern: Regex): Array[File] = {
val result = new ArrayBuffer[File]
for(file <- path.listFiles){
if( file.isDirectory ){
result ++= new Dir(file.getPath).glob(pattern)
}
else{
pattern.findFirstIn(file.getName) match {
case Some(s) => result += file
case _ =>
}
}
}
result.toArray
}
}
class MethodInfo( val name:String, val url:String ){
override def toString:String = "method_name: " + name + "; " + "method_url: " + url
}
case class ClazzType
case class ClassType extends ClazzType
case class ObjectType extends ClazzType
case class TraitType extends ClazzType
case class UnknownType extends ClazzType
case class PkgInfo( val name:String, url:String ) {
override def toString:String = {
return name + "; url = " + url
}
}
class ClassInfo( val name:String, val url:String, val pkg:PkgInfo, val methods:List[MethodInfo], val class_type:ClazzType ){
override def toString:String = {
"name: " + name + "\n" +
"url: " + url + "\n" +
"pkg: " + pkg.toString + "\n" +
"methods: " + methods.mkString("\n") + "\n" +
"class_type: " + class_type
}
}
def parsePkgName( src:Source ):Option[String] = {
val elements = src.getAllElements("class", "entity", false)
for( elem <- elements ){
val a = elem.getFirstElement(HTMLElementName.A)
if( a != null ){
return Some( a.getTextExtractor.toString.replaceAll("\\.[A-Z].*", "") )
}
}
return None
}
def parseClassName( src:Source ):Option[(String, ClazzType)] = {
val elements = src.getAllElements("class", "entity", false)
for( elem <- elements ){
val a = elem.getFirstElement(HTMLElementName.A)
if( a == null ){
"(.*)\\s+(.*)".r.findFirstMatchIn(elem.getTextExtractor.toString) match {
case Some(m) =>
val class_type:ClazzType = m.group(1) match {
case "object" => ObjectType()
case "class" => ClassType()
case "trait" => TraitType()
case _ => UnknownType()
}
return Some((m.group(2), class_type))
case _ => return None
}
}
}
return None
}
def parseMehodName( src:Source, base_dir:String ):Option[List[MethodInfo]] = {
val elements = src.getAllElements("class", "member", false)
var results = List[MethodInfo]()
for( elem <- elements ){
val tr = elem.getFirstElement(HTMLElementName.TR)
".*Method Summary.*".r.findFirstMatchIn( tr.getTextExtractor.toString ) match {
case Some(m) =>
val signatures = elem.getAllElements("class", "signature", false)
for( sig <- signatures ){
val em = sig.getFirstElement(HTMLElementName.EM)
val a = em.getFirstElement(HTMLElementName.A)
val url = base_dir + a.getAttributeValue("href")
val name = em.getTextExtractor.toString
results += new MethodInfo(name, url.replaceAll("\\.\\./", ""))
}
case _ =>
}
}
if( results.isEmpty )
return None
else
return Some(results)
}
val map = new HashMap[PkgInfo, List[ClassInfo]]();
val base_dir = "scala-2.7.7.final-devel-docs/api/"
for( file <- new Dir(base_dir).glob(".*\\.html".r) ){
val src = new Source(new FileInputStream(file))
val pkg = parsePkgName( src ) match {
case Some(pkg_str) => pkg_str
case _ => ""
}
val classtuple = parseClassName( src ) match {
case Some(classtuple) => classtuple
case _ => ("", UnknownType())
}
val methods = parseMehodName(src, base_dir) match {
case Some(mis) => mis
case _ => List()
}
val ci = new ClassInfo( classtuple._1, file.toString, PkgInfo(pkg, base_dir + pkg.replaceAll("\\.", "/") + "$content.html"), methods, classtuple._2)
if( ci.name != "" ){
map.get( ci.pkg ) match {
case Some( l ) =>
map += ( ci.pkg -> ( l ++ List(ci) ) )
case _ =>
map += ( ci.pkg -> List(ci) )
}
}
}
def makeIndexFile( map:Map[PkgInfo, List[ClassInfo]], file:String ){
val f = new FileWriter( file )
f.write("""|<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">
|<HTML>
|<HEAD>
|<!-- Sitemap 1.0 -->
|</HEAD><BODY>
|<UL>
|""".stripMargin )
for( kv <- map ){
for( c <- kv._2 ){
f.write("<LI><OBJECT type=\"text/sitemap\">\n")
f.write("<param name=\"Name\" value=\"" + c.name + "\">\n")
f.write("<param name=\"Local\" value=\"" + c.url + "\">\n")
f.write("</OBJECT></LI>\n")
for( m <- c.methods ){
val op = c.class_type match {
case ClassType() => "#"
case TraitType() => "#"
case ObjectType() => "."
}
f.write("<LI><OBJECT type=\"text/sitemap\">\n")
f.write("<param name=\"Name\" value=\"" + m.name + "\">\n")
f.write("<param name=\"Name\" value=\"" + c.name + op + m.name +"\">\n")
f.write("<param name=\"Local\" value=\"" + m.url + "\">\n")
f.write("</OBJECT></LI>\n")
f.write("<LI><OBJECT type=\"text/sitemap\">\n")
f.write("<param name=\"Name\" value=\"" + c.name + op + m.name +"\">\n")
f.write("<param name=\"Local\" value=\"" + m.url + "\">\n")
f.write("</OBJECT></LI>\n")
}
}
}
f.write("</UL>\n</BODY></HTML>")
f.close
}
def makeTableOfContentsFile( map:Map[PkgInfo, List[ClassInfo]], base_dir:String, file:String ){
val f = new FileWriter( file )
f.write("""|<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|<HTML>
|<HEAD>
|<!-- Sitemap 1.0 -->
|</HEAD><BODY>
|""".stripMargin )
f.write("<UL>\n<LI><OBJECT type=\"text/sitemap\">\n")
f.write("<param name=\"Name\" value=\"scala-api\">\n")
f.write("<param name=\"Local\" value=\"" + base_dir + "root-content.html\">\n")
f.write("</OBJECT>\n")
for( p <- map.keys.toList.sort( (a,b) => a.name.compareTo(b.name) < 0 ) ){
//for( kv <- map ){
f.write("<UL>\n<LI><OBJECT type=\"text/sitemap\">\n")
f.write("<param name=\"Name\" value=\"" + p.name + "\">\n")
f.write("<param name=\"Local\" value=\"" + p.url + "\">\n")
f.write("</OBJECT>\n")
f.write("<UL>\n")
val cs = map.getOrElse(p, List())
for( c <- cs.sort( (a,b) => a.name.compareTo(b.name) < 0 ) ){
f.write("<LI><OBJECT type=\"text/sitemap\">\n")
f.write("<param name=\"Name\" value=\"" + c.name + "\">\n")
f.write("<param name=\"Local\" value=\"" + c.url + "\">\n")
f.write("</OBJECT>\n</LI>\n")
}
f.write("</UL>\n")
f.write("</LI>\n</UL>\n")
}
f.write("</LI>\n</UL>\n</BODY></HTML>")
f.close
}
def makeProjectFile( base_dir:String, file:String ) {
val f = new FileWriter( file )
f.write("""|[OPTIONS]
|Compatibility=1.1 or later
|Compiled file=scala_api.chm
|Contents file=toc.hhc
|Default Window=titlewindow
|Default topic=scala-2.7.7.final-devel-docs/api/root-content.html
|Display compile progress=Yes
|Full-text search=Yes
|Index file=index.hhk
|Language=0x409
|Title=Scala-API
|
|[WINDOWS]
|titlewindow="Scala-API","toc.hhc","index.hhk",,,,,,,0x20420,,0x307e,,,,,,,,0
|""".stripMargin)
f.write("[FILES]\n")
for( content <- new Dir(base_dir).glob(".*".r) ){
f.write( content.toString + "\n" )
}
f.close
}
makeTableOfContentsFile( map, base_dir, "toc.hhc" )
makeIndexFile( map, "index.hhk" )
makeProjectFile( base_dir, "scala_api.hhp")