Module: Ducky::Format

Extended by:
Format
Included in:
Format
Defined in:
lib/ducky/format.rb

Instance Method Summary collapse

Instance Method Details

#format(information) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ducky/format.rb', line 6

def format(information)
  out = ""

  information[:head].each do |head|
    out << head << "\n"
  end

  body = ""
  information[:body].children.each do |child|
    content = word_wrap(child.content.gsub(/\s+/, " "))
    case child.name
    when "comment", "div"
    when "text"
      body << content
    when "p"
      body << content << "\n"
    when /h[1-6]/
      body << "\n\n" << content << "\n"
    when "pre"
      body << "\n\n    " <<
        child.content.split("\n").join("\n    ") << "\n\n"
    end
  end

  out << "\n\n" << body.strip << "\n\n" << information[:footer]
end

#word_wrap(text, options = {}) ⇒ Object

thanks, Rails!



34
35
36
37
38
39
40
# File 'lib/ducky/format.rb', line 34

def word_wrap(text, options = {})
  line_width = options.fetch(:line_width, 80)

  text.split("\n").collect! do |line|
    line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
  end * "\n"
end