Skip to content

Book#render patches and sub-section headings #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/bookie/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/bookie/emitters/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def build_section_heading(header)
@body << "<h2>#{header.contents}</h2>"
end

def build_sub_section_heading(header)
@body << "<h3>#{header.contents}</h3>"
end

def build_list(list)
list_elements = list.contents.map { |li| "<li>#{li}</li>" }.join
@body << "<ul>"+list_elements+"</ul>"
Expand Down
3 changes: 3 additions & 0 deletions lib/bookie/emitters/null.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions lib/bookie/emitters/pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 15 additions & 6 deletions lib/bookie/parser.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand Down