'use strict'

var entities = require('entities');
var util = require('hexo-util');
var cheerio = require('cheerio');
var asciidoctor = require('asciidoctor.js')();
var processor = asciidoctor.Asciidoctor(true);

var options = {
  auto_detect: false,
  lang: 'plain',
  gutter: false,
  wrap: false
};

var cheerio_load_option = {
  decodeEntities: false
};

function renderer(data, locals) {
  var html = processor.$convert(data.text, null);
  var $ = cheerio.load(html, cheerio_load_option);

  $('.highlight code').each(function(index, elem) {
    options.lang = elem.attribs['data-lang'];
    var code = entities.decodeXML($(elem).text());
    var content = util.highlight(code, options);
    $(elem).html(content);
  });

  return $.html()
             .replace(/{/g, '&#123;')
             .replace(/}/g, '&#125;');
}

hexo.extend.renderer.register('ad', 'html', renderer, true);
hexo.extend.renderer.register('adoc', 'html', renderer, true);
hexo.extend.renderer.register('asciidoc', 'html', renderer, true);

@@ -355,41 +355,159 @@
 %7D;%0A%0A
-function renderer(data, locals) %7B
+var asciidoc_options = %7B%0A  doctype: 'book'%0A%7D;%0A%0Afunction renderer(data, locals) %7B%0A  var asciidoc_options_opal = asciidoctor.Opal.hash(asciidoc_options);
 %0A  v
@@ -550,11 +550,28 @@
 xt, 
-nul
+asciidoc_options_opa
 l);%0A

Allow multiple level-0 headings by setting asciidoc's "doctype" option to "book"