Coder Social home page Coder Social logo

opal-haml's Introduction


Opal
Opal-Ruby 💛 JavaScript

Opal is a Ruby to JavaScript source-to-source compiler.
It also has an implementation of the Ruby corelib and stdlib.

Community:
Stack Overflow Backers on Open Collective Sponsors on Open Collective Slack Documentation
Code:
Gem Version Build Status Code Climate Coverage Status
Sponsors:
Nebulab: Open Source Fridays

Usage

See the website for more detailed instructions and guides for Rails, jQuery, Sinatra, rack, CDN, etc. https://opalrb.com.

Compiling Ruby code with the CLI (Command Line Interface)

Contents of app.rb:

puts 'Hello world!'

Then from the terminal

$ opal --compile app.rb > app.js # The Opal runtime is included by default
                                 # but can be skipped with the --no-opal flag

The resulting JavaScript file can be used normally from an HTML page:

<script src="app.js"></script>

Be sure to set the page encoding to UTF-8 inside your <head> tag as follows:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="app.js"></script></head>
  <body></body>
</html>

Just open this page in a browser and check the JavaScript console.

Compiling Ruby code from Ruby

Opal.compile is a simple interface to just compile a string of Ruby into a string of JavaScript code.

Opal.compile("puts 'wow'")  # => "(function() { ... self.$puts("wow"); ... })()"

Running this by itself is not enough; you need the opal runtime/corelib.

Using Opal::Builder

Opal::Builder can be used to build the runtime/corelib into a string.

Opal::Builder.build('opal') #=> "(function() { ... })()"

or to build an entire app including dependencies declared with require:

builder = Opal::Builder.new
builder.build_str('require "opal"; puts "wow"', '(inline)')
File.binwrite 'app.js', builder.to_s # must use binary mode for writing

Compiling Ruby code from HTML (or using it as you would with inline JavaScript)

opal-parser allows you to eval Ruby code directly from your HTML (and from Opal) files without needing any other building process.

So you can create a file like the one below, and start writing ruby for your web applications.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://cdn.opalrb.com/opal/current/opal.js"></script>
    <script src="https://cdn.opalrb.com/opal/current/opal-parser.js" onload="Opal.load('opal-parser')"></script>

    <script type="text/ruby">
      puts "hi"
    </script>

  </head>
  <body>
  </body>
</html>

Just open this page and check the JavaScript console.

NOTE: Although this is possible, this is not really recommended for production and should only be used as a quick way to get your hands on opal.

Running tests

Setup the project:

$ bin/setup

The test suite can be run using:

$ bundle exec rake

This command will run all RSpec and MSpec examples in sequence.

MSpec

MSpec tests can be run with:

$ rake mspec

Alternatively, you can just load up a rack instance using rackup, and visit http://localhost:9292/ in any web browser.

RSpec

RSpec tests can be run with:

$ rake rspec

Automated runs

A Guardfile with decent mappings between specs and lib/corelib/stdlib files is in place. Run bundle exec guard -i to start guard.

Code Overview

What code is supposed to run where?

  • lib/ code runs inside your Ruby env. It compiles Ruby to JavaScript.
  • opal/ is the runtime+corelib for our implementation (runs in browser).
  • stdlib/ is our implementation of Ruby's stdlib. It is optional (runs in browser).

lib/

The lib directory holds the Opal parser/compiler used to compile Ruby into JavaScript. It is also built ready for the browser into opal-parser.js to allow compilation in any JavaScript environment.

opal/

This directory holds the Opal runtime and corelib implemented in Ruby and JavaScript.

stdlib/

Holds the stdlib currently supported by Opal. This includes Observable, StringScanner, Date, etc.

Browser support

  • Internet Explorer 11
  • Firefox (Current - 1) or Current
  • Chrome (Current - 1) or Current
  • Safari (Current - 1) or Current
  • Opera (Current - 1) or Current

Any problems encountered using the browsers listed above should be reported as bugs.

(Current - 1) or Current denotes that we support the current stable version of the browser and the version that preceded it. For example, if the current version of a browser is 24.x, we support the 24.x and 23.x versions.

12.1x or (Current - 1) or Current denotes that we support Opera 12.1x as well as the last 2 versions of Opera. For example, if the current Opera version is 20.x, then we support Opera 12.1x, 19.x and 20.x but not Opera 15.x through 18.x.

Contributors

This project exists thanks to all the people who contribute. contributors

Versioning

Opal will broadly follow semver as a version policy, trying to bump the major version when introducing breaking changes. Being a language implementation we're also aware that there's a fine line between what can be considered breaking and what is expected to be "safe" or just "additive". Moving forward we'll attempt to better clarify what interfaces are meant to be public and what should be considered private.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Become a Backer Button

Sponsors

Donations

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

Become a Sponsor Button

Sponsored Contributions

Nebulab Logo

License

(The MIT License)

Copyright (C) 2013-2021 by Adam Beynon and the Opal contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

opal-haml's People

Contributors

adambeynon avatar elia avatar hmdne avatar papierkorb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

opal-haml's Issues

Passing url in JSON crashing rendering

Hello, I have a problem with rendering img tag.
I have a quite simple view

.rounded.overflow-hidden.border-2.border-grey-200.border-opacity-100
  %img.w-full{src: "#{self['image_url']}", alt: "Sunset in the mountains"}
  .px-6.py-4

and when i’m passing an object:

$document.at('#main-container').inner_html = Template['views/quiz/form'].render(res.json)

res:

{"id": "95b4406d-2bca-4d86-94f4-12078d11eca4", "image_url": "http://localhost:3000/derivations/crop/1600/1000/eyJpZCI6InF1aXovOTViNDQwNmQtMmJjYS00ZDg2LTk0ZjQtMTIwNzhkMTFlY2E0L2ltYWdlLzFkY2RlYTZhNDMwNGI0NzkwYThmZjI5MWQwYzY5NmQxLmpwZWciLCJzdG9yYWdlIjoic3RvcmUifQ?signature=1dd23cba5070cf334507f9eafc55b4e97c4539be9c502d05040f4678c9192e47" }

It throws

undefined method `html_escape_without_haml_xss' for Haml::Helpers

in the browser console. When I’m setting any string as url, not value from self it works. There is any way to bypass that?

template files with `haml` extension not found

since the latest updates haml template files have to be named example.js.haml instead of example.haml. Else the template is not found by Opal.

Due to this new behaviour, reusing files in app/views directory of a rails app is not possible.

with Rails - Template['myTemplate'] => nilClass

Could you give an example using the Rails Asset Pipeline?
Where do we put the haml template, and what is the exact file extension to use?
I've been trying many different combination with no success.

What exactly is the Template constant? What methods can we use for introspection?

I am using Rails 4 and Ruby 1.9.3 Everything else works like a charm!
Thanks for your work on this phenomenal tool!

Sprockets require_tree does not bring in templates

Given the following Opal app (on Rails):

application.rb

require 'opal'
require 'jquery'
require 'opal-jquery'
require 'opal-haml'
require 'app_templates'

p Template['foo']

app_templates.rb

#= require_tree ./templates

templates/foo.haml

%p Hello, World!

When I load it in the browser, I expect the console to display #<Template: 'templates/foo'> but I get nil.

I wonder if there's a way we can get this working. I've been looking through the Sprockets code but I can't find anything that would make this work for some files and not others. I'm open to pairing on it, as well.

Require does not work on *.haml files on Windows

Hi there,

I'm playing with ruby at the moment and decided to do with opal.

With *.opalerb templates everything works fine.
But I wanted to use haml templates, but it does not work.

Seems that what u call to "add *.haml" to sprockets does not work. Maybe a Windows related problem?

Here is my sample app:

# app/application.rb
require 'opal'
require 'jquery'
require 'opal-jquery'

# require views/example.haml
require 'views/example'

# require views/example2.opalerb - working fine!
require 'views/example2'

Document.ready? do
  template = Template['views/example']
  puts template.render(Example.new)
end

I added opal-haml to the Gemfile and did bundle install. I tried the gem from rubygems as well as the github version. The behaviour is the same.

This is my rakefile

# Rakefile
require 'opal'
require 'opal-jquery'
require 'sass'

desc "Build our app - transpiling rb to js and scss to css and copying some files ..."
task :build do

  Opal.append_path "app"
  Opal.append_path "app/views"
  Opal.append_path "app/class"

  #
  # FRONTEND JS
  #

  js_container = ""

  # RUBY -> JS
  js_container << Opal::Builder.build("application").to_s

  File.binwrite "public/lib/app.js", js_container

  #
  # FRONTEND CSS
  #

  css_container = ""

  # SASS/SCSS -> CSS
  sass_file = File.read "app/assets/stylesheets/application.scss"
  sass_engine = Sass::Engine.new sass_file, { :load_paths => [ "app/assets/stylesheets" ], :syntax => :scss }
  css_container << sass_engine.render {}

  # FONT AWESOME
  fa_file = File.read "app/lib/font-awesome-4.5.0/css/font-awesome.min.css"
  css_container << fa_file
  FileUtils::cp_r "app/lib/font-awesome-4.5.0/fonts/.","public/fonts"

  File.binwrite "public/lib/app.css", css_container

  puts "... finished"
end

When i execute rake build i get the following output:

** Invoke build (first_time)
** Execute build
rake aborted!
Opal::Builder::MissingRequire: A file required by "application" wasn't found.
can't find file: "views/example" in ["C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/opal", "C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/stdlib",
"C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib", "C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-jquery-0.4.1/lib", "D:/project/example/app", "D:/project/example/app/views", "D:/project/example/app/class"]
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:118:in `read'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:127:in `process_require'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `block in build_str'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `map'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `build_str'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:35:in `build'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:30:in `build'
D:/project/example/rakefile.rb:25:in `block in <top (required)>'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:240:in `call'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:235:in `each'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
C:/Ruby22-x64/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `each'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:75:in `run'
C:/Ruby22-x64/bin/rake:33:in `<main>'
Opal::Builder::MissingRequire: can't find file: "views/example" in ["C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/opal", "C:/Ruby22-x64/lib/ruby/gems/
2.2.0/gems/opal-0.9.0/stdlib", "C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib", "C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-jquery-0.4.1/lib", "
D:/project/example/app", "D:/project/example/app/views", "D:/FIRMA_MGH/PROJEKTE/_INTERN/_webg
ames/rubymaze/app/class"]
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:118:in `read'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:127:in `process_require'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `block in build_str'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `map'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:42:in `build_str'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:35:in `build'
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/opal-0.9.0/lib/opal/builder.rb:30:in `build'
D:/project/example/rakefile.rb:25:in `block in <top (required)>'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:240:in `call'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:235:in `each'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
C:/Ruby22-x64/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `each'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
C:/Ruby22-x64/lib/ruby/2.2.0/rake/application.rb:75:in `run'
C:/Ruby22-x64/bin/rake:33:in `<main>'
Tasks: TOP => build

Ruby x64 @ Win7 x64

Any idea?

Are dynamic attributes working?

I was trying to render this code, which works totally fine server-side

.keyboard
  -4.times do
    .octave
      -@keyboard.notes.each do |note|
        .key{class: "#{note.note_class}"}
          .name= note.name

But in opal, it's shooting an error because of the class attribute:

$NameError {name: "AttributeBuilder", message: "uninitialized constant Haml::AttributeBuilder", stack: "
    at singleton_class_alloc.TMP_1 [as $new] (htt…3ee36f324552023db5b5ff3e26a81fd2.js?body=1:29:94)"}message: "uninitialized constant Haml::AttributeBuilder"name: "AttributeBuilder"stack: "
    at singleton_class_alloc.TMP_1 [as $new] (http://localhost:7000/assets/corelib/error.self-df65ee27346f17b94c0b37c9857b1f2dec43c5344a0459172c0eac3145a96381.js?body=1:30:15)
    at module_constructor.$$const_missing [as $const_missing] (http://localhost:7000/assets/corelib/module.self-4cc9728db52c7473531359dd0cd8f324c4911311ff472f52ce46ebb69e3bbc82.js?body=1:401:50)
    at module_constructor.$$const_get [as $const_get] (http://localhost:7000/assets/corelib/module.self-4cc9728db52c7473531359dd0cd8f324c4911311ff472f52ce46ebb69e3bbc82.js?body=1:378:19)
    at TopScope.Opal.get (http://localhost:7000/assets/corelib/runtime.self-0a4ebf3da02b8566b78e10378a536b9a87b4d121dd9844c8aa2645449e4f4455.js?body=1:137:24)
    at TMP_3 (http://localhost:7000/assets/views/pages/index.self-ac868ac510e5c67eb322f25690b16ceb3ee36f324552023db5b5ff3e26a81fd2.js?body=1:21:66)
    at Object.Opal.yield1 (http://localhost:7000/assets/corelib/runtime.self-0a4ebf3da02b8566b78e10378a536b9a87b4d121dd9844c8aa2645449e4f4455.js?body=1:1197:14)
    at Array.$$each [as $each] (http://localhost:7000/assets/corelib/array.self-b9c43fc70584f48625b38b512188517d3f61cfd837bc66c0602cfdb25512dc7c.js?body=1:951:26)
    at TMP_2 (http://localhost:7000/assets/views/pages/index.self-ac868ac510e5c67eb322f25690b16ceb3ee36f324552023db5b5ff3e26a81fd2.js?body=1:27:102)
    at Number.$$times [as $times] (http://localhost:7000/assets/corelib/number.self-7125fe09e19841e9b269d53833f2f450330f49e78a7446f21b70a798fc78c6d2.js?body=1:782:9)
    at $PagesController.TMP_1 (http://localhost:7000/assets/views/pages/index.self-ac868ac510e5c67eb322f25690b16ceb3ee36f324552023db5b5ff3e26a81fd2.js?body=1:29:94)"__proto__: $StandardError$$class: singleton_class_alloc$initialize: function $$initialize(message, name)$name: function ()constructor: function $NameError()name:

Uncaught NoMethodError: undefined method `buffer' for #<Template::OutputBuffer:77>

Hi,
i get this when i try with the latest gem*

the haml is simple:
.col-md-12
%a{:href => '/manage/report'}
Reports

my opal/js in rails app is (shortened)
require "link"
Template["link"].render

I think this must be a versioning thing. When i try with erb it work.
Also the example works when i switch to all github versions.
But the haml github agains the gems does not work (as per readme)

Probably will solve itself soon.
I had another interesting thing when i misspelled the href in the template. Then i got undefined << for the same OutputBuffer. Try it. I looked at writing the spec for it, but i am very new...

So, a few random observation, but mainly i am impressed. Good going!!
T.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.