From 8a55733245ee64d1ca9e5caf2e43f7e2bedf236d Mon Sep 17 00:00:00 2001 From: James Strachan Date: Tue, 21 Feb 2012 12:44:23 +0000 Subject: [PATCH] moved template library into here temporarily (it maybe doesn't belong in the stdlib?) --- kdoc/kdoc.iml | 29 +++++- .../kotlin/org/jetbrains/kotlin/doc/KDoc.kt | 26 +++++- .../kotlin/doc/templates/IndexTemplate.kt | 56 +++++++++++ .../org/jetbrains/kotlin/model/KotlinModel.kt | 7 +- .../main/kotlin/std/template/TemplateCore.kt | 93 +++++++++++++++++++ .../org/jetbrains/kotlin/doc/ModelTest.kt | 23 +++-- .../org/jetbrains/kotlin/doc/TestAll.java | 28 ++++++ .../kotlin/template/TemplateCoreTest.kt | 45 +++++++++ 8 files changed, 292 insertions(+), 15 deletions(-) create mode 100644 kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/IndexTemplate.kt create mode 100644 kdoc/src/main/kotlin/std/template/TemplateCore.kt create mode 100644 kdoc/src/test/kotlin/org/jetbrains/kotlin/doc/TestAll.java create mode 100644 kdoc/src/test/kotlin/org/jetbrains/kotlin/template/TemplateCoreTest.kt diff --git a/kdoc/kdoc.iml b/kdoc/kdoc.iml index 172f564d709..9d8f6d6a742 100644 --- a/kdoc/kdoc.iml +++ b/kdoc/kdoc.iml @@ -4,11 +4,38 @@ + - + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDoc.kt b/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDoc.kt index f7afceddace..e450282390e 100644 --- a/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDoc.kt +++ b/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/KDoc.kt @@ -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) + } + } diff --git a/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/IndexTemplate.kt b/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/IndexTemplate.kt new file mode 100644 index 00000000000..f07e06c348e --- /dev/null +++ b/kdoc/src/main/kotlin/org/jetbrains/kotlin/doc/templates/IndexTemplate.kt @@ -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(""" + + + + + + +""") + print(model.title) + print(""") + + + + + + + + + + + + <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> + + + + """) + } +} diff --git a/kdoc/src/main/kotlin/org/jetbrains/kotlin/model/KotlinModel.kt b/kdoc/src/main/kotlin/org/jetbrains/kotlin/model/KotlinModel.kt index a435b025943..1acad7a725b 100644 --- a/kdoc/src/main/kotlin/org/jetbrains/kotlin/model/KotlinModel.kt +++ b/kdoc/src/main/kotlin/org/jetbrains/kotlin/model/KotlinModel.kt @@ -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() public val packageMap: SortedMap = TreeMap() diff --git a/kdoc/src/main/kotlin/std/template/TemplateCore.kt b/kdoc/src/main/kotlin/std/template/TemplateCore.kt new file mode 100644 index 00000000000..4dc0bce3642 --- /dev/null +++ b/kdoc/src/main/kotlin/std/template/TemplateCore.kt @@ -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()) + } +} diff --git a/kdoc/src/test/kotlin/org/jetbrains/kotlin/doc/ModelTest.kt b/kdoc/src/test/kotlin/org/jetbrains/kotlin/doc/ModelTest.kt index fd7ca3de3b8..09e3efad24f 100644 --- a/kdoc/src/test/kotlin/org/jetbrains/kotlin/doc/ModelTest.kt +++ b/kdoc/src/test/kotlin/org/jetbrains/kotlin/doc/ModelTest.kt @@ -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() } } diff --git a/kdoc/src/test/kotlin/org/jetbrains/kotlin/doc/TestAll.java b/kdoc/src/test/kotlin/org/jetbrains/kotlin/doc/TestAll.java new file mode 100644 index 00000000000..aee76f92ddd --- /dev/null +++ b/kdoc/src/test/kotlin/org/jetbrains/kotlin/doc/TestAll.java @@ -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); + } +} diff --git a/kdoc/src/test/kotlin/org/jetbrains/kotlin/template/TemplateCoreTest.kt b/kdoc/src/test/kotlin/org/jetbrains/kotlin/template/TemplateCoreTest.kt new file mode 100644 index 00000000000..7ad8d9d3b21 --- /dev/null +++ b/kdoc/src/test/kotlin/org/jetbrains/kotlin/template/TemplateCoreTest.kt @@ -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") + ) + } +} \ No newline at end of file