From 55d844f6672183ee98d05d4cdcbda326f6e7010a Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Thu, 17 Oct 2013 00:49:53 -0600 Subject: [PATCH 1/2] resolves #1043 added a renderer for AsciiDoc files --- .../core_extensions/rendering.rb | 7 +++ .../lib/middleman-core/renderers/asciidoc.rb | 52 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 middleman-core/lib/middleman-core/renderers/asciidoc.rb diff --git a/middleman-core/lib/middleman-core/core_extensions/rendering.rb b/middleman-core/lib/middleman-core/core_extensions/rendering.rb index 6b05ed6b..a77e6b2b 100644 --- a/middleman-core/lib/middleman-core/core_extensions/rendering.rb +++ b/middleman-core/lib/middleman-core/core_extensions/rendering.rb @@ -56,6 +56,13 @@ module Middleman require "middleman-core/renderers/markdown" app.register Middleman::Renderers::Markdown + # AsciiDoc Support + begin + require "middleman-core/renderers/asciidoc" + app.register Middleman::Renderers::AsciiDoc + rescue LoadError + end + # Liquid Support begin require "middleman-core/renderers/liquid" diff --git a/middleman-core/lib/middleman-core/renderers/asciidoc.rb b/middleman-core/lib/middleman-core/renderers/asciidoc.rb new file mode 100644 index 00000000..611b05ab --- /dev/null +++ b/middleman-core/lib/middleman-core/renderers/asciidoc.rb @@ -0,0 +1,52 @@ +require 'asciidoctor' + +module Middleman + module Renderers + module AsciiDoc + class << self + + def registered(app) + app.config.define_setting :asciidoc, { + :safe => :safe, + :backend => :html5, + :attributes => %W(showtitle env=middleman env-middleman middleman-version=#{::Middleman::VERSION}) + }, 'AsciiDoc engine options (Hash)' + app.config.define_setting :asciidoc_attributes, [], 'AsciiDoc custom attributes (Array)' + app.before_configuration do + template_extensions :adoc => :html + end + + app.after_configuration do + # QUESTION should base_dir be equal to docdir instead? + config[:asciidoc][:base_dir] = File.expand_path config[:source] + config[:asciidoc][:attributes].concat (config[:asciidoc_attributes] || []) + config[:asciidoc][:attributes] << %(imagesdir=#{File.join (config[:http_prefix] || '/').chomp('/'), config[:images_dir]}) + sitemap.provides_metadata(/\.adoc$/) do |path| + # read the AsciiDoc header only to set page options and data + # header values can be accessed via app.data.page. in the layout + doc = Asciidoctor.load_file path, :safe => :safe, :parse_header_only => true + + opts = {} + opts[:layout] = (doc.attr 'page-layout') if (doc.attr? 'page-layout') + opts[:layout_engine] = (doc.attr 'page-layout-engine') if (doc.attr? 'page-layout-engine') + # TODO override attributes to set docfile, docdir, docname, etc + # alternative is to set :renderer_options, which get merged into options by the rendering extension + #opts[:attributes] = config[:asciidoc][:attributes].dup + #opts[:attributes].concat %W(docfile=#{path} docdir=#{File.dirname path} docname=#{(File.basename path).sub(/\.adoc$/, '')}) + + page = {} + page[:title] = doc.doctitle + page[:date] = (doc.attr 'date') unless (doc.attr 'date').nil? + # TODO grab all the author information + page[:author] = (doc.attr 'author') unless (doc.attr 'author').nil? + + {:options => opts, :page => ::Middleman::Util.recursively_enhance(page)} + end + end + end + + alias :included :registered + end + end + end +end From 5439139b76a115e6a1f215c7232e5ee9f11da77b Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Fri, 18 Oct 2013 02:58:14 -0600 Subject: [PATCH 2/2] added cucumber tests for #1043, AsciiDoc support --- Gemfile | 1 + middleman-core/features/asciidoc.feature | 155 ++++++++++++++++++ .../fixtures/asciidoc-app/config.rb | 0 .../asciidoc-app/source/_include.adoc | 1 + .../fixtures/asciidoc-app/source/code.adoc | 3 + .../asciidoc-app/source/custom-attribute.adoc | 3 + .../fixtures/asciidoc-app/source/gallery.adoc | 1 + .../asciidoc-app/source/hello-no-layout.adoc | 2 + .../source/hello-with-front-matter.adoc | 5 + .../source/hello-with-layout.adoc | 2 + .../asciidoc-app/source/hello-with-title.adoc | 4 + .../fixtures/asciidoc-app/source/hello.adoc | 4 + .../asciidoc-app/source/images/tiger.gif | Bin 0 -> 43 bytes .../asciidoc-app/source/layouts/default.erb | 10 ++ .../fixtures/asciidoc-app/source/master.adoc | 3 + .../lib/middleman-core/renderers/asciidoc.rb | 15 +- 16 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 middleman-core/features/asciidoc.feature create mode 100644 middleman-core/fixtures/asciidoc-app/config.rb create mode 100644 middleman-core/fixtures/asciidoc-app/source/_include.adoc create mode 100644 middleman-core/fixtures/asciidoc-app/source/code.adoc create mode 100644 middleman-core/fixtures/asciidoc-app/source/custom-attribute.adoc create mode 100644 middleman-core/fixtures/asciidoc-app/source/gallery.adoc create mode 100644 middleman-core/fixtures/asciidoc-app/source/hello-no-layout.adoc create mode 100644 middleman-core/fixtures/asciidoc-app/source/hello-with-front-matter.adoc create mode 100644 middleman-core/fixtures/asciidoc-app/source/hello-with-layout.adoc create mode 100644 middleman-core/fixtures/asciidoc-app/source/hello-with-title.adoc create mode 100755 middleman-core/fixtures/asciidoc-app/source/hello.adoc create mode 100755 middleman-core/fixtures/asciidoc-app/source/images/tiger.gif create mode 100644 middleman-core/fixtures/asciidoc-app/source/layouts/default.erb create mode 100644 middleman-core/fixtures/asciidoc-app/source/master.adoc diff --git a/Gemfile b/Gemfile index 25460022..cee011ba 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,7 @@ gem "slim", :require => false gem "liquid", :require => false gem "less", "~> 2.3.0", :require => false gem "stylus", :require => false +gem "asciidoctor", :require => false # gem "pry", :require => false # gem "pry-debugger", :require => false diff --git a/middleman-core/features/asciidoc.feature b/middleman-core/features/asciidoc.feature new file mode 100644 index 00000000..14bca69e --- /dev/null +++ b/middleman-core/features/asciidoc.feature @@ -0,0 +1,155 @@ +Feature: AsciiDoc Support + In order to test included AsciiDoc support + + Scenario: Rendering html + Given the Server is running at "asciidoc-app" + When I go to "/hello.html" + Then I should see: + """ +
+

Hello, AsciiDoc! + Middleman, I am in you.

+
+ """ + + Scenario: Rendering html with default layout + Given a fixture app "asciidoc-app" + And a file named "config.rb" with: + """ + set :layout, :default + """ + Given the Server is running at "asciidoc-app" + When I go to "/hello.html" + Then I should see: + """ + + + + Fallback + + +
+

Hello, AsciiDoc! + Middleman, I am in you.

+
+ + + """ + + Scenario: Rendering html with explicit layout + Given the Server is running at "asciidoc-app" + When I go to "/hello-with-layout.html" + Then I should see: + """ + + + + Fallback + + +
+

Hello, AsciiDoc!

+
+ + + """ + + Scenario: Rendering html with no layout + Given the Server is running at "asciidoc-app" + When I go to "/hello-no-layout.html" + Then I should see: + """ +
+

Hello, AsciiDoc!

+
+ """ + + Scenario: Rendering html using title from document + Given the Server is running at "asciidoc-app" + When I go to "/hello-with-title.html" + Then I should see: + """ + + + + Page Title + + +

Page Title

+
+
+
+

Hello, AsciiDoc!

+
+
+
+ + + """ + + Scenario: Rendering html with title and layout from front matter + Given the Server is running at "asciidoc-app" + When I go to "/hello-with-front-matter.html" + Then I should see: + """ + + + + Page Title + + +
+

Hello, AsciiDoc!

+
+ + + """ + + Scenario: Including a file relative to source root + Given the Server is running at "asciidoc-app" + When I go to "/master.html" + Then I should see: + """ +
+
+
I'm included content.
+
+ """ + + Scenario: Linking to an image + Given the Server is running at "asciidoc-app" + When I go to "/gallery.html" + Then I should see: + """ +
+
+ tiger +
+ """ + + Scenario: Configuring custom AsciiDoc attributes + Given a fixture app "asciidoc-app" + And a file named "config.rb" with: + """ + set :asciidoc_attributes, %w(foo=bar) + """ + Given the Server is running at "asciidoc-app" + When I go to "/custom-attribute.html" + Then I should see "bar" + + Scenario: Highlighting source code + Given a fixture app "asciidoc-app" + And a file named "config.rb" with: + """ + set :asciidoc_attributes, %w(source-highlighter=html-pipeline) + """ + Given the Server is running at "asciidoc-app" + When I go to "/code.html" + Then I should see: + """ +
+
+
puts "Is this mic on?"
+
+
+ """ diff --git a/middleman-core/fixtures/asciidoc-app/config.rb b/middleman-core/fixtures/asciidoc-app/config.rb new file mode 100644 index 00000000..e69de29b diff --git a/middleman-core/fixtures/asciidoc-app/source/_include.adoc b/middleman-core/fixtures/asciidoc-app/source/_include.adoc new file mode 100644 index 00000000..b0cdec38 --- /dev/null +++ b/middleman-core/fixtures/asciidoc-app/source/_include.adoc @@ -0,0 +1 @@ +I'm included content. diff --git a/middleman-core/fixtures/asciidoc-app/source/code.adoc b/middleman-core/fixtures/asciidoc-app/source/code.adoc new file mode 100644 index 00000000..e0d6a519 --- /dev/null +++ b/middleman-core/fixtures/asciidoc-app/source/code.adoc @@ -0,0 +1,3 @@ +```ruby +puts "Is this mic on?" +``` diff --git a/middleman-core/fixtures/asciidoc-app/source/custom-attribute.adoc b/middleman-core/fixtures/asciidoc-app/source/custom-attribute.adoc new file mode 100644 index 00000000..b4eb729d --- /dev/null +++ b/middleman-core/fixtures/asciidoc-app/source/custom-attribute.adoc @@ -0,0 +1,3 @@ +++++ +{foo} +++++ diff --git a/middleman-core/fixtures/asciidoc-app/source/gallery.adoc b/middleman-core/fixtures/asciidoc-app/source/gallery.adoc new file mode 100644 index 00000000..4a9f37f1 --- /dev/null +++ b/middleman-core/fixtures/asciidoc-app/source/gallery.adoc @@ -0,0 +1 @@ +image::tiger.gif[] diff --git a/middleman-core/fixtures/asciidoc-app/source/hello-no-layout.adoc b/middleman-core/fixtures/asciidoc-app/source/hello-no-layout.adoc new file mode 100644 index 00000000..8156a16f --- /dev/null +++ b/middleman-core/fixtures/asciidoc-app/source/hello-no-layout.adoc @@ -0,0 +1,2 @@ +:page-layout: false +Hello, AsciiDoc! diff --git a/middleman-core/fixtures/asciidoc-app/source/hello-with-front-matter.adoc b/middleman-core/fixtures/asciidoc-app/source/hello-with-front-matter.adoc new file mode 100644 index 00000000..5e1f91a3 --- /dev/null +++ b/middleman-core/fixtures/asciidoc-app/source/hello-with-front-matter.adoc @@ -0,0 +1,5 @@ +--- +title: Page Title +layout: default +--- +Hello, AsciiDoc! diff --git a/middleman-core/fixtures/asciidoc-app/source/hello-with-layout.adoc b/middleman-core/fixtures/asciidoc-app/source/hello-with-layout.adoc new file mode 100644 index 00000000..763ba8c1 --- /dev/null +++ b/middleman-core/fixtures/asciidoc-app/source/hello-with-layout.adoc @@ -0,0 +1,2 @@ +:page-layout: default +Hello, AsciiDoc! diff --git a/middleman-core/fixtures/asciidoc-app/source/hello-with-title.adoc b/middleman-core/fixtures/asciidoc-app/source/hello-with-title.adoc new file mode 100644 index 00000000..e2c7673b --- /dev/null +++ b/middleman-core/fixtures/asciidoc-app/source/hello-with-title.adoc @@ -0,0 +1,4 @@ += Page Title +:page-layout: default + +Hello, AsciiDoc! diff --git a/middleman-core/fixtures/asciidoc-app/source/hello.adoc b/middleman-core/fixtures/asciidoc-app/source/hello.adoc new file mode 100755 index 00000000..6c1ab072 --- /dev/null +++ b/middleman-core/fixtures/asciidoc-app/source/hello.adoc @@ -0,0 +1,4 @@ +Hello, AsciiDoc! +ifdef::env-middleman[] +Middleman, I am in you. +endif::env-middleman[] diff --git a/middleman-core/fixtures/asciidoc-app/source/images/tiger.gif b/middleman-core/fixtures/asciidoc-app/source/images/tiger.gif new file mode 100755 index 0000000000000000000000000000000000000000..2498f1aac58dab923f0fd99b1c8ee6b8c53c7158 GIT binary patch literal 43 scmZ?wbhEHbWMp7uXkcKtd-pB_1B2pE79h#MpaUX6G7L;iE{qJ;0KEqWk^lez literal 0 HcmV?d00001 diff --git a/middleman-core/fixtures/asciidoc-app/source/layouts/default.erb b/middleman-core/fixtures/asciidoc-app/source/layouts/default.erb new file mode 100644 index 00000000..ec05c79c --- /dev/null +++ b/middleman-core/fixtures/asciidoc-app/source/layouts/default.erb @@ -0,0 +1,10 @@ + + + +<%= data.page.title || 'Fallback' %> + + +<%= yield %> + + + diff --git a/middleman-core/fixtures/asciidoc-app/source/master.adoc b/middleman-core/fixtures/asciidoc-app/source/master.adoc new file mode 100644 index 00000000..1b789b44 --- /dev/null +++ b/middleman-core/fixtures/asciidoc-app/source/master.adoc @@ -0,0 +1,3 @@ +.... +include::_include.adoc[] +.... diff --git a/middleman-core/lib/middleman-core/renderers/asciidoc.rb b/middleman-core/lib/middleman-core/renderers/asciidoc.rb index 611b05ab..6e89df16 100644 --- a/middleman-core/lib/middleman-core/renderers/asciidoc.rb +++ b/middleman-core/lib/middleman-core/renderers/asciidoc.rb @@ -18,16 +18,23 @@ module Middleman app.after_configuration do # QUESTION should base_dir be equal to docdir instead? - config[:asciidoc][:base_dir] = File.expand_path config[:source] - config[:asciidoc][:attributes].concat (config[:asciidoc_attributes] || []) - config[:asciidoc][:attributes] << %(imagesdir=#{File.join (config[:http_prefix] || '/').chomp('/'), config[:images_dir]}) + config[:asciidoc][:base_dir] = source_dir + config[:asciidoc][:attributes].concat(config[:asciidoc_attributes] || []) + config[:asciidoc][:attributes] << %(imagesdir=#{File.join((config[:http_prefix] || '/').chomp('/'), config[:images_dir])}) sitemap.provides_metadata(/\.adoc$/) do |path| # read the AsciiDoc header only to set page options and data # header values can be accessed via app.data.page. in the layout doc = Asciidoctor.load_file path, :safe => :safe, :parse_header_only => true opts = {} - opts[:layout] = (doc.attr 'page-layout') if (doc.attr? 'page-layout') + if doc.attr? 'page-layout' + case (layout = (doc.attr 'page-layout')) + when '', 'false' + opts[:layout] = false + else + opts[:layout] = layout + end + end opts[:layout_engine] = (doc.attr 'page-layout-engine') if (doc.attr? 'page-layout-engine') # TODO override attributes to set docfile, docdir, docname, etc # alternative is to set :renderer_options, which get merged into options by the rendering extension