Use spring-asciidoctor-extensions to provide tabbed code blocks (#153)

This commit is contained in:
Andy Wilkinson
2016-11-21 16:15:33 +00:00
committed by Marcin Grzejszczak
parent 475fa2a40b
commit 72e165e4ae
11 changed files with 4 additions and 249 deletions

View File

@@ -1,45 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-parent</artifactId>
<version>1.0.2.BUILD-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<artifactId>spring-cloud-contract-docs-extensions</artifactId>
<packaging>jar</packaging>
<name>Spring Cloud Contract Docs Extensions</name>
<description>Spring Cloud Docs Extensions</description>
<properties>
<asciidoctorj.version>1.5.2</asciidoctorj.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctorj</artifactId>
<version>${asciidoctorj.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,28 +0,0 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed 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.springframework.cloud.asciidoctor;
import org.asciidoctor.Asciidoctor;
import org.asciidoctor.extension.spi.ExtensionRegistry;
public class CodeBlockSwitchExtension implements ExtensionRegistry {
@Override
public void register(Asciidoctor asciidoctor) {
asciidoctor.javaExtensionRegistry().postprocessor(new CodeBlockSwitchPostProcessor());
}
}

View File

@@ -1,31 +0,0 @@
/*
* Copyright 2013-2016 the original author or authors.
*
* Licensed 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.springframework.cloud.asciidoctor;
import org.asciidoctor.ast.Document;
import org.asciidoctor.extension.Postprocessor;
class CodeBlockSwitchPostProcessor extends Postprocessor {
private final ResourceReplacer resourceReplacer = new ResourceReplacer();
@Override
public String process(Document document, String output) {
return this.resourceReplacer.replaceOutput(output);
}
}

View File

@@ -1,50 +0,0 @@
package org.springframework.cloud.asciidoctor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* @author Marcin Grzejszczak
*/
class ResourceReplacer {
String replaceOutput(String output) {
String css = resourceToText("/codeBlockSwitch.css");
String javascript = resourceToText("/codeBlockSwitch.js");
String replacement = "<style>\n" + css + "\n</style>\n"
+ "\n<script src=\"http://cdnjs.cloudflare.com/ajax/libs/zepto/1.1.6/zepto.min.js\"></script>\n"
+ "<script type=\"text/javascript\">\n" + javascript + "\n</script>\n"
+ "</head>";
return output.replace("</head>", replacement);
}
private String resourceToText(String name) {
return getStringFromInputStream(getClass().getResourceAsStream(name));
}
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
return "";
}
finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
return "";
}
}
}
return sb.toString();
}
}

View File

@@ -1 +0,0 @@
org.springframework.cloud.asciidoctor.CodeBlockSwitchExtension

View File

@@ -1,23 +0,0 @@
.hidden {
display: none;
}
.switch {
border-width: 1px 1px 0 1px;
border-style: solid;
border-color: #7a2518;
display: inline-block;
}
.switch--item {
padding: 10px;
background-color: #ffffff;
color: #7a2518;
display: inline-block;
cursor: pointer;
}
.switch--item.selected {
background-color: #7a2519;
color: #ffffff;
}

View File

@@ -1,45 +0,0 @@
function addBlockSwitches() {
$('.primary').each(function() {
primary = $(this);
createSwitchItem(primary, createBlockSwitch(primary)).item.addClass("selected");
primary.children('.title').remove();
});
$('.secondary').each(function(idx, node) {
secondary = $(node);
primary = findPrimary(secondary);
switchItem = createSwitchItem(secondary, primary.children('.switch'));
switchItem.content.addClass('hidden');
findPrimary(secondary).append(switchItem.content);
secondary.remove();
});
}
function createBlockSwitch(primary) {
blockSwitch = $('<div class="switch"></div>');
primary.prepend(blockSwitch);
return blockSwitch;
}
function findPrimary(secondary) {
candidate = secondary.prev();
while (!candidate.is('.primary')) {
candidate = candidate.prev();
}
return candidate;
}
function createSwitchItem(block, blockSwitch) {
blockName = block.children('.title').text();
content = block.children('.content').first().append(block.next('.colist'));
item = $('<div class="switch--item">' + blockName + '</div>');
item.on('click', '', content, function(e) {
$(this).addClass('selected');
$(this).siblings().removeClass('selected');
e.data.siblings('.content').addClass('hidden');
e.data.removeClass('hidden');
});
blockSwitch.append(item);
return {'item': item, 'content': content};
}
$(addBlockSwitches);

View File

@@ -1,21 +0,0 @@
package org.springframework.cloud.asciidoctor;
import org.junit.Test;
import static org.assertj.core.api.BDDAssertions.then;
/**
* @author Marcin Grzejszczak
*/
public class ResourceReplacerTests {
@Test
public void should_process_the_resources() throws Exception {
ResourceReplacer replacer = new ResourceReplacer();
String output = replacer.replaceOutput("<head></head>");
then(output).contains("http://cdnjs.cloudflare.com/ajax/libs/zepto/1.1.6/zepto.min.js");
}
}

View File

@@ -39,9 +39,9 @@
<inherited>false</inherited>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-docs-extensions</artifactId>
<version>${project.version}</version>
<groupId>io.spring.asciidoctor</groupId>
<artifactId>spring-asciidoctor-extensions</artifactId>
<version>0.1.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>

View File

@@ -37,7 +37,6 @@
<modules>
<module>spring-cloud-contract-dependencies</module>
<module>docs-extensions</module>
<module>docs</module>
<module>spring-cloud-contract-wiremock</module>
<module>spring-cloud-contract-verifier</module>

View File

@@ -7,7 +7,7 @@ set -e
echo "Generating docs for Spring Cloud Contract"
echo "Building main docs"
./mvnw clean install -P docs -DskipTests=true --pl docs-extensions,docs
./mvnw clean install -P docs -DskipTests=true --pl docs
echo "Building maven plugin docs"
./mvnw site -DskipTests=true --pl spring-cloud-contract-tools/spring-cloud-contract-maven-plugin