From b416ec0a5591c923cefd29cf40e009293bc2d493 Mon Sep 17 00:00:00 2001 From: Shane Emmons Date: Tue, 7 Jun 2011 09:06:19 -0400 Subject: [PATCH 1/4] allow a single Emitter to be passed to Book#render --- lib/bookie/book.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/bookie/book.rb b/lib/bookie/book.rb index 53ef59d..3d97418 100644 --- a/lib/bookie/book.rb +++ b/lib/bookie/book.rb @@ -14,6 +14,8 @@ def chapter(name, file) # FIXME: This is inefficient, it should be possible to fire up the parser # just once with many emitters. def render(basename, emitters) + emitters = [emitters] unless emitters.is_a? Array + emitters.each do |emitter| chapters.each_with_index do |(name, file), i| emitter.start_new_chapter(header: "Chapter #{i+1}", From 72c52b4174255f86e100a7d9a63a8d007c178af7 Mon Sep 17 00:00:00 2001 From: Shane Emmons Date: Tue, 7 Jun 2011 09:10:12 -0400 Subject: [PATCH 2/4] call .new on emitter if a Class is passed instead of an instance to Book#render --- lib/bookie/book.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/bookie/book.rb b/lib/bookie/book.rb index 3d97418..823f0b5 100644 --- a/lib/bookie/book.rb +++ b/lib/bookie/book.rb @@ -17,6 +17,8 @@ def render(basename, emitters) emitters = [emitters] unless emitters.is_a? Array emitters.each do |emitter| + emitter = emitter.new if emitter.is_a? Class + chapters.each_with_index do |(name, file), i| emitter.start_new_chapter(header: "Chapter #{i+1}", title: name) From 4e6450cf4d73c9d16661deee1eff6e38a7eeaead Mon Sep 17 00:00:00 2001 From: Shane Emmons Date: Tue, 7 Jun 2011 09:36:50 -0400 Subject: [PATCH 3/4] add sub-section heading support --- lib/bookie/emitters/html.rb | 4 ++++ lib/bookie/emitters/null.rb | 3 +++ lib/bookie/emitters/pdf.rb | 16 ++++++++++++++++ lib/bookie/parser.rb | 21 +++++++++++++++------ 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/bookie/emitters/html.rb b/lib/bookie/emitters/html.rb index 11dcf71..c08f10b 100644 --- a/lib/bookie/emitters/html.rb +++ b/lib/bookie/emitters/html.rb @@ -38,6 +38,10 @@ def build_section_heading(header) @body << "

#{header.contents}

" end + def build_sub_section_heading(header) + @body << "

#{header.contents}

" + end + def build_list(list) list_elements = list.contents.map { |li| "
  • #{li}
  • " }.join @body << "
      "+list_elements+"
    " diff --git a/lib/bookie/emitters/null.rb b/lib/bookie/emitters/null.rb index 1f88e47..32dc060 100644 --- a/lib/bookie/emitters/null.rb +++ b/lib/bookie/emitters/null.rb @@ -10,6 +10,9 @@ def build_raw_text(raw_text) def build_section_heading(header) end + def build_sub_section_heading(header) + end + def build_list(content) end end diff --git a/lib/bookie/emitters/pdf.rb b/lib/bookie/emitters/pdf.rb index 8578c92..45da4ad 100644 --- a/lib/bookie/emitters/pdf.rb +++ b/lib/bookie/emitters/pdf.rb @@ -66,6 +66,22 @@ def build_section_heading(section_text) end end + def build_sub_section_heading(section_text) + draw do + start_new_page unless cursor > in2pt(0.4) + + move_down in2pt(0.1) + + float do + font("sans", :style => :bold, :size => 10) do + text(section_text.contents.strip) + end + end + + move_down in2pt(0.3) + end + end + def build_paragraph(paragraph) para_text = convert_inlines(paragraph.contents) diff --git a/lib/bookie/parser.rb b/lib/bookie/parser.rb index a1506c8..e58ac9c 100644 --- a/lib/bookie/parser.rb +++ b/lib/bookie/parser.rb @@ -1,10 +1,11 @@ module Bookie - Paragraph = Struct.new(:contents) - RawText = Struct.new(:contents) - SectionHeading = Struct.new(:contents) - List = Struct.new(:contents) - NormalText = Struct.new(:contents) - CodeText = Struct.new(:contents) + Paragraph = Struct.new(:contents) + RawText = Struct.new(:contents) + SectionHeading = Struct.new(:contents) + SubSectionHeading = Struct.new(:contents) + List = Struct.new(:contents) + NormalText = Struct.new(:contents) + CodeText = Struct.new(:contents) class Parser def self.parse(raw_data, emitter=Bookie::Emitters::Null.new) @@ -56,6 +57,12 @@ def extract_section_heading(contents) parsed_content << header end + def extract_sub_section_heading(contents) + header = SubSectionHeading.new(contents.chomp) + @emitter.build_sub_section_heading(header) + parsed_content << header + end + def extract_list(contents) list = List.new(contents.map { |e| e.chomp }) @emitter.build_list(list) @@ -75,6 +82,8 @@ def parse_contents(raw_data) case line when /^## / extract_section_heading(line[3..-1]) + when /^### / + extract_sub_section_heading(line[4..-1]) when /^ {4,}/ mode = :raw chunk = line[4..-1] From a25cb42b798fe9d527326d99d6e0d62699076a73 Mon Sep 17 00:00:00 2001 From: Shane Emmons Date: Sat, 6 Aug 2011 20:29:29 -0300 Subject: [PATCH 4/4] better idiom via Greg --- lib/bookie/book.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/bookie/book.rb b/lib/bookie/book.rb index 823f0b5..68ea904 100644 --- a/lib/bookie/book.rb +++ b/lib/bookie/book.rb @@ -14,7 +14,7 @@ def chapter(name, file) # FIXME: This is inefficient, it should be possible to fire up the parser # just once with many emitters. def render(basename, emitters) - emitters = [emitters] unless emitters.is_a? Array + emitters = *emitters emitters.each do |emitter| emitter = emitter.new if emitter.is_a? Class