Accessing Middleman helper functions within Asciidoctor templates

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

Accessing Middleman helper functions within Asciidoctor templates

thoraxe
In relation to a few of my other posts, I'm marching along with trying to make a pretty HTML template with with ASCIIdoc(tor).

I have a data file in /data that has various source lookup information:

apacheconf   : "Apache Configuration"
apl          : "APL"
applescript  : "AppleScript"
asciidoc     : "AsciiDoc"
aspnet       : "ASP.NET (C#)"

I have a helper function that, when placed in config.rb and used with ERB works fine:

  def displayname(name)
    if data.displaynames[name]
      return data.displaynames[name]
    else
      return name.titlecase
    end
  end

I have two problems:

1) With "displayname" defined in config.rb and called from inside a template file (eg: /source/templates/block_listing.html.haml) I get an "undefined method" error.

2) With "displayname" defined in /source/templates/helpers.rb the function works, but then I get "undefined local variable or method 'data'"

So, I seem to be caught between a rock and a hard place. If Middleman knows about the helper/data, Asciidoctor's template processor doesn't know about the helper. If Asciidoctor knows about the helper, it doesn't know about Middleman's data.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing Middleman helper functions within Asciidoctor templates

thoraxe
I'm kind of thinking out loud here, but I am wondering if this is some kind of preprocessor extension that requires the middleman stuff and then makes the data tree and functions available, but I kind of sort of tried that without much success. I'll fuss with it a little more, but I wonder if that's even the right track.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing Middleman helper functions within Asciidoctor templates

thoraxe
I'm really just floundering here, but here's what I tried...

I created "lib" and then created "middleman-data.rb" which contains:

require 'middleman-core'
require 'middleman-core/core_extensions/data'
require 'asciidoctor'
require 'asciidoctor/extensions'

class MiddlemanDataPreprocessor < Asciidoctor::Extensions::Preprocessor
  def process document, reader
    Middleman::CoreExtensions::Data.initialize
  end
end

Asciidoctor::Extensions.register do
  preprocessor MiddlemanDataPreprocessor
end

This file looks like it's loaded by middleman:

...
== File Change: lib/middleman-data.rb
...
<raw>

I then put a binding.pry inside my source/templates/block_listing.html.haml but, inside Pry, it doesn't seem like this extension stuff happened:

<raw>
[1] pry(#<Asciidoctor::Block>)> ls
Asciidoctor::Substitutors#methods: 
  apply_header_subs  convert_quoted_text   lock_in_subs                  passthroughs                resolve_subs          sub_callouts        sub_post_replacements  sub_specialcharacters  
  apply_normal_subs  do_replacement        normalize_string              resolve_block_subs          restore_passthroughs  sub_inline_anchors  sub_quotes             sub_specialchars       
  apply_subs         extract_passthroughs  parse_attributes              resolve_lines_to_highlight  split_simple_csv      sub_inline_xrefs    sub_replacements       unescape_bracketed_text
  apply_title_subs   highlight_source      parse_quoted_text_attributes  resolve_pass_subs           sub_attributes        sub_macros          sub_source             unescape_brackets      
Asciidoctor::AbstractNode#methods: 
  add_role  attributes  document                    has_role?  id=        list_marker_keyword  normalize_asset_path   option?  read_asset     reftext?       role   set_attr         
  attr      context     generate_data_uri           icon_uri   image_uri  media_uri            normalize_system_path  parent   read_contents  relative_path  role?  set_option       
  attr?     converter   generate_data_uri_from_uri  id         is_uri?    node_name            normalize_web_path     parent=  reftext        remove_role    roles  update_attributes
Asciidoctor::AbstractBlock#methods: 
  <<      assign_caption  block?  blocks?  caption=         content_model   context=  file     inline?  level=  query             remove_sub  sections   source_location   style   sub?  title   title?
  append  assign_index    blocks  caption  captioned_title  content_model=  convert   find_by  level    lineno  reindex_sections  render      sections?  source_location=  style=  subs  title=
Asciidoctor::Block#methods: blockname  content  lines  lines=  source  to_s
Haml::Helpers::ActionViewExtensions#methods: generate_content_class_names  page_class  with_raw_haml_concat
Haml::Helpers::XssMods#methods: 
  capture_haml                find_and_preserve                haml_indent                html_escape                precede                 succeed               
  capture_haml_with_haml_xss  find_and_preserve_with_haml_xss  haml_indent_with_haml_xss  html_escape_with_haml_xss  precede_with_haml_xss   succeed_with_haml_xss 
  escape_once                 haml_concat                      haml_tag                   list_of                    preserve                surround              
  escape_once_with_haml_xss   haml_concat_with_haml_xss        haml_tag_with_haml_xss     list_of_with_haml_xss      preserve_with_haml_xss  surround_with_haml_xss
Haml::Helpers#methods: 
  autowidth?                     find_and_preserve_without_haml_xss  haml_tag_without_haml_xss     is_haml?                  preserve_without_haml_xss  succeed_without_haml_xss   url_query    
  block_is_haml?                 flatten                             html_attrs                    list_of_without_haml_xss  section_level              surround_without_haml_xss  video_iframe?
  capture_haml_without_haml_xss  haml_concat_without_haml_xss        html_escape_without_haml_xss  non_haml                  section_title              tab_down                   video_uri    
  escape_once_without_haml_xss   haml_indent_without_haml_xss        init_haml_helpers             precede_without_haml_xss  spread?                    tab_up                     with_tabs    
instance variables: 
  @attributes  @caption        @context       @document     @id     @lines               @next_section_number  @parent        @source_location  @subs 
  @blocks      @content_model  @default_subs  @haml_buffer  @level  @next_section_index  @node_name            @passthroughs  @style            @title
locals: _  __  __in_erb_template  _dir_  _erbout  _ex_  _file_  _haml_locals  _hamlout  _in_  _out_  _pry_  classes  code_class  code_lang  locals  nowrap  pre_class  pre_lang  this
[2] pry(#<Asciidoctor::Block>)> data
NameError: undefined local variable or method `data' for #<Asciidoctor::Block:0x007f68702fe858>
from (pry):1:in `block in singleton class'
[3] pry(#<Asciidoctor::Block>)> 
[4] pry(#<Asciidoctor::Block>)> 
[5] pry(#<Asciidoctor::Block>)> Asciidoctor::Extensions
NameError: uninitialized constant Asciidoctor::Extensions
from (pry):2:in `block in singleton class'

I'm sure I'm not doing any of this right, but that's what I've tried so far.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing Middleman helper functions within Asciidoctor templates

mojavelinux
Administrator
This is a namespacing issue. I'll try to figure out how to get Asciidoctor templates talking to the Middleman config namespace. Stay tuned.

-Dan

On Wed, Apr 6, 2016 at 10:20 AM, thoraxe [via Asciidoctor :: Discussion] <[hidden email]> wrote:
I'm really just floundering here, but here's what I tried...

I created "lib" and then created "middleman-data.rb" which contains:

require 'middleman-core'
require 'middleman-core/core_extensions/data'
require 'asciidoctor'
require 'asciidoctor/extensions'

class MiddlemanDataPreprocessor < Asciidoctor::Extensions::Preprocessor
  def process document, reader
    Middleman::CoreExtensions::Data.initialize
  end
end

Asciidoctor::Extensions.register do
  preprocessor MiddlemanDataPreprocessor
end

This file looks like it's loaded by middleman:

...
== File Change: lib/middleman-data.rb
...
<raw>

I then put a binding.pry inside my source/templates/block_listing.html.haml but, inside Pry, it doesn't seem like this extension stuff happened:

<raw>
[1] pry(#<Asciidoctor::Block>)> ls
Asciidoctor::Substitutors#methods: 
  apply_header_subs  convert_quoted_text   lock_in_subs                  passthroughs                resolve_subs          sub_callouts        sub_post_replacements  sub_specialcharacters  
  apply_normal_subs  do_replacement        normalize_string              resolve_block_subs          restore_passthroughs  sub_inline_anchors  sub_quotes             sub_specialchars       
  apply_subs         extract_passthroughs  parse_attributes              resolve_lines_to_highlight  split_simple_csv      sub_inline_xrefs    sub_replacements       unescape_bracketed_text
  apply_title_subs   highlight_source      parse_quoted_text_attributes  resolve_pass_subs           sub_attributes        sub_macros          sub_source             unescape_brackets      
Asciidoctor::AbstractNode#methods: 
  add_role  attributes  document                    has_role?  id=        list_marker_keyword  normalize_asset_path   option?  read_asset     reftext?       role   set_attr         
  attr      context     generate_data_uri           icon_uri   image_uri  media_uri            normalize_system_path  parent   read_contents  relative_path  role?  set_option       
  attr?     converter   generate_data_uri_from_uri  id         is_uri?    node_name            normalize_web_path     parent=  reftext        remove_role    roles  update_attributes
Asciidoctor::AbstractBlock#methods: 
  <<      assign_caption  block?  blocks?  caption=         content_model   context=  file     inline?  level=  query             remove_sub  sections   source_location   style   sub?  title   title?
  append  assign_index    blocks  caption  captioned_title  content_model=  convert   find_by  level    lineno  reindex_sections  render      sections?  source_location=  style=  subs  title=
Asciidoctor::Block#methods: blockname  content  lines  lines=  source  to_s
Haml::Helpers::ActionViewExtensions#methods: generate_content_class_names  page_class  with_raw_haml_concat
Haml::Helpers::XssMods#methods: 
  capture_haml                find_and_preserve                haml_indent                html_escape                precede                 succeed               
  capture_haml_with_haml_xss  find_and_preserve_with_haml_xss  haml_indent_with_haml_xss  html_escape_with_haml_xss  precede_with_haml_xss   succeed_with_haml_xss 
  escape_once                 haml_concat                      haml_tag                   list_of                    preserve                surround              
  escape_once_with_haml_xss   haml_concat_with_haml_xss        haml_tag_with_haml_xss     list_of_with_haml_xss      preserve_with_haml_xss  surround_with_haml_xss
Haml::Helpers#methods: 
  autowidth?                     find_and_preserve_without_haml_xss  haml_tag_without_haml_xss     is_haml?                  preserve_without_haml_xss  succeed_without_haml_xss   url_query    
  block_is_haml?                 flatten                             html_attrs                    list_of_without_haml_xss  section_level              surround_without_haml_xss  video_iframe?
  capture_haml_without_haml_xss  haml_concat_without_haml_xss        html_escape_without_haml_xss  non_haml                  section_title              tab_down                   video_uri    
  escape_once_without_haml_xss   haml_indent_without_haml_xss        init_haml_helpers             precede_without_haml_xss  spread?                    tab_up                     with_tabs    
instance variables: 
  @attributes  @caption        @context       @document     @id     @lines               @next_section_number  @parent        @source_location  @subs 
  @blocks      @content_model  @default_subs  @haml_buffer  @level  @next_section_index  @node_name            @passthroughs  @style            @title
locals: _  __  __in_erb_template  _dir_  _erbout  _ex_  _file_  _haml_locals  _hamlout  _in_  _out_  _pry_  classes  code_class  code_lang  locals  nowrap  pre_class  pre_lang  this
[2] pry(#<Asciidoctor::Block>)> data
NameError: undefined local variable or method `data' for #<Asciidoctor::Block:0x007f68702fe858>
from (pry):1:in `block in singleton class'
[3] pry(#<Asciidoctor::Block>)> 
[4] pry(#<Asciidoctor::Block>)> 
[5] pry(#<Asciidoctor::Block>)> Asciidoctor::Extensions
NameError: uninitialized constant Asciidoctor::Extensions
from (pry):2:in `block in singleton class'

I'm sure I'm not doing any of this right, but that's what I've tried so far.


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Accessing-Middleman-helper-functions-within-Asciidoctor-templates-tp4601p4603.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 helper functions within Asciidoctor templates

thoraxe
Any luck here? I've poked around but I'm not enough of a Rubyist to know...