-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rendering Rails helpers in haml_example blocks #233
Comments
I've handled this by passing the files through Rails (#172) and parsing the HAML like so: HELPERS_TO_FILTER = [:notification]
HELPERS_TO_FILTER_REGEX = /
(?<==)
\s*
(?=
(?:#{HELPERS_TO_FILTER.join('|')})
\b
)
/x
HELPER_PREFIX = 'view_context.'
protected
def apply_styleguide_filters(html)
apply_example_output_filter(html)
html.to_html
end
def looks_like_haml?(string)
!!(string =~ /\A\s*[=%.#]/m)
end
private
def apply_example_output_filter(html)
html.css('.exampleOutput').each do |example_output|
example_output_content = example_output.content.strip
next unless looks_like_haml?(example_output_content)
example_output_with_prefixed_helpers =
prefix_helpers(example_output_content)
rendered_helper =
render_helper_from_haml(example_output_with_prefixed_helpers)
example_output.children.remove
example_output.add_child rendered_helper
end
end
# Helpers must be called within the controller's view context.
def prefix_helpers(string)
string.gsub(HELPERS_TO_FILTER_REGEX, HELPER_PREFIX)
end
def render_helper_from_haml(string)
begin
rendered_helper = Haml::Engine.new(string).render(binding)
rescue => e
raise "#{h(e)}<br><br>Perhaps this is a custom helper that needs filtering?".html_safe
end
Nokogiri::HTML::fragment(rendered_helper)
end |
Appreciate the feedback, but I'm still getting an error when I follow these instructions and try to load a URL (I read this approach as running through the regular render process and not needing to generate the static files.
When I run
Here are my files in full: /hologram_config.yml
/config/routes.rb
/app/controllers/styleguide_controller.rb
/app/controllers/application.rb
Looks like I have everything in the right place, but I could have messed up something obvious. Thanks in advance. Your help is (and has been) appreciated. |
Ah, yes. I forgot one key element that is unfortunate: put your HAML example inside of Also, you'll need to adjust |
Still not working. I am now getting an error that says it can't copy an unknown file type. I'm going to try some other approaches that some of the developers I work with suggested and if I hit on anything, I'll reply back in this thread. Thanks again for your help. |
I wanted to follow up since one of our developers had some time and was able to look at this. We ended up creating a custom haml renderer. ./styleguide_assets/code_example_renderers/haml.rb
As it says, this is hacked together and can probably be a bit cleaner (but I honestly have no idea). However, it works perfectly, and renders all rails helpers, be they default helpers, custom helpers or gem helpers. Quite a nice addition. |
Out of the box, generating documentation that includes a rails helper in a haml_example code block results in the following error...
Does this need to be done with a custom renderer or is it not possible. Specifically, I'm looking at three types of helpers.
1. Common Rails Helpers
= link_to '#', '#'
2. Gem Helpers
3. Custom Helpers from application.rb
= custom_icon_helper 'iconName'
Thank you in advance. I tried to work through building a custom markdown renderer, but just ended up getting lost in my own head, as Ruby/Rails is not my expertise.
The text was updated successfully, but these errors were encountered: