chili_pdf 0.4.1 → 0.4.3
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.tar.gz.sig +0 -0
- data/History.txt +18 -0
- data/app/helpers/chili_pdf_helper.rb +47 -10
- data/app/views/extended_wiki/show.pdf.html.erb +1 -1
- data/app/views/settings/_chili_pdf_watermark_settings.html.erb +1 -1
- data/assets/stylesheets/chili_pdf_settings_tweaks.css +2 -0
- data/lib/chili_pdf.rb +1 -1
- data/lib/chili_pdf/tag_mangler.rb +33 -2
- data/test/chili_pdf/tag_mangler_test.rb +52 -2
- data/test/unit/helpers/chili_pdf_helper_test.rb +8 -0
- metadata +4 -4
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
Binary file
|
data/History.txt
CHANGED
@@ -1,3 +1,21 @@
|
|
1
|
+
=== 0.4.3 / 2011-06-20
|
2
|
+
|
3
|
+
* Bugfix:
|
4
|
+
* Yanked 0.4.2 release...this release fixes a mistake tests did not
|
5
|
+
catch...no features other than that on top of 0.4.2
|
6
|
+
|
7
|
+
=== 0.4.2 / 2011-06-20
|
8
|
+
|
9
|
+
* Minor enhancements:
|
10
|
+
* Updates added style for logo image in plugin settings to reflect
|
11
|
+
the size it will render as within a PDF.
|
12
|
+
|
13
|
+
* Bugfix:
|
14
|
+
* Fixed the issue with relative links not working. All links will point
|
15
|
+
to URLs for the time-being (vs in-document chapter links), as the
|
16
|
+
anchor-tag-to-chapter-link conversion doesn't seem to be working for
|
17
|
+
some reason. [closes #16]
|
18
|
+
|
1
19
|
=== 0.4.1 / 2011-06-19
|
2
20
|
|
3
21
|
* New features:
|
@@ -112,6 +112,35 @@ module ChiliPdfHelper
|
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
|
+
# Public: Converts 'href' attributes of any <a> tags to be compatible with
|
116
|
+
# the `wkhtmltopdf` executable requirements (to absolute URLs).
|
117
|
+
# Capable of handling URLs to both other domains and relative URLS.
|
118
|
+
#
|
119
|
+
# content - the String of HTML content to normalize/update
|
120
|
+
#
|
121
|
+
# Returns content un-modified if wants_html is true. Otherwise returns
|
122
|
+
# the content string with the modified 'href' attribute on all
|
123
|
+
# <a>-tags in content.
|
124
|
+
def update_a_hrefs(content)
|
125
|
+
update_tag_attribute(:a, :href, content, false, :absolute_url)
|
126
|
+
end
|
127
|
+
|
128
|
+
# Public: Converts 'href' attributes of any <a> tags and any 'src' attributes
|
129
|
+
# of <img> tags contained in 'content' to be compatible with
|
130
|
+
# the `wkhtmltopdf` executable (to absolute & 'file://-based' URLs).
|
131
|
+
#
|
132
|
+
# content - the String of HTML content to normalize/update
|
133
|
+
# wants_html - specifies whether the attribute should be formatted
|
134
|
+
# for HTML or PDF requests (local vs. relative paths). Added to
|
135
|
+
# keep excessive boolean logic out of views.
|
136
|
+
#
|
137
|
+
# Returns content un-modified if wants_html is true. Otherwise returns
|
138
|
+
# the content string with the modified 'href' attribute on all
|
139
|
+
# <a>-tags in content.
|
140
|
+
def update_link_and_image_urls(content, wants_html)
|
141
|
+
update_a_hrefs(update_img_src_tags_of(content, wants_html))
|
142
|
+
end
|
143
|
+
|
115
144
|
private
|
116
145
|
# Updates the value of the specified attribute of any `tag_type` tags
|
117
146
|
# contained in `content` to be compatible with the `wkhtmltopdf`
|
@@ -124,12 +153,12 @@ module ChiliPdfHelper
|
|
124
153
|
# wants_html - specifies whether the attribute should be formatted
|
125
154
|
# for HTML or PDF requests (local vs. relative paths). Added to
|
126
155
|
# keep excessive boolean logic out of views.
|
127
|
-
def update_tag_attribute(tag_type, attribute, content, wants_html)
|
156
|
+
def update_tag_attribute(tag_type, attribute, content, wants_html, url_type = :file)
|
128
157
|
return content if wants_html
|
129
158
|
|
130
159
|
doc = ::Nokogiri::HTML(content)
|
131
160
|
doc.xpath("//#{tag_type.to_s}[@#{attribute.to_s}]").each do |a_tag|
|
132
|
-
a_tag["#{attribute.to_s}"] = mangle(a_tag["#{attribute.to_s}"])
|
161
|
+
a_tag["#{attribute.to_s}"] = mangle(a_tag["#{attribute.to_s}"], url_type)
|
133
162
|
end
|
134
163
|
doc.to_s
|
135
164
|
end
|
@@ -148,8 +177,7 @@ module ChiliPdfHelper
|
|
148
177
|
# wants_html - specifies whether the 'src' attribute should be formatted
|
149
178
|
# for HTML or PDF requests (local vs. relative paths). Added to
|
150
179
|
# keep excessive boolean logic out of views.
|
151
|
-
#
|
152
|
-
# passed in as a parameter to &block)
|
180
|
+
#
|
153
181
|
# Returns an Array of the results of the &block executions.
|
154
182
|
def file_type_list(file_ext, src_dir, wants_html, &block)
|
155
183
|
# We want/need to prepend the Prototype script-tag before all other script-tags
|
@@ -164,9 +192,13 @@ module ChiliPdfHelper
|
|
164
192
|
|
165
193
|
# Converts the specified String to PDF-format-friendly version of itself.
|
166
194
|
# Supports the '/attachments/:id/:filename' and '/attachments/download/:id' routes
|
167
|
-
# of ChiliProject as well as standard static assets (nested under RAILS_ROOT/public)
|
195
|
+
# of ChiliProject as well as standard static assets (nested under RAILS_ROOT/public),
|
196
|
+
# and URLs (relative & absolute).
|
168
197
|
#
|
169
|
-
# content
|
198
|
+
# content - String to convert
|
199
|
+
# url_type - symbol of the type of url to convert (default: :file)
|
200
|
+
# :file - process as a local-file URL, if valid
|
201
|
+
# :absolute_url - process as if the content were a web URL
|
170
202
|
#
|
171
203
|
# Examples
|
172
204
|
#
|
@@ -177,9 +209,14 @@ module ChiliPdfHelper
|
|
177
209
|
# => "file:///path/to/rails_root/files/38278327_image.png"
|
178
210
|
#
|
179
211
|
# Returns modified String, unless requested asset either doesn't exist
|
180
|
-
# or is an invalid request (
|
181
|
-
#
|
182
|
-
def mangle(content)
|
183
|
-
|
212
|
+
# or is an invalid request (eg: an asset located above the 'RAILS_ROOT/public'
|
213
|
+
# sub-directory).
|
214
|
+
def mangle(content, url_type = :file)
|
215
|
+
case url_type
|
216
|
+
when :file then TagMangler.new(content).to_local_src
|
217
|
+
when :absolute_url then TagMangler.new(content, request).to_absolute_url
|
218
|
+
else
|
219
|
+
raise ArgumentError, "invalid url_type passed to #mangle (received: #{url_type.inspect})"
|
220
|
+
end
|
184
221
|
end
|
185
222
|
end
|
@@ -8,4 +8,4 @@
|
|
8
8
|
<%= logo_img_tag(@requesting_html_version) %>
|
9
9
|
|
10
10
|
<%- html_content = textilizable(@content.text, :attachments => @content.page.attachments) %>
|
11
|
-
<%=
|
11
|
+
<%= update_link_and_image_urls(html_content, @requesting_html_version) %>
|
@@ -13,3 +13,5 @@ p.chili-pdf-instructions {padding-left: 0;}
|
|
13
13
|
#pdf-token-help table {width: 100%; border: 1px #AAA solid;}
|
14
14
|
#pdf-token-help td {border-bottom: 1px dotted #CCC;}
|
15
15
|
#pdf-token-help thead td {font-weight: bold; border-bottom: 2px solid #CCC;}
|
16
|
+
|
17
|
+
#chili-pdf-logo {max-height:50px; border:none;}
|
data/lib/chili_pdf.rb
CHANGED
@@ -1,13 +1,28 @@
|
|
1
1
|
class TagMangler
|
2
|
-
def initialize(src)
|
3
|
-
@src_attribute = src
|
2
|
+
def initialize(src, request_like_object = nil)
|
3
|
+
@src_attribute = src # /roy
|
4
|
+
@request_like = request_like_object
|
4
5
|
end
|
5
6
|
|
6
7
|
def to_local_src
|
7
8
|
full_file_path.blank? ? @src_attribute : "file://#{full_file_path}"
|
8
9
|
end
|
9
10
|
|
11
|
+
def to_absolute_url
|
12
|
+
if relative_url?
|
13
|
+
"#{base_url}#{relative_url}"
|
14
|
+
elsif anchor_tag
|
15
|
+
"#{@request_like.url}#{anchor_tag}"
|
16
|
+
else
|
17
|
+
@src_attribute
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
10
21
|
private
|
22
|
+
def base_url
|
23
|
+
@request_like.url.sub(@request_like.request_uri, '/')
|
24
|
+
end
|
25
|
+
|
11
26
|
def full_file_path
|
12
27
|
if requesting_valid_static_asset?
|
13
28
|
requested_path
|
@@ -16,6 +31,22 @@ class TagMangler
|
|
16
31
|
end
|
17
32
|
end
|
18
33
|
|
34
|
+
def anchor_tag
|
35
|
+
@src_attribute[%r!^(#.*)!]
|
36
|
+
end
|
37
|
+
|
38
|
+
def anchor_tag?
|
39
|
+
!anchor_tag.blank
|
40
|
+
end
|
41
|
+
|
42
|
+
def relative_url
|
43
|
+
@src_attribute.match(%r!^/(.*)!) && $1
|
44
|
+
end
|
45
|
+
|
46
|
+
def relative_url?
|
47
|
+
!relative_url.blank?
|
48
|
+
end
|
49
|
+
|
19
50
|
def attachment
|
20
51
|
Attachment.find_by_id(attachment_id)
|
21
52
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../test_helper'
|
2
2
|
|
3
|
-
def make_mangler_with(asset_path)
|
4
|
-
TagMangler.new(asset_path)
|
3
|
+
def make_mangler_with(asset_path, request_like = nil)
|
4
|
+
TagMangler.new(asset_path, request_like)
|
5
5
|
end
|
6
6
|
|
7
7
|
# Stolen from the Rails.root test_helper...
|
@@ -112,4 +112,54 @@ class TokenManagerTest < Test::Unit::TestCase
|
|
112
112
|
end
|
113
113
|
end
|
114
114
|
end
|
115
|
+
|
116
|
+
context "#to_absolute_url" do
|
117
|
+
context "when the content passed in has a link to another domain" do
|
118
|
+
setup do
|
119
|
+
@requested_asset = "http://someotherhost.com/page.html"
|
120
|
+
@mangler = make_mangler_with(@requested_asset)
|
121
|
+
end
|
122
|
+
|
123
|
+
should "not be modified" do
|
124
|
+
assert_equal @requested_asset, @mangler.to_absolute_url
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context "when the content passed in has a relative link" do
|
129
|
+
setup do
|
130
|
+
@base_url = "http://example.com"
|
131
|
+
@requested_asset = "/page"
|
132
|
+
@absolute_url = "#{@base_url}#{@requested_asset}"
|
133
|
+
|
134
|
+
mock_request = 'Mocked Request'
|
135
|
+
mock_request.stubs(:url).returns(@absolute_url)
|
136
|
+
mock_request.stubs(:request_uri).returns(@requested_asset)
|
137
|
+
|
138
|
+
@mangler = make_mangler_with(@requested_asset, mock_request)
|
139
|
+
end
|
140
|
+
|
141
|
+
should "should return an absolute URL to the original page" do
|
142
|
+
assert_equal @absolute_url, @mangler.to_absolute_url
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context "when the content passed in has an anchor to another location in the current page" do
|
147
|
+
setup do
|
148
|
+
@base_url = "http://example.com"
|
149
|
+
@current_page = "/current_page"
|
150
|
+
@absolute_url = "#{@base_url}#{@current_page}"
|
151
|
+
|
152
|
+
mock_request = 'Mocked Request'
|
153
|
+
mock_request.stubs(:request_uri).returns(@current_page)
|
154
|
+
mock_request.stubs(:url).returns(@absolute_url)
|
155
|
+
|
156
|
+
@requested_asset = "#HeadingOnPage"
|
157
|
+
@mangler = make_mangler_with(@requested_asset, mock_request)
|
158
|
+
end
|
159
|
+
|
160
|
+
should "should return an absolute URL to the original page" do
|
161
|
+
assert_equal "#{@absolute_url}#{@requested_asset}", @mangler.to_absolute_url
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
115
165
|
end
|
@@ -3,6 +3,14 @@ require File.dirname(__FILE__) + '/../../test_helper'
|
|
3
3
|
class ChiliPdfHelperTest < HelperTestCase
|
4
4
|
include ChiliPdfHelper
|
5
5
|
|
6
|
+
def root_url
|
7
|
+
"http://example.com/"
|
8
|
+
end
|
9
|
+
|
10
|
+
def request
|
11
|
+
@request ||= ActionController::TestRequest.new
|
12
|
+
end
|
13
|
+
|
6
14
|
context "#chili_pdf_stylesheets" do
|
7
15
|
should "include the pdf.css stylesheet" do
|
8
16
|
assert_match %r(<link.*href=['"].*pdf\.css['"].*>), chili_pdf_stylesheets(false)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chili_pdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 3
|
10
|
+
version: 0.4.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tom Kersten
|
@@ -36,7 +36,7 @@ cert_chain:
|
|
36
36
|
NVADJA==
|
37
37
|
-----END CERTIFICATE-----
|
38
38
|
|
39
|
-
date: 2011-06-
|
39
|
+
date: 2011-06-20 00:00:00 -05:00
|
40
40
|
default_executable:
|
41
41
|
dependencies:
|
42
42
|
- !ruby/object:Gem::Dependency
|
metadata.gz.sig
CHANGED
Binary file
|