Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first decent version

  • Loading branch information...
commit 7e1c6498263c0b13ef0f944a192dfa63d9e8d07d 1 parent 4cbef08
Juan Maiz authored
Showing with 1,059 additions and 6 deletions.
  1. +1 −0  .rvmrc
  2. +6 −0 Gemfile
  3. +18 −0 Gemfile.lock
  4. +25 −6 README.md
  5. +15 −0 Rakefile
  6. +7 −0 bin/html2slim
  7. +9 −0 fixtures/_av_partial_1.haml
  8. +9 −0 fixtures/_av_partial_1_ugly.haml
  9. +5 −0 fixtures/_av_partial_2.haml
  10. +5 −0 fixtures/_av_partial_2_ugly.haml
  11. +3 −0  fixtures/_layout_for_partial.haml
  12. +8 −0 fixtures/_partial.haml
  13. +3 −0  fixtures/_text_area.haml
  14. +8 −0 fixtures/breakage.haml
  15. +8 −0 fixtures/content_for_layout.haml
  16. +11 −0 fixtures/eval_suppressed.haml
  17. +55 −0 fixtures/helpers.haml
  18. +32 −0 fixtures/nuke_inner_whitespace.haml
  19. +144 −0 fixtures/nuke_outer_whitespace.haml
  20. +17 −0 fixtures/original_engine.haml
  21. +7 −0 fixtures/partial_layout.haml
  22. +1 −0  fixtures/partialize.haml
  23. +12 −0 fixtures/partials.haml
  24. +2 −0  fixtures/render_layout.haml
  25. +40 −0 fixtures/silent_script.haml
  26. +43 −0 fixtures/standard.haml
  27. +21 −0 fixtures/tag_parsing.haml
  28. +4 −0 fixtures/very_basic.haml
  29. +6 −0 helper.rb
  30. +23 −0 html2slim.gemspec
  31. +8 −0 lib/html2slim.rb
  32. +89 −0 lib/html2slim/command.rb
  33. +12 −0 lib/html2slim/converter.rb
  34. +74 −0 lib/html2slim/hpricot_monkeypatches.rb
  35. +3 −0  lib/html2slim/version.rb
  36. BIN  pkg/html2slim-0.0.1.gem
  37. BIN  test/fixtures/.DS_Store
  38. +85 −0 test/fixtures/slim-lang.html
  39. +148 −0 test/fixtures/slim-lang.slim
  40. +6 −0 test/helper.rb
  41. +86 −0 test/test_html2slim.rb
View
1  .rvmrc
@@ -0,0 +1 @@
+rvm use 1.9.2@html2slim --force
View
6 Gemfile
@@ -0,0 +1,6 @@
+source 'https://rubygems.org'
+
+gemspec
+gem 'minitest'
+gem 'slim'
+# gem 'tmpdir'
View
18 Gemfile.lock
@@ -0,0 +1,18 @@
+PATH
+ remote: .
+ specs:
+ html2slim (0.0.1)
+ hpricot
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ hpricot (0.8.6)
+ minitest (4.7.1)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ html2slim!
+ minitest
View
31 README.md
@@ -1,14 +1,32 @@
-## HTML2SLIM
+## HTML2Slim
-Pre-alpha script for converting HTML (or sHTML) files to slim (slim-lang.org).
+Script for converting HTML (or sHTML) files to slim (slim-lang.org).
It's not perfect, but certainly it helps a lot!
It's based on Hpricot. Yeah, I'm old school.
-## USAGE
+## Usage
- ./html2slim file.html file.slim
+You may convert files using the included executable `html2slim`.
+
+ # html2slim -h
+
+ Usage: html2slim INPUT_FILENAME_OR_DIRECTORY [OUTPUT_FILENAME_OR_DIRECTORY] [options]
+ --trace Show a full traceback on error
+ -d, --delete Delete HTML files
+ -h, --help Show this message
+ -v, --version Print version
+
+Alternatively, to convert files or strings on the fly in your application, you may do so by calling `HTML2Slim.convert!`.
+
+## License
+
+This project is released under the MIT license.
+
+## Author
+
+[Maiz Lulkin] (https://github.com/joaomilho)
## OFFICIAL REPO
@@ -16,5 +34,6 @@ https://github.com/slim-template/html2slim
## ROADMAP
-1. Add a decent interface!
-2. ERB support, I guess
+1. Improve minitests
+2. ERB support, I guess...
+3. Merge with other *2slim gems. Would be handy.
View
15 Rakefile
@@ -0,0 +1,15 @@
+begin
+ require 'bundler'
+ Bundler::GemHelper.install_tasks
+rescue Exception => e
+end
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib' << 'test'
+ t.pattern = 'test/**/test_*.rb'
+ t.verbose = true
+end
+
+task :default => 'test'
View
7 bin/html2slim
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby
+
+$:.unshift File.dirname(__FILE__) + '/../lib'
+require 'html2slim/command'
+
+cmd = HTML2Slim::Command.new(ARGV)
+cmd.run
View
9 fixtures/_av_partial_1.haml
@@ -0,0 +1,9 @@
+%h2 This is a pretty complicated partial
+.partial
+ %p It has several nested partials,
+ %ul
+ - 5.times do
+ %li
+ %strong Partial:
+ - @nesting = 5
+ = render :partial => 'haml/templates/av_partial_2'
View
9 fixtures/_av_partial_1_ugly.haml
@@ -0,0 +1,9 @@
+%h2 This is a pretty complicated partial
+.partial
+ %p It has several nested partials,
+ %ul
+ - 5.times do
+ %li
+ %strong Partial:
+ - @nesting = 5
+ = render :partial => 'haml/templates/av_partial_2_ugly'
View
5 fixtures/_av_partial_2.haml
@@ -0,0 +1,5 @@
+- @nesting -= 1
+.partial{:level => @nesting}
+ %h3 This is a crazy deep-nested partial.
+ %p== Nesting level #{@nesting}
+ = render :partial => 'haml/templates/av_partial_2' if @nesting > 0
View
5 fixtures/_av_partial_2_ugly.haml
@@ -0,0 +1,5 @@
+- @nesting -= 1
+.partial{:level => @nesting}
+ %h3 This is a crazy deep-nested partial.
+ %p== Nesting level #{@nesting}
+ = render :partial => 'haml/templates/av_partial_2_ugly' if @nesting > 0
View
3  fixtures/_layout_for_partial.haml
@@ -0,0 +1,3 @@
+.partial-layout
+ %h2 This is inside a partial layout
+ = yield
View
8 fixtures/_partial.haml
@@ -0,0 +1,8 @@
+%p
+ @foo =
+ = @foo
+- @foo = 'value three'
+== Toplevel? #{haml_buffer.toplevel?}
+%p
+ @foo =
+ = @foo
View
3  fixtures/_text_area.haml
@@ -0,0 +1,3 @@
+.text_area_test_area
+ ~ "<textarea>" + value + "</textarea>"
+= "<textarea>BLAH\n</textarea>"
View
8 fixtures/breakage.haml
@@ -0,0 +1,8 @@
+%p
+ %h1 Hello!
+ = "lots of lines"
+ - raise "Oh no!"
+ %p
+ this is after the exception
+ %strong yes it is!
+ho ho ho.
View
8 fixtures/content_for_layout.haml
@@ -0,0 +1,8 @@
+!!!
+%html
+ %head
+ %body
+ #yieldy
+ = yield :layout
+ #nosym
+ = yield
View
11 fixtures/eval_suppressed.haml
@@ -0,0 +1,11 @@
+= "not me!"
+= "nor me!"
+- haml_concat "not even me!"
+%p= "NO!"
+%p~ "UH-UH!"
+%h1 Me!
+#foo
+ %p#bar All
+ %br/
+ %p.baz This
+ Should render
View
55 fixtures/helpers.haml
@@ -0,0 +1,55 @@
+= h("&&&&&&&&&&&") # This is an ActionView Helper... should load
+- foo = capture do # This ActionView Helper is designed for ERB, but should work with haml
+ %div
+ %p.title Title
+ %p.text
+ Woah this is really crazy
+ I mean wow,
+ man.
+- 3.times do
+ = foo
+%p foo
+- tab_up
+%p reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
+- tab_down
+.woah
+ #funky
+ = capture_haml do
+ %div
+ %h1 Big!
+ %p Small
+ / Invisible
+ = capture do
+ .dilly
+ %p foo
+ %h1 bar
+ = surround '(', ')' do
+ %strong parentheses!
+= precede '*' do
+ %span.small Not really
+click
+= succeed '.' do
+ %a{:href=>"thing"} here
+%p baz
+- haml_buffer.tabulation = 10
+%p boom
+- concat "foo\n"
+- haml_buffer.tabulation = 0
+= list_of({:google => 'http://www.google.com'}) do |name, link|
+ %a{ :href => link }= name
+%p
+ - haml_concat "foo"
+ %div
+ - haml_concat "bar"
+ - haml_concat "boom"
+ baz
+ - haml_concat "boom, again"
+- haml_tag :table do
+ - haml_tag :tr do
+ - haml_tag :td, {:class => 'cell'} do
+ - haml_tag :strong, "strong!"
+ - haml_concat "data"
+ - haml_tag :td do
+ - haml_concat "more_data"
+- haml_tag :hr
+- haml_tag :div, ''
View
32 fixtures/nuke_inner_whitespace.haml
@@ -0,0 +1,32 @@
+%p
+ %q< Foo
+%p
+ %q{:a => 1 + 1}< Foo
+%p
+ %q<= "Foo\nBar"
+%p
+ %q{:a => 1 + 1}<= "Foo\nBar"
+%p
+ %q<
+ Foo
+ Bar
+%p
+ %q{:a => 1 + 1}<
+ Foo
+ Bar
+%p
+ %q<
+ %div
+ Foo
+ Bar
+%p
+ %q{:a => 1 + 1}<
+ %div
+ Foo
+ Bar
+
+-# Regression test
+%p
+ %q<= "foo"
+ %q{:a => 1 + 1}
+ bar
View
144 fixtures/nuke_outer_whitespace.haml
@@ -0,0 +1,144 @@
+%p
+ %p
+ %q>
+ Foo
+%p
+ %p
+ %q{:a => 1 + 1}>
+ Foo
+%p
+ %p
+ %q> Foo
+%p
+ %p
+ %q{:a => 1 + 1}> Foo
+%p
+ %p
+ %q>
+ = "Foo"
+%p
+ %p
+ %q{:a => 1 + 1}>
+ = "Foo"
+%p
+ %p
+ %q>= "Foo"
+%p
+ %p
+ %q{:a => 1 + 1}>= "Foo"
+%p
+ %p
+ %q>
+ = "Foo\nBar"
+%p
+ %p
+ %q{:a => 1 + 1}>
+ = "Foo\nBar"
+%p
+ %p
+ %q>= "Foo\nBar"
+%p
+ %p
+ %q{:a => 1 + 1}>= "Foo\nBar"
+%p
+ %p
+ - tab_up
+ foo
+ %q>
+ Foo
+ bar
+ - tab_down
+%p
+ %p
+ - tab_up
+ foo
+ %q{:a => 1 + 1}>
+ Foo
+ bar
+ - tab_down
+%p
+ %p
+ - tab_up
+ foo
+ %q> Foo
+ bar
+ - tab_down
+%p
+ %p
+ - tab_up
+ foo
+ %q{:a => 1 + 1}> Foo
+ bar
+ - tab_down
+%p
+ %p
+ - tab_up
+ foo
+ %q>
+ = "Foo"
+ bar
+ - tab_down
+%p
+ %p
+ - tab_up
+ foo
+ %q{:a => 1 + 1}>
+ = "Foo"
+ bar
+ - tab_down
+%p
+ %p
+ - tab_up
+ foo
+ %q>= "Foo"
+ bar
+ - tab_down
+%p
+ %p
+ - tab_up
+ foo
+ %q{:a => 1 + 1}>= "Foo"
+ bar
+ - tab_down
+%p
+ %p
+ - tab_up
+ foo
+ %q>
+ = "Foo\nBar"
+ bar
+ - tab_down
+%p
+ %p
+ - tab_up
+ foo
+ %q{:a => 1 + 1}>
+ = "Foo\nBar"
+ bar
+ - tab_down
+%p
+ %p
+ - tab_up
+ foo
+ %q>= "Foo\nBar"
+ bar
+ - tab_down
+%p
+ %p
+ - tab_up
+ foo
+ %q{:a => 1 + 1}>= "Foo\nBar"
+ bar
+ - tab_down
+%p
+ %p
+ %q>
+%p
+ %p
+ %q>/
+%p
+ %p
+ %q{:a => 1 + 1}>
+%p
+ %p
+ %q{:a => 1 + 1}>/
View
17 fixtures/original_engine.haml
@@ -0,0 +1,17 @@
+!!!
+%html
+ %head
+ %title Stop. haml time
+ #content
+ %h1 This is a title!
+ %p Lorem ipsum dolor sit amet, consectetur adipisicing elit
+ %p{:class => 'foo'} Cigarettes!
+ %h2 Man alive!
+ %ul.things
+ %li Slippers
+ %li Shoes
+ %li Bathrobe
+ %li Coffee
+ %pre
+ This is some text that's in a pre block!
+ Let's see what happens when it's rendered! What about now, since we're on a new line?
View
7 fixtures/partial_layout.haml
@@ -0,0 +1,7 @@
+%h1 Partial layout used with for block:
+- if Haml::Util.ap_geq_3?
+ = render :layout => 'layout_for_partial.haml' do
+ %p Some content within a layout
+- else
+ - render :layout => 'layout_for_partial.haml' do
+ %p Some content within a layout
View
1  fixtures/partialize.haml
@@ -0,0 +1 @@
+= render :file => "#{name}.haml"
View
12 fixtures/partials.haml
@@ -0,0 +1,12 @@
+- @foo = 'value one'
+%p
+ @foo =
+ = @foo
+- @foo = 'value two'
+%p
+ @foo =
+ = @foo
+= test_partial "partial"
+%p
+ @foo =
+ = @foo
View
2  fixtures/render_layout.haml
@@ -0,0 +1,2 @@
+= render :layout => 'layout' do
+ During
View
40 fixtures/silent_script.haml
@@ -0,0 +1,40 @@
+%div
+ %h1 I can count!
+ - (1..20).each do |i|
+ = i
+ %h1 I know my ABCs!
+ %ul
+ - ('a'..'z').each do |i|
+ %li= i
+ %h1 I can catch errors!
+ - begin
+ - String.silly
+ - rescue NameError => e
+ = "Oh no! \"#{e}\" happened!"
+ %p
+ "false" is:
+ - if false
+ = "true"
+ - else
+ = "false"
+ - if true
+ - 5.times do |i|
+ - if i % 2 == 1
+ Odd!
+ - else
+ Even!
+ - else
+ = "This can't happen!"
+- 13 |
+.foo
+ %strong foobar
+- 5.times |
+ do |
+ |a| |
+ %strong= a
+.test
+ - "foo |
+ bar |
+ baz" |
+
+ %p boom
View
43 fixtures/standard.haml
@@ -0,0 +1,43 @@
+!!!
+%html{:xmlns => "http://www.w3.org/1999/xhtml", "xml:lang" => "en-US", "lang" => "en-US"}
+ %head
+ %title Hampton Catlin Is Totally Awesome
+ %meta{"http-equiv" => "Content-Type", :content => "text/html; charset=utf-8"}
+ %body
+ / You're In my house now!
+ .header
+ Yes, ladies and gentileman. He is just that egotistical.
+ Fantastic! This should be multi-line output
+ The question is if this would translate! Ahah!
+ = 1 + 9 + 8 + 2 #numbers should work and this should be ignored
+ #body= " Quotes should be loved! Just like people!"
+ - 120.times do |number|
+ = number
+ Wow.|
+ %p{:code => 1 + 2}
+ = "Holy cow " + |
+ "multiline " + |
+ "tags! " + |
+ "A pipe (|) even!" |
+ = [1, 2, 3].collect { |n| "PipesIgnored|" }.join
+ = [1, 2, 3].collect { |n| |
+ n.to_s |
+ }.join("|") |
+ - bar = 17
+ %div.silent{:foo => bar}
+ - foo = String.new
+ - foo << "this"
+ - foo << " shouldn't"
+ - foo << " evaluate"
+ = foo + " but now it should!"
+ -# Woah crap a comment!
+
+ -# That was a line that shouldn't close everything.
+ %ul.really.cool
+ - ('a'..'f').each do |a|
+ %li= a
+ #combo.of_divs_with_underscore= @should_eval = "with this text"
+ = "foo".each_line do |line|
+ - nil
+ .footer
+ %strong.shout= "This is a really long ruby quote. It should be loved and wrapped because its more than 50 characters. This value may change in the future and this test may look stupid. \nSo, I'm just making it *really* long. God, I hope this works"
View
21 fixtures/tag_parsing.haml
@@ -0,0 +1,21 @@
+%div.tags
+ %foo 1
+ %FOO 2
+ %fooBAR 3
+ %fooBar 4
+ %foo_bar 5
+ %foo-bar 6
+ %foo:bar 7
+ %foo.bar 8
+ %fooBAr_baz:boom_bar 9
+ %foo13 10
+ %foo2u 11
+%div.classes
+ %p.foo.bar#baz
+ .fooBar a
+ .foo-bar b
+ .foo_bar c
+ .FOOBAR d
+ .foo16 e
+ .123 f
+ .foo2u g
View
4 fixtures/very_basic.haml
@@ -0,0 +1,4 @@
+!!!
+%html
+ %head
+ %body
View
6 helper.rb
@@ -0,0 +1,6 @@
+require 'rubygems'
+require 'minitest/unit'
+require 'slim'
+require 'haml2slim'
+
+MiniTest::Unit.autorun
View
23 html2slim.gemspec
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path("../lib/html2slim/version", __FILE__)
+require "date"
+
+Gem::Specification.new do |s|
+ s.name = "html2slim"
+ s.version = HTML2Slim::VERSION
+ s.date = Date.today.to_s
+ s.authors = ["Maiz Lulkin"]
+ s.email = ["maiz@lulk.in"]
+ s.summary = %q{HTML to Slim converter.}
+ s.description = %q{Convert HTML to Slim templates. Because HTML sux and Slim rules. That's why.}
+ s.homepage = %q{https://github.com/slim-template/html2slim}
+ s.extra_rdoc_files = ["README.md"]
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.files = `git ls-files -- lib/* bin/* README.md`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+
+ s.add_dependency "hpricot"
+ s.add_development_dependency 'minitest'
+ s.add_development_dependency "slim", [">= 1.0.0"]
+end
View
8 lib/html2slim.rb
@@ -0,0 +1,8 @@
+require_relative 'html2slim/version'
+require_relative 'html2slim/converter'
+
+module HTML2Slim
+ def self.convert!(input)
+ Converter.new(input)
+ end
+end
View
89 lib/html2slim/command.rb
@@ -0,0 +1,89 @@
+require 'optparse'
+require 'html2slim'
+
+module HTML2Slim
+ class Command
+ def initialize(args)
+ @args = args
+ @options = {}
+ end
+
+ def run
+ @opts = OptionParser.new(&method(:set_opts))
+ @opts.parse!(@args)
+ process!
+ exit 0
+ rescue Exception => ex
+ raise ex if @options[:trace] || SystemExit === ex
+ $stderr.print "#{ex.class}: " if ex.class != RuntimeError
+ $stderr.puts ex.message
+ $stderr.puts ' Use --trace for backtrace.'
+ exit 1
+ end
+
+ protected
+
+ def set_opts(opts)
+ opts.banner = "Usage: html2slim INPUT_FILENAME_OR_DIRECTORY [OUTPUT_FILENAME_OR_DIRECTORY] [options]"
+
+ opts.on('--trace', :NONE, 'Show a full traceback on error') do
+ @options[:trace] = true
+ end
+
+ opts.on_tail('-h', '--help', 'Show this message') do
+ puts opts
+ exit
+ end
+
+ opts.on_tail('-v', '--version', 'Print version') do
+ puts "html2slim #{HTML2Slim::VERSION}"
+ exit
+ end
+
+ opts.on('-d', '--delete', 'Delete HTML files') do
+ @options[:delete] = true
+ end
+ end
+
+ def process!
+ args = @args.dup
+
+ @options[:input] = file = args.shift
+ @options[:output] = destination = args.shift
+
+ @options[:input] = file = "-" unless file
+
+ if File.directory?(@options[:input])
+ Dir["#{@options[:input]}/**/*.html"].each { |file| _process(file, destination) }
+ else
+ _process(file, destination)
+ end
+ end
+
+ private
+
+ def _process(file, destination = nil)
+ require 'fileutils'
+ slim_file = file.sub(/\.html$/, '.slim')
+
+ if File.directory?(@options[:input]) && destination
+ FileUtils.mkdir_p(File.dirname(slim_file).sub(@options[:input].chomp('/'), destination))
+ slim_file.sub!(@options[:input].chomp('/'), destination)
+ else
+ slim_file = destination || slim_file
+ end
+
+ in_file = if @options[:input] == "-"
+ $stdin
+ else
+ File.open(file, 'r')
+ end
+
+ @options[:output] = slim_file && slim_file != '-' ? File.open(slim_file, 'w') : $stdout
+ @options[:output].puts HTML2Slim.convert!(in_file)
+ @options[:output].close
+
+ File.delete(file) if @options[:delete]
+ end
+ end
+end
View
12 lib/html2slim/converter.rb
@@ -0,0 +1,12 @@
+require_relative 'hpricot_monkeypatches'
+# html = File.read(ARGV[0])
+module HTML2Slim
+ class Converter
+ def initialize(html)
+ @slim = Hpricot(html).to_slim
+ end
+ def to_s
+ @slim
+ end
+ end
+end
View
74 lib/html2slim/hpricot_monkeypatches.rb
@@ -0,0 +1,74 @@
+require 'hpricot'
+
+class Hpricot::BogusETag
+ def to_slim(lvl=0)
+ nil
+ end
+end
+
+class Hpricot::Text
+ def to_slim(lvl=0)
+ if to_s.strip.empty?
+ nil
+ else
+ (' ' * lvl) + %(| #{to_s.gsub(/\s+/, ' ')})
+ end
+ 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
+ end
+end
+
+class Hpricot::DocType
+ def to_slim(lvl=0)
+ 'doctype'
+ end
+end
+
+class Hpricot::Elem
+ def slim(lvl=0)
+ r = (' ' * lvl)
+ if self.name == 'div' and (self.has_attribute?('id') || self.has_attribute?('class'))
+ r += ''
+ else
+ r += self.name
+ end
+ if(self.has_attribute?('id'))
+ r += "##{self['id']}"
+ self.remove_attribute('id')
+ end
+ if(self.has_attribute?('class'))
+ r += ".#{self['class'].split(/\s+/).join('.')}"
+ self.remove_attribute('class')
+ end
+ unless attributes_as_html.to_s.strip.empty?
+ r += "[#{attributes_as_html.to_s.strip}]"
+ end
+ r
+ end
+ def to_slim(lvl=0)
+ if respond_to?(:children) and children
+ return %(#{self.slim(lvl)}\n#{children.map { |x| x.to_slim(lvl+1) }.select{|e| !e.nil? }.join("\n")})
+ else
+ self.slim(lvl)
+ end
+ end
+end
+
+class Hpricot::Doc
+ def to_slim
+ if respond_to?(:children) and children
+ children.map { |x| x.to_slim }.select{|e| !e.nil? }.join("\n")
+ else
+ ''
+ end
+ end
+end
View
3  lib/html2slim/version.rb
@@ -0,0 +1,3 @@
+module HTML2Slim
+ VERSION = '0.0.1'
+end
View
BIN  pkg/html2slim-0.0.1.gem
Binary file not shown
View
BIN  test/fixtures/.DS_Store
Binary file not shown
View
85 test/fixtures/slim-lang.html
@@ -0,0 +1,85 @@
+<!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>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
+ <meta name="description" content="Slim is a template language whose goal is reduce html syntax to the essential parts without becoming cryptic."/>
+ <link href="css/slim.css" media="screen" rel="stylesheet" type="text/css"/>
+ <title>Slim - A Fast, Lightweight Template Engine for Ruby</title>
+ </head>
+ <body>
+ <div id="side">
+ <div class="wrap">
+ <h1>Slim > Home</h1>
+ <h2 id="slogan">A lightweight templating engine</h2>
+ <ul id="links">
+ <li><a href="index.html" class="active">Home</a></li>
+ <li><a href="about.html">About</a></li>
+ <li><a href="http://rdoc.info/github/slim-template/slim">Docs</a></li>
+ <li><a href="http://rdoc.info/github/slim-template/slim#logicless">Logic less</a></li>
+ <li><a href="contribute.html">Contribute</a></li>
+ <li class="last"><a href="http://rdoc.info/github/slim-template/slim/master/frames">API</a></li>
+ </ul>
+ <p><a href="https://github.com/slim-template/slim" class='github'>slim-template<span>/</span><strong>slim</strong></a></p>
+ <p>
+ <a href="http://rubygems.org/gems/slim"><img src="https://badge.fury.io/rb/slim.png"/></a>
+
+ <a href="http://travis-ci.org/slim-template/slim" title="Build Status"><img src="https://secure.travis-ci.org/slim-template/slim.png?branch=master"/></a><br/>
+ <a href="https://gemnasium.com/slim-template/slim" title="Dependency Status"><img src="https://gemnasium.com/slim-template/slim.png?travis"/></a>
+ <a href="https://codeclimate.com/github/slim-template/slim" title="Code Climate"><img src="https://codeclimate.com/github/slim-template/slim.png"/></a>
+ </p>
+ </div>
+ </div>
+ <div id="main" class="home">
+ <div class="wrap">
+ <pre>
+ <code>
+<span class='tag'>doctype</span> html
+<span class='tag'>html</span>
+ <span class='tag'>head</span>
+ <span class='tag'>title</span> Slim Examples
+ <span class='tag'>meta</span> name=<span class='string'>"keywords"</span> content=<span class='string'>"template language"</span>
+ <span class='tag'>meta</span> name=<span class='string'>"author"</span> content=author
+ <span class='filter'>javascript:</span>
+ alert('Slim supports embedded javascript!')
+
+ <span class='tag'>body</span>
+ <span class='tag'>h1</span> Markup examples
+
+ <span class='tag'>#content</span>
+ <span class='tag'>p</span> This example shows you how a basic Slim file looks like.
+
+ == <span class='keyword'>yield</span>
+
+ - <span class='keyword'>unless</span> items.empty?
+ <span class='tag'>table</span>
+ - <span class='keyword'>for</span> item <span class='keyword'>in</span> items <span class='keyword'>do</span>
+ <span class='tag'>tr</span>
+ <span class='tag'>td.name</span> = item.name
+ <span class='tag'>td.price</span> = item.price
+ - <span class='keyword'>else</span>
+ <span class='tag'>p</span>
+ | No items found. Please add some inventory.
+ Thank you!
+
+ <span class='tag'>div</span> id=<span class='string'>"footer"</span>
+ = <span class='keyword'>render</span> <span class='string'>'footer'</span>
+ | Copyright &copy; #{year} #{author}
+ </pre>
+ </code>
+ </div>
+ <p class="foot up">
+ Source: <a href="http://github.com/slim-template/slim">slim-template/slim</a>&nbsp;|&nbsp;Slim maintained by <a href="http://github.com/minad">minad</a>&nbsp;|&nbsp;Logo &amp; design by <a href="http://github.com/activestylus">activestylus</a><br/>
+ </p>
+ </div>
+
+ <a id="forkme" href="http://github.com/slim-template/slim">Fork me on Github</a>
+ <script type="text/javascript">
+ var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
+ document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
+ try {
+ var pageTracker = _gat._getTracker("UA-19270476-1");
+ pageTracker._trackPageview();
+ } catch(err) {}
+ </script>
+ </body>
+</html>
View
148 test/fixtures/slim-lang.slim
@@ -0,0 +1,148 @@
+doctype
+html[xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"]
+ head
+ meta[http-equiv="Content-Type" content="text/html; charset=utf-8"]
+ meta[name="description" content="Slim is a template language whose goal is reduce html syntax to the essential parts without becoming cryptic."]
+ link[href="css/slim.css" media="screen" rel="stylesheet" type="text/css"]
+ title
+ | Slim - A Fast, Lightweight Template Engine for Ruby
+ body
+ #side
+ .wrap
+ h1
+ | Slim > Home
+ h2#slogan
+ | A lightweight templating engine
+ ul#links
+ li
+ a.active[href="index.html"]
+ | Home
+ li
+ a[href="about.html"]
+ | About
+ li
+ a[href="http://rdoc.info/github/slim-template/slim"]
+ | Docs
+ li
+ a[href="http://rdoc.info/github/slim-template/slim#logicless"]
+ | Logic less
+ li
+ a[href="contribute.html"]
+ | Contribute
+ li.last
+ a[href="http://rdoc.info/github/slim-template/slim/master/frames"]
+ | API
+ p
+ a.github[href="https://github.com/slim-template/slim"]
+ | slim-template
+ span
+ | /
+ strong
+ | slim
+ p
+ a[href="http://rubygems.org/gems/slim"]
+ img[src="https://badge.fury.io/rb/slim.png"]
+ a[href="http://travis-ci.org/slim-template/slim" title="Build Status"]
+ img[src="https://secure.travis-ci.org/slim-template/slim.png?branch=master"]
+ br
+ a[href="https://gemnasium.com/slim-template/slim" title="Dependency Status"]
+ img[src="https://gemnasium.com/slim-template/slim.png?travis"]
+ a[href="https://codeclimate.com/github/slim-template/slim" title="Code Climate"]
+ img[src="https://codeclimate.com/github/slim-template/slim.png"]
+ #main.home
+ .wrap
+ pre
+ code
+ span.tag
+ | doctype
+ | html
+ span.tag
+ | html
+ span.tag
+ | head
+ span.tag
+ | title
+ | Slim Examples
+ span.tag
+ | meta
+ | name=
+ span.string
+ | "keywords"
+ | content=
+ span.string
+ | "template language"
+ span.tag
+ | meta
+ | name=
+ span.string
+ | "author"
+ | content=author
+ span.filter
+ | javascript:
+ | alert('Slim supports embedded javascript!')
+ span.tag
+ | body
+ span.tag
+ | h1
+ | Markup examples
+ span.tag
+ | #content
+ span.tag
+ | p
+ | This example shows you how a basic Slim file looks like. ==
+ span.keyword
+ | yield
+ | -
+ span.keyword
+ | unless
+ | items.empty?
+ span.tag
+ | table
+ | -
+ span.keyword
+ | for
+ | item
+ span.keyword
+ | in
+ | items
+ span.keyword
+ | do
+ span.tag
+ | tr
+ span.tag
+ | td.name
+ | = item.name
+ span.tag
+ | td.price
+ | = item.price -
+ span.keyword
+ | else
+ span.tag
+ | p
+ | | No items found. Please add some inventory. Thank you!
+ span.tag
+ | div
+ | id=
+ span.string
+ | "footer"
+ | =
+ span.keyword
+ | render
+ span.string
+ | 'footer'
+ | | Copyright © #{year} #{author}
+ p.foot.up
+ | Source:
+ a[href="http://github.com/slim-template/slim"]
+ | slim-template/slim
+ |  | Slim maintained by
+ a[href="http://github.com/minad"]
+ | minad
+ |  | Logo & design by
+ a[href="http://github.com/activestylus"]
+ | activestylus
+ br
+ a#forkme[href="http://github.com/slim-template/slim"]
+ | Fork me on Github
+ script[type="text/javascript"]
+ | var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); try { var pageTracker = _gat._getTracker("UA-19270476-1"); pageTracker._trackPageview(); } catch(err) {}
View
6 test/helper.rb
@@ -0,0 +1,6 @@
+require 'rubygems'
+require 'minitest/autorun'
+require 'slim'
+require_relative '../lib/html2slim'
+
+MiniTest::Unit.autorun
View
86 test/test_html2slim.rb
@@ -0,0 +1,86 @@
+require_relative 'helper'
+require 'tmpdir'
+
+class TestHTML2Slim < MiniTest::Unit::TestCase
+ def setup
+ Slim::Engine.default_options[:id_delimiter] = '_'
+ create_html_file
+ end
+
+ def teardown
+ cleanup_tmp_files
+ end
+
+ Dir.glob("test/fixtures/*.html").each do |file|
+ define_method("test_template_#{File.basename(file, '.html')}") do
+ assert_valid?(file)
+ end
+ end
+
+ def test_convert_slim_lang_html
+ IO.popen("bin/html2slim test/fixtures/slim-lang.html -", "r") do |f|
+ assert_equal File.read("test/fixtures/slim-lang.slim"), f.read
+ end
+ end
+
+ def test_convert_file_to_stdout
+ File.open(html_file, "w") do |f|
+ f.puts "<p><h1>Hello</h1></p>"
+ end
+
+ IO.popen("bin/html2slim #{html_file} -", "r") do |f|
+ assert_equal "p\n h1\n | Hello\n", f.read
+ end
+ end
+
+ def test_convert_stdin_to_stdout
+ File.open(html_file, "w") do |f|
+ f.puts "<p><h1>Hello</h1></p>"
+ end
+
+ IO.popen("cat #{html_file} | bin/html2slim", "r") do |f|
+ assert_equal "p\n h1\n | Hello\n", f.read
+ end
+ end
+
+ def test_data_attributes
+ html = '<a href="test" data-param1="var" data-param2="(1 + 1)" data-param3="string"></a>'
+ slim = 'a[href="test" data-param1="var" data-param2="(1 + 1)" data-param3="string"]'
+ assert_html_to_slim html, slim
+ end
+
+
+ private
+
+ def assert_html_to_slim(actual_html, expected_slim)
+ File.open(html_file, "w") do |f|
+ f.puts actual_html
+ end
+
+ IO.popen("cat #{html_file} | bin/html2slim", "r") do |f|
+ assert_equal expected_slim, f.read.strip
+ end
+ end
+
+ def tmp_dir
+ @tmp_dir ||= Dir.mktmpdir("html2slim.")
+ end
+
+ def create_html_file
+ `touch #{html_file}`
+ end
+
+ def html_file
+ File.join(tmp_dir, "dummy.html")
+ end
+
+ def cleanup_tmp_files
+ FileUtils.rm_rf(tmp_dir)
+ end
+
+ def assert_valid?(source)
+ html = File.open(source)
+ slim = HTML2Slim.convert!(html)
+ assert_instance_of String, Slim::Engine.new.call(slim.to_s)
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.