From 9db6e7a044b12ba545f08cdce39031ee9a11fffa Mon Sep 17 00:00:00 2001 From: Cyrus Beer Date: Wed, 4 Jan 2023 09:58:59 -0500 Subject: [PATCH 1/5] simple change to test --- lib/prawn_html/tags/li.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/prawn_html/tags/li.rb b/lib/prawn_html/tags/li.rb index 52215c2..08cb0ea 100644 --- a/lib/prawn_html/tags/li.rb +++ b/lib/prawn_html/tags/li.rb @@ -15,7 +15,9 @@ def block? def before_content return if @before_content_once - @before_content_once = @counter ? "#{@counter}. " : "#{@symbol} " + @before_content_once = @counter ? "bb. " : "#{@symbol} " +# @before_content_once = @counter ? "#{@counter}. " : "#{@symbol} " +# @before_content_once = @counter ? "#{counter_display(parent.attrs('type'), @counter)}. " : "#{@symbol} " end def block_styles @@ -34,6 +36,10 @@ def on_context_add(_context) @symbol = parent.styles[:list_style_type] || '•' end end + + def counter_display(ol_type, counter) + 'aa' + end end end end From 8a5493ed3fedc151fff502f4a913a2701d54ce09 Mon Sep 17 00:00:00 2001 From: Cyrus Beer Date: Wed, 4 Jan 2023 11:46:13 -0500 Subject: [PATCH 2/5] added logging message --- lib/prawn_html/document_renderer.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index 32f5cb7..97af157 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -9,6 +9,7 @@ class DocumentRenderer # # @param pdf [PdfWrapper] target PDF wrapper def initialize(pdf) + puts 'prawnhtmlinitialize' @before_content = [] @buffer = [] @context = Context.new From 8d79def09e9ef73c50affab9b2a661b23819bf19 Mon Sep 17 00:00:00 2001 From: Cyrus Beer Date: Wed, 4 Jan 2023 13:11:55 -0500 Subject: [PATCH 3/5] Added support for ol types --- lib/prawn_html/document_renderer.rb | 1 - lib/prawn_html/tags/li.rb | 55 ++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb index 97af157..32f5cb7 100644 --- a/lib/prawn_html/document_renderer.rb +++ b/lib/prawn_html/document_renderer.rb @@ -9,7 +9,6 @@ class DocumentRenderer # # @param pdf [PdfWrapper] target PDF wrapper def initialize(pdf) - puts 'prawnhtmlinitialize' @before_content = [] @buffer = [] @context = Context.new diff --git a/lib/prawn_html/tags/li.rb b/lib/prawn_html/tags/li.rb index 08cb0ea..fbf65f7 100644 --- a/lib/prawn_html/tags/li.rb +++ b/lib/prawn_html/tags/li.rb @@ -15,9 +15,8 @@ def block? def before_content return if @before_content_once - @before_content_once = @counter ? "bb. " : "#{@symbol} " # @before_content_once = @counter ? "#{@counter}. " : "#{@symbol} " -# @before_content_once = @counter ? "#{counter_display(parent.attrs('type'), @counter)}. " : "#{@symbol} " + @before_content_once = @counter ? "#{counter_display(@counter, parent.attrs('type'))}. " : "#{@symbol} " end def block_styles @@ -37,8 +36,56 @@ def on_context_add(_context) end end - def counter_display(ol_type, counter) - 'aa' + def counter_display(counter, ol_type = '1') + case ol_type + when '1' + counter.to_s + when 'a' + to_char(counter).downcase + when 'A' + to_char(counter) + when 'i' + to_roman(counter).downcase + when 'I' + to_roman(counter) + else + counter.to_s + end + end + + def to_roman(num) + roman_arr = { + 1000 => "M", + 900 => "CM", + 500 => "D", + 400 => "CD", + 100 => "C", + 90 => "XC", + 50 => "L", + 40 => "XL", + 10 => "X", + 9 => "IX", + 5 => "V", + 4 => "IV", + 1 => "I" + } + roman_arr.reduce("") do |res, (arab, roman)| + whole_part, num = num.divmod(arab) + res << roman * whole_part + end + end + + def to_char(num) + chars = ("A".."Z").to_a + return "" if num < 1 + s = "" + q = num + loop do + q, r = (q - 1).divmod(26) + s.prepend(chars[r]) + break if q.zero? + end + s end end end From eb6362b45d490cba569351b6456d6027962a5cee Mon Sep 17 00:00:00 2001 From: Cyrus Beer Date: Wed, 4 Jan 2023 13:59:39 -0500 Subject: [PATCH 4/5] Added tests --- lib/prawn_html/tags/li.rb | 14 +++++++++----- spec/units/prawn_html/tags/li_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/prawn_html/tags/li.rb b/lib/prawn_html/tags/li.rb index fbf65f7..5592ed9 100644 --- a/lib/prawn_html/tags/li.rb +++ b/lib/prawn_html/tags/li.rb @@ -14,9 +14,9 @@ def block? def before_content return if @before_content_once - + puts "counteraaa #{@counter} parenttype #{parent.attrs.type}" # @before_content_once = @counter ? "#{@counter}. " : "#{@symbol} " - @before_content_once = @counter ? "#{counter_display(@counter, parent.attrs('type'))}. " : "#{@symbol} " + @before_content_once = @counter ? "#{counter_display(@counter, parent.attrs.type)}. " : "#{@symbol} " end def block_styles @@ -37,6 +37,7 @@ def on_context_add(_context) end def counter_display(counter, ol_type = '1') + puts "counter #{counter} olPtype #{ol_type}" case ol_type when '1' counter.to_s @@ -54,6 +55,7 @@ def counter_display(counter, ol_type = '1') end def to_roman(num) + puts "to_roman #{num}" roman_arr = { 1000 => "M", 900 => "CM", @@ -69,16 +71,18 @@ def to_roman(num) 4 => "IV", 1 => "I" } - roman_arr.reduce("") do |res, (arab, roman)| + roman_arr.reduce(+"") do |roman_res, (arab, roman)| whole_part, num = num.divmod(arab) - res << roman * whole_part + puts "res #{roman_res} roman #{roman} whole_part #{whole_part}" + roman_res << roman * whole_part end end def to_char(num) + puts "to_char #{num}" chars = ("A".."Z").to_a return "" if num < 1 - s = "" + s = +"" q = num loop do q, r = (q - 1).divmod(26) diff --git a/spec/units/prawn_html/tags/li_spec.rb b/spec/units/prawn_html/tags/li_spec.rb index 1d5ad76..e277724 100644 --- a/spec/units/prawn_html/tags/li_spec.rb +++ b/spec/units/prawn_html/tags/li_spec.rb @@ -32,6 +32,10 @@ let(:context) { instance_double(PrawnHtml::Context) } let(:parent) { PrawnHtml::Tags::Ol.new(:ol) } + let(:parent_i) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'i' }) } + let(:parent_I) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'I' }) } + let(:parent_a) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'a' }) } + let(:parent_A) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'A' }) } before do li.parent = parent @@ -41,5 +45,27 @@ it 'sets the counter in before content' do expect(before_content).to eq('1. ') end + + it 'sets the counter in before content with type i' do + li.parent = parent_i + expect(before_content).to eq('i. ') + end + + it 'sets the counter in before content with type I' do + li.parent = parent_I + expect(before_content).to eq('I. ') + end + + it 'sets the counter in before content with type a' do + li.parent = parent_a + expect(before_content).to eq('a. ') + end + + it 'sets the counter in before content with type A' do + li.parent = parent_A + expect(before_content).to eq('A. ') + end + end + end From 5180e4e8fab9e0adbd3337e8a489f48e49ca66d9 Mon Sep 17 00:00:00 2001 From: Cyrus Beer Date: Wed, 4 Jan 2023 14:42:37 -0500 Subject: [PATCH 5/5] Added tests and cleanup --- lib/prawn_html/tags/li.rb | 6 -- spec/units/prawn_html/tags/li_spec.rb | 84 +++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 11 deletions(-) diff --git a/lib/prawn_html/tags/li.rb b/lib/prawn_html/tags/li.rb index 5592ed9..eefb899 100644 --- a/lib/prawn_html/tags/li.rb +++ b/lib/prawn_html/tags/li.rb @@ -14,8 +14,6 @@ def block? def before_content return if @before_content_once - puts "counteraaa #{@counter} parenttype #{parent.attrs.type}" -# @before_content_once = @counter ? "#{@counter}. " : "#{@symbol} " @before_content_once = @counter ? "#{counter_display(@counter, parent.attrs.type)}. " : "#{@symbol} " end @@ -37,7 +35,6 @@ def on_context_add(_context) end def counter_display(counter, ol_type = '1') - puts "counter #{counter} olPtype #{ol_type}" case ol_type when '1' counter.to_s @@ -55,7 +52,6 @@ def counter_display(counter, ol_type = '1') end def to_roman(num) - puts "to_roman #{num}" roman_arr = { 1000 => "M", 900 => "CM", @@ -73,13 +69,11 @@ def to_roman(num) } roman_arr.reduce(+"") do |roman_res, (arab, roman)| whole_part, num = num.divmod(arab) - puts "res #{roman_res} roman #{roman} whole_part #{whole_part}" roman_res << roman * whole_part end end def to_char(num) - puts "to_char #{num}" chars = ("A".."Z").to_a return "" if num < 1 s = +"" diff --git a/spec/units/prawn_html/tags/li_spec.rb b/spec/units/prawn_html/tags/li_spec.rb index e277724..55dacb2 100644 --- a/spec/units/prawn_html/tags/li_spec.rb +++ b/spec/units/prawn_html/tags/li_spec.rb @@ -32,10 +32,6 @@ let(:context) { instance_double(PrawnHtml::Context) } let(:parent) { PrawnHtml::Tags::Ol.new(:ol) } - let(:parent_i) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'i' }) } - let(:parent_I) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'I' }) } - let(:parent_a) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'a' }) } - let(:parent_A) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'A' }) } before do li.parent = parent @@ -46,7 +42,12 @@ expect(before_content).to eq('1. ') end - it 'sets the counter in before content with type i' do + let(:parent_i) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'i' }) } + let(:parent_I) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'I' }) } + let(:parent_a) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'a' }) } + let(:parent_A) { PrawnHtml::Tags::Ol.new(:ol, attributes: { 'type' => 'A' }) } + + it 'sets the counter in before content with type i' do li.parent = parent_i expect(before_content).to eq('i. ') end @@ -68,4 +69,77 @@ end + describe 'before_content for different types' do + subject(:before_content) { li.before_content } + + let(:context) { instance_double(PrawnHtml::Context) } + let(:parent) { PrawnHtml::Tags::Ol.new(:ol) } + + before do + li.parent = parent + li.on_context_add(context) + end + + it 'sets the counter for type i' do + expect(li.counter_display(1, 'i')).to eq('i') + expect(li.counter_display(2, 'i')).to eq('ii') + expect(li.counter_display(3, 'i')).to eq('iii') + expect(li.counter_display(4, 'i')).to eq('iv') + expect(li.counter_display(5, 'i')).to eq('v') + expect(li.counter_display(6, 'i')).to eq('vi') + expect(li.counter_display(7, 'i')).to eq('vii') + expect(li.counter_display(8, 'i')).to eq('viii') + expect(li.counter_display(9, 'i')).to eq('ix') + expect(li.counter_display(10, 'i')).to eq('x') + end + it 'sets the counter for type I' do + expect(li.counter_display(1, 'I')).to eq('I') + expect(li.counter_display(2, 'I')).to eq('II') + expect(li.counter_display(3, 'I')).to eq('III') + expect(li.counter_display(4, 'I')).to eq('IV') + expect(li.counter_display(5, 'I')).to eq('V') + expect(li.counter_display(6, 'I')).to eq('VI') + expect(li.counter_display(7, 'I')).to eq('VII') + expect(li.counter_display(8, 'I')).to eq('VIII') + expect(li.counter_display(9, 'I')).to eq('IX') + expect(li.counter_display(10, 'I')).to eq('X') + end + it 'sets the counter for type A' do + expect(li.counter_display(1, 'A')).to eq('A') + expect(li.counter_display(2, 'A')).to eq('B') + expect(li.counter_display(3, 'A')).to eq('C') + expect(li.counter_display(4, 'A')).to eq('D') + expect(li.counter_display(5, 'A')).to eq('E') + expect(li.counter_display(6, 'A')).to eq('F') + expect(li.counter_display(7, 'A')).to eq('G') + expect(li.counter_display(8, 'A')).to eq('H') + expect(li.counter_display(9, 'A')).to eq('I') + expect(li.counter_display(10, 'A')).to eq('J') + end + it 'sets the counter for type a' do + expect(li.counter_display(1, 'a')).to eq('a') + expect(li.counter_display(2, 'a')).to eq('b') + expect(li.counter_display(3, 'a')).to eq('c') + expect(li.counter_display(4, 'a')).to eq('d') + expect(li.counter_display(5, 'a')).to eq('e') + expect(li.counter_display(6, 'a')).to eq('f') + expect(li.counter_display(7, 'a')).to eq('g') + expect(li.counter_display(8, 'a')).to eq('h') + expect(li.counter_display(9, 'a')).to eq('i') + expect(li.counter_display(10, 'a')).to eq('j') + end + it 'sets the counter for type 1' do + expect(li.counter_display(1)).to eq('1') + expect(li.counter_display(2)).to eq('2') + expect(li.counter_display(3)).to eq('3') + expect(li.counter_display(4)).to eq('4') + expect(li.counter_display(5)).to eq('5') + expect(li.counter_display(6)).to eq('6') + expect(li.counter_display(7)).to eq('7') + expect(li.counter_display(8)).to eq('8') + expect(li.counter_display(9)).to eq('9') + expect(li.counter_display(10)).to eq('10') + end + + end end