chili_pdf 0.2.0 → 0.3.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.tar.gz.sig CHANGED
Binary file
data/.autotest ADDED
@@ -0,0 +1,14 @@
1
+ Autotest.add_hook(:initialize) {|at|
2
+ %w{.git .svn .hg .swp .DS_Store ._* tmp}.each do |exception|
3
+ at.add_exception(exception)
4
+ end
5
+
6
+ at.add_mapping(%r%^test/chili_pdf/.*_test.rb$%) { |filename, _|
7
+ filename
8
+ }
9
+ at.add_mapping(/test_helper.rb/) {
10
+ at.files_matching /^test.*rb$/
11
+ }
12
+ #nil
13
+ }
14
+
data/Gemfile CHANGED
@@ -3,8 +3,14 @@ source 'http://rubygems.org'
3
3
  gem 'wicked_pdf'
4
4
 
5
5
  group :development do
6
- gem 'autotest-rails', '~> 4.1.0'
6
+ gem 'autotest-rails', '~> 4.1'
7
7
  gem 'fakeweb'
8
- gem 'hoe', '~> 2.8.0'
8
+ gem 'hoe', '~> 2.8'
9
+ gem 'hoe-git'
9
10
  gem 'ZenTest'
11
+ gem 'redmine_plugin_support'
12
+ gem 'cucumber', '0.10'
13
+ gem 'rspec', '1.3.1'
14
+ gem 'rspec-rails', '1.3.2'
15
+ gem 'i18n', '0.4.2'
10
16
  end
data/Gemfile.lock CHANGED
@@ -4,10 +4,31 @@ GEM
4
4
  ZenTest (4.5.0)
5
5
  autotest-rails (4.1.0)
6
6
  ZenTest
7
+ builder (3.0.0)
8
+ cucumber (0.10.0)
9
+ builder (>= 2.1.2)
10
+ diff-lcs (~> 1.1.2)
11
+ gherkin (~> 2.3.2)
12
+ json (~> 1.4.6)
13
+ term-ansicolor (~> 1.0.5)
14
+ diff-lcs (1.1.2)
7
15
  fakeweb (1.3.0)
16
+ gherkin (2.3.3)
17
+ json (~> 1.4.6)
8
18
  hoe (2.8.0)
9
19
  rake (>= 0.8.7)
20
+ hoe-git (1.4.0)
21
+ hoe (>= 2.2.0)
22
+ i18n (0.4.2)
23
+ json (1.4.6)
24
+ rack (1.0.1)
10
25
  rake (0.8.7)
26
+ redmine_plugin_support (0.0.4)
27
+ rspec (1.3.1)
28
+ rspec-rails (1.3.2)
29
+ rack (>= 1.0.0)
30
+ rspec (>= 1.3.0)
31
+ term-ansicolor (1.0.5)
11
32
  wicked_pdf (0.7.0)
12
33
 
13
34
  PLATFORMS
@@ -15,7 +36,13 @@ PLATFORMS
15
36
 
16
37
  DEPENDENCIES
17
38
  ZenTest
18
- autotest-rails (~> 4.1.0)
39
+ autotest-rails (~> 4.1)
40
+ cucumber (= 0.10)
19
41
  fakeweb
20
- hoe (~> 2.8.0)
42
+ hoe (~> 2.8)
43
+ hoe-git
44
+ i18n (= 0.4.2)
45
+ redmine_plugin_support
46
+ rspec (= 1.3.1)
47
+ rspec-rails (= 1.3.2)
21
48
  wicked_pdf
data/History.txt CHANGED
@@ -1,3 +1,21 @@
1
+ === 0.3.0 / 2011-06-13
2
+
3
+ * New features
4
+ * Added header/footer support [closes #2]
5
+ * Implemented 'footer_enabled' and 'header_enabled' settings to
6
+ control whether footers/headers are included [closes #3]
7
+ * Added 'dynamic' content which can be used to customize the
8
+ header/footer sections of exported PDFs (see TokenManager and
9
+ TokenString for details) [closes #4]
10
+
11
+ * Minor enhancements:
12
+
13
+ * Added testing harness
14
+ * Updated layout to include any :header_tag content_for blocks
15
+ * PDF content was previously not being wrapped in PDF layout...fixed
16
+ * Threw in a fix for ul->li's showing numbers [closes #6]
17
+ * Added Formatter class which will be used for managing PDF formatting
18
+
1
19
  === 0.2.0 / 2011-05-23
2
20
 
3
21
  * Fixed layout & stylesheet inclusions
data/Manifest.txt CHANGED
@@ -1,3 +1,4 @@
1
+ .autotest
1
2
  Gemfile
2
3
  Gemfile.lock
3
4
  History.txt
@@ -9,12 +10,25 @@ app/controllers/extended_wiki_controller.rb
9
10
  app/helpers/chili_pdf_helper.rb
10
11
  app/views/extended_wiki/show.pdf.html.erb
11
12
  app/views/layouts/pdf.pdf.erb
13
+ app/views/settings/_chili_pdf_settings.html.erb
12
14
  assets/stylesheets/pdf.css
13
15
  autotest/discover.rb
16
+ config/locales/en.yml
14
17
  config/routes.rb
15
18
  init.rb
19
+ lang/en.yml
16
20
  lib/chili_pdf.rb
21
+ lib/chili_pdf/config.rb
22
+ lib/chili_pdf/formatter.rb
23
+ lib/chili_pdf/string_token.rb
24
+ lib/chili_pdf/token_manager.rb
17
25
  lib/tasks/chili_pdf_tasks.rb
26
+ lib/tasks/contributor_tasks.rb
27
+ lib/tasks/test_lib_dir.rake
18
28
  lib/user_patch.rb
19
29
  rails/init.rb
30
+ test/chili_pdf/config_test.rb
31
+ test/chili_pdf/formatter_test.rb
32
+ test/chili_pdf/string_token_test.rb
33
+ test/chili_pdf/token_manager_test.rb
20
34
  test/test_helper.rb
data/README.md CHANGED
@@ -30,7 +30,7 @@ ChiliProject (/Redmine) plugin which implements/enhances PDF-export functionalit
30
30
 
31
31
  ## SCREENSHOTS:
32
32
 
33
- You can find a few screenshots [here](http://www.flickr.com/photos/tomkersten/sets/72157626768112790/).
33
+ You can find a few screenshots [here](http://www.flickr.com/photos/tomkersten/sets/72157626827940413/).
34
34
 
35
35
  ## PROBLEMS:
36
36
 
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ RedminePluginSupport::Base.setup do |plugin|
11
11
  plugin.redmine_root = File.expand_path(File.dirname(__FILE__) + '/../../../')
12
12
  end
13
13
 
14
- Dir["/lib/tasks/**/*.rake"].sort.each { |ext| load ext }
14
+ Dir["lib/tasks/**/*.rake"].sort.each { |ext| load ext }
15
15
 
16
16
  task :default => [:test]
17
17
 
@@ -7,22 +7,21 @@ class ExtendedWikiController < WikiController
7
7
  super
8
8
  unless performed? # Prevent double render
9
9
  respond_to do |format|
10
- format.html {render :template => 'wiki/show'}
11
- format.pdf {
12
- filename = "#{@project.name.underscore}_#{@page.title}".gsub(/\s/,'')
13
- render :pdf => filename,
14
- :template => 'extended_wiki/show.pdf.html.erb',
15
- :page_size => "Letter",
16
- :show_as_html => params[:debug],
17
- :margin => {
18
- :top => "0.5in",
19
- :bottom => "0.5in",
20
- :left => "0.5in",
21
- :right => "0.5in"
22
- },
23
- :layout => 'pdf.pdf.erb'
24
- }
10
+ format.pdf {render formatter.render_options}
25
11
  end
26
12
  end
27
13
  end
14
+
15
+ private
16
+ def formatter
17
+ ChiliPDF::Formatter.new(filename, page_title)
18
+ end
19
+
20
+ def filename
21
+ "#{@project.name.underscore}_#{@page.title}".gsub(/\s/,'')
22
+ end
23
+
24
+ def page_title
25
+ "#{@project.name}, #{@page.title}"
26
+ end
28
27
  end
@@ -13,8 +13,6 @@
13
13
  <link rel="stylesheet" href="<%= root_url %>stylesheets/pdf.css">
14
14
  <%- end %>
15
15
 
16
- <!-- Mobile viewport optimized: j.mp/bplateviewport -->
17
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
18
16
  <%= yield(:header_tags) %>
19
17
  </head>
20
18
 
@@ -0,0 +1,82 @@
1
+ <% content_for :header_tags do %>
2
+ <%= stylesheet_link_tag '/plugin_assets/chili_pdf/stylesheets/pdf' %>
3
+ <% end %>
4
+
5
+ <p>
6
+ <label for='settings_<%= ChiliPDF::Config::HEADER_ENABLED_KEYNAME %>'><%= l(:header_enabled_label) %></label>
7
+ <%= check_box_tag "settings[#{ChiliPDF::Config::HEADER_ENABLED_KEYNAME}]", ChiliPDF::Config::ENABLED_VALUE, ChiliPDF::Config.header_enabled? %>
8
+ </p>
9
+
10
+ <p>
11
+ <label for='settings_<%= ChiliPDF::Config::FOOTER_ENABLED_KEYNAME %>'><%= l(:footer_enabled_label) %></label>
12
+ <%= check_box_tag "settings[#{ChiliPDF::Config::FOOTER_ENABLED_KEYNAME}]", ChiliPDF::Config::ENABLED_VALUE, ChiliPDF::Config.footer_enabled? %>
13
+ </p>
14
+
15
+ <h3>Custom Header/Footer Content</h3>
16
+
17
+ <h4>Rules</h4>
18
+ <ol>
19
+ <li>Any "plain text" will render, as is...</li>
20
+ <li>
21
+ There are a few 'variables' you can use to dynamically replace content. For example,
22
+ you can add the current date to any page.
23
+ </li>
24
+ <li>All input will be escaped HTML, so injecting dynamic content via JavaScript will not work.</li>
25
+ </ol>
26
+
27
+ <table>
28
+ <thead>
29
+ <tr>
30
+ <td></td>
31
+ <td>Left</td>
32
+ <td>Center</td>
33
+ <td>Right</td>
34
+ </tr>
35
+ </thead>
36
+ <tbody>
37
+ <tr>
38
+ <td>Header</td>
39
+ <td>
40
+ <%= text_field_tag "settings[#{ChiliPDF::Config::HEADER_LEFT_KEYNAME}]", ChiliPDF::Config.header_values[:left] %>
41
+ </td>
42
+ <td>
43
+ <%= text_field_tag "settings[#{ChiliPDF::Config::HEADER_CENTER_KEYNAME}]", ChiliPDF::Config.header_values[:center] %>
44
+ </td>
45
+ <td>
46
+ <%= text_field_tag "settings[#{ChiliPDF::Config::HEADER_RIGHT_KEYNAME}]", ChiliPDF::Config.header_values[:right] %>
47
+ </td>
48
+ </tr>
49
+ <tr>
50
+ <td>Footer</td>
51
+ <td>
52
+ <%= text_field_tag "settings[#{ChiliPDF::Config::FOOTER_LEFT_KEYNAME}]", ChiliPDF::Config.footer_values[:left] %>
53
+ </td>
54
+ <td>
55
+ <%= text_field_tag "settings[#{ChiliPDF::Config::FOOTER_CENTER_KEYNAME}]", ChiliPDF::Config.footer_values[:center] %>
56
+ </td>
57
+ <td>
58
+ <%= text_field_tag "settings[#{ChiliPDF::Config::FOOTER_RIGHT_KEYNAME}]", ChiliPDF::Config.footer_values[:right] %>
59
+ </td>
60
+ </tr>
61
+ </tbody>
62
+ </table>
63
+
64
+ <div id="pdf-token-help">
65
+ <h4>Dynamic text legend</h4>
66
+ <table>
67
+ <thead>
68
+ <tr>
69
+ <td>Enter this</td>
70
+ <td>...to show this</td>
71
+ </tr>
72
+ </thead>
73
+ <tbody>
74
+ <%- ChiliPDF::TokenManager.tokens.each do |token| %>
75
+ <tr>
76
+ <td class="token"><%= token.matcher_with_delimiters %></td>
77
+ <td class="description"><%= token.description %></td>
78
+ </tr>
79
+ <%- end %>
80
+ </tbody>
81
+ </table>
82
+ </div>
@@ -29,5 +29,16 @@ a.wiki-anchor { display:none; }
29
29
 
30
30
  h1, h2, h3, h4 { margin: 0.5em 0; }
31
31
  ul,ol {margin: 0 0 0 3em; padding: 0.1em;}
32
- li { list-style-type: decimal; padding: 0.1em; }
32
+ li { padding: 0.1em; }
33
+ ol li {list-style-type: decimal;}
34
+ ul li {list-style-type: disc;}
33
35
  h1, h2 {font-weight: bold;}
36
+
37
+ #pdf-token-help {
38
+ margin-top: 20px;
39
+ }
40
+
41
+ #pdf-token-help table {width: 100%; border: 1px #AAA solid;}
42
+ #pdf-token-help td {border-bottom: 1px dotted #CCC;}
43
+
44
+ #pdf-token-help thead td {font-weight: bold; border-bottom: 2px solid #CCC;}
data/autotest/discover.rb CHANGED
@@ -1,4 +1,2 @@
1
- Autotest.add_discovery do
2
- "rails"
3
- end
1
+ Autotest.add_discovery {"rails"}
4
2
 
@@ -0,0 +1,5 @@
1
+ # English strings go here for Rails i18n
2
+ en:
3
+ chili_pdf_instructions: 'You can customize the behavior of the ChiliPDf plugin with the settings below'
4
+ header_enabled_label: Display page header?
5
+ footer_enabled_label: Display page footer?
data/init.rb CHANGED
@@ -10,6 +10,9 @@ Redmine::Plugin.register :chili_pdf do
10
10
  version ChiliPDF::VERSION
11
11
  url 'https://github.com/tomkersten/chili_pdf'
12
12
  author_url 'http://tomkersten.com/'
13
+
14
+ settings :partial => 'settings/chili_pdf_settings',
15
+ :default => ChiliPDF::Config.defaults
13
16
  end
14
17
 
15
18
  Dispatcher.to_prepare :chili_pdf do
data/lang/en.yml ADDED
@@ -0,0 +1,3 @@
1
+ chili_pdf_instructions: 'You can customize the behavior of the ChiliPDf plugin with the settings below'
2
+ header_enabled_label: Display page header?
3
+ footer_enabled_label: Display page footer?
data/lib/chili_pdf.rb CHANGED
@@ -1,3 +1,8 @@
1
+ require 'chili_pdf/config'
2
+ require 'chili_pdf/token_manager'
3
+ require 'chili_pdf/formatter'
4
+ require 'chili_pdf/string_token'
5
+
1
6
  module ChiliPDF
2
- VERSION = '0.2.0'
7
+ VERSION = '0.3.0'
3
8
  end
@@ -0,0 +1,112 @@
1
+ module ChiliPDF
2
+ module Config
3
+ extend self
4
+
5
+ PLUGIN_KEYNAME = 'plugin_chili_pdf'
6
+ ENABLED_VALUE = '1'
7
+ DISABLED_VALUE = '0'
8
+ FOOTER_ENABLED_KEYNAME = 'footer_enabled'
9
+ HEADER_ENABLED_KEYNAME = 'header_enabled'
10
+
11
+ HEADER_LEFT_KEYNAME = :header_content_left
12
+ HEADER_CENTER_KEYNAME = :header_content_center
13
+ HEADER_RIGHT_KEYNAME = :header_content_right
14
+ FOOTER_LEFT_KEYNAME = :footer_content_left
15
+ FOOTER_CENTER_KEYNAME = :footer_content_center
16
+ FOOTER_RIGHT_KEYNAME = :footer_content_right
17
+
18
+ HEADER_LEFT_DEFAULT_VALUE = '{{page_title}}'
19
+ HEADER_CENTER_DEFAULT_VALUE = ''
20
+ HEADER_RIGHT_DEFAULT_VALUE = ''
21
+ FOOTER_LEFT_DEFAULT_VALUE = '{{datestamp}}'
22
+ FOOTER_CENTER_DEFAULT_VALUE = ''
23
+ FOOTER_RIGHT_DEFAULT_VALUE = '{{current_page}}/{{total_pages}}'
24
+
25
+ def defaults
26
+ {
27
+ FOOTER_ENABLED_KEYNAME => ENABLED_VALUE,
28
+ HEADER_ENABLED_KEYNAME => ENABLED_VALUE,
29
+ HEADER_LEFT_KEYNAME => HEADER_LEFT_DEFAULT_VALUE,
30
+ HEADER_CENTER_KEYNAME => HEADER_CENTER_DEFAULT_VALUE,
31
+ HEADER_RIGHT_KEYNAME => HEADER_RIGHT_DEFAULT_VALUE,
32
+ FOOTER_LEFT_KEYNAME => FOOTER_LEFT_DEFAULT_VALUE,
33
+ FOOTER_CENTER_KEYNAME => FOOTER_CENTER_DEFAULT_VALUE,
34
+ FOOTER_RIGHT_KEYNAME => FOOTER_RIGHT_DEFAULT_VALUE
35
+ }
36
+ end
37
+
38
+ # Public: Returns whether the footer will be printed when exporting
39
+ # a PDF.
40
+ #
41
+ # Returns either true or false.
42
+ def footer_enabled?
43
+ footer_enabled == ENABLED_VALUE
44
+ end
45
+
46
+ # Public: Returns whether the header will be printed when exporting
47
+ # a PDF.
48
+ #
49
+ # Returns either true or false.
50
+ def header_enabled?
51
+ header_enabled == ENABLED_VALUE
52
+ end
53
+
54
+ # Public: Accepts anything which responds to '[]' and '.has_key?'
55
+ # (ie: Hash-like objects)
56
+ #
57
+ # Utilizes following keys (values of):
58
+ # - HEADER_ENABLED_KEYNAME
59
+ # - FOOTER_ENABLED_KEYNAME
60
+ #
61
+ # Returns true. Always.
62
+ def update(options)
63
+ Setting[PLUGIN_KEYNAME] = defaults.merge(options)
64
+ true
65
+ end
66
+
67
+ def header_values
68
+ default_header_values.merge(stored_header_settings)
69
+ end
70
+
71
+ def footer_values
72
+ default_footer_values.merge(stored_footer_settings)
73
+ end
74
+
75
+ private
76
+ def default_header_values
77
+ {:left => HEADER_LEFT_DEFAULT_VALUE,
78
+ :center => HEADER_CENTER_DEFAULT_VALUE,
79
+ :right => HEADER_RIGHT_DEFAULT_VALUE}
80
+ end
81
+
82
+ def stored_header_settings
83
+ {:left => plugin_settings[HEADER_LEFT_KEYNAME],
84
+ :center => plugin_settings[HEADER_CENTER_KEYNAME],
85
+ :right => plugin_settings[HEADER_RIGHT_KEYNAME]}.reject {|k,v| v.nil?}
86
+ end
87
+
88
+ def default_footer_values
89
+ {:left => FOOTER_LEFT_DEFAULT_VALUE,
90
+ :center => FOOTER_CENTER_DEFAULT_VALUE,
91
+ :right => FOOTER_RIGHT_DEFAULT_VALUE}
92
+ end
93
+
94
+ def stored_footer_settings
95
+ {:left => plugin_settings[FOOTER_LEFT_KEYNAME],
96
+ :center => plugin_settings[FOOTER_CENTER_KEYNAME],
97
+ :right => plugin_settings[FOOTER_RIGHT_KEYNAME]}.reject {|k,v| v.nil?}
98
+ end
99
+
100
+ def footer_enabled
101
+ plugin_settings[FOOTER_ENABLED_KEYNAME]
102
+ end
103
+
104
+ def header_enabled
105
+ plugin_settings[HEADER_ENABLED_KEYNAME]
106
+ end
107
+
108
+ def plugin_settings
109
+ Setting[PLUGIN_KEYNAME]
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,80 @@
1
+ module ChiliPDF
2
+ class Formatter
3
+ HEADER_FOOTER_FONT_SIZE = 8
4
+ DEFAULT_MARGIN = '0.5in'
5
+ DEFAULT_PAGE_SIZE = "Letter"
6
+ DEFAULT_PAGE_TITLE = "Untitled"
7
+ DEFAULT_LAYOUT = 'pdf.pdf.erb'
8
+ DEFAULT_VIEW_TEMPLATE = 'extended_wiki/show.pdf.html.erb'
9
+
10
+ attr_reader :page_title
11
+
12
+ # TODO: Hack...need to come up with better approach for this...
13
+ TokenManager.add_token_definition do
14
+ {:page_title => {:replacement_object => (DEFAULT_PAGE_TITLE),
15
+ :description => "The project & wiki page name (eg: 'My Project, Wiki Page Title')"}}
16
+ end
17
+
18
+ def initialize(filename, title = nil)
19
+ @page_title = title
20
+ @filename = filename
21
+
22
+ # TODO: Hack...need to come up with better approach for this...
23
+ TokenManager.add_token_definition do
24
+ {:page_title => {:replacement_object => (@page_title || DEFAULT_PAGE_TITLE),
25
+ :description => "The project & wiki page name (eg: 'My Project, Wiki Page Title')"}}
26
+ end
27
+ end
28
+
29
+ def render_options
30
+ default_options = {
31
+ :pdf => @filename,
32
+ :template => view_template,
33
+ :page_size => DEFAULT_PAGE_SIZE,
34
+ :margin => {
35
+ :top => DEFAULT_MARGIN,
36
+ :bottom => DEFAULT_MARGIN,
37
+ :left => DEFAULT_MARGIN,
38
+ :right => DEFAULT_MARGIN
39
+ },
40
+ :layout => DEFAULT_LAYOUT
41
+ }
42
+
43
+ default_options.merge!(footer_options) if ChiliPDF::Config.footer_enabled?
44
+ default_options.merge!(header_options) if ChiliPDF::Config.header_enabled?
45
+ default_options
46
+ end
47
+
48
+ private
49
+ def footer_options
50
+ {:footer => {
51
+ :font_size => 8,
52
+ :line => true,
53
+ :spacing => 2
54
+ }.merge(substitute_tokens(ChiliPDF::Config.footer_values))}
55
+ end
56
+
57
+ def header_options
58
+ {:header => {
59
+ :font_size => HEADER_FOOTER_FONT_SIZE,
60
+ :line => true,
61
+ :spacing => 2
62
+ }.merge(substitute_tokens(ChiliPDF::Config.header_values))}
63
+ end
64
+
65
+ def substitute_tokens(hash)
66
+ hash.inject({}) do |converted_list, unconverted_item|
67
+ header_location,header_location_value = unconverted_item
68
+ converted_list.merge({header_location => replace_tokens_in(header_location_value)})
69
+ end
70
+ end
71
+
72
+ def replace_tokens_in(string)
73
+ TokenManager.apply_tokens_to string
74
+ end
75
+
76
+ def view_template
77
+ DEFAULT_VIEW_TEMPLATE
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,73 @@
1
+ module ChiliPDF
2
+ # Associates documentation of a "string to replace" (matcher) with it's
3
+ # "replacement text" (replacement_object) and allows either either static
4
+ # or dynamic string replacement declarations.
5
+ class StringToken
6
+ # Description used if the default description is empty
7
+ DEFAULT_DESCRIPTION = "[No description provided]"
8
+
9
+ # String used to denote the begining of a token
10
+ STARTING_DELIMITER = "{{"
11
+
12
+ # String used to denote the ending of a token
13
+ ENDING_DELIMITER = "}}"
14
+
15
+ # Public: Get the matcher of the token
16
+ # Returns the String matcher of token
17
+ attr_reader :matcher
18
+
19
+ # Returns the String replacement_object of token
20
+ attr_reader :replacement_object
21
+
22
+ # Returns the String description of the token
23
+ attr_reader :description
24
+
25
+
26
+ # Public: Create new instance of StringToken class
27
+ #
28
+ # matcher - the #to_s value to search for
29
+ # replacement_object - the value to replace 'matcher' with
30
+ # description - a friendly description which can be used
31
+ # to explain what purpose the matcher is
32
+ # intended to serve
33
+ #
34
+ # 'replacement_object' may be any object, but special behavior
35
+ # exists if the value is a Proc object. Refer to the #apply_to
36
+ # documentation for details on said behavior.
37
+ #
38
+ # Returns new instance of StringToken
39
+ def initialize(matcher, replacement_object, description = nil)
40
+ @matcher = matcher
41
+ @replacement_object = replacement_object
42
+ @description = description.blank? ? DEFAULT_DESCRIPTION : description
43
+ end
44
+
45
+ # Public: Substitutes any occurrences of #replacement_object in the specified
46
+ # object's response to #to_s. If #replacement_object returns a Proc
47
+ # object, #call will be issued to it.
48
+ #
49
+ # string - string to search for token occurrences in
50
+ #
51
+ # Examples:
52
+ # Returns copy of string with matcher text occurrences replaced.
53
+ def apply_to(string)
54
+ string.gsub(replacement_regexp(string), replacement_value.to_s)
55
+ end
56
+
57
+ def matcher_with_delimiters
58
+ "#{STARTING_DELIMITER}#{matcher}#{ENDING_DELIMITER}"
59
+ end
60
+ private
61
+ def replacement_regexp(string)
62
+ Regexp.new(/#{escaped(STARTING_DELIMITER)}#{matcher.to_s}#{escaped(ENDING_DELIMITER)}/)
63
+ end
64
+
65
+ def replacement_value
66
+ replacement_object.is_a?(Proc) ? replacement_object.call(self) : replacement_object
67
+ end
68
+
69
+ def escaped(string)
70
+ Regexp.escape string
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,45 @@
1
+ module ChiliPDF
2
+ module TokenManager
3
+ extend self
4
+
5
+ MAPPINGS = {:current_page => {
6
+ :replacement_object => "[page]",
7
+ :description => "The current page of the PDF document."},
8
+ :total_pages => {
9
+ :replacement_object => '[topage]',
10
+ :description => "The total number of pages in the PDF document."},
11
+ :datestamp => {
12
+ :replacement_object => lambda {Time.now.strftime('%d-%b-%Y')},
13
+ :description => "The current date in the format of DD-MON-YYYY'"},
14
+ :current_quarter => {
15
+ :replacement_object => lambda {calculate_quarter.to_s},
16
+ :description => "The current fiscal quarter (assuming Jan-Mar is thefirst quarter). Example output: '1'."},
17
+ :current_year => {
18
+ :replacement_object => lambda {Time.now.strftime('%Y')},
19
+ :description => "The current year in YYYY-format"}}
20
+
21
+ def tokens
22
+ raw_tokens.map {|matcher, meta|
23
+ StringToken.new(matcher, meta[:replacement_object], meta[:description])
24
+ }
25
+ end
26
+
27
+ def add_token_definition
28
+ raw_tokens.merge!(yield)
29
+ end
30
+
31
+ def apply_tokens_to(string)
32
+ tokens.inject(string.dup) {|modified, token| modified = token.apply_to modified}
33
+ end
34
+
35
+ private
36
+ def raw_tokens
37
+ @raw_tokens ||= MAPPINGS.dup
38
+ end
39
+
40
+ def self.calculate_quarter
41
+ month = Time.now.strftime('%m').to_i
42
+ ((month - 1) / 3) + 1
43
+ end
44
+ end
45
+ end
@@ -22,7 +22,6 @@ class ChiliPDFTasks < Rake::TaskLib
22
22
  task :symlink_assets => [:environment] do
23
23
  # HACK: Symlinks the files from plugindir/assets to the appropriate place in
24
24
  # the rails application
25
- remove_symlink(asset_destination_dir)
26
25
  puts "Symlinking assets (stylesheets, etc)..."
27
26
  add_symlink asset_source_dir, asset_destination_dir
28
27
  end
@@ -0,0 +1,73 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+
4
+ class ContributorTasks < Rake::TaskLib
5
+ def initialize
6
+ define
7
+ end
8
+
9
+ def define
10
+ desc "Update contributors list in README"
11
+ task :update_contributors do
12
+ if new_contributors?
13
+ puts "New contributors!"
14
+ new_contributors.each {|name| puts "- #{name}"}
15
+ puts ""
16
+
17
+ print "Updating the 'Contributors' section of README..."
18
+
19
+ File.open("README.md", "w+") do |file|
20
+ file.puts readme_without_contributors_section
21
+ file.puts "## Contributors (sorted alphabetically)"
22
+ file.puts ""
23
+ committers.each {|name| file.puts "* #{name}"}
24
+ end
25
+ puts "done!"
26
+ else
27
+ puts "No new contributors."
28
+ end
29
+ end
30
+
31
+ task :release do
32
+ puts "#####################"
33
+ puts "For the twitters"
34
+ puts "#####################"
35
+ puts "[ANN] chili_pdf v#{ChiliPDF::VERSION} released. Changes: http://bit.ly/lEfOaJ, Repo: http://bit.ly/lu5bjP /cc @chiliproject"
36
+ puts "#####################"
37
+
38
+ end
39
+ end
40
+
41
+ private
42
+ def all_committers
43
+ all_names = `git shortlog -s |cut -s -f2`
44
+ all_names.gsub!(/\sand\s/, "\n").split("\n").uniq!.sort!
45
+ end
46
+
47
+ def committers
48
+ excluded_committers = ["Tom Kersten"]
49
+ @committers ||= all_committers - excluded_committers
50
+ end
51
+
52
+ def readme_file_contents
53
+ @readme_contents ||= `cat README.md`
54
+ end
55
+
56
+ def existing_contributors
57
+ existing_contributors = readme_file_contents[/## Contributors(.|\n)*/].strip.split("\n* ")
58
+ existing_contributors.shift # remove "Contributors..." line
59
+ existing_contributors
60
+ end
61
+
62
+ def new_contributors
63
+ @new_contributors ||= committers - existing_contributors
64
+ end
65
+
66
+ def new_contributors?
67
+ !new_contributors.empty?
68
+ end
69
+
70
+ def readme_without_contributors_section
71
+ readme_file_contents.sub(/## Contributors(.|\n)*/, '')
72
+ end
73
+ end
@@ -0,0 +1,12 @@
1
+ namespace :test do
2
+ desc "Test lib source"
3
+ Rake::TestTask.new(:lib) do |t|
4
+ t.libs << "test"
5
+ t.pattern = 'test/chili_pdf/**/*_test.rb'
6
+ t.verbose = true
7
+ end
8
+ end
9
+
10
+ lib_task = Rake::Task["test:lib"]
11
+ test_task = Rake::Task[:test]
12
+ test_task.enhance { lib_task.invoke }
@@ -0,0 +1,69 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class ConfigTest < ActiveSupport::TestCase
4
+ setup do
5
+ Setting["plugin_chili_pdf"] = HashWithIndifferentAccess.new(ChiliPDF::Config.defaults)
6
+ end
7
+
8
+ context '#header_values' do
9
+ context 'when no customizations have taken place' do
10
+ should "return the 'defaults' values for the header fields" do
11
+ defaults = {
12
+ :left => ChiliPDF::Config.defaults[ChiliPDF::Config::HEADER_LEFT_KEYNAME],
13
+ :center => ChiliPDF::Config.defaults[ChiliPDF::Config::HEADER_CENTER_KEYNAME],
14
+ :right => ChiliPDF::Config.defaults[ChiliPDF::Config::HEADER_RIGHT_KEYNAME]
15
+ }
16
+ assert_equal defaults, ChiliPDF::Config.header_values
17
+ end
18
+ end
19
+
20
+ context 'when the content has been customized' do
21
+ setup do
22
+ @left = 'left_header'
23
+ @center = 'center header'
24
+ @right = 'right header'
25
+
26
+ Setting["plugin_chili_pdf"] = {ChiliPDF::Config::HEADER_LEFT_KEYNAME => @left,
27
+ ChiliPDF::Config::HEADER_CENTER_KEYNAME => @center,
28
+ ChiliPDF::Config::HEADER_RIGHT_KEYNAME => @right}
29
+ end
30
+
31
+ should "return the customized values for the header fields" do
32
+ assert_equal @left, ChiliPDF::Config.header_values[:left]
33
+ assert_equal @center, ChiliPDF::Config.header_values[:center]
34
+ assert_equal @right, ChiliPDF::Config.header_values[:right]
35
+ end
36
+ end
37
+ end
38
+
39
+ context '#footer_values' do
40
+ context 'when no customizations have taken place' do
41
+ should "return the 'defaults' values for the footer fields" do
42
+ defaults = {
43
+ :left => ChiliPDF::Config.defaults[ChiliPDF::Config::FOOTER_LEFT_KEYNAME],
44
+ :center => ChiliPDF::Config.defaults[ChiliPDF::Config::FOOTER_CENTER_KEYNAME],
45
+ :right => ChiliPDF::Config.defaults[ChiliPDF::Config::FOOTER_RIGHT_KEYNAME]
46
+ }
47
+ assert_equal defaults, ChiliPDF::Config.footer_values
48
+ end
49
+ end
50
+
51
+ context 'when the footer content has been customized' do
52
+ setup do
53
+ @left = 'left footer'
54
+ @center = 'center footer'
55
+ @right = 'right footer'
56
+
57
+ Setting["plugin_chili_pdf"] = {ChiliPDF::Config::FOOTER_LEFT_KEYNAME => @left,
58
+ ChiliPDF::Config::FOOTER_CENTER_KEYNAME => @center,
59
+ ChiliPDF::Config::FOOTER_RIGHT_KEYNAME => @right}
60
+ end
61
+
62
+ should "return the customized values for the footer fields" do
63
+ assert_equal @left, ChiliPDF::Config.footer_values[:left]
64
+ assert_equal @center, ChiliPDF::Config.footer_values[:center]
65
+ assert_equal @right, ChiliPDF::Config.footer_values[:right]
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,134 @@
1
+ require 'test_helper'
2
+
3
+ class FormatterTest < Test::Unit::TestCase
4
+ include HeaderHelpers
5
+
6
+ def setup
7
+ @formatter = Formatter.new('filename.pdf')
8
+ end
9
+
10
+ context "#render_options" do
11
+ should "render_options returns hash with pdf filename set" do
12
+ assert_equal 'filename.pdf', Formatter.new('filename.pdf').render_options[:pdf]
13
+ end
14
+
15
+ should "set the return the default layout of 'pdf.pdf.erb'" do
16
+ assert_equal ChiliPDF::Formatter::DEFAULT_LAYOUT, @formatter.render_options[:layout]
17
+ end
18
+
19
+ should "set 'extended_wiki/show.pdf.html.erb' as the default template" do
20
+ assert_equal ChiliPDF::Formatter::DEFAULT_VIEW_TEMPLATE, @formatter.render_options[:template]
21
+ end
22
+
23
+ should "set the default page_size to be 'Letter'" do
24
+ assert_equal ChiliPDF::Formatter::DEFAULT_PAGE_SIZE, @formatter.render_options[:page_size]
25
+ end
26
+
27
+ should "set the default margins" do
28
+ def_margin = ChiliPDF::Formatter::DEFAULT_MARGIN
29
+ default_margins = {:top => def_margin, :bottom => def_margin, :left => def_margin, :right => def_margin}
30
+ assert_equal default_margins, @formatter.render_options[:margin]
31
+ end
32
+
33
+ context 'when footers are enabled' do
34
+ setup do
35
+ enable_footers
36
+ end
37
+
38
+ should 'include footer options' do
39
+ assert ChiliPDF::Config.footer_enabled?
40
+ assert @formatter.render_options.has_key?(:footer)
41
+ end
42
+ end
43
+
44
+ context 'when footers are disabled' do
45
+ setup do
46
+ disable_footers
47
+ end
48
+
49
+ should "not include a 'footer' key" do
50
+ assert !@formatter.render_options.has_key?(:footer)
51
+ end
52
+ end
53
+
54
+ context 'when headers are enabled' do
55
+ setup do
56
+ enable_headers
57
+ end
58
+
59
+ should 'include header options' do
60
+ assert @formatter.render_options.has_key?(:header)
61
+ end
62
+ end
63
+
64
+ context 'when headers are disabled' do
65
+ setup do
66
+ disable_headers
67
+ end
68
+
69
+ should "not include a 'header' key" do
70
+ assert !@formatter.render_options.has_key?(:header)
71
+ end
72
+ end
73
+
74
+ context "replacing 'dynamic tokens' with expected content" do
75
+ should "substitues '{{current_page}}' with '[page]'" do
76
+ set_header_field_to "{{current_page}}"
77
+
78
+ assert ChiliPDF::Config.footer_enabled?
79
+ assert_equal '[page]', @formatter.render_options[:header][:left]
80
+ end
81
+
82
+ should "substitues '{{total_pages}}' with '[topage]'" do
83
+ set_header_field_to "{{total_pages}}"
84
+
85
+ assert ChiliPDF::Config.footer_enabled?
86
+ assert_equal '[topage]', @formatter.render_options[:header][:left]
87
+ end
88
+
89
+ should "substitues '{{datestamp}}' with a date formated as 'DD-MON-YYYY'" do
90
+ todays_date = Time.now.strftime('%d-%b-%Y')
91
+
92
+ set_header_field_to "{{datestamp}}"
93
+ assert_equal todays_date, header_value
94
+ end
95
+
96
+ context "when a page title is supplied" do
97
+ setup do
98
+ @custom_title = 'Custom title'
99
+ @formatter = ChiliPDF::Formatter.new('filename.pdf', @custom_title)
100
+ end
101
+
102
+ should "substitute '{{page_title}}' with a string value passed in as the 'page_title' argument" do
103
+ set_header_field_to "{{page_title}}"
104
+
105
+ assert ChiliPDF::Config.footer_enabled?
106
+ assert_equal @custom_title, header_value
107
+ end
108
+ end
109
+
110
+ context "when a page title is not supplied" do
111
+ should "substitues '{{page_title}}' with the default page title value '#{ChiliPDF::Formatter::DEFAULT_PAGE_TITLE}'" do
112
+ set_header_field_to "{{page_title}}"
113
+ assert_equal ChiliPDF::Formatter::DEFAULT_PAGE_TITLE, header_value
114
+ end
115
+ end
116
+
117
+ # The next couple are slightly hacky (or...maybe super-hacky)
118
+ should "substitues '{{current_year}}' with the current year (formated as 'YYYY')" do
119
+ todays_date = Time.now.strftime('%Y')
120
+
121
+ set_header_field_to "{{current_year}}"
122
+ assert_equal todays_date, header_value
123
+ end
124
+
125
+ should "substitues '{{current_quarter}}' with a string value of 1, 2, 3, 4...depending on month of year" do
126
+ month = Time.now.strftime('%m').to_i
127
+ expected = (((month - 1) / 3) + 1).to_s
128
+
129
+ set_header_field_to '{{current_quarter}}'
130
+ assert_equal expected, header_value
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,58 @@
1
+ require 'test_helper'
2
+
3
+ class StringTokenTest < Test::Unit::TestCase
4
+ context ".apply_to" do
5
+ setup do
6
+ @sdelim = StringToken::STARTING_DELIMITER
7
+ @edelim = StringToken::ENDING_DELIMITER
8
+ @matcher = "matcher_here"
9
+ @replacement_object = 'text_added'
10
+ @token = StringToken.new(@matcher, @replacement_object)
11
+ end
12
+
13
+ should "returns does not modify the original string passed in" do
14
+ original = '{{test}}'
15
+ string = original.dup
16
+ assert_not_equal original.object_id, @token.apply_to(string).object_id
17
+ end
18
+
19
+ should "set a default value for a description when not provided" do
20
+ assert_equal StringToken::DEFAULT_DESCRIPTION, @token.description
21
+ end
22
+
23
+ context "when the token's matcher does not exist in the specified string" do
24
+ should "return the same string value" do
25
+ assert_equal 'string', @token.apply_to('string')
26
+ end
27
+ end
28
+
29
+ context "when the token's matcher does exist in the specified string" do
30
+ setup do
31
+ @string = "This does have the matcher twice '#{@sdelim}#{@matcher}#{@edelim}' '#{@sdelim}#{@matcher}#{@edelim}'"
32
+ end
33
+
34
+ should "return a string value with only the matcher text replaced" do
35
+ expected_string = "This does have the matcher twice '#{@replacement_object}' '#{@replacement_object}'"
36
+ assert_equal expected_string, @token.apply_to(@string)
37
+ end
38
+
39
+ context "and the replacement object is a Proc object" do
40
+ setup do
41
+ @string = "This does have the matcher twice '#{@sdelim}#{@matcher}#{@edelim}' '#{@sdelim}#{@matcher}#{@edelim}'"
42
+ @result = 'resulting_replaced_text'
43
+ faker_class = Struct.new("Faker", :name)
44
+ faker = faker_class.new(@result)
45
+
46
+ @replacement_object = lambda {faker.name}
47
+ end
48
+
49
+ should "return a string value with the result of calling the Proc and passing 'self' as an argument" do
50
+ token = StringToken.new(@matcher, @replacement_object)
51
+
52
+ expected_string = "This does have the matcher twice '#{@result}' '#{@result}'"
53
+ assert_equal expected_string, token.apply_to(@string)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class TokenManagerTest < Test::Unit::TestCase
4
+ context ".tokens" do
5
+ should "return list of objects which respond to #apply_to" do
6
+ assert TokenManager.tokens.first.respond_to?(:apply_to)
7
+ end
8
+ end
9
+ end
data/test/test_helper.rb CHANGED
@@ -1,77 +1,36 @@
1
+ require 'test/unit'
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/../../../../test/test_helper')
2
4
 
3
5
  # Ensure that we are using the temporary fixture path
4
6
  Engines::Testing.set_fixture_path
5
7
 
8
+ require 'chili_pdf'
9
+ include ChiliPDF
6
10
 
7
- require 'webrat'
8
- require 'fakeweb'
9
- require 'chili_videos'
10
-
11
- FakeWeb.allow_net_connect = false
12
-
13
- Webrat.configure do |config|
14
- config.mode = :rails
15
- end
16
-
17
- module IntegrationTestHelper
18
- def login_as(user="existing", password="existing")
19
- visit "/login"
20
- fill_in 'Login', :with => user
21
- fill_in 'Password', :with => password
22
- click_button 'login'
23
- assert_response :success
24
- assert User.current.logged?
25
- end
26
-
27
- def visit_project(project)
28
- visit '/'
29
- assert_response :success
30
-
31
- click_link 'Projects'
32
- assert_response :success
33
-
34
- click_link project.name
35
- assert_response :success
11
+ module HeaderHelpers
12
+ def set_header_field_to(value)
13
+ ChiliPDF::Config.update({ChiliPDF::Config::HEADER_LEFT_KEYNAME => value})
36
14
  end
37
15
 
38
- def assert_forbidden
39
- assert_response :forbidden
40
- assert_template 'common/403'
16
+ def header_value
17
+ @formatter.render_options[:header][:left]
41
18
  end
42
19
 
43
- # Cleanup current_url to remove the host; sometimes it's present, sometimes it's not
44
- def current_path
45
- return nil if current_url.nil?
46
- return current_url.gsub("http://www.example.com","")
20
+ def disable_footers
21
+ ChiliPDF::Config.update(ChiliPDF::Config::FOOTER_ENABLED_KEYNAME => ChiliPDF::Config::DISABLED_VALUE)
47
22
  end
48
23
 
49
- end
50
-
51
- module TransloaditServiceHelper
52
- def workflow_results(identifier = :standard)
53
- YAML.load(File.open("test/fixtures/#{identifier}_transloadit_response.json"))
24
+ def enable_footers
25
+ ChiliPDF::Config.update({ChiliPDF::Config::FOOTER_ENABLED_KEYNAME => ChiliPDF::Config::ENABLED_VALUE})
54
26
  end
55
27
 
56
- def stub_assembly_url(assembly = nil, fixture_base_name = :single_video_processed)
57
- response = "test/fixtures/#{fixture_base_name.to_s}_assembly.json"
58
- url = assembly.blank? ? assembly_url : assembly.assembly_url
59
- FakeWeb.register_uri(:get, url, :response => response)
28
+ def enable_headers
29
+ ChiliPDF::Config.update({ChiliPDF::Config::HEADER_ENABLED_KEYNAME => ChiliPDF::Config::ENABLED_VALUE})
60
30
  end
61
31
 
62
- def assembly_url
63
- 'http://fake.transload.it/assembly_url'
32
+ def disable_headers
33
+ ChiliPDF::Config.update({ChiliPDF::Config::HEADER_ENABLED_KEYNAME => ChiliPDF::Config::DISABLED_VALUE})
64
34
  end
65
35
  end
66
36
 
67
- class ActionController::IntegrationTest
68
- include IntegrationTestHelper
69
- end
70
-
71
- class ActiveSupport::TestCase
72
- include TransloaditServiceHelper
73
- end
74
-
75
- class ActionController::TestCase
76
- include TransloaditServiceHelper
77
- end
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: 23
5
- prerelease: false
4
+ hash: 19
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
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-05-23 00:00:00 -05:00
39
+ date: 2011-06-13 00:00:00 -05:00
40
40
  default_executable:
41
41
  dependencies:
42
42
  - !ruby/object:Gem::Dependency
@@ -84,6 +84,7 @@ extra_rdoc_files:
84
84
  - README.md
85
85
  - LICENSE
86
86
  files:
87
+ - .autotest
87
88
  - Gemfile
88
89
  - Gemfile.lock
89
90
  - History.txt
@@ -95,14 +96,27 @@ files:
95
96
  - app/helpers/chili_pdf_helper.rb
96
97
  - app/views/extended_wiki/show.pdf.html.erb
97
98
  - app/views/layouts/pdf.pdf.erb
99
+ - app/views/settings/_chili_pdf_settings.html.erb
98
100
  - assets/stylesheets/pdf.css
99
101
  - autotest/discover.rb
102
+ - config/locales/en.yml
100
103
  - config/routes.rb
101
104
  - init.rb
105
+ - lang/en.yml
102
106
  - lib/chili_pdf.rb
107
+ - lib/chili_pdf/config.rb
108
+ - lib/chili_pdf/formatter.rb
109
+ - lib/chili_pdf/string_token.rb
110
+ - lib/chili_pdf/token_manager.rb
103
111
  - lib/tasks/chili_pdf_tasks.rb
112
+ - lib/tasks/contributor_tasks.rb
113
+ - lib/tasks/test_lib_dir.rake
104
114
  - lib/user_patch.rb
105
115
  - rails/init.rb
116
+ - test/chili_pdf/config_test.rb
117
+ - test/chili_pdf/formatter_test.rb
118
+ - test/chili_pdf/string_token_test.rb
119
+ - test/chili_pdf/token_manager_test.rb
106
120
  - test/test_helper.rb
107
121
  has_rdoc: true
108
122
  homepage: http://github.com/tomkersten/chili_pdf
@@ -135,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
149
  requirements: []
136
150
 
137
151
  rubyforge_project: chili_pdf
138
- rubygems_version: 1.3.7
152
+ rubygems_version: 1.6.2
139
153
  signing_key:
140
154
  specification_version: 3
141
155
  summary: ChiliProject (/Redmine) plugin which implements/enhances PDF-export functionality using the Webkit rendering engine.
metadata.gz.sig CHANGED
Binary file