Module: TXT

Extended by:
TXT
Included in:
TXT
Defined in:
lib/txt.rb

Instance Method Summary collapse

Instance Method Details

#clean_forward_blank(content) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/txt.rb', line 20

def clean_forward_blank(content)
  begin
    content = content.gsub("\r","")
  rescue 
    content = Utils.to_utf8(content)
    if content.nil?
      return ''
    else
      content = content.gsub("\r","")
    end
  end
  lines = content.split(/\n/)
  
  while line = lines.shift
    break if line.present?
  end

  if line.present?
    lines.unshift(line)   
    lines.join("\n")
  else
    ""
  end
end

#extract_book_part(filename) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/txt.rb', line 7

def extract_book_part(filename)
  content = File.open(filename).read

  return nil,nil if content.nil?

  content = clean_forward_blank(content)

  title,content = extract_title_and_content(content,:title=>File.basename(filename,'.txt'))
  outlines,content = extract_outlines_and_content(content)

  [title,outlines,content]
end

#extract_outlines_and_content(content) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/txt.rb', line 57

def extract_outlines_and_content(content)
  lines = content.split(/\n/)
  outlines = []
  while line = lines.shift
    if HeaderDetect.guess_header?(line)
      outlines << line
    else
      break
    end
  end

  lines.unshift(line) if line.present?

  if outlines.count > 1
    [outlines.join("\n"),lines.join("\n")]
  else
    content = (outlines + lines).join("\n")
    [nil,content]
  end
end

#extract_title_and_content(content, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/txt.rb', line 45

def extract_title_and_content(content,options={})
  title = options[:title] || ''
  lines = content.split("\n")
  item = lines.shift
  if Utils.text_similarity(item,title) > 0.8
    title = item
  else
    lines.unshift(item)
  end
  [title, lines.join("\n")]
end

#gen_html_from_txt_book(title, outlines, content, options = {}) ⇒ Object



78
79
80
81
82
# File 'lib/txt.rb', line 78

def gen_html_from_txt_book(title,outlines,content,options={})
  html = "<h1>#{title}</h1>"
  html = html + gen_html_from_txt_outlines(outlines,options)
  html = html + gen_html_from_txt_content(content,options)
end

#gen_html_from_txt_content(content, options = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/txt.rb', line 95

def gen_html_from_txt_content(content,options={})
  row_index = 0
  html = content.split("\n").map do |line| 
    row_index += 1
    if line.present? 
      if HeaderDetect.guess_header?(line)
        "<h2 id='#{row_index}'>#{Utils.escape_html(Utils.clean_text(line))}</h2>"
      else
        "<p class='division'>#{Utils.escape_html(Utils.clean_text(line))}</p>" 
      end
    end
  end.compact.join("")
end

#gen_html_from_txt_outlines(outlines, options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/txt.rb', line 84

def gen_html_from_txt_outlines(outlines,options={})
  if outlines.present?
    html = outlines.split("\n").map{|item|
      "<li>#{item}</li>"
    }
    "<ol class='outlines'>#{html}</ol>"
  else
    ''
  end
end