Skip to content

Instantly share code, notes, and snippets.

@bodiam
Created April 19, 2016 13:50
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bodiam/7ea203cd1b928377f8afaaf9ac060d69 to your computer and use it in GitHub Desktop.
AsciidoctorJ has a dependency on JRuby, which is quite a heavy library to download and load. This one is based on Groovy, and could easily be ported to Java, and while limited, does provide quite some functionality.
/**
* A very basic regex based parser for Asciidoc.
*
* Based on Slimdown (Markdown) parsers:
* - https://gist.github.com/jbroadway/2836900
* - https://gist.github.com/renehamburger/12f14a9bd9297394e5bd
* - https://gist.github.com/paulcuth/8967731
*
* @author Erik Pragt
*/
class AsciiLite {
private static def para = {
String line = it[1]
String trimmed = line.trim()
if (trimmed =~ /^<\/?(ul|ol|li|h|p|bl)|(<!--)/) {
return "\n${line}\n"
} else {
return "\n<p>$trimmed</p>\n"
}
}
private static def blockquote = {
return "\n<blockquote>${it[2].trim()}</blockquote>"
}
private static def ul_list = {
return "\n<ul>\n\t<li>${it[1].trim()}</li>\n</ul>"
}
private static def ol_list = {
return "\n<ol>\n\t<li>${it[1].trim()}</li>\n</ol>"
}
private static def header = {
def s = it[1].size()
return "<h$s>${it[2].trim()}</h$s>"
}
private static def rules = [
/(=+)(.*)/ : header, // header
/(^|\s)\/\/\s?(.*)/ : '<!-- $2 -->', // comment
/image::(.*)\[\]/ : '<img src="$1">', // image
/([^\n)]+)\[([^\[]+)\]/ : '<a href="$1">$2</a>', // links
/(\*)(.*?)\1/ : '<strong>$2</strong>', // bold
/(_)(.*?)\1/ : '<em>$2</em>', // emphasis
/(\[line-through\])(.*)/ : '<del>$2</del>', // del
// '/\:\"(.*?)\"\:/' => '<q>\1</q>', // quote /??
/(\+)(.*?)\1/ : '<code>$2</code>', // inline code
/\n\*(.*)/ : ul_list, // ul lists
/\n\.(.*)/ : ol_list, // ol lists
/\n(&gt;|\>)(.*)/ : blockquote, // blockquotes
/'''/ : '<hr />', // horizontal rule
/\n([^\n]+)\n/ : para, // add paragraphs
/<\/ul>\s?<ul>/ : '', // fix extra ul
/<\/ol>\s?<ol>/ : '', // fix extra ol
/<\/blockquote>\n<blockquote>/: "\n", // fix extra blockquote
/<p><\/p>/ : '' // fix extra p
]
static String render(String input) {
def result = "\n" + input + "\n"
rules.each { k, v ->
result = result.replaceAll(k, v)
}
result.trim()
}
}
@SuperMMX
Copy link

For someone who is interested, I am working on a Groovy implementation of AsciiDoc called "AsciiDog": https://github.com/SuperMMX/asciidog, now it has a very basic working version that can output to HTML5 and epub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment