How to render html file as haml - html

I'm fairly new to ruby and working on building a front-end styleguide that has html snippets I'd like to render as haml into a pre tag. I'm building a helper for middleman and have figured out how to read an HTML file and output its contents. Now I'd like to convert the html to haml and output that.
Looking around it seems like the html2haml gem is what I want to use, though the doc on that gem seems to only cover using it on the command line, whereas I'm trying to add this functionality to a helper.
Here is what I have so far for a helper
helpers do
def render_snippet(page)
p1 = ("<pre><code>").html_safe
p2 = File.read("source/"+"#{page}")
p3 = ("</code></pre>").html_safe
p0+p1+p2+p3
end
end
Here is how I'm using the helper
= render_snippet "partials/examples/typography/elements.html"

To answer your question, this is how you can make a helper to use html2haml gem outside the terminal shell commands
# some_view.html.erb
<%= render html_2_haml("home/my_partial.html") %>
# app/helpers/application_helper.rb
module ApplicationHelper
def html_2_haml(path)
file_name = path.split("/").last
path_with_underscore = path.gsub(file_name, "_#{file_name}")
system "html2haml app/views/#{path_with_underscore} app/views/#{path_with_underscore}.haml"
"#{path}.haml"
end
end
Now i'd like to say this definitely will not work in production (as it's dynamically creating a new file and hosting services like Heroku just won't allow that) but if your just making yourself a development helper for this-and-that then perhaps this could be helpful to you.

I ended up working on this some more and ended up with the following:
def render_html2haml(file)
templateSource = preserve(File.read("source/"+"#{file}"))
haml = Html2haml::HTML.new(templateSource, {:erb => nil})
content_tag(:pre, content_tag(:code, haml.render))
end

Related

How to render HTML using a Ruby on Rails 5 API Application

We're using Ruby on Rails 5.0.0.1 in API-Mode. What Middleware and Configurations do I need to add, in order to being able to render html instead of json in an controller.
Edit:
Thank you all for your answers.
Want I wanted is to render normal erb/haml views. not html in json like 'kartik upadhyay' mentioned. Since the Application is primary an JSON API we didn't want the full blown default Rails Installation, which mean our Application Controller extends from ActionController::API.
My Plan was to make a PdfRendering Controller which includes all Modules needed for ActionView to work. What I rather did was reading the asset pipeline (because we wanted to use sass) and inherit the application controller from ActionController::Base. as other rails apps would do.
It's not the nicest solution since you're including the whole ActionController::Base and all of it's Feature that you may not be using. But it's still slimmer that a full Blown Rails installation (especially the middleware aka. sessions etc.)
include ActionView::Layouts
include ActionController::Rendering
Add these to your controller
You can use render to string method of rails for rendering your html as string inside json, put this inside your controller:
render json: { data: render_to_string('html_file_name') }
this will render html as response like:
{"data": "<html>\n<h3>hello</h3>\n</html>"}
you can render erb/haml/slim etc. files like the following:
format.html { render :file_name }
You can simply use this gem RABL.
https://github.com/nesquena/rabl

Config.ru running as server not reading html

I recently began programming and learning Ruby and JavaScript and was attempting to read my html file through my Sinatra server using a config.ru file.
The server runs, its hitting all the routes but I think there may be something wrong with the server code for the index page:
get("/") do
content_type :html
File.read( File.expand_path("../views/index.html", __FILE__) )
end
Put index.html in public folder. Sinatra will serve files in public as is. So you need to request it directly e.g. http://localhost/index.html.
If you want to handle empty route i.e. get '/' use snippet below (from here):
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end
In order to be sure in settings.public_folder please check does it work correctly, does it return correct path.
Cheers!

page_url vs navigate_to in page-object gem + Jruby

I am trying to use jruby + page-object gem + Cucumber for a proof of concept. I used the following statement.
app_url = 'https:\\google.com'
page_url(app_url)
I get a
NoMethodError: undefined method `page_url' for #
However,
navigate_to(app_url)
works fine. page_url works fine in Ruby.
Is this the way this works in jRuby? Though navigate_to works, is this any different?
Thank you for your help!
page_url is a class method provided by including the PageObject module. It sets the url for the page so you can use the visit_page factory in your test:
object MyPage
include PageObject
page_url "http://example.com/"
end
In a test somewhere:
visit_page MyPage do |page|
page.some_object_element.do_something
end
navigate_to is browser functionality exposed directly in your test via some World magic.

How to include my own Ruby (on rails) functions inside an HTML page

I'm creating a web-app.
While it works great, writing whole pieces of code inside "<% %>" tags in ruby on rails is pretty ugly.
I tried to define a function in my controller.rb and then call it from the html page.
This does not work, as it does not recognize the function.
I'd appreciate any help here. Did I put my function in the correct place? Do I need to load using "require"?
For example:
controller file:
class WelcomeController < ApplicationController
def index
end
def myfunc(x)
puts x
end
end
HTML file (index.html):
<h1>Welcome</h1>
<p>
<%= myfunc(5) %>
</p>
What you are referring to is called a helper in Rails. There are two ways that you could implement this.
Option number one is to place the method you want to access inside a helper module. The most common one is ApplicationHelper which you can find in RAILS_ROOT/app/helpers/application_helper.rb. If you place the method in there it will be accessible from the views.
Another way if you still want/need to have the method in the controller, then you can use the helper_method function like this:
def WelcomeController < ApplicationController
helper_method :my_func
private
def my_func(x)
puts x
end
end
The usage of private is not needed, but only good practice so that the method cannot be accidentally used as a Controller action or something.
Ruby on rails has a Model-View-Controller architecture - in those architectures you can't access the controller from the view.
You could set a variable with the output from your function, and access that variable from your view

HTML tidy/cleaning in Ruby 1.9

I'm currently using the RubyTidy Ruby bindings for HTML tidy to make sure HTML I receive is well-formed. Currently this library is the only thing holding me back from getting a Rails application on Ruby 1.9. Are there any alternative libraries out there that will tidy up chunks of HTML on Ruby 1.9?
http://github.com/libc/tidy_ffi/blob/master/README.rdoc works with ruby 1.9 (latest version)
If you are working on windows, you need to set the library_path eg
require 'tidy_ffi'
TidyFFI.library_path = 'lib\\tidy\\bin\\tidy.dll'
tidy = TidyFFI::Tidy.new('test')
puts tidy.clean
(It uses the same dll as tidy) The above links gives you more example of the usage.
I am using Nokogiri to fix invalid html:
Nokogiri::HTML::DocumentFragment.parse(html).to_html
Here is a nice example of how to make your html look better using tidy:
require 'tidy'
Tidy.path = '/opt/local/lib/libtidy.dylib' # or where ever your tidylib resides
nice_html = ""
Tidy.open(:show_warnings=>true) do |tidy|
tidy.options.output_xhtml = true
tidy.options.wrap = 0
tidy.options.indent = 'auto'
tidy.options.indent_attributes = false
tidy.options.indent_spaces = 4
tidy.options.vertical_space = false
tidy.options.char_encoding = 'utf8'
nice_html = tidy.clean(my_nasty_html_string)
end
# remove excess newlines
nice_html = nice_html.strip.gsub(/\n+/, "\n")
puts nice_html
For more tidy options, check out the man page.
Currently this library is the only
thing holding me back from getting a
Rails application on Ruby 1.9.
Watch out, the Ruby Tidy bindings have some nasty memory leaks. It's currently unusable in long running processes. (for the record, I'm using http://github.com/ak47/tidy)
I just had to remove it from a production Rails 2.3 application because it was leaking about 1MB/min.