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!(/</, "&lt;")
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!(/\[i\](.+?)\[\/i\]/i, '<em>\1</em>')
str.gsub!(/\[s\](.+?)\[\/s\]/i, '<s>\1</s>')
@@ -51,6 +51,12 @@ class DText
str
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)
str.gsub(/("[^"]+":(https?:\/\/|\/)[^\s\r\n<>]+|https?:\/\/[^\s\r\n<>]+)+/) do |url|
if url =~ /^"([^"]+)":(.+)$/
@@ -165,6 +171,8 @@ class DText
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]\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
str.gsub!(/(?:\r?\n){3,}/, "\n\n")
@@ -218,6 +226,20 @@ class DText
""
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\](?!\])/
flags[:code] = true
stack << "pre"
@@ -249,6 +271,8 @@ class DText
else
if flags[:code]
CGI.escape_html(block) + "\n\n"
elsif flags[:table]
parse_table_elements(block)
else
'<p>' + parse_inline(block) + '</p>'
end
@@ -266,6 +290,8 @@ class DText
html << "</div>"
elsif tag == "expandable"
html << "</div></div>"
elsif tag == "table"
html << "</table>"
end
end

View File

@@ -160,4 +160,8 @@ class DTextTest < ActiveSupport::TestCase
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]"))
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