Compare commits

...

28 Commits

Author SHA1 Message Date
Robert Winkler
23d1db4fd5 Fixed markup-document-builder version dependency 2015-03-23 08:59:49 +01:00
Robert Winkler
903d3ed452 Merge pull request #3 from RobWin/master
fixed wrong dependency
2015-03-17 12:11:42 +01:00
Robert Winkler
1ad78ba9f6 fixed wrong dependency 2015-03-17 12:01:38 +01:00
Robert Winkler
82416a33cc Fixed maven POM issue 2015-02-20 11:02:23 +01:00
Robert Winkler
e67b32d537 Merge branch 'master' of https://github.com/RobWin/swagger2markup 2015-02-20 10:13:32 +01:00
Robert Winkler
dbb6f9ace8 Merge branch 'release/0.2.1' 2015-02-20 10:13:01 +01:00
Robert Winkler
19384f97b9 Published to Maven Central 2015-02-20 10:12:28 +01:00
Robert Winkler
90de0a3c6a Fixed build 2015-02-20 09:54:56 +01:00
Robert Winkler
ae77e814f7 Updated Bintry badge 2015-02-20 09:53:37 +01:00
Robert Winkler
d165a0f842 Added GPG Signing and Maven Central Sync 2015-02-20 09:47:47 +01:00
Robert Winkler
0833a74c37 Updated documentation 2015-02-20 08:50:36 +01:00
Robert Winkler
4ec7ecf25b Updated documentation 2015-02-20 08:46:30 +01:00
Robert Winkler
99dd062fc9 Added markup-document-builder library and removed Builder from project
Updated documentation
2015-02-20 08:41:17 +01:00
Robert Winkler
52462466ff Added markup-document-builder library 2015-02-20 08:24:09 +01:00
Robert Winkler
d2c69abb71 Updated wrong groupId 2015-02-19 20:22:28 +01:00
Robert Winkler
11d4311986 Updated documentation 2015-02-19 16:38:21 +01:00
Robert Winkler
e2f1bca8f1 Removed header 2015-02-19 15:48:17 +01:00
Robert Winkler
1db1af705b Updated documentation 2015-02-19 14:49:09 +01:00
Robert Winkler
dd140a532b Updated documentation 2015-02-19 14:17:56 +01:00
Robert Winkler
1f51a9bc91 Merge branch 'develop' 2015-02-19 14:15:16 +01:00
Robert Winkler
5f28eb4073 Updated documentation 2015-02-19 14:15:05 +01:00
Robert Winkler
97541bfc83 Updated documentation 2015-02-19 14:14:17 +01:00
Robert Winkler
9144155200 Merge branch 'develop' 2015-02-19 14:07:20 +01:00
Robert Winkler
84d64ec6f6 Updated documentation 2015-02-19 14:07:07 +01:00
Robert Winkler
e7c9ed54db Merge branch 'develop' 2015-02-19 11:59:39 +01:00
Robert Winkler
44838f0b6f Updated maven repository 2015-02-19 11:59:28 +01:00
Robert Winkler
d7efd403c7 Merge branch 'release/v0.2.0' 2015-02-19 11:38:31 +01:00
Robert Winkler
31a4c21aa1 Merge branch 'release/v0.2.0' into develop 2015-02-19 11:38:31 +01:00
19 changed files with 173 additions and 600 deletions

View File

@@ -1,19 +1,23 @@
= Swagger2Markup
:author: Robert Winkler
:version: 0.1.0
:version: 0.2.1
:hardbreaks:
image:https://travis-ci.org/RobWin/swagger2markup.svg["Build Status", link="https://travis-ci.org/RobWin/swagger2markup"] image:https://coveralls.io/repos/RobWin/swagger2markup/badge.svg["Coverage Status", link="https://coveralls.io/r/RobWin/swagger2markup"] image:http://img.shields.io/:version-{version}-blue.svg["Semantic Versioning", link="https://bintray.com/robwin/maven/swagger2markup/0.1.0/view"] image:http://img.shields.io/badge/license-ASF2-blue.svg["Apache License 2", link="http://www.apache.org/licenses/LICENSE-2.0.txt"]
image:https://travis-ci.org/RobWin/swagger2markup.svg["Build Status", link="https://travis-ci.org/RobWin/swagger2markup"] image:https://coveralls.io/repos/RobWin/swagger2markup/badge.svg["Coverage Status", link="https://coveralls.io/r/RobWin/swagger2markup"] image:https://api.bintray.com/packages/robwin/maven/swagger2markup/images/download.svg[link="https://bintray.com/robwin/maven/swagger2markup/_latestVersion"] image:http://img.shields.io/badge/license-ASF2-blue.svg["Apache License 2", link="http://www.apache.org/licenses/LICENSE-2.0.txt"]
== Overview
This project is a Swagger to Markup (AsciiDoc and Markdown) converter. The *Swagger2MarkupConverter* takes a swagger.json or swagger.yaml file as source and converts it into an AsciiDoc or Markdown document. The Swagger source file can be located locally or remotely accessible via HTTP. The Swagger2MarkupConverter supports the Swagger 1.2 and 2.0 specification. Internally it uses the _official_ https://github.com/swagger-api/swagger-parser[swagger-parser].
This project is a Swagger to Markup (AsciiDoc and Markdown) converter. The *Swagger2MarkupConverter* takes a swagger.json or swagger.yaml file as source and converts it into an AsciiDoc or Markdown document. The Swagger source file can be located locally or remotely accessible via HTTP. The Swagger2MarkupConverter supports the Swagger 1.2 and 2.0 specification. Internally it uses the _official_ https://github.com/swagger-api/swagger-parser[swagger-parser] and my https://github.com/RobWin/markup-document-builder[markup-document-builder].
The primary goal of this project is to simplify the documentation of RESTful APIs. The result is intended to be an easy-to-read, on- and offline user guide, comparable to https://developer.github.com/v3/[GitHub's API documentation].
Swagger2Markup can be used together with https://github.com/spring-projects/spring-restdocs[spring-restdocs]. See <<integration-with-spring-restdocs, Integration with spring-restdocs>>.
The project requires at least JDK 7.
== Usage
=== Adding Swagger2Markup to your project
The project is published in JCenter and Maven Central.
==== Maven
@@ -21,16 +25,19 @@ The primary goal of this project is to simplify the documentation of RESTful API
----
<repositories>
<repository>
<id>jcenter-release</id>
<name>jcenter</name>
<url>http://oss.jfrog.org/artifactory/oss-release-local/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>bintray</name>
<url>http://jcenter.bintray.com</url>
</repository>
</repositories>
<dependency>
<groupId>io.github.robwin.swagger2markup</groupId>
<groupId>io.github.robwin</groupId>
<artifactId>swagger2markup</artifactId>
<version>0.2.0</version>
<version>0.2.1</version>
</dependency>
----
@@ -42,7 +49,7 @@ repositories {
jcenter()
}
compile "io.github.robwin:swagger2markup:0.2.0"
compile "io.github.robwin:swagger2markup:0.2.1"
----
=== Using Swagger2Markup
@@ -60,17 +67,19 @@ public class Swagger2MarkupTest {
@Test
public void convertSwaggerToMarkup() {
//Remote
//Remote Swagger source
//Markdown
Swagger2MarkupConverter.from("http://localhost:8080/api-docs").
withMarkupLanguage(MarkupLanguage.MARKDOWN).
withExamples("docs/generated").withSchemas("docs/schemas").build()
withMarkupLanguage(MarkupLanguage.MARKDOWN).build()
.intoFolder("src/docs/markdown");
Swagger2MarkupConverter.from("http://localhost:8080/api-docs").
withExamples("docs").withSchemas("docs/schemas").build()
//Remote Swagger source
//Default is AsciiDoc
Swagger2MarkupConverter.from("http://localhost:8080/api-docs").build()
.intoFolder("src/docs/asciidoc");
//Local
//Local Swagger source
//Default is AsciiDoc
File file = new File(Swagger2MarkupTest.class.getResource("/json/swagger.json").getFile());
Swagger2MarkupConverter.from(file.getAbsolutePath()).build()
.intoFolder("src/docs/asciidoc");
@@ -134,13 +143,71 @@ image::images/mkdocs_html.PNG[mkdocs_html]
=== Generated PDF using AsciidoctorJ
image::images/asciidoc_pdf.PNG[asciidoc_pdf]
== Document Builder
The Swagger2Markup library allows to build an AsciiDoc or Markdown document via the Builder pattern:
== Integration with spring-restdocs
https://github.com/spring-projects/spring-restdocs[spring-restdocs] can be used together with Swagger2Markup.
Swagger2Markup can include the generated examples from spring-restdocs into the generated AsciiDoc document.
Currently it does not work for Markdown, since spring-restdocs generates only AsciiDoc files.
Let's say I have a Swagger-annotated Spring RestController method with an ApiOperation value: _"Create a quota"_
[source,java]
----
DocumentBuilder builder = DocumentBuilders.documentBuilder(MarkupLanguage.ASCIIDOC);
builder.documentTitle("Test title").textLine("Text line").writeToFile("/tmp", "test.adoc", StandardCharsets.UTF_8);
DocumentBuilder builder = DocumentBuilders.documentBuilder(MarkupLanguage.MARKDOWN);
builder.documentTitle("Test title").textLine("Text line").writeToFile("/tmp", "test.adoc", StandardCharsets.UTF_8);
@ApiOperation(value = "Create a quota", notes = "Create a quota allows bla bla bla bla")
public void createMailStorageQuota(@ApiParam(name = "MailStorageQuota",
value = "MailStorageQuota", required = true) @RequestBody MailStorageQuota mailStorageQuota) {
}
----
I'm using spring-restdocs in combination with https://github.com/jayway/rest-assured to test the Controller.
The target folder of the generated request and response example files must be _"create_a_quota"_ (similar to the value of the ApiOperation).
[source,java]
----
given().contentType(ContentType.XML).body(storageQuota).resultHandlers(document("create_a_quota")).
when().put("/quotas").
then().statusCode(204);
----
The spring-restdocs output directory is configured as follows:
[source]
----
io.restdocumented.outputDir = docs/generated
----
The Swagger2MarkupConverter must know the output directory of spring-restdocs.
[source,java]
----
Swagger2MarkupConverter.from("http://localhost:8080/api-docs").
withExamples("docs/generated").build()
.intoFolder("src/docs/asciidoc");
----
The Swagger2MarkupConverter searches for a Swagger ApiOperation with value: _"Create a quota"_ in a folder called _"docs/generated/create_a_quota"_ and includes the _request.asciidoc_ and _response.asciidoc_ files, if they are available.
== Integration of JSON and XML Schema files.
Swagger2Markup can also include JSON and XML Schema files into the generated document.
[source,java]
----
Swagger2MarkupConverter.from("http://localhost:8080/api-docs").
withMarkupLanguage(MarkupLanguage.MARKDOWN).
withExamples("docs/generated").withSchemas("docs/schemas").build()
.intoFolder("src/docs/markdown");
----
I create the Schemas files in Unit-Tests as follows:
[source,java]
----
RestDocumented restDocumented = RestDocumented.fromProperties();
restDocumented.documentJsonSchema(MailStorageQuota.class, "docs/schemas");
restDocumented.documentXmlSchema(MailStorageQuota.class, "docs/schemas");
----
I will make RestDocumented public soon. RestDocumented creates a MailStorageQuota.xsd and MailStorageQuota.json file in the folder "docs/schemas".
The Swagger2MarkupConverter will include the JSON and XML Schemas, if a Swagger Operation uses the MailStorageQuota class as Input or Output.
See example: http://spring-swagger2markup-demo.readthedocs.org/en/latest/generated/definitions/[ReadTheDocs-demo]

View File

@@ -4,4 +4,7 @@
* Initial version with support for AsciiDoc and Markdown
== Version 0.2.0
* This version is not downward compatible. This version supports includes of example files and JSON/XML Schema files. See documentation.
* This version is not downward compatible. This version supports includes of example files and JSON/XML Schema files. See documentation.
=== Version 0.2.1
* Signed jar files and published in Maven Central

View File

@@ -6,14 +6,14 @@ buildscript {
dependencies {
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.2'
classpath 'org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.6'
classpath 'io.spring.gradle:dependency-management-plugin:0.3.1.RELEASE'
classpath 'io.spring.gradle:dependency-management-plugin:0.4.0.RELEASE'
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.0.1'
classpath 'org.asciidoctor:asciidoctorj:1.5.2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
}
}
description = 'swagger2markup Build'
version = '0.2.0'
version = '0.2.2'
group = 'io.github.robwin'
apply plugin: 'java'
@@ -39,6 +39,7 @@ repositories {
}
dependencies {
compile 'io.github.robwin:markup-document-builder'
compile 'io.swagger:swagger-compat-spec-parser'
compile 'commons-collections:commons-collections'
compile 'commons-io:commons-io'
@@ -48,6 +49,7 @@ dependencies {
dependencyManagement {
dependencies {
"io.github.robwin:markup-document-builder" "0.1.1"
"io.swagger:swagger-compat-spec-parser" "1.0.0"
"commons-collections:commons-collections" "3.2.1"
"commons-io:commons-io" "2.4"
@@ -58,6 +60,21 @@ dependencyManagement {
}
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
asciidoctor {
backends = ['html5', 'pdf']
attributes = [
@@ -85,4 +102,4 @@ tasks.asciidoctor {
task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
}
}

View File

@@ -25,10 +25,15 @@ jar {
if (!project.hasProperty('bintrayUsername')) ext.bintrayUsername = ''
if (!project.hasProperty('bintrayApiKey')) ext.bintrayApiKey = ''
if (!project.hasProperty('gpgPassphrase')) ext.gpgPassphrase = ''
if (!project.hasProperty('ossUser')) ext.ossUser = ''
if (!project.hasProperty('ossPassword')) ext.ossPassword = ''
bintray {
user = project.bintrayUsername
key = project.bintrayApiKey
dryRun = false //Whether to run this as dry-run, without deploying
publish = true //If version should be auto published after an upload
publications = ['mavenJava']
pkg {
repo = 'maven'
@@ -40,6 +45,15 @@ bintray {
licenses = ['Apache-2.0']
version {
vcsTag = project.version
gpg {
sign = true //Determines whether to GPG sign the files. The default is false
passphrase = project.gpgPassphrase //Optional. The passphrase for GPG signing'
}
mavenCentralSync {
sync = true //Optional (true by default). Determines whether to sync the version to Maven Central.
user = ossUser //OSS user token
password = ossPassword //OSS user password
}
}
}
}
@@ -51,6 +65,15 @@ publishing {
pom.withXml {
def devs = ['RobWin': 'Robert Winkler']
def root = asNode()
root.dependencies.'*'.findAll() {
it.scope.text() == 'runtime' && project.configurations.compile.allDependencies.find { dep ->
dep.name == it.artifactId.text()
}
}.each() {
it.scope*.value = 'compile'
}
root.appendNode('name', 'swagger2markup')
root.appendNode('packaging', 'jar')
root.appendNode('url', 'https://github.com/RobWin/swagger2markup')
@@ -70,6 +93,8 @@ publishing {
d.appendNode('name', it.value)
}
}
artifact sourcesJar
artifact javadocJar
}
}
}

View File

@@ -1,9 +1,9 @@
package io.github.robwin.swagger2markup;
import com.wordnik.swagger.models.Swagger;
import io.github.robwin.markup.builder.MarkupLanguage;
import io.github.robwin.swagger2markup.builder.document.DefinitionsDocument;
import io.github.robwin.swagger2markup.builder.document.PathsDocument;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import io.swagger.parser.SwaggerParser;
import org.apache.commons.lang.Validate;
import org.slf4j.Logger;

View File

@@ -3,7 +3,7 @@ package io.github.robwin.swagger2markup.builder.document;
import com.wordnik.swagger.models.Model;
import com.wordnik.swagger.models.Swagger;
import com.wordnik.swagger.models.properties.Property;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import io.github.robwin.markup.builder.MarkupLanguage;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
@@ -18,11 +18,7 @@ import java.util.List;
import java.util.Map;
/**
* Project: swagger2markup
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
* @author Robert Winkler
*/
public class DefinitionsDocument extends MarkupDocument {
@@ -69,7 +65,7 @@ public class DefinitionsDocument extends MarkupDocument {
*/
private void definitions(Map<String, Model> definitions) throws IOException {
if(MapUtils.isNotEmpty(definitions)){
this.documentBuilder.sectionTitleLevel1(DEFINITIONS);
this.markupDocBuilder.sectionTitleLevel1(DEFINITIONS);
for(Map.Entry<String, Model> definitionsEntry : definitions.entrySet()){
String definitionName = definitionsEntry.getKey();
if(StringUtils.isNotBlank(definitionName)) {
@@ -106,7 +102,7 @@ public class DefinitionsDocument extends MarkupDocument {
* @param model the Swagger Model of the definition
*/
private void definition(String definitionName, Model model) {
this.documentBuilder.sectionTitleLevel2(definitionName);
this.markupDocBuilder.sectionTitleLevel2(definitionName);
Map<String, Property> properties = model.getProperties();
List<String> csvContent = new ArrayList<>();
csvContent.add(NAME_COLUMN + DELIMITER + TYPE_COLUMN + DELIMITER + REQUIRED_COLUMN);
@@ -114,7 +110,7 @@ public class DefinitionsDocument extends MarkupDocument {
Property property = propertyEntry.getValue();
csvContent.add(propertyEntry.getKey() + DELIMITER + property.getType() + DELIMITER + property.getRequired());
}
this.documentBuilder.tableWithHeaderRow(csvContent);
this.markupDocBuilder.tableWithHeaderRow(csvContent);
}
private void definitionSchema(String definitionName) throws IOException {
@@ -129,8 +125,8 @@ public class DefinitionsDocument extends MarkupDocument {
private void schema(String title, String schemasFolderPath, String schemaName, String language) throws IOException {
java.nio.file.Path path = Paths.get(schemasFolderPath, schemaName);
if (Files.isReadable(path)) {
this.documentBuilder.sectionTitleLevel3(title);
this.documentBuilder.source(FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8).trim(), language);
this.markupDocBuilder.sectionTitleLevel3(title);
this.markupDocBuilder.source(FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8).trim(), language);
if (logger.isInfoEnabled()) {
logger.info("Schema file processed: {}", path);
}

View File

@@ -1,9 +1,9 @@
package io.github.robwin.swagger2markup.builder.document;
import com.wordnik.swagger.models.Swagger;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilder;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilders;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import io.github.robwin.markup.builder.MarkupDocBuilder;
import io.github.robwin.markup.builder.MarkupDocBuilders;
import io.github.robwin.markup.builder.MarkupLanguage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -11,11 +11,7 @@ import java.io.IOException;
import java.nio.charset.Charset;
/**
* Project: swagger2markup
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
* @author Robert Winkler
*/
public abstract class MarkupDocument {
@@ -24,12 +20,12 @@ public abstract class MarkupDocument {
protected Logger logger = LoggerFactory.getLogger(getClass());
protected Swagger swagger;
protected MarkupLanguage markupLanguage;
protected DocumentBuilder documentBuilder;
protected MarkupDocBuilder markupDocBuilder;
MarkupDocument(Swagger swagger, MarkupLanguage markupLanguage){
this.swagger = swagger;
this.markupLanguage = markupLanguage;
this.documentBuilder = DocumentBuilders.documentBuilder(markupLanguage);
this.markupDocBuilder = MarkupDocBuilders.documentBuilder(markupLanguage);
}
/**
@@ -44,7 +40,7 @@ public abstract class MarkupDocument {
* Returns a string representation of the document.
*/
public String toString(){
return documentBuilder.toString();
return markupDocBuilder.toString();
}
/**
@@ -56,6 +52,6 @@ public abstract class MarkupDocument {
* @throws IOException if the file cannot be written
*/
public void writeToFile(String directory, String fileName, Charset charset) throws IOException{
documentBuilder.writeToFile(directory, fileName, charset);
markupDocBuilder.writeToFile(directory, fileName, charset);
}
}

View File

@@ -2,7 +2,7 @@ package io.github.robwin.swagger2markup.builder.document;
import com.wordnik.swagger.models.*;
import com.wordnik.swagger.models.parameters.Parameter;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import io.github.robwin.markup.builder.MarkupLanguage;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.FileUtils;
@@ -17,11 +17,7 @@ import java.util.List;
import java.util.Map;
/**
* Project: swagger2markup
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
* @author Robert Winkler
*/
public class PathsDocument extends MarkupDocument {
@@ -73,7 +69,7 @@ public class PathsDocument extends MarkupDocument {
* @param info the Swagger Info
*/
private void documentHeader(Info info) {
this.documentBuilder
this.markupDocBuilder
.documentTitle(info.getTitle())
.textLine(info.getDescription())
.textLine(VERSION + info.getVersion())
@@ -124,11 +120,11 @@ public class PathsDocument extends MarkupDocument {
String title;
if(StringUtils.isNotBlank(summary)) {
title = summary;
this.documentBuilder.sectionTitleLevel1(title);
this.documentBuilder.listing(httpMethod + " " + resourcePath);
this.markupDocBuilder.sectionTitleLevel1(title);
this.markupDocBuilder.listing(httpMethod + " " + resourcePath);
}else{
title = httpMethod + " " + resourcePath;
this.documentBuilder.sectionTitleLevel1(title);
this.markupDocBuilder.sectionTitleLevel1(title);
}
if (logger.isInfoEnabled()) {
logger.info("Path processed: {}", title);
@@ -138,8 +134,8 @@ public class PathsDocument extends MarkupDocument {
private void descriptionSection(Operation operation) {
String description = operation.getDescription();
if(StringUtils.isNotBlank(description)){
this.documentBuilder.sectionTitleLevel2(DESCRIPTION);
this.documentBuilder.paragraph(description);
this.markupDocBuilder.sectionTitleLevel2(DESCRIPTION);
this.markupDocBuilder.paragraph(description);
}
}
@@ -151,16 +147,16 @@ public class PathsDocument extends MarkupDocument {
for(Parameter parameter : parameters){
csvContent.add(parameter.getName() + DELIMITER + parameter.getIn() + DELIMITER + parameter.getDescription() + DELIMITER + parameter.getRequired());
}
this.documentBuilder.sectionTitleLevel2(PARAMETERS);
this.documentBuilder.tableWithHeaderRow(csvContent);
this.markupDocBuilder.sectionTitleLevel2(PARAMETERS);
this.markupDocBuilder.tableWithHeaderRow(csvContent);
}
}
private void consumesSection(Operation operation) {
List<String> consumes = operation.getConsumes();
if(CollectionUtils.isNotEmpty(consumes)){
this.documentBuilder.sectionTitleLevel2(CONSUMES);
this.documentBuilder.unorderedList(consumes);
this.markupDocBuilder.sectionTitleLevel2(CONSUMES);
this.markupDocBuilder.unorderedList(consumes);
}
}
@@ -168,8 +164,8 @@ public class PathsDocument extends MarkupDocument {
private void producesSection(Operation operation) {
List<String> produces = operation.getProduces();
if(CollectionUtils.isNotEmpty(produces)){
this.documentBuilder.sectionTitleLevel2(PRODUCES);
this.documentBuilder.unorderedList(produces);
this.markupDocBuilder.sectionTitleLevel2(PRODUCES);
this.markupDocBuilder.unorderedList(produces);
}
}
@@ -202,8 +198,8 @@ public class PathsDocument extends MarkupDocument {
for (String fileNameExtension : markupLanguage.getFileNameExtensions()) {
java.nio.file.Path path = Paths.get(examplesFolderPath, exampleFolder, exampleFileName + fileNameExtension);
if (Files.isReadable(path)) {
this.documentBuilder.sectionTitleLevel2(title);
this.documentBuilder.paragraph(FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8).trim());
this.markupDocBuilder.sectionTitleLevel2(title);
this.markupDocBuilder.paragraph(FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8).trim());
if (logger.isInfoEnabled()) {
logger.info("Example file processed: {}", path);
}
@@ -225,8 +221,8 @@ public class PathsDocument extends MarkupDocument {
Response response = entry.getValue();
csvContent.add(entry.getKey() + DELIMITER + response.getDescription());
}
this.documentBuilder.sectionTitleLevel2(RESPONSES);
this.documentBuilder.tableWithHeaderRow(csvContent);
this.markupDocBuilder.sectionTitleLevel2(RESPONSES);
this.markupDocBuilder.tableWithHeaderRow(csvContent);
}
}

View File

@@ -1,97 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
/**
* @author Robert Winkler
*/
public abstract class AbstractDocumentBuilder implements DocumentBuilder {
protected StringBuilder documentBuilder = new StringBuilder();
protected String newLine = System.getProperty("line.separator");
protected Logger logger = LoggerFactory.getLogger(getClass());
protected void documentTitle(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
}
protected void sectionTitleLevel1(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
}
protected void sectionTitleLevel2(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
}
protected void sectionTitleLevel3(Markup markup, String title){
documentBuilder.append(markup).append(title).append(newLine);
}
@Override
public DocumentBuilder textLine(String text){
documentBuilder.append(text).append(newLine);
return this;
}
protected void paragraph(Markup markup, String text){
documentBuilder.append(markup).append(newLine).append(text).append(newLine).append(newLine);
}
protected void listing(Markup markup, String text){
delimitedTextLine(markup, text);
}
protected void delimitedTextLine(Markup markup, String text){
documentBuilder.append(markup).append(newLine).append(text).append(newLine).append(markup).append(newLine).append(newLine);
}
protected void preserveLineBreaks(Markup markup){
documentBuilder.append(markup).append(newLine);
}
protected void boldTextLine(Markup markup, String text){
delimitedTextLine(markup, text);
}
protected void italicTextLine(Markup markup, String text){
delimitedTextLine(markup, text);
}
protected void unorderedList(Markup markup, List<String> list){
for(String listEntry : list){
documentBuilder.append(markup).append(listEntry).append(newLine);
}
documentBuilder.append(newLine);
}
@Override
public DocumentBuilder newLine(){
documentBuilder.append(newLine);
return this;
}
@Override
public String toString(){
return documentBuilder.toString();
}
@Override
public void writeToFile(String directory, String fileNameWithExtension, Charset charset) throws IOException {
Files.createDirectories(Paths.get(directory));
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(directory, fileNameWithExtension), charset)){
writer.write(documentBuilder.toString());
}
if (logger.isInfoEnabled()) {
logger.info("{} was written to: {}", fileNameWithExtension, directory);
}
documentBuilder = new StringBuilder();
}
}

View File

@@ -1,51 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
/**
* @author Robert Winkler
*/
public interface DocumentBuilder {
DocumentBuilder documentTitle(String title);
DocumentBuilder sectionTitleLevel1(String title);
DocumentBuilder sectionTitleLevel2(String title);
DocumentBuilder sectionTitleLevel3(String title);
DocumentBuilder textLine(String text);
DocumentBuilder paragraph(String text);
DocumentBuilder listing(String text);
DocumentBuilder source(String text, String language);
DocumentBuilder boldTextLine(String text);
DocumentBuilder italicTextLine(String text);
DocumentBuilder unorderedList(List<String> list);
DocumentBuilder tableWithHeaderRow(List<String> rowsInCSV);
DocumentBuilder newLine();
/**
* Returns a string representation of the document.
*/
String toString();
/**
* Writes the content of the builder to a file and clears the builder.
*
* @param directory the directory where the generated file should be stored
* @param fileName the name of the file
* @param charset the the charset to use for encoding
* @throws IOException if the file cannot be written
*/
void writeToFile(String directory, String fileName, Charset charset) throws IOException;
}

View File

@@ -1,25 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup;
import io.github.robwin.swagger2markup.builder.markup.asciidoc.AsciiDocBuilder;
import io.github.robwin.swagger2markup.builder.markup.markdown.MarkdownBuilder;
/**
* Project: swagger2markup
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
*/
public final class DocumentBuilders {
private DocumentBuilders(){};
public static DocumentBuilder documentBuilder(MarkupLanguage markupLanguage){
switch(markupLanguage){
case MARKDOWN: return new MarkdownBuilder();
case ASCIIDOC: return new AsciiDocBuilder();
default: return new AsciiDocBuilder();
}
}
}

View File

@@ -1,12 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup;
/**
* Project: swagger2asciidoc
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
*/
public interface Markup {
public String toString();
}

View File

@@ -1,25 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup;
import java.util.Arrays;
import java.util.List;
/**
* @author Robert Winkler
*/
public enum MarkupLanguage {
ASCIIDOC(".adoc,.asciidoc"),
MARKDOWN(".md,.markdown");
private final String fileNameExtensions;
/**
* @param fileNameExtensions file name suffix
*/
private MarkupLanguage(final String fileNameExtensions) {
this.fileNameExtensions = fileNameExtensions;
}
public List<String> getFileNameExtensions() {
return Arrays.asList(fileNameExtensions.split(","));
}
}

View File

@@ -1,37 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup.asciidoc;
import io.github.robwin.swagger2markup.builder.markup.Markup;
/**
* @author Robert Winkler
*/
public enum AsciiDoc implements Markup {
LABELED(":: "),
TABLE("|==="),
LISTING("----"),
HARDBREAKS(":hardbreaks:"),
DOCUMENT_TITLE("= "),
SECTION_TITLE_LEVEL1("== "),
SECTION_TITLE_LEVEL2("=== "),
SECTION_TITLE_LEVEL3("==== "),
BOLD("*"),
ITALIC("*"),
LIST_ENTRY("* ");
private final String markup;
/**
* @param markup AsciiDoc markup
*/
private AsciiDoc(final String markup) {
this.markup = markup;
}
/* (non-Javadoc)
* @see java.lang.Enum#toString()
*/
@Override
public String toString() {
return markup;
}
}

View File

@@ -1,93 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup.asciidoc;
import io.github.robwin.swagger2markup.builder.markup.AbstractDocumentBuilder;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilder;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
/**
* @author Robert Winkler
*/
public class AsciiDocBuilder extends AbstractDocumentBuilder{
@Override
public DocumentBuilder documentTitle(String title){
documentTitle(AsciiDoc.DOCUMENT_TITLE, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel1(String title){
sectionTitleLevel1(AsciiDoc.SECTION_TITLE_LEVEL1, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel2(String title){
sectionTitleLevel2(AsciiDoc.SECTION_TITLE_LEVEL2, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel3(String title){
sectionTitleLevel3(AsciiDoc.SECTION_TITLE_LEVEL3, title);
return this;
}
@Override
public DocumentBuilder paragraph(String text){
paragraph(AsciiDoc.HARDBREAKS, text);
return this;
}
@Override
public DocumentBuilder listing(String text){
listing(AsciiDoc.LISTING, text);
return this;
}
@Override
public DocumentBuilder boldTextLine(String text){
boldTextLine(AsciiDoc.BOLD, text);
return this;
}
@Override
public DocumentBuilder italicTextLine(String text) {
italicTextLine(AsciiDoc.ITALIC, text);
return this;
}
@Override
public DocumentBuilder unorderedList(List<String> list){
unorderedList(AsciiDoc.LIST_ENTRY, list);
return this;
}
@Override
public DocumentBuilder source(String text, String language){
documentBuilder.append(String.format("[source,%s]", language)).append(newLine);
listing(AsciiDoc.LISTING, text);
return this;
}
@Override
public DocumentBuilder tableWithHeaderRow(List<String> rowsInCSV){
documentBuilder.append("[format=\"csv\", options=\"header\"]").append(newLine);
documentBuilder.append(AsciiDoc.TABLE).append(newLine);
for(String row : rowsInCSV){
documentBuilder.append(row).append(newLine);
}
documentBuilder.append(AsciiDoc.TABLE).append(newLine).append(newLine);
return this;
}
@Override
public void writeToFile(String directory, String fileName, Charset charset) throws IOException {
String fileNameWithExtension = fileName + ".adoc";
super.writeToFile(directory, fileNameWithExtension, charset);
}
}

View File

@@ -1,41 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup.markdown;
import io.github.robwin.swagger2markup.builder.markup.Markup;
/**
* Project: swagger2asciidoc
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
*/
public enum Markdown implements Markup {
HARDBREAKS(""),
TABLE_COLUMN("|"),
TABLE_ROW("-"),
LISTING("```"),
DOCUMENT_TITLE("# "),
SECTION_TITLE_LEVEL1("## "),
SECTION_TITLE_LEVEL2("### "),
SECTION_TITLE_LEVEL3("### "),
BOLD("**"),
ITALIC("*"),
LIST_ENTRY("* ");
private final String markup;
/**
* @param markup AsciiDoc markup
*/
private Markdown(final String markup) {
this.markup = markup;
}
/* (non-Javadoc)
* @see java.lang.Enum#toString()
*/
@Override
public String toString() {
return markup;
}
}

View File

@@ -1,120 +0,0 @@
package io.github.robwin.swagger2markup.builder.markup.markdown;
import io.github.robwin.swagger2markup.builder.markup.AbstractDocumentBuilder;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilder;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
/**
* Project: swagger2asciidoc
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
*/
public class MarkdownBuilder extends AbstractDocumentBuilder
{
@Override
public DocumentBuilder documentTitle(String title){
documentTitle(Markdown.DOCUMENT_TITLE, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel1(String title){
sectionTitleLevel1(Markdown.SECTION_TITLE_LEVEL1, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel2(String title){
sectionTitleLevel2(Markdown.SECTION_TITLE_LEVEL2, title);
return this;
}
@Override
public DocumentBuilder sectionTitleLevel3(String title){
sectionTitleLevel3(Markdown.SECTION_TITLE_LEVEL3, title);
return this;
}
@Override
public DocumentBuilder paragraph(String text){
paragraph(Markdown.HARDBREAKS, text);
return this;
}
@Override
public DocumentBuilder listing(String text){
listing(Markdown.LISTING, text);
return this;
}
@Override
public DocumentBuilder source(String text, String language){
documentBuilder.append(Markdown.LISTING).append(language).append(newLine).
append(text).append(newLine).
append(Markdown.LISTING).append(newLine).append(newLine);
return this;
}
@Override
public DocumentBuilder boldTextLine(String text){
boldTextLine(Markdown.BOLD, text);
return this;
}
@Override
public DocumentBuilder italicTextLine(String text) {
italicTextLine(Markdown.ITALIC, text);
return this;
}
@Override
public DocumentBuilder unorderedList(List<String> list){
unorderedList(Markdown.LIST_ENTRY, list);
return this;
}
@Override
public DocumentBuilder tableWithHeaderRow(List<String> rowsInCSV){
String headersInCSV = rowsInCSV.get(0);
List<String> contentRowsInCSV = rowsInCSV.subList(1, rowsInCSV.size());
List<String> headers = Arrays.asList(headersInCSV.split(","));
// Header
documentBuilder.append(Markdown.TABLE_COLUMN);
for(String header : headers){
documentBuilder.append(header).append(Markdown.TABLE_COLUMN);
}
newLine();
// Header/Content separator
documentBuilder.append(Markdown.TABLE_COLUMN);
for(String header : headers){
for(int i = 1; i<5; i++) {
documentBuilder.append(Markdown.TABLE_ROW);
}
documentBuilder.append(Markdown.TABLE_COLUMN);
}
newLine();
// Content
for(String contentRow : contentRowsInCSV){
documentBuilder.append(Markdown.TABLE_COLUMN);
List<String> columns = Arrays.asList(contentRow.split(","));
for(String columnText : columns){
documentBuilder.append(columnText).append(Markdown.TABLE_COLUMN);
}
newLine();
}
newLine().newLine();
return this;
}
@Override
public void writeToFile(String directory, String fileName, Charset charset) throws IOException {
String fileNameWithExtension = fileName + ".md";
super.writeToFile(directory, fileNameWithExtension, charset);
}
}

View File

@@ -1,6 +1,6 @@
package io.github.robwin.swagger2markup;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import io.github.robwin.markup.builder.MarkupLanguage;
import org.junit.Test;
import java.io.File;

View File

@@ -1,26 +0,0 @@
package io.github.robwin.swagger2markup.builder;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilder;
import io.github.robwin.swagger2markup.builder.markup.DocumentBuilders;
import io.github.robwin.swagger2markup.builder.markup.MarkupLanguage;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* Project: swagger2markup
* Copyright: Deutsche Telekom AG
*
* @author Robert Winkler <robert.winkler@telekom.de>
* @since 2.0.0
*/
public class DocumentBuilderTest {
@Test
public void testToFile() throws IOException {
DocumentBuilder builder = DocumentBuilders.documentBuilder(MarkupLanguage.ASCIIDOC);
builder.documentTitle("Test title").textLine("Text line").writeToFile("/tmp", "test.adoc", StandardCharsets.UTF_8);
}
}