* Continued work on improving post view templates

* Added statistics-based estimator for related tag calculator
* Fleshed out IpBan class based on changes to Danbooru 1.xx
This commit is contained in:
albert
2010-04-29 17:32:15 -04:00
parent 3666364469
commit 23656e3fa9
39 changed files with 816 additions and 86 deletions

View File

@@ -104,6 +104,10 @@ class AnonymousUser
"Eastern Time (US & Canada)"
end
def default_image_size
"medium"
end
%w(member banned privileged contributor janitor moderator admin).each do |name|
define_method("is_#{name}?") do
false

138
app/logical/d_text.rb Normal file
View File

@@ -0,0 +1,138 @@
require 'cgi'
class DText
def self.parse_inline(str, options = {})
str = str.gsub(/&/, "&")
str.gsub!(/</, "&lt;")
str.gsub!(/>/, "&gt;")
str.gsub!(/\[\[.+?\]\]/m) do |tag|
tag = tag[2..-3]
if tag =~ /^(.+?)\|(.+)$/
tag = $1
name = $2
'<a href="/wiki/show?title=' + CGI.escape(CGI.unescapeHTML(tag.tr(" ", "_").downcase)) + '">' + name + '</a>'
else
'<a href="/wiki/show?title=' + CGI.escape(CGI.unescapeHTML(tag.tr(" ", "_").downcase)) + '">' + tag + '</a>'
end
end
str.gsub!(/\{\{.+?\}\}/m) do |tag|
tag = tag[2..-3]
'<a href="/post/index?tags=' + CGI.escape(CGI.unescapeHTML(tag)) + '">' + tag + '</a>'
end
str.gsub!(/[Pp]ost #(\d+)/, '<a href="/post/show/\1">post #\1</a>')
str.gsub!(/[Ff]orum #(\d+)/, '<a href="/forum/show/\1">forum #\1</a>')
str.gsub!(/[Cc]omment #(\d+)/, '<a href="/comment/show/\1">comment #\1</a>')
str.gsub!(/[Pp]ool #(\d+)/, '<a href="/pool/show/\1">pool #\1</a>')
str.gsub!(/\n/m, "<br>")
str.gsub!(/\[b\](.+?)\[\/b\]/, '<strong>\1</strong>')
str.gsub!(/\[i\](.+?)\[\/i\]/, '<em>\1</em>')
str.gsub!(/\[spoilers?\](.+?)\[\/spoilers?\]/m, '<span class="spoiler">\1</span>')
str.gsub!(/("[^"]+":(http:\/\/|\/)\S+|http:\/\/\S+)/m) do |link|
if link =~ /^"([^"]+)":(.+)$/
text = $1
link = $2
else
text = link
end
if link =~ /([;,.!?\)\]<>])$/
link.chop!
ch = $1
else
ch = ""
end
link.gsub!(/"/, '&quot;')
'<a href="' + link + '">' + text + '</a>' + ch
end
str
end
def self.parse_list(str, options = {})
html = ""
layout = []
nest = 0
str.split(/\n/).each do |line|
if line =~ /^\s*(\*+) (.+)/
nest = $1.size
content = parse_inline($2)
else
content = parse_inline(line)
end
if nest > layout.size
html += "<ul>"
layout << "ul"
end
while nest < layout.size
elist = layout.pop
if elist
html += "</#{elist}>"
end
end
html += "<li>#{content}</li>"
end
while layout.any?
elist = layout.pop
html += "</#{elist}>"
end
html
end
def self.parse(str, options = {})
return "" if str.blank?
# Make sure quote tags are surrounded by newlines
unless options[:inline]
str.gsub!(/\s*\[quote\]\s*/m, "\n\n[quote]\n\n")
str.gsub!(/\s*\[\/quote\]\s*/m, "\n\n[/quote]\n\n")
end
str.gsub!(/(?:\r?\n){3,}/, "\n\n")
str.strip!
blocks = str.split(/(?:\r?\n){2}/)
html = blocks.map do |block|
case block
when /^(h[1-6])\.\s*(.+)$/
tag = $1
content = $2
if options[:inline]
"<h6>" + parse_inline(content, options) + "</h6>"
else
"<#{tag}>" + parse_inline(content, options) + "</#{tag}>"
end
when /^\s*\*+ /
parse_list(block, options)
when "[quote]"
if options[:inline]
""
else
'<blockquote>'
end
when "[/quote]"
if options[:inline]
""
else
'</blockquote>'
end
else
'<p>' + parse_inline(block) + "</p>"
end
end
html.join("").html_safe
end
end

View File

@@ -1,26 +1,34 @@
class RelatedTagCalculator
def find_tags(tag, limit)
ActiveRecord::Base.select_values_sql("SELECT tag_string FROM posts WHERE tag_index @@ to_tsquery('danbooru', ?) ORDER BY id DESC LIMIT ?", tag, limit)
Post.find_by_tags(tag, :limit => limit, :select => "posts.tag_string", :order => "posts.md5").map(&:tag_string)
end
def calculate_from_sample(name, limit, category_constraint = nil)
counts = Hash.new {|h, k| h[k] = 0}
case category_constraint
when Tag.categories.artist
limit *= 5
when Tag.categories.copyright
limit *= 4
when Tag.categories.character
limit *= 3
end
find_tags(name, limit).each do |tags|
tag_array = Tag.scan_tags(tags)
if category_constraint
categories = Tag.categories_for(tag_array)
tag_array.each do |tag|
if categories[tag] == category_constraint && tag != name
category = Tag.category_for(tag)
if category == category_constraint
counts[tag] += 1
end
end
else
tag_array.each do |tag|
if tag != name
counts[tag] += 1
end
counts[tag] += 1
end
end
end
@@ -28,7 +36,11 @@ class RelatedTagCalculator
counts
end
def convert_hash_to_array(hash)
hash.to_a.sort_by {|x| -x[1]}.slice(0, 25)
end
def convert_hash_to_string(hash)
hash.to_a.sort_by {|x| -x[1]}.flatten.join(" ")
convert_hash_to_array(hash).flatten.join(" ")
end
end