Class: Nsf::Heading

Inherits:
Object show all
Defined in:
lib/nsf.rb,
lib/nsf/formats/nsf.rb,
lib/nsf/formats/pdf.rb,
lib/nsf/formats/rtf.rb,
lib/nsf/formats/html.rb

Constant Summary collapse

PDF_FONT_RATIOS =
[2.22222, 1.66666, 1.33333]
PDF_HEADING_LEADING =
0.6

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, level) ⇒ Heading

Returns a new instance of Heading.



63
64
65
66
# File 'lib/nsf.rb', line 63

def initialize(text, level)
  @text = text
  @level = level
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



61
62
63
# File 'lib/nsf.rb', line 61

def level
  @level
end

#textObject (readonly)

Returns the value of attribute text.



61
62
63
# File 'lib/nsf.rb', line 61

def text
  @text
end

Class Method Details

.from_nsf(text) ⇒ Object



68
69
70
71
# File 'lib/nsf.rb', line 68

def self.from_nsf(text)
  text =~ /^(#+) (.*?)$/
  self.new($2, $1.length)  
end

Instance Method Details

#ref_htmlObject



168
169
170
# File 'lib/nsf/formats/html.rb', line 168

def ref_html
  "heading_#{level}_#{CGI.escapeHTML(text)}"
end

#to_htmlObject



164
165
166
# File 'lib/nsf/formats/html.rb', line 164

def to_html
  "<h#{level} id=\"#{ref_html}\">#{CGI.escapeHTML(text)}</h#{level}>"
end

#to_nsfObject



31
32
33
# File 'lib/nsf/formats/nsf.rb', line 31

def to_nsf
  "#{"#" * level} #{text}"
end

#to_pdf(pdf) ⇒ Object



40
41
42
43
44
# File 'lib/nsf/formats/pdf.rb', line 40

def to_pdf(pdf)
  size = (PDF_FONT_RATIOS[level - 1] * pdf.font_size).round
  pdf.text text, :size => size, :leading => (size * Document::PDF_LEADING).round, :style => :bold
  pdf.move_down (size * PDF_HEADING_LEADING).round
end

#to_rtfObject



65
66
67
# File 'lib/nsf/formats/rtf.rb', line 65

def to_rtf
  @text.gsub("\n", " ")
end