moved template library into here temporarily (it maybe doesn't belong in the stdlib?)

This commit is contained in:
James Strachan
2012-02-21 12:44:23 +00:00
parent a403af575c
commit 8a55733245
8 changed files with 292 additions and 15 deletions
+28 -1
View File
@@ -4,11 +4,38 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/kotlin" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/kotlin" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="stdlib" exported="" />
<orderEntry type="module" module-name="frontend" exported="" />
<orderEntry type="module-library" exported="" scope="TEST">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../testlib/lib/junit-4.9.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../dist/kotlinc/lib/intellij-core.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../dist/kotlinc/lib/kotlin-compiler.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
@@ -1,4 +1,28 @@
package org.jetbrains.kotlin.doc
class KDocProcessor {
import std.template.*
import org.jetbrains.kotlin.doc.templates.*
import org.jetbrains.kotlin.model.KModel
import java.io.File
class KDocProcessor(val model: KModel, val outputDir: File) {
fun execute(): Unit {
run("index.html", IndexTemplate(model))
}
protected fun run(fileName: String, template: TextTemplate): Unit {
val file = File(outputDir, fileName)
file.getParentFile()?.mkdirs()
log("Generating $fileName")
template.renderTo(file)
}
protected fun log(text: String) {
println(text)
}
}
@@ -0,0 +1,56 @@
package org.jetbrains.kotlin.doc.templates
import std.*
import std.template.*
import std.io.*
import std.util.*
import java.util.*
import org.jetbrains.kotlin.model.KModel
class IndexTemplate(val model: KModel) : TextTemplate() {
override fun render() {
print("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<!--NewPage-->
<HTML>
<HEAD>
<!-- Generated by javadoc on Mon Jan 09 13:32:00 EST 2012-->
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
""")
print(model.title)
print(""")
</TITLE>
<SCRIPT type="text/javascript">
targetPage = "" + window.location.search;
if (targetPage != "" && targetPage != "undefined")
targetPage = targetPage.substring(1);
if (targetPage.indexOf(":") != -1)
targetPage = "undefined";
function loadFrames() {
if (targetPage != "" && targetPage != "undefined")
top.classFrame.location = top.targetPage;
}
</SCRIPT>
<NOSCRIPT>
</NOSCRIPT>
</HEAD>
<FRAMESET cols="20%,80%" title="" onLoad="top.loadFrames()">
<FRAMESET rows="30%,70%" title="" onLoad="top.loadFrames()">
<FRAME src="overview-frame.html" name="packageListFrame" title="All Packages">
<FRAME src="allclasses-frame.html" name="packageFrame" title="All classes and interfaces (except non-static nested types)">
</FRAMESET>
<FRAME src="overview-summary.html" name="classFrame" title="Package, class and interface descriptions" scrolling="yes">
<NOFRAMES>
<H2>
Frame Alert</H2>
<P>
This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client.
<BR>
Link to<A HREF="overview-summary.html">Non-frame version.</A>
</NOFRAMES>
</FRAMESET>
</HTML>
""")
}
}
@@ -1,13 +1,12 @@
package org.jetbrains.kotlin.model
import std.*
import java.lang.String
//import std.*
import std.util.*
import java.util.*
import org.jetbrains.jet.lang.psi.*
class KModel {
class KModel(var title: String = "Documentation") {
// TODO generates java.lang.NoSuchMethodError: std.util.namespace.hashMap(Ljet/TypeInfo;Ljet/TypeInfo;)Ljava/util/HashMap;
//val packages = sortedMap<String,KPackage>()
public val packageMap: SortedMap<String,KPackage> = TreeMap<String,KPackage>()
@@ -0,0 +1,93 @@
package std.template
import std.io.*
import java.io.Writer
import java.io.StringWriter
import java.io.OutputStream
import java.io.OutputStreamWriter
import java.io.FileWriter
import java.io.File
/**
* Represents a generic API to templates which should be usable
* in JavaScript in a browser or on the server side stand alone or in a web app etc.
*
* To make things easier to implement in JS this namespace won't refer to any java.io or servlet
* stuff
*/
trait Template {
var printer: Printer
/** Renders the template to the output printer */
fun render(): Unit
}
/**
* Represents the output of a template which is a stream of objects
* usually strings which then write to some underlying output stream
* such as a java.io.Writer on the server side.
*
* We abstract away java.io.* APIs here to make the JS implementation simpler
*/
trait Printer {
fun print(value: Any): Unit
//fun print(text: String): Unit
}
class NullPrinter() : Printer {
//override fun print(text: String) = none()
override fun print(value: Any) {
throw UnsupportedOperationException("No Printer defined on the Template")
}
}
/**
* Base class for template implementations to hold any helpful behaviour
*/
abstract class TemplateSupport : Template {
}
/**
* Base class for templates generating text output
* by printing values to some underlying Printer which
* will typically output to a String, File, stream etc.
*/
abstract class TextTemplate() : TemplateSupport(), Printer {
override var printer: Printer = NullPrinter()
fun String.plus(): Unit {
printer.print(this)
}
//override fun print(value: String) = printer.print(value)
override fun print(value: Any) = printer.print(value)
fun renderToText(): String {
val buffer = StringWriter()
renderTo(buffer)
return buffer.toString().sure()
}
fun renderTo(writer: Writer): Unit {
this.printer = WriterPrinter(writer)
this.render()
}
fun renderTo(os: OutputStream): Unit = renderTo(OutputStreamWriter(os))
fun renderTo(file: File): Unit = renderTo(FileWriter(file))
}
/**
* A Printer implementation which uses a Writer
*/
class WriterPrinter(val writer: Writer) : Printer {
override fun print(value: Any) {
// TODO should be using a formatter to do the conversion
writer.write(value.toString())
}
}
@@ -6,8 +6,11 @@ import std.util.*
import org.jetbrains.kotlin.model.*
import junit.framework.TestCase
import junit.framework.Assert
// TODO should use the ktest library really for nicer asserts
import junit.framework.Assert.assertEquals
import org.jetbrains.kotlin.doc.KDocProcessor
import java.io.File
class ModelTest : TestCase() {
val model = KModel()
@@ -18,10 +21,9 @@ class ModelTest : TestCase() {
println("Got class: $c")
// TODO should use the ktest library really for nicer asserts
Assert.assertEquals("foo.bar.Cheese", c.name)
Assert.assertEquals("foo.bar", c.packageName)
Assert.assertEquals("Cheese", c.simpleName)
assertEquals("foo.bar.Cheese", c.name)
assertEquals("foo.bar", c.packageName)
assertEquals("Cheese", c.simpleName)
}
fun testGetClassViaQualifiedName() {
@@ -29,15 +31,15 @@ class ModelTest : TestCase() {
println("Got class: $c")
// TODO should use the ktest library really for nicer asserts
Assert.assertEquals("something.else.Foo", c.name)
Assert.assertEquals("something.else", c.packageName)
Assert.assertEquals("Foo", c.simpleName)
assertEquals("something.else.Foo", c.name)
assertEquals("something.else", c.packageName)
assertEquals("Foo", c.simpleName)
}
fun testModelWalk() {
model.getClass("something.else.Aaa")
model.getClass("something.else.Zzz")
model.getClass("another.Cheese")
// now lets iterate through the model
println("Walking the model...")
@@ -48,5 +50,8 @@ class ModelTest : TestCase() {
}
println()
}
val processor = KDocProcessor(model, File("target/test-data/ModelTest"))
processor.execute()
}
}
@@ -0,0 +1,28 @@
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.doc;
import junit.framework.TestSuite;
/**
*/
public class TestAll {
public static TestSuite suite() {
return TestSuite(ModelTest.class);
}
}
@@ -0,0 +1,45 @@
package test.org.jetbrains.kotlin.template
import std.*
import std.template.*
import std.io.*
import std.util.*
import java.util.*
import junit.framework.TestCase
import junit.framework.Assert.*
class EmailTemplate(var name: String = "James", var time: Date = Date()) : TextTemplate() {
override fun render() {
print("Hello there $name and how are you? Today is $time. Kotlin rocks")
}
}
class MoreDryTemplate(var name: String = "James", var time: Date = Date()) : TextTemplate() {
override fun render() {
+"Hey there $name and how are you? Today is $time. Kotlin rocks"
}
}
class TemplateCoreTest() : TestCase() {
fun testDefaultValues() {
val text = EmailTemplate().renderToText()
assertTrue(
text.startsWith("Hello there James")
)
}
fun testDifferentValues() {
val text = EmailTemplate("Andrey").renderToText()
assertTrue(
text.startsWith("Hello there Andrey")
)
}
fun testMoreDryTemplate() {
val text = MoreDryTemplate("Alex").renderToText()
assertTrue(
text.startsWith("Hey there Alex")
)
}
}