Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collapse Maven and Gradle setup into one section with switchable exam…
…ples This commit adds an Asciidoctor extension that post-processes code blocks, collapsing any secondary blocks into the preceding primary block. The primary block is then augmented with a switch. This switch contains one item for each block. Clicking an item causes the associated block’s content to be displayed. Each item is named using its block’s title. The getting started instructions have been updated to take advantage of this new switching support, with the Maven and Gradle instructions being collapsed into a single section. Closes gh-189
- Loading branch information
1 parent
4389914
commit 5f4541b
Showing
7 changed files
with
211 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
compile 'org.asciidoctor:asciidoctorj:1.5.2' | ||
} |
28 changes: 28 additions & 0 deletions
28
.../src/main/groovy/org/springframework/restdocs/asciidoctor/CodeBlockSwitchExtension.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2014-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.restdocs.asciidoctor | ||
|
||
import org.asciidoctor.Asciidoctor | ||
import org.asciidoctor.extension.spi.ExtensionRegistry | ||
|
||
class CodeBlockSwitchExtension implements ExtensionRegistry { | ||
|
||
@Override | ||
public void register(Asciidoctor asciidoctor) { | ||
asciidoctor.javaExtensionRegistry().postprocessor(new CodeBlockSwitchPostProcessor()); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
.../main/groovy/org/springframework/restdocs/asciidoctor/CodeBlockSwitchPostProcessor.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2014-2015 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.restdocs.asciidoctor | ||
|
||
import org.asciidoctor.ast.Document | ||
import org.asciidoctor.extension.Postprocessor | ||
|
||
class CodeBlockSwitchPostProcessor extends Postprocessor { | ||
|
||
String process(Document document, String output) { | ||
def css = getClass().getResource("/codeBlockSwitch.css").text | ||
def javascript = getClass().getResource("/codeBlockSwitch.js").text | ||
def replacement = """<style> | ||
$css | ||
</style> | ||
<script src=\"http://cdnjs.cloudflare.com/ajax/libs/zepto/1.1.6/zepto.min.js\"></script> | ||
<script type="text/javascript"> | ||
$javascript | ||
</script> | ||
</head> | ||
""" | ||
return output.replace("</head>", replacement); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...dSrc/src/main/resources/META-INF/services/org.asciidoctor.extension.spi.ExtensionRegistry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.springframework.restdocs.asciidoctor.CodeBlockSwitchExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters