Skip to content

Instantly share code, notes, and snippets.

@glaforge
Last active January 11, 2016 11:19
Show Gist options
  • Save glaforge/4450854 to your computer and use it in GitHub Desktop.
Save glaforge/4450854 to your computer and use it in GitHub Desktop.
Compare HTML output of Pegdown and XWiki Rendering's upcoming Markdown support
@Grab("org.pegdown:pegdown:1.2.1")
import org.pegdown.*
@Grab("org.xwiki.commons:xwiki-commons-component-default:4.5-SNAPSHOT")
import org.xwiki.component.embed.EmbeddableComponentManager
import org.xwiki.component.annotation.*
@Grab("org.xwiki.rendering:xwiki-rendering-syntax-markdown:4.5-SNAPSHOT")
import org.xwiki.rendering.parser.Parser
import org.xwiki.rendering.converter.Converter
import org.xwiki.rendering.syntax.Syntax
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter
import org.xwiki.rendering.macro.*
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor
import org.xwiki.rendering.block.*
@Grab("org.xwiki.rendering:xwiki-rendering-transformation-macro:4.4")
import org.xwiki.rendering.transformation.*
import org.xwiki.properties.*
import org.xwiki.properties.annotation.PropertyMandatory
import org.xwiki.properties.annotation.PropertyDescription
import javax.inject.Named
import static org.apache.commons.lang3.StringEscapeUtils.*
@Component
@Named("code")
class CustomCodeMacro extends AbstractMacro<CodeParams> {
CustomCodeMacro() { super("code", "Code macro", new DefaultContentDescriptor("code macro"), CodeParams) }
List<Block> execute(CodeParams parameters, String content, MacroTransformationContext context) {
def language = context.currentMacroBlock.parameters.language
def code = "<code"
if (language)
code += " language='${language}'"
code += ">${escapeXml(content)}</code>"
if (!context.inline)
code = "<pre>" + code + "</pre>"
return [new RawBlock(code, Syntax.XHTML_1_0)]
}
boolean supportsInlineMode() { true }
}
class CodeParams {
@PropertyMandatory
@PropertyDescription("Language of the code snippet")
String language
}
def htmlTemplate = { "<!DOCTYPE html><html><head><meta charset='UTF-8'/></head><body>${it}</body></html>" }
// Let's output with the Pegdown html serializer
def mdproc = new PegDownProcessor(Extensions.ALL & ~Extensions.HARDWRAPS)
new File(args[0] + '.out-pegdown.html').text = htmlTemplate(mdproc.markdownToHtml(new File(args[0]).text))
// Let's output with our XWiki Markdown parser
def cl = this.class.classLoader
def cm = new EmbeddableComponentManager()
cm.initialize(cl)
def loader = new ComponentAnnotationLoader()
loader.initialize(cm, cl, [new ComponentDeclaration('CustomCodeMacro')])
assert cm.getInstance(Macro, "code")
def converter = cm.getInstance(Converter)
def printer = new DefaultWikiPrinter()
converter.convert(new FileReader(args[0]), Syntax.MARKDOWN_1_0, Syntax.XHTML_1_0, printer)
new File(args[0] + '.out-xwiki.html').text = htmlTemplate(printer.toString())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment