httpng 0.1.1 → 0.2.0

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.
data/README.md CHANGED
@@ -32,11 +32,9 @@ by setting the `--port` or `-p` option on the command line.
32
32
 
33
33
  ## EXPORTING
34
34
 
35
- Once your server is running, simple add the following code to your HTML page in
36
- the `<head>` tag:
37
-
38
- <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
39
- <script src="/httpng.js"></script>
35
+ The server automatically inserts the JavaScript tags required for httpng to work
36
+ so just make sure your file includes `<HTML>` or `<HEAD>` and the file extension
37
+ is `html`.
40
38
 
41
39
  When you load your page then you should see a bar at the top that gives you a
42
40
  button to export your images.
data/bin/httpng CHANGED
@@ -54,7 +54,7 @@ elsif !File.directory?(options[:output])
54
54
  end
55
55
 
56
56
  # Run the web server
57
- Httpng::App.set(:public_folder, Dir.getwd())
57
+ Httpng::App.set(:public_dir, Dir.getwd())
58
58
  Httpng::App.set(:output_path, options[:output])
59
59
  Httpng::App.run!(:port => options[:port])
60
60
 
@@ -5,12 +5,26 @@ require 'sinatra'
5
5
 
6
6
  module Httpng
7
7
  class App < Sinatra::Base
8
+ ##############################################################################
9
+ #
10
+ # Settings
11
+ #
12
+ ##############################################################################
13
+
14
+ enable :logging
15
+
16
+
8
17
  ##############################################################################
9
18
  #
10
19
  # Routes
11
20
  #
12
21
  ##############################################################################
13
22
 
23
+ # HTML files.
24
+ get '/*.html' do
25
+ render_html(request.path_info)
26
+ end
27
+
14
28
  # The JavaScript library.
15
29
  get %r{/(httpng|html2canvas|jquery.plugin.html2canvas).js} do
16
30
  file = File.join(File.dirname(__FILE__), "assets/#{request.path_info}")
@@ -46,8 +60,45 @@ module Httpng
46
60
  if request.path_info == '/'
47
61
  redirect '/index.html'
48
62
  else
49
- return "No file found at: #{settings.public_folder}#{request.path_info}"
63
+ return "No file found at: #{settings.public_dir}#{request.path_info}"
50
64
  end
51
65
  end
66
+
67
+
68
+ private
69
+
70
+ # Renders an HTML page to the browser.
71
+ def render_html(path)
72
+ path = path.gsub('..', '')
73
+
74
+ file = File.join(settings.public_dir, path)
75
+
76
+ if File.exists?(file)
77
+ content = IO.read(file)
78
+ content = auto_insert_js(content)
79
+
80
+ content_type :html
81
+ return content
82
+ else
83
+ halt 404
84
+ end
85
+ end
86
+
87
+ # Automatically inserts the required JavaScript into the HEAD or HTML tags.
88
+ def auto_insert_js(html)
89
+ # Construct script tags.
90
+ js = []
91
+ js << '<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>'
92
+ js << '<script src="/httpng.js"></script>'
93
+ js << ''
94
+ js = js.join("\n")
95
+
96
+ # Insert into <head> if possible.
97
+ if html.sub!('<head>') {|text| "#{text}\n#{js}"}.nil?
98
+ html.sub!('<html>') {|text| "#{text}\n<head>\n#{js}</head>\n"}.nil?
99
+ end
100
+
101
+ return html
102
+ end
52
103
  end
53
104
  end
@@ -1,4 +1,4 @@
1
- // Preload html2canvas.
1
+ // Preload jQuery and html2canvas.
2
2
  document.write('<script src="/html2canvas.js"></script>');
3
3
  document.write('<script src="/jquery.plugin.html2canvas.js"></script>');
4
4
 
@@ -1,5 +1,5 @@
1
1
  # encoding: binary
2
2
 
3
3
  module Httpng
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httpng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-03-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: OptionParser
16
- requirement: &70196687482020 !ruby/object:Gem::Requirement
16
+ requirement: &70229391325820 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.5.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70196687482020
24
+ version_requirements: *70229391325820
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sinatra
27
- requirement: &70196687481480 !ruby/object:Gem::Requirement
27
+ requirement: &70229391325320 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 1.3.2
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70196687481480
35
+ version_requirements: *70229391325320
36
36
  description:
37
37
  email:
38
38
  - benbjohnson@yahoo.com