html_skeleton 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +42 -0
  3. data/lib/html_skeleton.rb +152 -0
  4. metadata +65 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Dittmar Krall - http://matique.de
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ HTML Calendar
2
+ =============
3
+
4
+ A simple helper for creating an HTML calendar.
5
+ The "calendar" method will be available to your view templates.
6
+
7
+ Blocks (Proc.new) may be supplied by the user to generate
8
+ particular HTML-code for a day, a month or a year.
9
+ As an example (see below) clicking a day triggers an action.
10
+
11
+
12
+ Examples
13
+ --------
14
+
15
+ calendar # calendar for current year
16
+ calendar :year = 2012 # calendar for year 2012
17
+ calendar :year = 2012, :month => 8 # calendar for August 2012
18
+
19
+ calendar {|date|
20
+ link ="/#{controller_name}/toggle/#{@row.id}?date=#{date}"
21
+ style = @row.holidays.include?(date.to_s) ?
22
+ 'font-weight:bold; color:red' : ''
23
+ %Q{ <a style="#{style}" href="#{link}"> #{date.day.to_s} </a>}
24
+ }
25
+
26
+
27
+ Default Options
28
+ ---------------
29
+ :year => DateTime.now.year,
30
+ :calendar_class => 'calendar',
31
+ :day_names => Date::DAYNAMES.dup,
32
+ :abbrev => (0..1),
33
+ :day_block => block || Proc.new {|d| d.day.to_s},
34
+ :month_block => Proc.new {|d| d.strftime('%B')},
35
+ :year_block => Proc.new {|d| d.to_s},
36
+ :first_day_of_week => 1
37
+
38
+
39
+ Inspired by calendar_helper:
40
+
41
+ * Jeremy Voorhis -- http://jvoorhis.com
42
+ * Geoffrey Grosenbach -- http://nubyonrails.com
@@ -0,0 +1,152 @@
1
+ require 'date'
2
+
3
+ class HtmlSkeleton
4
+ attr_reader :options
5
+
6
+ def table(rows, cols, options = {}, &block)
7
+ set_table_options(options, &block)
8
+ <<-EOS
9
+ <table class="#{@options[:table_class]}">
10
+ #{table_header(cols)}
11
+ #{table_body(rows, cols)}
12
+ </table>
13
+ EOS
14
+ end
15
+
16
+ def calendar(options = {}, &block)
17
+ set_calendar_options(options, &block)
18
+ frame = @options[:month] ? 'div' : 'table'
19
+ body = @options[:month] ?
20
+ a_month(@options[:year], @options[:month]) :
21
+ a_year(@options[:year])
22
+ %Q{<#{frame} class="#{@options[:calendar_class]}"> #{body} </#{frame}>}
23
+ end
24
+ end
25
+
26
+
27
+ # table methods ######################################################
28
+ class HtmlSkeleton
29
+ protected
30
+ def defaultProc
31
+ Proc.new(&:to_s)
32
+ end
33
+
34
+ def set_table_options(options, &block)
35
+ @options = {
36
+ :title => '',
37
+ :table_class => 'skeleton',
38
+ :legend_proc => defaultProc,
39
+ :header_proc => defaultProc,
40
+ :cell_proc => block || Proc.new {|row, col| "<td>#{row} #{col}</td>"},
41
+ }.merge options
42
+ end
43
+
44
+ def table_header(cols)
45
+ proc = @options[:header_proc]
46
+ col_header = cols.collect { |col| "<th>#{proc.call(col)}</th>" }.join
47
+ %Q{<thead><th class="legend">#{@options[:title]}</th>#{col_header}</thead>}
48
+ end
49
+
50
+ def table_body(rows, cols)
51
+ legend_proc = @options[:legend_proc]
52
+ rows.collect { |row|
53
+ cells = table_row(row, cols)
54
+ %Q{<tr><td class="legend">#{legend_proc.call(row)}</td>#{cells}</tr>}
55
+ }.join("\n")
56
+ end
57
+
58
+ def table_row(row, cols)
59
+ cell_proc = @options[:cell_proc]
60
+ cols.collect { |col| cell_proc.call(row, col) }.join('')
61
+ end
62
+
63
+ end
64
+
65
+
66
+ # calendar methods ###################################################
67
+ class HtmlSkeleton
68
+ attr_reader :day_header
69
+
70
+ protected
71
+ def set_calendar_options(options, &block)
72
+ year = DateTime.now.year
73
+ @options = {
74
+ :year => year,
75
+ :title => year,
76
+ :calendar_class => 'skeleton',
77
+ :month_names => Date::MONTHNAMES,
78
+ :abbrev => (0..1),
79
+ :cell_proc => block || Proc.new {|d| d.day.to_s},
80
+ :first_day_of_week => 1
81
+ }.merge options
82
+
83
+ names = options[:day_names] || Date::DAYNAMES.dup
84
+ @options[:first_day_of_week].times { names.push(names.shift) }
85
+
86
+ @day_header = names.collect { |day|
87
+ abbr = day[@options[:abbrev]]
88
+ str = abbr == day ? day : %Q{<abbr title='#{day}'>#{abbr}</abbr>}
89
+ %Q{<th scope='col'>#{str}</th>}
90
+ }.join('')
91
+ end
92
+
93
+ def a_year(year)
94
+ rows, cols = 3, 4
95
+ body = (0..rows - 1).collect {|y|
96
+ str = (1..cols).collect {|x| "<td>#{a_month(year, y * cols + x)}</td>"}
97
+ "<tr>#{str.join('')}</tr>"
98
+ }
99
+ <<-EOS
100
+ <thead><th colspan="2">#{@options[:title]}</th></thead>
101
+ #{body.join('')}
102
+ EOS
103
+ end
104
+
105
+ def a_month(year, month)
106
+ title = @options[:month_names][month]
107
+ <<-EOS
108
+ <table class="month">
109
+ <tr class="monthName"><th colspan="7">#{title}</th></tr>
110
+ <tr class="dayName">#{@day_header}</tr>
111
+ #{days_of_month(year, month)}
112
+ </table>
113
+ EOS
114
+ end
115
+
116
+ def days_of_month(year, month)
117
+ first_weekday = @options[:first_day_of_week]
118
+ last_weekday = first_weekday > 0 ? first_weekday - 1 : 6
119
+ cell_proc = @options[:cell_proc]
120
+ today = (Time.respond_to?(:zone) &&
121
+ !(zone = Time.zone).nil? ? zone.now.to_date : Date.today)
122
+
123
+ first = Date.civil(year, month, 1)
124
+ last = Date.civil(year, month, -1)
125
+
126
+ cal = '<tr>'
127
+ cal << '<td></td>' * days_between(first_weekday, first.wday)
128
+ first.upto(last) {|cur|
129
+ cal << a_day(cur, today, cell_proc)
130
+ cal << '</tr> <tr>' if cur.wday == last_weekday
131
+ }
132
+ cal << '<td></td>' * days_between((last + 1).wday, first_weekday + 7)
133
+ cal << '</tr>'
134
+ end
135
+
136
+ def a_day(date, today, block)
137
+ attrs = 'day'
138
+ attrs += ' weekendDay' if weekend?(date)
139
+ attrs += ' today' if date == today
140
+ "<td class=\"#{attrs}\">#{block.call(date)}</td>"
141
+ # "<td class=\"#{attrs}\">##</td>"
142
+ end
143
+
144
+ def weekend?(date)
145
+ [0, 6].include?(date.wday)
146
+ end
147
+
148
+ def days_between(first, second)
149
+ first > second ? second + (7 - first) : second - first
150
+ end
151
+
152
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_skeleton
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dittmar Krall
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: ! " A simple helper for creating HTML calendars and tables.\n\n An
31
+ example in a view: <%= HtmlSkeleton.new.calendar %>\n\n The calendar/table may
32
+ be embelished by user supplied Proc.new's,\n e.g. for inserting link_to.\n"
33
+ email: dittmar.krall@matique.de
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - lib/html_skeleton.rb
39
+ - README.md
40
+ - MIT-LICENSE
41
+ homepage:
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.24
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A simple helper for creating HTML calendars and tables
65
+ test_files: []