hubdown 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+
data/README.md CHANGED
@@ -46,7 +46,7 @@ $ hubdown my_file.md -o my_file.html
46
46
  When passing the `-o` flag with a file name, hubdown will write the full html output (as though you passed the `-w`) to the file you pass with `-o`. See Scraping for CSS below.
47
47
  ***
48
48
  ### Scraping for CSS
49
- Note that when you use either the `-w` or `-o` flags, hubdown will use your network connection to scrap the currently used CSS from github.com.
49
+ Note that when you use either the `-w` or `-o` flags, hubdown will use your network connection to scrap the currently used CSS from github.com. It will do it's best to cache that the results of that network call for future non-connected uses.
50
50
 
51
51
  ***
52
52
 
data/hubdown.gemspec CHANGED
@@ -16,6 +16,9 @@ Gem::Specification.new do |gem|
16
16
  gem.add_dependency('mixlib-cli', '>=1.2.2')
17
17
  gem.add_dependency('nokogiri', '>=1.5.6')
18
18
 
19
+ gem.add_development_dependency 'pry'
20
+ gem.add_development_dependency "rspec", ">= 2.0.0"
21
+
19
22
  gem.files = `git ls-files`.split($/)
20
23
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
21
24
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
File without changes
@@ -1,4 +1,4 @@
1
- require 'hubdown/style_scraper'
1
+ require 'hubdown/style_generator'
2
2
  require 'erb'
3
3
 
4
4
  module Hubdown
@@ -8,11 +8,11 @@ module Hubdown
8
8
  @body = args.fetch("body"){ '' }
9
9
  @uri = args.fetch("uri"){ 'https://github.com/knomedia/hubdown' }
10
10
  @filename = args.fetch("filename"){ '' }
11
- @scraper = StyleScraper.new( @uri )
11
+ @style_gen = StyleGenerator.new( @uri )
12
12
  end
13
13
 
14
14
  def get_page
15
- links = @scraper.get_css_links
15
+ links = @style_gen.get_css_links
16
16
  body = @body
17
17
  filename = @filename
18
18
  template_path = File.dirname(__FILE__) + "/template.html.erb"
@@ -0,0 +1,87 @@
1
+ require 'hubdown/style_sheet'
2
+ require 'fileutils'
3
+
4
+ class StyleCache
5
+ attr_accessor :style_data
6
+ attr_accessor :tmp
7
+ attr_accessor :latest_file
8
+
9
+ def initialize
10
+ @latest_file = File.join(File.dirname(File.expand_path(__FILE__)), "cache/latest_styles.txt")
11
+ end
12
+
13
+
14
+ def get_css_links
15
+ load_latest_files
16
+ end
17
+
18
+ def save_links links
19
+ links.each do |link|
20
+ link.download_content
21
+ end
22
+ create_tmp_file
23
+ write_link_cache_file links
24
+ write_link_contents links
25
+ remove_old_files
26
+
27
+ end
28
+
29
+ def create_tmp_file
30
+ @tmp = get_cache_file "tmp.txt"
31
+ FileUtils.touch tmp
32
+ end
33
+
34
+ def write_link_cache_file links
35
+ @style_data = ""
36
+ links.each do |link|
37
+ style_data << "#{link.name},#{link.url}\n"
38
+ end
39
+ File.open( tmp, 'w') {|f| f.write( style_data ) }
40
+ end
41
+
42
+ def write_link_contents links
43
+ links.each do |link|
44
+ style_file = get_cache_file link.name
45
+ FileUtils.touch style_file
46
+ File.open(style_file, 'w') {|f| f.write( link.content ) }
47
+ end
48
+ end
49
+
50
+ def remove_old_files
51
+ prev_sheets = load_latest_files
52
+ prev_sheets.each do |sheet|
53
+ FileUtils.rm(get_cache_file( sheet.name ))
54
+ end
55
+ File.open( @latest_file, 'w') {|f| f.write( style_data ) }
56
+ FileUtils.rm tmp
57
+ end
58
+
59
+ def get_cache_file name
60
+ File.join(File.dirname(File.expand_path(__FILE__)), "cache/#{name}")
61
+ end
62
+
63
+ def load_latest_files
64
+ cached_links = []
65
+ File.readlines( @latest_file ).each do |line|
66
+ contents = line.split(",")
67
+ ss = StyleSheet.new
68
+ ss.name = contents[0]
69
+ ss.url = contents[1]
70
+ ss.url = "file://" + (get_cache_file(''))
71
+ ss.content = get_style_content ss.name
72
+ cached_links << ss
73
+ end
74
+ cached_links
75
+ end
76
+
77
+ def get_style_content filename
78
+ file = get_cache_file filename
79
+ contents = ""
80
+ File.open( file, 'r' ) {|f| contents = f.read() }
81
+ contents
82
+ end
83
+
84
+
85
+
86
+
87
+ end
@@ -0,0 +1,60 @@
1
+ require 'hubdown/style_scraper'
2
+ require 'hubdown/style_cache'
3
+ require 'pry'
4
+
5
+ class StyleGenerator
6
+ def initialize uri
7
+ @uri = uri
8
+ @scraper = StyleScraper.new( @uri )
9
+ @cache = StyleCache.new
10
+
11
+ determine_css
12
+
13
+ end
14
+
15
+ def determine_css
16
+ live_links = @scraper.get_css_links
17
+ cached_links = @cache.get_css_links
18
+ if (live_links.length == 0)
19
+ if (cached_links.length == 0)
20
+ notify_error
21
+ else
22
+ @links = cached_links
23
+ end
24
+ else
25
+ if cached_links.length != 0
26
+ if !links_same(cached_links, live_links)
27
+ @cache.save_links live_links
28
+ end
29
+ else
30
+ @cache.save_links live_links
31
+ end
32
+ @links = @cache.get_css_links
33
+ end
34
+ end
35
+
36
+ def links_same cached, live
37
+ same = true
38
+ same = cached.length == live.length;
39
+ if same
40
+ cached.each do |cache_link|
41
+ live.each do |live_link|
42
+ same = cache_link.name == live_link.name
43
+ break unless !same
44
+ end
45
+ end
46
+ end
47
+ same
48
+ end
49
+
50
+ def notify_error
51
+ puts "It appears we are unable to connect to: #{@uri} for CSS scraping."
52
+ puts "There is no cached version of the styles available"
53
+ puts "Please check your internet connection, or drop the -w param"
54
+ exit 0
55
+ end
56
+
57
+ def get_css_links
58
+ @links
59
+ end
60
+ end
@@ -1,5 +1,6 @@
1
1
  require 'nokogiri'
2
2
  require 'open-uri'
3
+ require 'hubdown/style_sheet'
3
4
 
4
5
  class StyleScraper
5
6
 
@@ -8,15 +9,20 @@ class StyleScraper
8
9
  begin
9
10
  @page = Nokogiri::HTML( open( @uri ) )
10
11
  rescue
11
- puts "It appears we are unable to connect to: #{@uri} for CSS scraping."
12
- puts "Please check your internet connection"
13
- exit 0
12
+ # do nothing
14
13
  end
15
14
  end
16
15
 
17
16
  def get_css_links
18
17
  links = []
19
- links = @page.css("link").select{ |link| link['rel'] == 'stylesheet' }
18
+ if @page
19
+ tag_links = @page.css("link").select{ |link| link['rel'] == 'stylesheet' }
20
+ tag_links.each do |tag|
21
+ ss = StyleSheet.new
22
+ ss.from_tag(tag.to_s)
23
+ links << ss
24
+ end
25
+ end
20
26
  links
21
27
  end
22
28
 
@@ -0,0 +1,25 @@
1
+ require 'open-uri'
2
+
3
+ class StyleSheet
4
+ attr_accessor :name
5
+ attr_accessor :url
6
+ attr_accessor :content
7
+
8
+
9
+ def from_tag tag
10
+ matcher = /href=['|"](([\w:.\/])*)\/([\w|-]+.css)/.match( tag.to_s )
11
+ if matcher
12
+ @url = matcher.captures[0]
13
+ @name = matcher.captures[2]
14
+ end
15
+ end
16
+
17
+ def to_tag
18
+ "<link href=\"#{url}/#{name}\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\">"
19
+ end
20
+
21
+ def download_content
22
+ @content ||= open("#{@url}/#{name}").read
23
+ end
24
+
25
+ end
@@ -3,8 +3,8 @@
3
3
  <head>
4
4
  <title><%= filename || ''%></title>
5
5
  <% links.each do |link| -%>
6
- <%= link -%>
7
- <% end -%>
6
+ <%= link.to_tag %>
7
+ <% end %>
8
8
  <style>
9
9
  #wrapper {
10
10
  width: 920px;
@@ -1,3 +1,3 @@
1
1
  module Hubdown
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'hubdown'
4
+ require 'hubdown/style_sheet.rb'
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe StyleSheet do
4
+
5
+ it "outputs a proper tag" do
6
+ sh = StyleSheet.new
7
+ sh.name = "test.css"
8
+ sh.url = "http://localhost:3000/styles"
9
+ tag = sh.to_tag
10
+ tag.should eq '<link href="http://localhost:3000/styles/test.css" media="screen" rel="stylesheet" type="text/css">'
11
+ end
12
+
13
+ it "strips attributes from a given tag" do
14
+ sh = StyleSheet.new
15
+ sh.from_tag "<link href=\"https://a248.e.akamai.net/assets.github.com/assets/github-6c7984e384129edf1958345326c26471eedcdc23.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\">\n"
16
+
17
+ sh.name.should eq "github-6c7984e384129edf1958345326c26471eedcdc23.css"
18
+ sh.url.should eq "https://a248.e.akamai.net/assets.github.com/assets"
19
+ end
20
+
21
+ it "loads web content" do
22
+ sh = StyleSheet.new
23
+ sh.from_tag '<link href="http://dev.knomedia.com/hubdown_css/test.css" media="screen" rel="stylesheet" type="text/css">'
24
+ sh.download_content
25
+ sh.content.should eq "body { margin: 0; }"
26
+ end
27
+
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hubdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-29 00:00:00.000000000 Z
12
+ date: 2013-02-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: github-markdown
@@ -59,6 +59,38 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: 1.5.6
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 2.0.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 2.0.0
62
94
  description: CLI for GitHub Flavored markdown to html convervsion
63
95
  email:
64
96
  - knomedia@gmail.com
@@ -68,6 +100,7 @@ extensions: []
68
100
  extra_rdoc_files: []
69
101
  files:
70
102
  - .gitignore
103
+ - .rspec
71
104
  - Gemfile
72
105
  - LICENSE.txt
73
106
  - README.md
@@ -75,11 +108,17 @@ files:
75
108
  - bin/hubdown
76
109
  - hubdown.gemspec
77
110
  - lib/hubdown.rb
111
+ - lib/hubdown/cache/latest_styles.txt
78
112
  - lib/hubdown/cli_parser.rb
79
113
  - lib/hubdown/page_builder.rb
114
+ - lib/hubdown/style_cache.rb
115
+ - lib/hubdown/style_generator.rb
80
116
  - lib/hubdown/style_scraper.rb
117
+ - lib/hubdown/style_sheet.rb
81
118
  - lib/hubdown/template.html.erb
82
119
  - lib/hubdown/version.rb
120
+ - spec/spec_helper.rb
121
+ - spec/style_sheet_spec.rb
83
122
  homepage: https://github.com/knomedia/hubdown
84
123
  licenses: []
85
124
  post_install_message:
@@ -104,4 +143,6 @@ rubygems_version: 1.8.24
104
143
  signing_key:
105
144
  specification_version: 3
106
145
  summary: CLI for GitHub Flavored markdown to html conversion
107
- test_files: []
146
+ test_files:
147
+ - spec/spec_helper.rb
148
+ - spec/style_sheet_spec.rb