Last active
August 29, 2015 14:05
-
-
Save aalmiray/bd63c3eaa305f50970b8 to your computer and use it in GitHub Desktop.
Trivial Asciidoctor extension that demonstrates how one can write extensions inside a Gradle build
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
buildscript { | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.0' | |
} | |
} | |
apply plugin: 'org.asciidoctor.gradle.asciidoctor' |
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
repositories { | |
jcenter() | |
} | |
dependencies { | |
compile 'org.asciidoctor:asciidoctorj:1.5.0' | |
} |
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
package org.asciidoctor.extension | |
import org.asciidoctor.Asciidoctor | |
import org.asciidoctor.extension.spi.ExtensionRegistry | |
class ExampleExtensionRegistry implements ExtensionRegistry { | |
void register(Asciidoctor asciidoctor) { | |
asciidoctor.javaExtensionRegistry().block 'pig-latin', PigLatinBlock | |
} | |
} |
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
= Example Manual | |
Doc Writer <doc.writer@example.org> | |
v1.0 | |
:example-caption!: | |
Pig Latin is a constructed language game where words in English are altered | |
according to a simple set of rules. Pig Latin takes the first consonant | |
(or consonant cluster) of an English word, moves it to the end of the word | |
and suffixes an _ay_ (IPA [eɪ]) (for example, _pig_ yields _igpay_, _banana_ | |
yields _ananabay_, and _trash_ yields _ashtray_). Also in the Northeastern | |
United States many people only move the first letter not the first syllable | |
so 'trash' yields 'rashtay'. | |
The objective is to conceal the meaning of the words from others not familiar | |
with the rules. The reference to Latin is a deliberate misnomer, as it is | |
simply a form of jargon, used only for its English connotations as a "strange | |
and foreign-sounding language." | |
== Example | |
The quick brown fox jumped over the lazy dog. | |
[pig-latin] | |
The quick brown fox jumped over the lazy dog. |
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
org.asciidoctor.extension.ExampleExtensionRegistry |
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
package org.asciidoctor.extension | |
import org.asciidoctor.extension.BlockProcessor | |
import org.asciidoctor.ast.AbstractBlock | |
import org.asciidoctor.extension.Reader | |
class PigLatinBlock extends BlockProcessor { | |
private static final String VOWELS = 'AEIOUaeiou' | |
private static final String VOWELSY = 'AEIOUaeiouYy' | |
PigLatinBlock(String name, Map<String, Object> config) { | |
super(name, [contexts: [':paragraph'], content_model: ':simple']) | |
} | |
def process(AbstractBlock parent, Reader reader, Map<String, Object> attributes) { | |
createBlock(parent, 'paragraph', toPigLatin(reader), attributes, [:]) | |
} | |
def toPigLatin(Reader reader) { | |
reader.lines().collect { line -> | |
line.split(/\W/).collect { String word -> toPigLatin(word) }.join(' ') | |
}.join('') | |
} | |
def toPigLatin(String word) { | |
if (VOWELS.contains(word[0])) return word + 'way' | |
String suffix = '' | |
String previous = '' | |
for (c in word) { | |
if (VOWELSY.contains(c)) { | |
if (previous.toLowerCase() == 'q' && c.toLowerCase() == 'u') { | |
suffix += c // moves 'qu' at the end | |
} | |
break | |
} | |
suffix += c | |
previous = c | |
} | |
word = word[suffix.size()..-1] | |
if (!Character.isLowerCase(suffix[0] as char)) word = word.capitalize() | |
word + suffix.toLowerCase() + 'ay' | |
} | |
} |
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
. | |
├── README.adoc | |
├── build.gradle | |
├── buildSrc | |
│ ├── build.gradle | |
│ └── src | |
│ └── main | |
│ ├── groovy | |
│ │ └── org | |
│ │ └── asciidoctor | |
│ │ └── extension | |
│ │ ├── ExampleExtensionRegistry.groovy | |
│ │ └── PigLatinBlock.groovy | |
│ └── resources | |
│ └── META-INF | |
│ └── services | |
│ └── org.asciidoctor.extension.spi.ExtensionRegistry | |
└── src | |
└── asciidoc | |
└── index.adoc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment