Module: Writetheman::Article::Utils

Included in:
Base
Defined in:
lib/writetheman/article/utils.rb

Constant Summary collapse

REPLACE_CHARACTER =
"|"

Class Method Summary collapse

Class Method Details

.encoding_from_windows(content) ⇒ Object

if Windows-31J characters (don’t need it with rich editor) ex problem of encoding : www.weeklystandard.com/articles/silicon-chasm_768037.html solution from stackoverflow.com/questions/225471/how-do-i-replace-accented-latin-characters-in-ruby



16
17
18
# File 'lib/writetheman/article/utils.rb', line 16

def self.encoding_from_windows(content)
  return ActiveSupport::Multibyte::Chars.new(content).mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').to_s
end

.format_content_from_file(content) ⇒ Object



25
26
27
# File 'lib/writetheman/article/utils.rb', line 25

def self.format_content_from_file(content)
  content = special_encoding(content.force_encoding('utf-8')).gsub("\r", "")
end

.format_readable_html(content) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/writetheman/article/utils.rb', line 30

def self.format_readable_html(content)
  content.gsub("<br><h2>", "<h2>").gsub("<div><br></div><h1>", "<h1>")
    .gsub("><div>", "> \n<div>").gsub("</div><", "</div> \n<")
    .gsub("><p>", "> \n<p>").gsub("</p><", "</p> \n<")
    .gsub("><br>", "> \n<br> \n").gsub(".<br>", ". \n<br> \n").gsub("<br >", "<br>")
    .gsub("<br></h1>", "</h1>").gsub("<br></h2>", "</h2>")
    .gsub("  ", "&nbsp;&nbsp;").gsub("&amp;amp;lt;", "<").gsub("<p> </p>", "<p>&nbsp;</p>")
end

.regex_body_from_content(content) ⇒ Object



39
40
41
42
# File 'lib/writetheman/article/utils.rb', line 39

def self.regex_body_from_content(content)        
  content = format_content_from_file(content)
  content.match(/$(\n---\n)\s*$(.*)/ms).to_s.gsub("\n---\n", "")
end

.regex_header_from_content(content) ⇒ Object



7
8
9
10
11
# File 'lib/writetheman/article/utils.rb', line 7

def self.regex_header_from_content(content)
  #content = format_multibyte(content)
  content.gsub("\n", REPLACE_CHARACTER).match(/---(.*)---/).to_s.gsub(REPLACE_CHARACTER, "\n")
    .gsub("\r", "").gsub("---\n", "").gsub("\n---", "").gsub("---", "")
end

.special_encoding(content) ⇒ Object



20
21
22
23
# File 'lib/writetheman/article/utils.rb', line 20

def self.special_encoding(content)
  content = encoding_from_windows(content)
  content = format_readable_html(content)
end