This commit is contained in:
r888888888
2014-06-03 15:23:16 -07:00
parent d019389043
commit 331377a32b
2 changed files with 31 additions and 1 deletions

View File

@@ -36,7 +36,7 @@ class DText
str.gsub!(/&/, "&") str.gsub!(/&/, "&")
str.gsub!(/</, "&lt;") str.gsub!(/</, "&lt;")
str.gsub!(/>/, "&gt;") str.gsub!(/>/, "&gt;")
str.gsub!(/\n/m, "<br>") str.gsub!(/\n/m, "<br>") unless options[:ignore_newlines]
str.gsub!(/\[b\](.+?)\[\/b\]/i, '<strong>\1</strong>') str.gsub!(/\[b\](.+?)\[\/b\]/i, '<strong>\1</strong>')
str.gsub!(/\[i\](.+?)\[\/i\]/i, '<em>\1</em>') str.gsub!(/\[i\](.+?)\[\/i\]/i, '<em>\1</em>')
str.gsub!(/\[s\](.+?)\[\/s\]/i, '<s>\1</s>') str.gsub!(/\[s\](.+?)\[\/s\]/i, '<s>\1</s>')
@@ -51,6 +51,12 @@ class DText
str str
end end
def self.parse_table_elements(str)
str = parse_inline(str, :ignore_newlines => true)
str.gsub!(/\[(\/?(?:tr|td|th|thead|tbody))\]/, '<\1>')
str
end
def self.parse_links(str) def self.parse_links(str)
str.gsub(/("[^"]+":(https?:\/\/|\/)[^\s\r\n<>]+|https?:\/\/[^\s\r\n<>]+)+/) do |url| str.gsub(/("[^"]+":(https?:\/\/|\/)[^\s\r\n<>]+|https?:\/\/[^\s\r\n<>]+)+/) do |url|
if url =~ /^"([^"]+)":(.+)$/ if url =~ /^"([^"]+)":(.+)$/
@@ -165,6 +171,8 @@ class DText
str.gsub!(/^(h[1-6]\.\s*.+)$/, "\n\n\\1\n\n") str.gsub!(/^(h[1-6]\.\s*.+)$/, "\n\n\\1\n\n")
str.gsub!(/\s*\[expand(\=[^\]]*)?\]\s*/m, "\n\n[expand\\1]\n\n") str.gsub!(/\s*\[expand(\=[^\]]*)?\]\s*/m, "\n\n[expand\\1]\n\n")
str.gsub!(/\s*\[\/expand\]\s*/m, "\n\n[/expand]\n\n") str.gsub!(/\s*\[\/expand\]\s*/m, "\n\n[/expand]\n\n")
str.gsub!(/\s*\[table\]\s*/m, "\n\n[table]\n\n")
str.gsub!(/\s*\[\/table\]\s*/m, "\n\n[/table]\n\n")
end end
str.gsub!(/(?:\r?\n){3,}/, "\n\n") str.gsub!(/(?:\r?\n){3,}/, "\n\n")
@@ -218,6 +226,20 @@ class DText
"" ""
end end
when "[table]"
stack << "table"
flags[:table] = true
'<table class="striped">'
when "[/table]"
if stack.last == "table"
stack.pop
flags[:table] = false
"</table>"
else
""
end
when /\[code\](?!\])/ when /\[code\](?!\])/
flags[:code] = true flags[:code] = true
stack << "pre" stack << "pre"
@@ -249,6 +271,8 @@ class DText
else else
if flags[:code] if flags[:code]
CGI.escape_html(block) + "\n\n" CGI.escape_html(block) + "\n\n"
elsif flags[:table]
parse_table_elements(block)
else else
'<p>' + parse_inline(block) + '</p>' '<p>' + parse_inline(block) + '</p>'
end end
@@ -266,6 +290,8 @@ class DText
html << "</div>" html << "</div>"
elsif tag == "expandable" elsif tag == "expandable"
html << "</div></div>" html << "</div></div>"
elsif tag == "table"
html << "</table>"
end end
end end

View File

@@ -160,4 +160,8 @@ class DTextTest < ActiveSupport::TestCase
def test_complex_links_2 def test_complex_links_2
assert_equal("<p>Tags <strong>(<a href=\"/wiki_pages/show_or_new?title=howto%3Atag\">Tagging Guidelines</a> | <a href=\"/wiki_pages/show_or_new?title=howto%3Atag_checklist\">Tag Checklist</a> | <a href=\"/wiki_pages/show_or_new?title=tag_groups\">Tag Groups</a>)</strong></p>", p("Tags [b]([[howto:tag|Tagging Guidelines]] | [[howto:tag_checklist|Tag Checklist]] | [[Tag Groups]])[/b]")) assert_equal("<p>Tags <strong>(<a href=\"/wiki_pages/show_or_new?title=howto%3Atag\">Tagging Guidelines</a> | <a href=\"/wiki_pages/show_or_new?title=howto%3Atag_checklist\">Tag Checklist</a> | <a href=\"/wiki_pages/show_or_new?title=tag_groups\">Tag Groups</a>)</strong></p>", p("Tags [b]([[howto:tag|Tagging Guidelines]] | [[howto:tag_checklist|Tag Checklist]] | [[Tag Groups]])[/b]"))
end end
def test_table
assert_equal("<table class=\"striped\"><thead><tr><th>header</th></tr></thead><tbody><tr><td><a href=\"/posts/100\">post #100</a></td></tr></tbody></table>", p("[table][thead][tr][th]header[/th][/tr][/thead][tbody][tr][td]post #100[/td][/tr][/tbody][/table]"))
end
end end