theme_bandit 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b98fe31887c36eb16097ad233376fb08bb748b2
4
- data.tar.gz: 5e02c75d4d02ed06326a4b3bd5ecece88c31b05f
3
+ metadata.gz: 81c723824fb7b4fc26fdffc3fced9bac1977b425
4
+ data.tar.gz: 186064cc76b40f3bcf43b5783b7e7153906e55af
5
5
  SHA512:
6
- metadata.gz: 0c3013136157eff463dafa95e1d4a1f15a02538fc9a277193988563b619e4a9ce6cd83376dced43e334cf818872fecb66c0502eb14dd4362d3436d1c9301386b
7
- data.tar.gz: 7a037df92b3d056545e97f7f93f7b0985ead53307b7db864759e66cde7715fb6e36a26e8be8354ddecf03e757c6e707027d281f723e47c5dfecba195531d01c9
6
+ metadata.gz: c282373169582e752f8b333f638424fe79f33f23a37782e6bf2b21e79f48ea31321bb628828f85dd4b167ca78d71c346ec3466f765c81f66504d5481cf22bbe6
7
+ data.tar.gz: 8c8f7d1ffffa25f0ac7788fdb023cfaf21474a1be1b1f986e8a69c03a9d7a9ab29b5f620aa7a828a3afa7a6c4cc43001d43fc3ce8fc456634457434a83c00af2
data/README.md CHANGED
@@ -2,10 +2,12 @@
2
2
 
3
3
  Enjoy rapid prototyping with theme bandit :heart:
4
4
 
5
- Convert any site template (Wordpress, Joomla, HTML) into a small and
6
- simple sinatra rack application.
5
+ Wordpress themes are beautiful! Now you can easily convert your favorite wordpress theme to a small ruby app.
7
6
 
8
- `bandit`
7
+ This gem converts any site template (Wordpress, Joomla, HTML) into a small and
8
+ simple sinatra rack application, handling all of the JS and CSS for you.
9
+
10
+ Usage from the command line: `bandit`
9
11
  - Select a url to download
10
12
  - Select a templating engine (erb, haml, slim)
11
13
  - Start your rack app!
@@ -25,7 +27,7 @@ Or install it yourself as:
25
27
  $ gem install theme_bandit
26
28
 
27
29
  ## Usage
28
- `./bin/bandit/` -> Builds a rack application in the `theme` directory of
30
+ `bandit` -> Builds a rack application in the `theme` directory of
29
31
  your project root.
30
32
 
31
33
  ## Caveats
@@ -33,11 +35,18 @@ Not all templating engines play nicely with html. If you run into
33
35
  templating issues, `erb` will be your safest bet because it renders as
34
36
  pure html.
35
37
 
36
- ## TODO
38
+ Example: The gem converts html for page A to slim, erb, or haml.
39
+ Sometimes the html for page A is messy and in turn produces bad slim,
40
+ which can cause the application to blow up until corrected.
41
+
42
+ ## TODO / Coming Soon
43
+ - Replace & fetch @import declarations in CSS
44
+ - Fetch ajax resources
45
+ - HTML Sanitization
37
46
  - Support for binaries (images + fonts + embeds)
38
47
  - Support for multiple pages
39
- - HTML Sanitization
40
48
  - HTML Formatting
49
+ - Verfiy inline tags
41
50
 
42
51
  ## Tests
43
52
 
data/bin/bandit CHANGED
@@ -12,7 +12,7 @@ SUPPORTED_ENGINES = /^(erb|haml|slim)$/
12
12
  def get_root
13
13
  begin
14
14
  Gem::Specification.find_by_name('theme_bandit').gem_dir
15
- rescue
15
+ rescue Gem::LoadError
16
16
  Dir.pwd
17
17
  end
18
18
  end
@@ -21,36 +21,72 @@ module ThemeBandit
21
21
  write_html_revision
22
22
  end
23
23
 
24
- def html
25
- document.to_html
26
- end
24
+ private
27
25
 
28
26
  def write_html_revision
27
+ make_dir(CSS_FOLDER)
28
+ make_dir(JS_FOLDER)
29
29
  download_css(get_css_files)
30
30
  download_js(get_js_files)
31
31
  revise_head_tags
32
32
  write_html_file
33
33
  end
34
34
 
35
- # def build_rack_app
36
- # ThemeBandit::RackGenerator.build
37
- # end
35
+
36
+ def html
37
+ document.to_html
38
+ end
38
39
 
39
40
  def make_dir(folder)
40
41
  FileUtils.mkdir_p folder
41
42
  end
42
43
 
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) }
44
+ def download_css(files, these_are_import_files=false)
45
+ files.each_with_index do |file, order|
46
+ if these_are_import_files
47
+ download_and_replace_import_in_css_file(file, order)
48
+ else
49
+ download_single_css_file(file, order)
50
+ end
51
+ end
52
+ end
53
+
54
+ def download_and_replace_import_in_css_file(file, order)
55
+ destination = file[:destination]
56
+ rule = file[:rule]
57
+ doc = Downloader.fetch(destination, {}).body
58
+ new_doc = @doc_with_imports.gsub(rule, doc)
59
+ new_file_name = @file_with_imports.split('/').last
60
+ File.open("#{CSS_FOLDER}#{order}_#{new_file_name}", 'w') { |f| f.write(new_doc) }
61
+ download_css_imports(new_doc, destination) do |imports|
62
+ download_css(imports, true) if imports
63
+ end
64
+ end
65
+
66
+ def download_single_css_file(file_name, order)
67
+ doc = Downloader.fetch(file_name, {}).body
68
+ download_css_imports(doc, file_name) do |imports|
69
+ if imports
70
+ download_css(imports, true)
71
+ else
72
+ new_file_name = file_name.split('/').last
73
+ File.open("#{CSS_FOLDER}#{order}_#{new_file_name}", 'w') { |file| file.write(doc) }
74
+ end
75
+ end
76
+ end
77
+
78
+ # Use recurison to get to deepest level
79
+ def download_css_imports(doc, file_name)
80
+ if imports = get_import_urls(doc, file_name)
81
+ @doc_with_imports = doc
82
+ @file_with_imports = file_name
83
+ yield imports
84
+ else
85
+ yield false
49
86
  end
50
87
  end
51
88
 
52
89
  def download_js(files)
53
- make_dir(JS_FOLDER)
54
90
  files.each_with_index do |file_name, order|
55
91
  doc = Downloader.fetch(file_name, {})
56
92
  new_file = file_name.split('/').last
@@ -1,7 +1,10 @@
1
1
  module ThemeBandit
2
2
  module CSSParser
3
+
3
4
  include ThemeBandit::URLFormatter
4
5
 
6
+ RE_IMPORT = /\@import\s*(?:url\s*)?(?:\()?(?:\s*)["']?([^'"\s\)]*)["']?\)?([\w\s\,^\]\(\))]*)\)?[;\n]?/
7
+
5
8
  def get_css_files
6
9
  link_tag_values
7
10
  end
@@ -22,5 +25,28 @@ module ThemeBandit
22
25
  end
23
26
  end
24
27
  end
28
+
29
+ def get_import_urls(file, file_name)
30
+ imports = file.scan(RE_IMPORT).map { |import| import.first }.select {|import| import[/\.css/] }
31
+ imports_arr = []
32
+ imports.each do |import|
33
+ dot_dots_length = import.split('/').select { |x| x[/\.\./] }.length
34
+ if dot_dots_length > 0
35
+ file_uri = URI.parse(file_name)
36
+ matcher = file_uri.path.split('/').last(dot_dots_length + 1).join('/')
37
+ destination = import.split('/').last(2).join('/')
38
+ dest_file = file_name.gsub(matcher, destination)
39
+ imports_arr << { destination: dest_file, rule: file.match(/\@import.*#{import}(.*\;)?/)[0] }
40
+ else
41
+ file_uri = URI.parse(file_name)
42
+ matcher = file_uri.path.split('/').last
43
+ destination = import
44
+ dest_file = file_name.gsub(matcher, destination)
45
+ imports_arr << { destination: dest_file, rule: file.match(/\@import.*#{import}(.*\;)?/)[0] }
46
+ end
47
+ end
48
+ imports_arr.length > 0 ? imports_arr : nil
49
+ end
50
+
25
51
  end
26
52
  end
@@ -1,12 +1,17 @@
1
1
  module ThemeBandit
2
2
  module HTMLParser
3
3
  def revise_head_tags
4
+ remove_base_tags
4
5
  remove_link_tags
5
6
  remove_script_tags
6
7
  inject_link_nodes
7
8
  inject_script_nodes
8
9
  end
9
10
 
11
+ def remove_base_tags
12
+ document.search('base').remove
13
+ end
14
+
10
15
  def remove_link_tags
11
16
  document.search('link').remove
12
17
  end
@@ -33,7 +33,7 @@ module ThemeBandit
33
33
  absolute_to_relative(File.read(index_html))
34
34
  end
35
35
 
36
- # Convert all links in html document to relative for ruby app
36
+
37
37
  def absolute_to_relative(contents)
38
38
  contents.gsub("#{Dir.pwd}/theme/public", '')
39
39
  end
@@ -1,3 +1,3 @@
1
1
  module ThemeBandit
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -0,0 +1 @@
1
+ h1 {color:blue;}
@@ -0,0 +1 @@
1
+ body {color:red}
@@ -0,0 +1,2 @@
1
+ @import url('import.css');
2
+ body {color:red}
@@ -5,6 +5,7 @@
5
5
  <title>A very simple webpage</title>
6
6
  <basefont size=4>
7
7
  <link rel='stylesheet' href='/css/style.css'></link>
8
+ <link rel='stylesheet' href='/css/style_with_import.css'></link>
8
9
  <script type='text/javascripts' src='/js/script.js'></script>
9
10
  <script type='text/javascripts' src='/js/script_2.js'></script>
10
11
  </head>
@@ -1,5 +1,5 @@
1
- require 'rubygems'
2
1
  require 'pry'
2
+ require 'rubygems'
3
3
  require 'minitest/autorun'
4
4
  require 'webmock/minitest'
5
5
 
@@ -11,29 +11,57 @@ def load_html_fixture
11
11
  @fixture ||= File.read(File.open("#{Dir.pwd}/test/fixtures/index.html", 'r'))
12
12
  end
13
13
 
14
+ def load_css_fixture
15
+ File.read(File.open("#{Dir.pwd}/test/fixtures/css/style.css", 'r'))
16
+ end
17
+
18
+ def load_import_fixture
19
+ File.read(File.open("#{Dir.pwd}/test/fixtures/css/import.css", 'r'))
20
+ end
21
+
22
+ def load_style_with_import_fixture
23
+ File.read(File.open("#{Dir.pwd}/test/fixtures/css/style_with_import.css", 'r'))
24
+ end
25
+
26
+ def test_url
27
+ 'http://www.example.com'
28
+ end
29
+
14
30
  def stub_request_stack
15
- @url = 'http://www.example.com'
16
- stub_request(:get, @url).to_return(body: load_html_fixture)
31
+ stub_request(:any, test_url).to_return(body: load_html_fixture)
17
32
  stub_css
33
+ stub_import_css
34
+ stub_style_with_import_css
18
35
  stub_js
19
36
  end
20
37
 
21
38
  def prep_config
22
39
  ThemeBandit.configure do |config|
23
40
  config.template_engine = 'erb'
24
- config.url = 'http://www.example.com'
41
+ config.url = test_url
25
42
  config.gem_root = Dir.pwd
26
43
  end
27
44
  end
28
45
 
29
46
  def stub_css
30
47
  url = 'http://www.example.com/css/style.css'
31
- stub_request(:get, url).to_return(body: '')
48
+ stub_request(:any, url).to_return(body: load_css_fixture)
49
+ end
50
+
51
+ def stub_import_css
52
+ url = 'http://www.example.com/css/import.css'
53
+ stub_request(:any, url).to_return(body: load_import_fixture)
32
54
  end
33
55
 
56
+ def stub_style_with_import_css
57
+ url = 'http://www.example.com/css/style_with_import.css'
58
+ stub_request(:any, url).to_return(body: load_style_with_import_fixture)
59
+ end
60
+
61
+
34
62
  def stub_js
35
63
  url = 'http://www.example.com/js/script.js'
36
- stub_request(:get, url).to_return(body: '')
64
+ stub_request(:any, url).to_return(body: '')
37
65
  url = 'http://www.example.com/js/script_2.js'
38
- stub_request(:get, url).to_return(body: '')
66
+ stub_request(:any, url).to_return(body: '')
39
67
  end
@@ -3,9 +3,9 @@ require 'helper'
3
3
  describe ThemeBandit::DocumentWriter do
4
4
 
5
5
  before do
6
- stub_request_stack
7
6
  prep_config
8
- @subject = ThemeBandit::DocumentWriter.new(load_html_fixture, @url)
7
+ stub_request_stack
8
+ @subject = ThemeBandit::DocumentWriter.new(load_html_fixture, test_url)
9
9
  end
10
10
 
11
11
  after do
@@ -32,9 +32,12 @@ describe ThemeBandit::DocumentWriter do
32
32
  it 'writes index.html' do
33
33
  assert File.file?("#{Dir.pwd}/theme/public/index.html")
34
34
  end
35
+
35
36
  it 'writes styles' do
36
- assert File.file?("#{Dir.pwd}/theme/public/css/style.css")
37
+
38
+ assert File.file?("#{Dir.pwd}/theme/public/css/0_style.css")
37
39
  end
40
+
38
41
  describe 'script writers' do
39
42
  # Preserve order of script tags
40
43
  it 'orders and renames script 1' do
@@ -44,18 +47,38 @@ describe ThemeBandit::DocumentWriter do
44
47
  assert File.file?("#{Dir.pwd}/theme/public/js/1_script_2.js")
45
48
  end
46
49
  end
50
+
51
+ describe 'import output' do
52
+ it 'merges css files' do
53
+ contents = File.read(File.open("#{Dir.pwd}/theme/public/css/0_style_with_import.css", 'r'))
54
+ expected = "h1 {color:blue;}\n\nbody {color:red}\n"
55
+ assert_equal contents, expected
56
+ end
57
+ end
58
+
47
59
  end
48
60
 
49
- describe 'parsers/mixin behavior' do
61
+ describe 'parser mixin behavior' do
62
+
50
63
  describe ThemeBandit::CSSParser do
51
64
  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'])
65
+ assert_equal(@subject.get_css_files, ["http://www.example.com#{Dir.pwd}/theme/public/css/0_style.css", "http://www.example.com#{Dir.pwd}/theme/public/css/0_style_with_import.css" ])
66
+ end
67
+
68
+ describe '#get_import_urls' do
69
+ it 'returns false if none found' do
70
+ assert_equal(@subject.get_import_urls(load_css_fixture, 'import.css'), nil )
71
+ end
72
+ it 'returns import urls' do
73
+ expected = [{:destination=>"import.css", :rule=>"@import url('import.css');"}]
74
+ assert_equal(@subject.get_import_urls(load_style_with_import_fixture, 'style_with_import.css'), expected)
75
+ end
53
76
  end
54
77
  end
55
78
 
56
79
  describe ThemeBandit::JSParser do
57
80
  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'])
81
+ assert_equal(@subject.get_js_files, ["http://www.example.com#{Dir.pwd}/theme/public/js/0_script.js", "http://www.example.com#{Dir.pwd}/theme/public/js/1_script_2.js"])
59
82
  end
60
83
  end
61
84
  end
@@ -5,10 +5,9 @@ describe ThemeBandit::RackGenerator do
5
5
  supported_engines = %w(slim haml erb)
6
6
 
7
7
  supported_engines.each do |engine|
8
- describe "#builds a recipe #{engine}" do
8
+ describe "#builds #{engine} recipe" do
9
9
 
10
10
  before do
11
- stub_request_stack
12
11
  @engine = engine
13
12
  configure_and_write_files
14
13
  ThemeBandit::RackGenerator.build
@@ -26,13 +25,13 @@ describe ThemeBandit::RackGenerator do
26
25
  assert File.file?("#{Dir.pwd}/theme/config.ru")
27
26
  end
28
27
 
29
- it 'config.ru' do
28
+ it 'application.rb' do
30
29
  assert File.file?("#{Dir.pwd}/theme/app/application.rb")
31
30
  end
32
31
 
33
32
  describe 'app/views/templates' do
34
- it "build .#{@engine} file" do
35
- assert File.file?("#{Dir.pwd}/theme/app/views/templates/index.#{@engine}")
33
+ it "build .#{engine} file" do
34
+ assert File.file?("#{Dir.pwd}/theme/app/views/templates/index.#{engine}")
36
35
  end
37
36
  end
38
37
  end
@@ -40,12 +39,14 @@ describe ThemeBandit::RackGenerator do
40
39
  private
41
40
 
42
41
  def configure_and_write_files
42
+ prep_config
43
43
  ThemeBandit.configure do |config|
44
44
  config.template_engine = @engine
45
+ config.url = test_url
45
46
  config.gem_root = Dir.pwd
46
- config.url = 'http://www.example.com'
47
47
  end
48
- ThemeBandit::DocumentWriter.new(load_html_fixture, @url).write
48
+ stub_request_stack
49
+ ThemeBandit::DocumentWriter.new(load_html_fixture, ThemeBandit.configuration.url).write
49
50
  end
50
51
  end
51
52
 
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.authors = ['Luke Fender']
11
11
  spec.summary = 'Convert any site to a simple rack app'
12
12
  spec.description = 'Convert any site to a simple rack app'
13
- spec.homepage = ''
13
+ spec.homepage = 'https://github.com/lfender6445/theme_bandit'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: theme_bandit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Fender
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-16 00:00:00.000000000 Z
11
+ date: 2014-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -159,7 +159,9 @@ files:
159
159
  - lib/theme_bandit/url_formatter.rb
160
160
  - lib/theme_bandit/util/configure.rb
161
161
  - lib/theme_bandit/util/version.rb
162
+ - test/fixtures/css/import.css
162
163
  - test/fixtures/css/style.css
164
+ - test/fixtures/css/style_with_import.css
163
165
  - test/fixtures/index.html
164
166
  - test/fixtures/js/script.js
165
167
  - test/fixtures/js/script_2.js
@@ -170,7 +172,7 @@ files:
170
172
  - test/test_rack_generator.rb
171
173
  - test/test_url_formatter.rb
172
174
  - theme_bandit.gemspec
173
- homepage: ''
175
+ homepage: https://github.com/lfender6445/theme_bandit
174
176
  licenses:
175
177
  - MIT
176
178
  metadata: {}
@@ -195,7 +197,9 @@ signing_key:
195
197
  specification_version: 4
196
198
  summary: Convert any site to a simple rack app
197
199
  test_files:
200
+ - test/fixtures/css/import.css
198
201
  - test/fixtures/css/style.css
202
+ - test/fixtures/css/style_with_import.css
199
203
  - test/fixtures/index.html
200
204
  - test/fixtures/js/script.js
201
205
  - test/fixtures/js/script_2.js