require 'cgi' require 'nokogiri' module DText def parse(str) state = ['newline'] result = "" # Normalize newlines str.strip str.gsub!(/(\r?\n)/, "\n") str.gsub!(/\n{3,}/, "\n\n") # Keep newline, use carriage return for split str.gsub!(/(\n+)/, '\1' + "\r") data = str.split("\r") data.each do |d| result << parseline(d, state) end Nokogiri::HTML::DocumentFragment.parse(result).to_html end def parseinline(str) str = CGI.escapeHTML str parseurl str str.gsub! /\[b\](.+)\[\/b\]/, '\1' str.gsub! /\[i\](.+)\[\/i\]/, '\1' str.gsub! /[Pp]ost #(\d+)/, 'post #\1' str.gsub! /[Ff]orum #(\d+)/, 'forum #\1' str.gsub! /[Cc]omment #(\d+)/, 'comment #\1' str.gsub! /[Pp]ool #(\d+)/, 'pool #\1' str.gsub! /\[spoilers?\]/, 'spoiler