module Writetheman::Article::Utils

Constants

REPLACE_CHARACTER

Public Class Methods

encoding_from_windows(content) click to toggle source

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

# 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) click to toggle source
# 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) click to toggle source

@todo : move in the rails admin (format from the editor)

# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/writetheman/article/utils.rb, line 20
def self.special_encoding(content)
  content = encoding_from_windows(content)
  content = format_readable_html(content)
end