diff --git a/lib/bookie/book.rb b/lib/bookie/book.rb index 53ef59d..68ea904 100644 --- a/lib/bookie/book.rb +++ b/lib/bookie/book.rb @@ -14,7 +14,11 @@ 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 + 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) 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 << "" 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]