Permalink
Browse files

ERB pre-alpha

  • Loading branch information...
1 parent 8eb3624 commit 85b5d9218fcc923f539e254ece863327d1fa7bec Juan Maiz committed Apr 17, 2013
View
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby
+
+$:.unshift File.dirname(__FILE__) + '/../lib'
+require 'html2slim/command'
+
+cmd = HTML2Slim::ERBCommand.new(ARGV)
+cmd.run
View
@@ -3,5 +3,5 @@
$:.unshift File.dirname(__FILE__) + '/../lib'
require 'html2slim/command'
-cmd = HTML2Slim::Command.new(ARGV)
+cmd = HTML2Slim::HTMLCommand.new(ARGV)
cmd.run
View
@@ -2,7 +2,11 @@
require_relative 'html2slim/converter'
module HTML2Slim
- def self.convert!(input)
- Converter.new(input)
+ def self.convert!(input, format=:html)
+ if format.to_s == "html"
+ HTMLConverter.new(input)
+ else
+ ERBConverter.new(input)
+ end
end
end
@@ -80,10 +80,15 @@ def _process(file, destination = nil)
end
@options[:output] = slim_file && slim_file != '-' ? File.open(slim_file, 'w') : $stdout
- @options[:output].puts HTML2Slim.convert!(in_file)
+ # raise "|||#{self.class.inspect}|||"
+ @options[:output].puts HTML2Slim.convert!(in_file, (self.class.to_s =~ /ERB/ ? :erb : :html))
@options[:output].close
File.delete(file) if @options[:delete]
end
end
+ class HTMLCommand < Command
+ end
+ class ERBCommand < Command
+ end
end
@@ -1,12 +1,34 @@
require_relative 'hpricot_monkeypatches'
-# html = File.read(ARGV[0])
+
module HTML2Slim
class Converter
+ def to_s
+ @slim
+ end
+ end
+ class HTMLConverter < Converter
def initialize(html)
@slim = Hpricot(html).to_slim
end
- def to_s
- @slim
+ end
+ class ERBConverter < Converter
+ def initialize(file)
+ # open.read makes it works for files & IO
+ erb = File.exists?(file) ? open(file).read : file
+
+ erb.gsub!(/<%(.+?)\s*\{\s*(\|.+?\|)?\s*%>/){ %(<%#{$1} do #{$2}%>) }
+
+ # while, case, if, for and blocks...
+ erb.gsub!(/<%(\s*while .+?|\s*case .+?|\s*if .+?|\s*for .+?|.+?do\s*(\|.+?\|)?\s*)%>/){ %(<ruby code="#{$1.gsub(/"/, '&quot;')}">) }
+ # else
+ erb.gsub!(/<%\s*else\s*%>/, %(</ruby><ruby code="else">))
+ # elsif
+ erb.gsub!(/<%\s*(elsif .+?)\s*%>/){ %(</ruby><ruby code="#{$1}">) }
+ # when
+ erb.gsub!(/<%\s*(when .+?)\s*%>/){ %(</ruby><ruby code="#{$1}">) }
+ erb.gsub!(/<%\s*(end|})\s*%>/, %(</ruby>))
+ erb.gsub!(/<%(.+?)\s*%>/){ %(<ruby code="#{$1.gsub(/"/, '&quot;')}"></ruby>) }
+ @slim ||= Hpricot(erb).to_slim
end
end
end
@@ -1,5 +1,7 @@
require 'hpricot'
+Hpricot::XHTMLTransitional.tagset[:ruby] = [:code]
+# raise Hpricot::XHTMLTransitional.tagset.inspect
class Hpricot::BogusETag
def to_slim(lvl=0)
nil
@@ -8,22 +10,14 @@ def to_slim(lvl=0)
class Hpricot::Text
def to_slim(lvl=0)
- if to_s.strip.empty?
- nil
- else
- (' ' * lvl) + %(| #{to_s.gsub(/\s+/, ' ')})
- end
+ return nil if to_s.strip.empty?
+ (' ' * lvl) + %(| #{to_s.gsub(/\s+/, ' ')})
end
end
class Hpricot::Comment
def to_slim(lvl=0)
- # For SHTML <!--#include virtual="foo.html" -->
- if self.content =~ /include (file|virtual)="(.+)"/
- (' ' * lvl) + "= render '#{$~[2]}'"
- else
- nil
- end
+ nil
end
end
@@ -36,6 +30,14 @@ def to_slim(lvl=0)
class Hpricot::Elem
def slim(lvl=0)
r = (' ' * lvl)
+ if self.name == "ruby"
+ if self.attributes["code"].strip[0] == "="
+ return r += self.attributes["code"].strip
+ else
+ return r += "- " + self.attributes["code"].strip
+ end
+ end
+
r += self.name unless self.name == 'div' and (self.has_attribute?('id') || self.has_attribute?('class'))
r += "##{self['id']}" if self.has_attribute?('id')
self.remove_attribute('id')
@@ -0,0 +1,76 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+ <head>
+ <title><%= @title %></title>
+ </head>
+ <body>
+ <% @list = [1,2,3] %>
+ Maybe someone does <% a = "weird" %> kind of <% MARKUP %>
+ <p>Maybe someone does <% a = "weird" %> kind of <% MARKUP %></p>
+ Maybe someone does <% a = "weird" %> kind of <% MARKUP %>
+ <h1>Hi! My name is <%= @name %>!!!</h1>
+ <h1>Hi! 2+2 is <%= 2 + 2 %>!!!</h1>
+ <h1>Hi! a+b is <%= "a" + "b" %> and also <%= "your #{@name}" %>!!!</h1>
+ <h1>Hi! a+b is <%= 'a' + 'b' %> and also <%= "your #{@name}" %>!!!</h1>
+
+ <ul>
+ <% for item in @list %>
+ <li>
+ <h2><%= item.method_a %></h2>
+ <%= link_to item.method_b, item, class: "something", :"data-foo" => "The two spaces here must be respected." %>
+ </li>
+ <% end %>
+ </ul>
+ <ul>
+ <% @list.each do |item| %>
+ <li>
+ <h2><%= item.method_a %></h2>
+ <%= link_to item.method_b, item, class: "something", :"data-foo" => "The two spaces here must be respected." %>
+ </li>
+ <% end %>
+ </ul>
+ <ul>
+ <% @list.each{|item| %>
+ <li>
+ <h2><%= item.method_a %></h2>
+ <%= link_to item.method_b, item, class: "something", :"data-foo" => "The two spaces here must be respected." %>
+ </li>
+ <% } %>
+ </ul>
+ <ul>
+ <% @list.each{|a,b,c,d| %>
+ <li>
+ <h2><%= item.method_a %></h2>
+ <%= link_to item.method_b, item, class: "something", :"data-foo" => "The two spaces here must be respected." %>
+ </li>
+ <% } %>
+ </ul>
+ <ul>
+ <% 10.times { %>
+ <li> Hey! </li>
+ <% } %>
+ </ul>
+ <% if 1 == 1 %>
+ <p>1 is 1 man!</p>
+ <% end %>
+ <% if 1 == 1 %>
+ <p>1 is 1 man!</p>
+ <% else %>
+ <p>1 is not 1 man!</p>
+ <% end %>
+ <% if 1 == 1 %>
+ <p>1 is 1 man!</p>
+ <% elsif 2 == 2 %>
+ <p>2 is 2 man!</p>
+ <% end %>
+ <% case @foo %>
+ <% when 1 %>1
+ <% when 2 %>2
+ <% else %>3
+ <% end %>
+ <% while @foo.next %>
+ NEXT!
+ <% end %>
+
+ </body>
+</html>
@@ -0,0 +1,93 @@
+doctype
+html[xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"]
+ head
+ title
+ = @title
+ body
+ - @list = [1,2,3]
+ | Maybe someone does
+ - a = "weird"
+ | kind of
+ - MARKUP
+ p
+ | Maybe someone does
+ - a = "weird"
+ | kind of
+ - MARKUP
+ | Maybe someone does
+ - a = "weird"
+ | kind of
+ - MARKUP
+ h1
+ | Hi! My name is
+ = @name
+ | !!!
+ h1
+ | Hi! 2+2 is
+ = 2 + 2
+ | !!!
+ h1
+ | Hi! a+b is
+ = "a" + "b"
+ | and also
+ = "your #{@name}"
+ | !!!
+ h1
+ | Hi! a+b is
+ = 'a' + 'b'
+ | and also
+ = "your #{@name}"
+ | !!!
+ ul
+ - for item in @list
+ li
+ h2
+ = item.method_a
+ = link_to item.method_b, item, class: "something", :"data-foo" => "The two spaces here must be respected."
+ ul
+ - @list.each do |item|
+ li
+ h2
+ = item.method_a
+ = link_to item.method_b, item, class: "something", :"data-foo" => "The two spaces here must be respected."
+ ul
+ - @list.each do |item|
+ li
+ h2
+ = item.method_a
+ = link_to item.method_b, item, class: "something", :"data-foo" => "The two spaces here must be respected."
+ ul
+ - @list.each do |a,b,c,d|
+ li
+ h2
+ = item.method_a
+ = link_to item.method_b, item, class: "something", :"data-foo" => "The two spaces here must be respected."
+ ul
+ - 10.times do
+ li
+ | Hey!
+ - if 1 == 1
+ p
+ | 1 is 1 man!
+ - if 1 == 1
+ p
+ | 1 is 1 man!
+ - else
+ p
+ | 1 is not 1 man!
+ - if 1 == 1
+ p
+ | 1 is 1 man!
+ - elsif 2 == 2
+ p
+ | 2 is 2 man!
+ - case @foo
+
+ - when 1
+ | 1
+ - when 2
+ | 2
+ - else
+ | 3
+ - while @foo.next
+ | NEXT!
Oops, something went wrong.

0 comments on commit 85b5d92

Please sign in to comment.