Accessing Middleman functions from within Asciidoctor

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing Middleman functions from within Asciidoctor

thoraxe
Middleman provides useful helpers: https://middlemanapp.com/basics/helper_methods/

I'm wondering how I can access them within Asciidoc(tor)?

For example, I want to use the lorem ipsum generator:

https://middlemanapp.com/basics/helper_methods/#lorem-ipsum-placehold-it-helpers

In ERB this would just be:

<%= lorem.sentences 5 %>

But I'm having problems figuring out the "interpolation" syntax for Asciidoctor in middleman.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing Middleman functions from within Asciidoctor

thoraxe
Ah never mind -- this is not currently supported and there's a thread on here talking about why. Oh well!
Reply | Threaded
Open this post in threaded view
|

Re: Accessing Middleman functions from within Asciidoctor

mojavelinux
Administrator
Erik,

The way you implement this in Asciidoctor is by using a macro extension. In fact, a lorem ipsum generator is a fantastic example of a block or inline macro.

You'd introduce syntax such as:

lorem::sentences[5]

Then you'd use an block macro extension to handle this macro (aka tag) and insert content into the output.

Something like:

require 'middleman-core'
require 'middleman-core/extensions/lorem'
require 'asciidoctor'
require 'asciidoctor/extensions'

Asciidoctor::Extensions.register do
  block_macro :lorem do
    name_positional_attributes 'length'
    process do |parent, target, attrs|
      lorem = Middleman::Extensions::Lorem::LoremObject
      method = target.to_sym
      if lorem.respond_to? method
        content = lorem.send(method, attrs.fetch('length', 1).to_i.abs)
        create_paragraph parent, content, {}
      else
        warn 'Unknown lorem target for lorem block macro'
        nil
      end
    end
  end
end

I'll add this to the extensions lab.

Cheers,

-Dan

On Fri, Apr 1, 2016 at 7:53 AM, thoraxe [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Ah never mind -- this is not currently supported and there's a thread on here talking about why. Oh well!


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Accessing-Middleman-functions-from-within-Asciidoctor-tp4577p4582.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | http://google.com/profiles/dan.j.allen
Reply | Threaded
Open this post in threaded view
|

Re: Accessing Middleman functions from within Asciidoctor

mojavelinux
Administrator
In reply to this post by thoraxe
> I'm having problems figuring out the "interpolation" syntax for Asciidoctor in middleman. 

The general philosophy in AsciiDoc & Asciidoctor is to keep your content free of non-AsciiDoc content (even HTML). The way to do this when the normal AsciiDoc syntax isn't cutting it is to use the extension points.

While it's possible in theory to allow arbitrary interpolation in the AsciiDoc content, it couples the AsciiDoc with the presentation pipeline, while at the same time muddying the intent. The extension points are designed to capture intent so they can be implemented appropriately for whatever pipeline you're using.

I recognize this is a bit of a purist approach, but my recommendation is to at least try to make an extension work before dropping down to something more low level.

Cheers,

-Dan

On Fri, Apr 1, 2016 at 3:12 PM, Dan Allen <[hidden email]> wrote:
Erik,

The way you implement this in Asciidoctor is by using a macro extension. In fact, a lorem ipsum generator is a fantastic example of a block or inline macro.

You'd introduce syntax such as:

lorem::sentences[5]

Then you'd use an block macro extension to handle this macro (aka tag) and insert content into the output.

Something like:

require 'middleman-core'
require 'middleman-core/extensions/lorem'
require 'asciidoctor'
require 'asciidoctor/extensions'

Asciidoctor::Extensions.register do
  block_macro :lorem do
    name_positional_attributes 'length'
    process do |parent, target, attrs|
      lorem = Middleman::Extensions::Lorem::LoremObject
      method = target.to_sym
      if lorem.respond_to? method
        content = lorem.send(method, attrs.fetch('length', 1).to_i.abs)
        create_paragraph parent, content, {}
      else
        warn 'Unknown lorem target for lorem block macro'
        nil
      end
    end
  end
end

I'll add this to the extensions lab.

Cheers,

-Dan

On Fri, Apr 1, 2016 at 7:53 AM, thoraxe [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Ah never mind -- this is not currently supported and there's a thread on here talking about why. Oh well!


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Accessing-Middleman-functions-from-within-Asciidoctor-tp4577p4582.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | http://google.com/profiles/dan.j.allen



--
Dan Allen | @mojavelinux | http://google.com/profiles/dan.j.allen