theme_bandit 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +30 -0
  3. data/Gemfile +9 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +53 -0
  6. data/Rakefile +17 -0
  7. data/bin/bandit +93 -0
  8. data/lib/theme_bandit/document_writer.rb +65 -0
  9. data/lib/theme_bandit/downloader.rb +30 -0
  10. data/lib/theme_bandit/init.rb +16 -0
  11. data/lib/theme_bandit/parser/css.rb +26 -0
  12. data/lib/theme_bandit/parser/html.rb +65 -0
  13. data/lib/theme_bandit/parser/js.rb +26 -0
  14. data/lib/theme_bandit/rack_generator.rb +56 -0
  15. data/lib/theme_bandit/recipes/sinatra/erb/Gemfile +4 -0
  16. data/lib/theme_bandit/recipes/sinatra/erb/app/application.rb +10 -0
  17. data/lib/theme_bandit/recipes/sinatra/erb/app/views/.keep +0 -0
  18. data/lib/theme_bandit/recipes/sinatra/erb/app/views/templates/.keep +0 -0
  19. data/lib/theme_bandit/recipes/sinatra/erb/config.ru +25 -0
  20. data/lib/theme_bandit/recipes/sinatra/haml/Gemfile +5 -0
  21. data/lib/theme_bandit/recipes/sinatra/haml/app/application.rb +10 -0
  22. data/lib/theme_bandit/recipes/sinatra/haml/config.ru +25 -0
  23. data/lib/theme_bandit/recipes/sinatra/slim/Gemfile +5 -0
  24. data/lib/theme_bandit/recipes/sinatra/slim/app/application.rb +10 -0
  25. data/lib/theme_bandit/recipes/sinatra/slim/config.ru +25 -0
  26. data/lib/theme_bandit/url_formatter.rb +7 -0
  27. data/lib/theme_bandit/util/configure.rb +50 -0
  28. data/lib/theme_bandit/util/version.rb +3 -0
  29. data/lib/theme_bandit.rb +4 -0
  30. data/test/fixtures/css/style.css +0 -0
  31. data/test/fixtures/index.html +83 -0
  32. data/test/fixtures/js/script.js +0 -0
  33. data/test/fixtures/js/script_2.js +0 -0
  34. data/test/helper.rb +39 -0
  35. data/test/test_configure.rb +14 -0
  36. data/test/test_document_writer.rb +64 -0
  37. data/test/test_downloader.rb +39 -0
  38. data/test/test_rack_generator.rb +52 -0
  39. data/test/test_url_formatter.rb +12 -0
  40. data/theme_bandit.gemspec +29 -0
  41. metadata +206 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MmM3OTY5MzM4ZWExYjI2NDZmZTU2M2U5ZTNmOWUwOWMzYTM1ZDIxMw==
5
+ data.tar.gz: !binary |-
6
+ YmM5MTg1N2NjMzc4ZTFmMjU5YjNhY2RhMjc0MDEwYWMyYzU3YTc3YQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YjY4ZDM3NTViN2YxYjhhNTg0MGYxMDdkYjc2NWQ1ZmQ3MzkwYjM1MWY4ZTE2
10
+ M2IyZjZlZjlmNjYzNjRiN2EzNmFhMDA2YjE2YjU5Njg1ODg4ZWZlOTdmM2I1
11
+ ZjQ4MWY4MzdkMjM4MzA2MGJiZTZkNTU3NTY0ZjFiZjBlMjhmZmQ=
12
+ data.tar.gz: !binary |-
13
+ MWZlYTIyOWYwMzQzOTFjNjhhYmU4MWU2MzcyNWIwMTAzZDNjZjEyNTE4N2Yx
14
+ MGI4MzNkZTJlOTQ3YWE3NThmY2I0M2QzZTEwNDcyMTg1NjRhNjE3ZmZjYmU0
15
+ NGI4ODRhOWEyM2M5YWE3NGQxYjA0MTlkN2VlYTI0ZTlhMWE2OTY=
data/.gitignore ADDED
@@ -0,0 +1,30 @@
1
+ theme
2
+
3
+ *.gem
4
+ *.rbc
5
+ *.so
6
+ *.o
7
+ *.a
8
+ .bundle
9
+ .config
10
+ .yardoc
11
+ .rvmrc
12
+ .ruby-gemset
13
+ .ruby-version
14
+ vendor/ruby
15
+ doc
16
+ tmp
17
+ pkg
18
+ rdoc
19
+ Gemfile.lock
20
+ InstalledFiles
21
+ _yardoc
22
+ lib/bundler/man
23
+ spec/reports
24
+ test/tmp
25
+ test/version_tmp
26
+ mkmf.log
27
+ .bundle
28
+ coverage
29
+ .swp
30
+ .dump
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ ruby '1.9.3'
2
+ source 'https://rubygems.org'
3
+
4
+ # Specify your gem's dependencies in theme_bandit.gemspec
5
+ gemspec
6
+
7
+ group :development do
8
+ gem 'pry-debugger'
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Luke Fender
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # ThemeBandit
2
+
3
+ Enjoy rapid prototyping with theme bandit :heart:
4
+
5
+ Convert any site template (Wordpress, Joomla, HTML) into a small and
6
+ simple sinatra rack application.
7
+
8
+ `bandit`
9
+ - Select a url to download
10
+ - Select a templating engine (erb, haml, slim)
11
+ - Start your rack app!
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'theme_bandit'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install theme_bandit
26
+
27
+ ## Usage
28
+ `./bin/bandit/` -> Builds a rack application in the `theme` directory of
29
+ your project root.
30
+
31
+ ## Caveats
32
+ Not all templating engines play nicely with html. If you run into
33
+ templating issues, `erb` will be your safest bet because it renders as
34
+ pure html.
35
+
36
+ ## TODO
37
+ - Support for binaries (images + fonts + embeds)
38
+ - Support for multiple pages
39
+ - HTML Sanitization
40
+ - HTML Formatting
41
+
42
+ ## Tests
43
+
44
+ ```
45
+ rake test
46
+ ```
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ begin
4
+ require 'bundler'
5
+ Bundler::GemHelper.install_tasks
6
+ rescue => e
7
+ end
8
+
9
+ require 'rake/testtask'
10
+
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << 'lib' << 'test'
13
+ t.pattern = 'test/**/test_*.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ task default: 'test'
data/bin/bandit ADDED
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
+
5
+ require 'fileutils'
6
+ require 'theme_bandit'
7
+ require 'uri'
8
+ require 'bundler'
9
+
10
+ SUPPORTED_ENGINES = /^(erb|haml|slim)$/
11
+
12
+ def get_root
13
+ begin
14
+ Gem::Specification.find_by_name('theme_bandit').gem_dir
15
+ rescue
16
+ Dir.pwd
17
+ end
18
+ end
19
+
20
+ def init
21
+ ThemeBandit.configure do |config|
22
+ config.url = ask_user_for_domain
23
+ config.template_engine = ask_user_for_language
24
+ config.gem_root = get_root
25
+ end
26
+ start_fresh
27
+ document = ThemeBandit::Downloader.get_theme
28
+ generate_app(document)
29
+ ask_user_to_start_rack_app
30
+ end
31
+
32
+ def start_fresh
33
+ `rm -rf theme`
34
+ make_a_directory
35
+ end
36
+
37
+ def generate_app(document)
38
+ ThemeBandit::DocumentWriter.new(document).write
39
+ ThemeBandit::RackGenerator.build
40
+ end
41
+
42
+ def make_a_directory
43
+ FileUtils.mkdir_p 'theme'
44
+ end
45
+
46
+ def ask_user_for_domain
47
+ puts 'Enter the URL of the theme you wish to download (example: http://www.google.com)'
48
+ url = gets.chomp
49
+ if url == ''
50
+ raise 'Invalid url'
51
+ else
52
+ url
53
+ end
54
+ end
55
+
56
+ def ask_user_for_language
57
+ puts 'Select your favorite template engine (erb, haml, slim)'
58
+ answer = gets.chomp
59
+ fail "You must select a supported template engine (erb, haml, or slim)" unless match = answer.match(SUPPORTED_ENGINES)
60
+ match[0]
61
+ end
62
+
63
+ def ask_user_to_start_rack_app
64
+ puts 'Do you want to start your new rack app? y/n'
65
+ answer = gets.chomp
66
+ if answer == 'y'
67
+ app_message
68
+ puts 'changing directory to ./theme'
69
+ Dir.chdir 'theme' do
70
+ Bundler.with_clean_env do
71
+ puts "running bundle in #{Dir.pwd}/"
72
+ `bundle install`
73
+ puts 'running `bundle exec rackup -p 3000`'
74
+ system('bundle exec rackup -p 3000')
75
+ end
76
+ end
77
+ else
78
+ app_message
79
+ puts "Don't forget to bundle before starting!"
80
+ end
81
+ end
82
+
83
+ def app_message
84
+ puts "Your rack app can be found at #{Dir.pwd}/theme/"
85
+ end
86
+
87
+ begin
88
+ require 'theme_bandit'
89
+ init
90
+ rescue LoadError => e
91
+ require 'rubygems'
92
+ require 'theme_bandit'
93
+ end
@@ -0,0 +1,65 @@
1
+ module ThemeBandit
2
+ class DocumentWriter
3
+ include ThemeBandit::CSSParser
4
+ include ThemeBandit::JSParser
5
+ include ThemeBandit::HTMLParser
6
+ # TODO: image parser
7
+ # TODO: font parser
8
+
9
+ attr_accessor :document
10
+ attr_reader :url
11
+
12
+ CSS_FOLDER = './theme/public/css/'
13
+ JS_FOLDER = './theme/public/js/'
14
+ HTML_FOLDER = './theme/public/'
15
+
16
+ def initialize(doc, url = ThemeBandit.configuration.url)
17
+ @document, @url = Nokogiri::HTML(doc), URI.parse(url)
18
+ end
19
+
20
+ def write
21
+ write_html_revision
22
+ end
23
+
24
+ def html
25
+ document.to_html
26
+ end
27
+
28
+ def write_html_revision
29
+ download_css(get_css_files)
30
+ download_js(get_js_files)
31
+ revise_head_tags
32
+ write_html_file
33
+ end
34
+
35
+ # def build_rack_app
36
+ # ThemeBandit::RackGenerator.build
37
+ # end
38
+
39
+ def make_dir(folder)
40
+ FileUtils.mkdir_p folder
41
+ end
42
+
43
+ def download_css(files)
44
+ make_dir(CSS_FOLDER)
45
+ files.each_with_index do |file_name, _count|
46
+ doc = Downloader.fetch(file_name, {})
47
+ new_file = file_name.split('/').last
48
+ File.open("#{CSS_FOLDER}#{new_file}", 'w') { |file| file.write(doc.body) }
49
+ end
50
+ end
51
+
52
+ def download_js(files)
53
+ make_dir(JS_FOLDER)
54
+ files.each_with_index do |file_name, order|
55
+ doc = Downloader.fetch(file_name, {})
56
+ new_file = file_name.split('/').last
57
+ File.open("#{JS_FOLDER}#{order}_#{new_file}", 'w') { |file| file.write(doc.body) }
58
+ end
59
+ end
60
+
61
+ def write_html_file
62
+ File.open("#{HTML_FOLDER}index.html", 'w') { |file| file.write(html) }
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,30 @@
1
+ require 'httparty'
2
+
3
+ module ThemeBandit
4
+ class Downloader
5
+ include HTTParty
6
+
7
+ def self.get_theme(url = ThemeBandit.configuration.url)
8
+ url ? fetch(url) : error
9
+ end
10
+
11
+ def self.fetch(url, options = {})
12
+ new(url, options).document
13
+ end
14
+
15
+ def self.error
16
+ fail 'Invalid configuration, please configure through ./bin wrapper'
17
+ end
18
+
19
+ attr_reader :url, :options, :document
20
+
21
+ def initialize(url, options = {})
22
+ @url, @options = url, options
23
+ @document = get_document(url)
24
+ end
25
+
26
+ def get_document(url)
27
+ self.class.get(url, options)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ require 'uri'
2
+ require 'nokogiri'
3
+
4
+ require_relative 'downloader'
5
+ require_relative 'url_formatter'
6
+
7
+ require_relative 'parser/css'
8
+ require_relative 'parser/js'
9
+ require_relative 'parser/html'
10
+
11
+ require_relative 'document_writer'
12
+
13
+ require_relative 'rack_generator'
14
+
15
+ require_relative 'util/configure'
16
+ require_relative 'util/version'
@@ -0,0 +1,26 @@
1
+ module ThemeBandit
2
+ module CSSParser
3
+ include ThemeBandit::URLFormatter
4
+
5
+ def get_css_files
6
+ link_tag_values
7
+ end
8
+
9
+ def link_tags
10
+ document.css('link').select do |tag|
11
+ tag.attribute('rel').value == 'stylesheet'
12
+ end
13
+ end
14
+
15
+ def link_tag_values
16
+ link_tags.map do |tag|
17
+ href = URI.parse(tag.attribute('href'))
18
+ if href.absolute?
19
+ strip_query_string href.to_s
20
+ else
21
+ strip_query_string "#{url.scheme}://#{url.host}#{href}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,65 @@
1
+ module ThemeBandit
2
+ module HTMLParser
3
+ def revise_head_tags
4
+ remove_link_tags
5
+ remove_script_tags
6
+ inject_link_nodes
7
+ inject_script_nodes
8
+ end
9
+
10
+ def remove_link_tags
11
+ document.search('link').remove
12
+ end
13
+
14
+ def remove_script_tags
15
+ document.search('script').remove
16
+ end
17
+
18
+ def local_link_names
19
+ path = "#{Dir.pwd}/theme/public/css/"
20
+ Dir.entries(path).map do |file_name|
21
+ "#{path}#{file_name}" if file_name['css']
22
+ end.compact
23
+ end
24
+
25
+ def local_script_names
26
+ path = "#{Dir.pwd}/theme/public/js/"
27
+ Dir.entries(path).map do |file_name|
28
+ "#{path}#{file_name}" if file_name['js']
29
+ end.compact.sort
30
+ end
31
+
32
+ def link_nodes
33
+ [].tap do |arr|
34
+ local_link_names.each do |link_path|
35
+ link = Nokogiri::XML::Node.new 'link', document
36
+ link.set_attribute('rel', 'stylesheet')
37
+ link.set_attribute('href', "#{link_path}")
38
+ arr << link
39
+ end
40
+ end
41
+ end
42
+
43
+ def script_nodes
44
+ [].tap do |arr|
45
+ local_script_names.each do |script_path|
46
+ script = Nokogiri::XML::Node.new 'script', document
47
+ script.set_attribute('src', "#{script_path}")
48
+ arr << script
49
+ end
50
+ end
51
+ end
52
+
53
+ def inject_link_nodes
54
+ link_nodes.each do |node|
55
+ document.search('head').first.add_child(node)
56
+ end
57
+ end
58
+
59
+ def inject_script_nodes
60
+ script_nodes.each do |node|
61
+ document.search('head').first.add_child(node)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,26 @@
1
+ module ThemeBandit
2
+ module JSParser
3
+ include ThemeBandit::URLFormatter
4
+
5
+ def get_js_files
6
+ script_tag_values
7
+ end
8
+
9
+ def script_tags
10
+ document.css('script').select do |tag|
11
+ tag.attribute('src')
12
+ end
13
+ end
14
+
15
+ def script_tag_values
16
+ script_tags.map do |tag|
17
+ src = URI.parse(tag.attribute('src'))
18
+ if src.absolute?
19
+ strip_query_string src.to_s
20
+ else
21
+ strip_query_string "#{url.scheme}://#{url.host}#{src}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,56 @@
1
+ require 'html2slim/command'
2
+
3
+ require 'html2haml'
4
+
5
+ module ThemeBandit
6
+ class RackGenerator
7
+ def self.build
8
+ new
9
+ end
10
+
11
+ THEME_DIR = "#{Dir.pwd}/theme/"
12
+ TEMPLATE_ERROR = 'Templating engine not supported'
13
+
14
+ def initialize
15
+ copy_template_to_dir(THEME_DIR)
16
+ generate_view
17
+ end
18
+
19
+ def get_recipe(root = ThemeBandit.configuration.gem_root)
20
+ src = "#{root}/lib/theme_bandit/recipes/sinatra/#{ThemeBandit.configuration.template_engine}/"
21
+ Dir.glob("#{src}/**/*")
22
+ end
23
+
24
+ # NOTE: to copy the innards of a dir, use a /., with a dot at the end
25
+ # example - recipes/sinatra/.
26
+ def copy_template_to_dir(destination)
27
+ t = get_recipe
28
+ FileUtils.cp_r t, destination
29
+ end
30
+
31
+ def index_file_contents
32
+ index_html = File.open("#{Dir.pwd}/theme/public/index.html", 'r')
33
+ absolute_to_relative(File.read(index_html))
34
+ end
35
+
36
+ # Convert all links in html document to relative for ruby app
37
+ def absolute_to_relative(contents)
38
+ contents.gsub("#{Dir.pwd}/theme/public", '')
39
+ end
40
+
41
+ def generate_view(root = Dir.pwd)
42
+ case ThemeBandit.configuration.template_engine
43
+ when 'slim'
44
+ slim_contents = HTML2Slim.convert!(index_file_contents, :html)
45
+ File.open("#{root}/theme/app/views/templates/index.slim", 'w') { |file| file.write(slim_contents) }
46
+ when ('erb' || 'html')
47
+ File.open("#{root}/theme/app/views/templates/index.erb", 'w') { |file| file.write(index_file_contents) }
48
+ when 'haml'
49
+ haml_contents = Haml::HTML.new(index_file_contents, erb: nil).render
50
+ File.open("#{root}/theme/app/views/templates/index.haml", 'w') { |file| file.write(haml_contents) }
51
+ else
52
+ fail TEMPLATE_ERROR
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rack'
4
+ gem 'sinatra', '~> 1.4.2'
@@ -0,0 +1,10 @@
1
+ require 'erb'
2
+
3
+ class Application < Sinatra::Application
4
+ set :views, proc { File.join(ROOT, 'app/views/') }
5
+ set :public_folder, proc { File.join(ROOT, 'public/') }
6
+
7
+ get '/' do
8
+ erb :'templates/index'
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+
3
+ def env
4
+ (ENV['RACK_ENV'] || 'development').to_sym
5
+ end
6
+
7
+ gemfile = File.expand_path('../../Gemfile', __FILE__)
8
+
9
+ ROOT = Dir.pwd
10
+
11
+ begin
12
+ ENV['BUNDLE_GEMFILE'] = gemfile
13
+ require 'bundler'
14
+ Bundler.setup
15
+ rescue Bundler::GemNotFound => e
16
+ STDERR.puts e.message
17
+ STDERR.puts 'Try running `bundle install`.'
18
+ exit!
19
+ end if File.exist?(gemfile)
20
+
21
+ Bundler.require(:default, env)
22
+
23
+ require File.join(File.dirname(__FILE__),'app','application.rb')
24
+
25
+ run Application
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rack'
4
+ gem 'haml'
5
+ gem 'sinatra', '~> 1.4.2'
@@ -0,0 +1,10 @@
1
+ require 'haml'
2
+
3
+ class Application < Sinatra::Application
4
+ set :views, proc { File.join(ROOT, 'app/views/') }
5
+ set :public_folder, proc { File.join(ROOT, 'public/') }
6
+
7
+ get '/' do
8
+ haml :'templates/index'
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+
3
+ def env
4
+ (ENV['RACK_ENV'] || 'development').to_sym
5
+ end
6
+
7
+ gemfile = File.expand_path('../../Gemfile', __FILE__)
8
+
9
+ ROOT = Dir.pwd
10
+
11
+ begin
12
+ ENV['BUNDLE_GEMFILE'] = gemfile
13
+ require 'bundler'
14
+ Bundler.setup
15
+ rescue Bundler::GemNotFound => e
16
+ STDERR.puts e.message
17
+ STDERR.puts 'Try running `bundle install`.'
18
+ exit!
19
+ end if File.exist?(gemfile)
20
+
21
+ Bundler.require(:default, env)
22
+
23
+ require File.join(File.dirname(__FILE__),'app','application.rb')
24
+
25
+ run Application
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rack'
4
+ gem 'slim'
5
+ gem 'sinatra', '~> 1.4.2'
@@ -0,0 +1,10 @@
1
+ require 'slim'
2
+
3
+ class Application < Sinatra::Application
4
+ set :views, proc { File.join(ROOT, 'app/views/') }
5
+ set :public_folder, proc { File.join(ROOT, 'public/') }
6
+
7
+ get '/' do
8
+ slim :'templates/index'
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+
3
+ def env
4
+ (ENV['RACK_ENV'] || 'development').to_sym
5
+ end
6
+
7
+ gemfile = File.expand_path('../../Gemfile', __FILE__)
8
+
9
+ ROOT = Dir.pwd
10
+
11
+ begin
12
+ ENV['BUNDLE_GEMFILE'] = gemfile
13
+ require 'bundler'
14
+ Bundler.setup
15
+ rescue Bundler::GemNotFound => e
16
+ STDERR.puts e.message
17
+ STDERR.puts 'Try running `bundle install`.'
18
+ exit!
19
+ end if File.exist?(gemfile)
20
+
21
+ Bundler.require(:default, env)
22
+
23
+ require File.join(File.dirname(__FILE__),'app','application.rb')
24
+
25
+ run Application
@@ -0,0 +1,7 @@
1
+ module ThemeBandit
2
+ module URLFormatter
3
+ def strip_query_string(str)
4
+ str.split('?').first
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,50 @@
1
+ module ThemeBandit
2
+ class << self
3
+ attr_accessor :configuration
4
+ end
5
+
6
+ def self.configure
7
+ self.configuration ||= Config.new
8
+ yield configuration
9
+ configuration
10
+ end
11
+
12
+ class Config
13
+ attr_accessor :data
14
+
15
+ def initialize(data = {})
16
+ @data = {}
17
+ update!(data)
18
+ end
19
+
20
+ def update!(data)
21
+ data.each do |key, value|
22
+ self[key.to_sym] = value
23
+ end
24
+ end
25
+
26
+ def [](key)
27
+ @data[key.to_sym]
28
+ end
29
+
30
+ def []=(key, value)
31
+ @data[key.to_sym] = value
32
+ end
33
+
34
+ def keys
35
+ @data.keys
36
+ end
37
+
38
+ def to_hash
39
+ @data
40
+ end
41
+
42
+ def method_missing(sym, *args)
43
+ if sym.to_s =~ /(.+)=$/
44
+ self[Regexp.last_match[1]] = args.first
45
+ else
46
+ self[sym]
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module ThemeBandit
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,4 @@
1
+ require_relative 'theme_bandit/init'
2
+
3
+ module ThemeBandit
4
+ end
File without changes
@@ -0,0 +1,83 @@
1
+ <html>
2
+
3
+ <head>
4
+
5
+ <title>A very simple webpage</title>
6
+ <basefont size=4>
7
+ <link rel='stylesheet' href='/css/style.css'></link>
8
+ <script type='text/javascripts' src='/js/script.js'></script>
9
+ <script type='text/javascripts' src='/js/script_2.js'></script>
10
+ </head>
11
+
12
+ <body bgcolor=FFFFFF>
13
+
14
+ <h1>A very simple webpage. This is an "h1" level header.</h1>
15
+
16
+ <h2>This is a level h2 header.</h2>
17
+
18
+ <h6>This is a level h6 header. Pretty small!</h6>
19
+
20
+ <p>This is a standard paragraph.</p>
21
+
22
+ <p align=center>Now I've aligned it in the center of the screen.</p>
23
+
24
+ <p align=right>Now aligned to the right</p>
25
+
26
+ <p><b>Bold text</b></p>
27
+
28
+ <p><strong>Strongly emphasized text</strong> Can you tell the difference vs. bold?</p>
29
+
30
+ <p><i>Italics</i></p>
31
+
32
+ <p><em>Emphasized text</em> Just like Italics!</p>
33
+
34
+ <p>Here is a pretty picture: <img src=example/prettypicture.jpg alt="Pretty Picture"></p>
35
+
36
+ <p>Same thing, aligned differently to the paragraph: <img align=top src=example/prettypicture.jpg alt="Pretty Picture"></p>
37
+
38
+ <hr>
39
+
40
+ <h2>How about a nice ordered list!</h2>
41
+ <ol>
42
+ <li>This little piggy went to market
43
+ <li>This little piggy went to SB228 class
44
+ <li>This little piggy went to an expensive restaurant in Downtown Palo Alto
45
+ <li>This little piggy ate too much at Indian Buffet.
46
+ <li>This little piggy got lost
47
+ </ol>
48
+
49
+ <h2>Unordered list</h2>
50
+ <ul>
51
+ <li>First element
52
+ <li>Second element
53
+ <li>Third element
54
+ </ul>
55
+
56
+ <hr>
57
+
58
+ <h2>Nested Lists!</h2>
59
+ <ul>
60
+ <li>Things to to today:
61
+ <ol>
62
+ <li>Walk the dog
63
+ <li>Feed the cat
64
+ <li>Mow the lawn
65
+ </ol>
66
+ <li>Things to do tomorrow:
67
+ <ol>
68
+ <li>Lunch with mom
69
+ <li>Feed the hamster
70
+ <li>Clean kitchen
71
+ </ol>
72
+ </ul>
73
+
74
+ <p>And finally, how about some <a href=http://www.yahoo.com/>Links?</a></p>
75
+
76
+ <p>Or let's just link to <a href=../../index.html>another page on this server</a></p>
77
+
78
+ <p>Remember, you can view the HTMl code from this or any other page by using the "View Page Source" command of your browser.</p>
79
+
80
+ </body>
81
+
82
+ </html>
83
+
File without changes
File without changes
data/test/helper.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ require 'pry'
3
+ require 'minitest/autorun'
4
+ require 'webmock/minitest'
5
+
6
+ require_relative '../lib/theme_bandit'
7
+
8
+ MiniTest.autorun
9
+
10
+ def load_html_fixture
11
+ @fixture ||= File.read(File.open("#{Dir.pwd}/test/fixtures/index.html", 'r'))
12
+ end
13
+
14
+ def stub_request_stack
15
+ @url = 'http://www.example.com'
16
+ stub_request(:get, @url).to_return(body: load_html_fixture)
17
+ stub_css
18
+ stub_js
19
+ end
20
+
21
+ def prep_config
22
+ ThemeBandit.configure do |config|
23
+ config.template_engine = 'erb'
24
+ config.url = 'http://www.example.com'
25
+ config.gem_root = Dir.pwd
26
+ end
27
+ end
28
+
29
+ def stub_css
30
+ url = 'http://www.example.com/css/style.css'
31
+ stub_request(:get, url).to_return(body: '')
32
+ end
33
+
34
+ def stub_js
35
+ url = 'http://www.example.com/js/script.js'
36
+ stub_request(:get, url).to_return(body: '')
37
+ url = 'http://www.example.com/js/script_2.js'
38
+ stub_request(:get, url).to_return(body: '')
39
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'helper'
2
+
3
+ class TestConfigure < MiniTest::Test
4
+ def setup
5
+ @subject = ThemeBandit
6
+ end
7
+
8
+ def test_configuration
9
+ @subject.configure do |config|
10
+ config.foo = 'bar'
11
+ end
12
+ assert_equal @subject.configuration.foo, 'bar'
13
+ end
14
+ end
@@ -0,0 +1,64 @@
1
+ require 'helper'
2
+
3
+ describe ThemeBandit::DocumentWriter do
4
+
5
+ before do
6
+ stub_request_stack
7
+ prep_config
8
+ @subject = ThemeBandit::DocumentWriter.new(load_html_fixture, @url)
9
+ end
10
+
11
+ after do
12
+ `rm -rf theme`
13
+ end
14
+
15
+ describe 'attributes' do
16
+ it 'document' do
17
+ assert_kind_of(Nokogiri::HTML::Document, @subject.document)
18
+ end
19
+
20
+ it 'url' do
21
+ assert_kind_of(URI, @subject.url)
22
+ end
23
+ end
24
+
25
+ describe '#write' do
26
+
27
+ before do
28
+ @subject.write
29
+ end
30
+
31
+ describe '#write' do
32
+ it 'writes index.html' do
33
+ assert File.file?("#{Dir.pwd}/theme/public/index.html")
34
+ end
35
+ it 'writes styles' do
36
+ assert File.file?("#{Dir.pwd}/theme/public/css/style.css")
37
+ end
38
+ describe 'script writers' do
39
+ # Preserve order of script tags
40
+ it 'orders and renames script 1' do
41
+ assert File.file?("#{Dir.pwd}/theme/public/js/0_script.js")
42
+ end
43
+ it 'orders and renames script 2' do
44
+ assert File.file?("#{Dir.pwd}/theme/public/js/1_script_2.js")
45
+ end
46
+ end
47
+ end
48
+
49
+ describe 'parsers/mixin behavior' do
50
+ describe ThemeBandit::CSSParser do
51
+ it '#get_css_files' do
52
+ assert_equal(@subject.get_css_files, ['http://www.example.com/Users/lfender/source/theme_bandit/theme/public/css/style.css'])
53
+ end
54
+ end
55
+
56
+ describe ThemeBandit::JSParser do
57
+ it '#get_js_files' do
58
+ assert_equal(@subject.get_js_files, ['http://www.example.com/Users/lfender/source/theme_bandit/theme/public/js/0_script.js', 'http://www.example.com/Users/lfender/source/theme_bandit/theme/public/js/1_script_2.js'])
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,39 @@
1
+ require_relative 'helper'
2
+
3
+ describe ThemeBandit::Downloader do
4
+
5
+ before do
6
+ @url = 'https://www.example.com/'
7
+ stub_request(:get, @url).to_return(body: load_html_fixture)
8
+ end
9
+
10
+ describe '.get_theme' do
11
+ before do
12
+ @subject = ThemeBandit::Downloader
13
+ end
14
+
15
+ it 'requires url configuration' do
16
+ ThemeBandit.configure { |config| config.url = nil }
17
+ lambda { @subject.get_theme }.must_raise RuntimeError
18
+ end
19
+
20
+ describe 'attributes' do
21
+ before do
22
+ @instance = @subject.new(@url)
23
+ end
24
+
25
+ it '#url' do
26
+ assert_equal(@instance.url, @url)
27
+ end
28
+
29
+ it '#options' do
30
+ assert_equal(@instance.options, {})
31
+ end
32
+
33
+ it '#document returns html' do
34
+ assert_kind_of(String, @instance.document)
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,52 @@
1
+ require 'helper'
2
+
3
+ describe ThemeBandit::RackGenerator do
4
+
5
+ supported_engines = %w(slim haml erb)
6
+
7
+ supported_engines.each do |engine|
8
+ describe "#builds a recipe #{engine}" do
9
+
10
+ before do
11
+ stub_request_stack
12
+ @engine = engine
13
+ configure_and_write_files
14
+ ThemeBandit::RackGenerator.build
15
+ end
16
+
17
+ after do
18
+ `rm -rf theme`
19
+ end
20
+
21
+ it 'Gemfile' do
22
+ assert File.file?("#{Dir.pwd}/theme/Gemfile")
23
+ end
24
+
25
+ it 'config.ru' do
26
+ assert File.file?("#{Dir.pwd}/theme/config.ru")
27
+ end
28
+
29
+ it 'config.ru' do
30
+ assert File.file?("#{Dir.pwd}/theme/app/application.rb")
31
+ end
32
+
33
+ describe 'app/views/templates' do
34
+ it "build .#{@engine} file" do
35
+ assert File.file?("#{Dir.pwd}/theme/app/views/templates/index.#{@engine}")
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def configure_and_write_files
43
+ ThemeBandit.configure do |config|
44
+ config.template_engine = @engine
45
+ config.gem_root = Dir.pwd
46
+ config.url = 'http://www.example.com'
47
+ end
48
+ ThemeBandit::DocumentWriter.new(load_html_fixture, @url).write
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'helper'
2
+
3
+ describe ThemeBandit::URLFormatter do
4
+
5
+ include ThemeBandit::URLFormatter
6
+
7
+ it '#strip_query_string' do
8
+ url = 'http://www.example.com?foo=bar'
9
+ assert_equal(strip_query_string(url), 'http://www.example.com')
10
+ end
11
+
12
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'theme_bandit/util/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'theme_bandit'
8
+ spec.version = ThemeBandit::VERSION
9
+ spec.email = ['lfender6445@gmail.com']
10
+ spec.authors = ['Luke Fender']
11
+ spec.summary = 'Convert any site to a simple rack app'
12
+ spec.description = 'Convert any site to a simple rack app'
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables << 'bandit'
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+ spec.add_dependency 'httparty', '~> 0.11.0'
21
+ spec.add_dependency 'nokogiri', '~> 1.6.3.1'
22
+ spec.add_dependency 'html2slim', '~> 0.1.0'
23
+ spec.add_dependency 'html2haml', '~> 1.0.1'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.6'
26
+ spec.add_development_dependency 'rake', '~> 10.3.2'
27
+ spec.add_development_dependency 'minitest', '~> 5.4.1'
28
+ spec.add_development_dependency 'webmock', '~> 1.18.0'
29
+ end
metadata ADDED
@@ -0,0 +1,206 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: theme_bandit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Luke Fender
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.11.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.11.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.3.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.3.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: html2slim
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: html2haml
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 10.3.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 10.3.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: 5.4.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: 5.4.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.18.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: 1.18.0
125
+ description: Convert any site to a simple rack app
126
+ email:
127
+ - lfender6445@gmail.com
128
+ executables:
129
+ - bandit
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - bin/bandit
139
+ - lib/theme_bandit.rb
140
+ - lib/theme_bandit/document_writer.rb
141
+ - lib/theme_bandit/downloader.rb
142
+ - lib/theme_bandit/init.rb
143
+ - lib/theme_bandit/parser/css.rb
144
+ - lib/theme_bandit/parser/html.rb
145
+ - lib/theme_bandit/parser/js.rb
146
+ - lib/theme_bandit/rack_generator.rb
147
+ - lib/theme_bandit/recipes/sinatra/erb/Gemfile
148
+ - lib/theme_bandit/recipes/sinatra/erb/app/application.rb
149
+ - lib/theme_bandit/recipes/sinatra/erb/app/views/.keep
150
+ - lib/theme_bandit/recipes/sinatra/erb/app/views/templates/.keep
151
+ - lib/theme_bandit/recipes/sinatra/erb/config.ru
152
+ - lib/theme_bandit/recipes/sinatra/haml/Gemfile
153
+ - lib/theme_bandit/recipes/sinatra/haml/app/application.rb
154
+ - lib/theme_bandit/recipes/sinatra/haml/config.ru
155
+ - lib/theme_bandit/recipes/sinatra/slim/Gemfile
156
+ - lib/theme_bandit/recipes/sinatra/slim/app/application.rb
157
+ - lib/theme_bandit/recipes/sinatra/slim/config.ru
158
+ - lib/theme_bandit/url_formatter.rb
159
+ - lib/theme_bandit/util/configure.rb
160
+ - lib/theme_bandit/util/version.rb
161
+ - test/fixtures/css/style.css
162
+ - test/fixtures/index.html
163
+ - test/fixtures/js/script.js
164
+ - test/fixtures/js/script_2.js
165
+ - test/helper.rb
166
+ - test/test_configure.rb
167
+ - test/test_document_writer.rb
168
+ - test/test_downloader.rb
169
+ - test/test_rack_generator.rb
170
+ - test/test_url_formatter.rb
171
+ - theme_bandit.gemspec
172
+ homepage: ''
173
+ licenses:
174
+ - MIT
175
+ metadata: {}
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ! '>='
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubyforge_project:
192
+ rubygems_version: 2.2.2
193
+ signing_key:
194
+ specification_version: 4
195
+ summary: Convert any site to a simple rack app
196
+ test_files:
197
+ - test/fixtures/css/style.css
198
+ - test/fixtures/index.html
199
+ - test/fixtures/js/script.js
200
+ - test/fixtures/js/script_2.js
201
+ - test/helper.rb
202
+ - test/test_configure.rb
203
+ - test/test_document_writer.rb
204
+ - test/test_downloader.rb
205
+ - test/test_rack_generator.rb
206
+ - test/test_url_formatter.rb