Class: Habari2md::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/habari2md.rb

Class Method Summary collapse

Class Method Details

.html2text(content) ⇒ Object

Fork (!) html2text.py to convert form HTML to Markdown.

Parameters:

  • content (String)


31
32
33
34
35
36
37
38
39
# File 'lib/habari2md.rb', line 31

def self.html2text(content)
  IO.popen(html2text_script, "r+") do |io|
    io.write content
    io.close_write
    content = io.read
    io.close_read
  end
  content
end

.simple_format(text) ⇒ String

Shameless snatch from Rails.

Parameters:

  • text (String)

Returns:

  • (String)


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/habari2md.rb', line 14

def self.simple_format(text)
  text = '' if text.nil?
  text = text.dup
  start_tag = '<p>'
  text = text.to_str
  text.gsub!(/\r\n?/, "\n")                    # \r\n and \r -> \n
  text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}")  # 2+ newline  -> paragraph
  text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline   -> br
  text.insert(0, start_tag)
  text << '</p>'
  return text
end