From 331377a32b70eb3fa9b63a0a6d3ce7b85462a6b6 Mon Sep 17 00:00:00 2001 From: r888888888 Date: Tue, 3 Jun 2014 15:23:16 -0700 Subject: [PATCH] implements #982 --- app/logical/d_text.rb | 28 +++++++++++++++++++++++++++- test/unit/dtext_test.rb | 4 ++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/logical/d_text.rb b/app/logical/d_text.rb index b1cb7a5bc..75569161f 100644 --- a/app/logical/d_text.rb +++ b/app/logical/d_text.rb @@ -36,7 +36,7 @@ class DText str.gsub!(/&/, "&") str.gsub!(//, ">") - str.gsub!(/\n/m, "
") + str.gsub!(/\n/m, "
") unless options[:ignore_newlines] str.gsub!(/\[b\](.+?)\[\/b\]/i, '\1') str.gsub!(/\[i\](.+?)\[\/i\]/i, '\1') str.gsub!(/\[s\](.+?)\[\/s\]/i, '\1') @@ -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 + '' + + when "[/table]" + if stack.last == "table" + stack.pop + flags[:table] = false + "
" + 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 '

' + parse_inline(block) + '

' end @@ -266,6 +290,8 @@ class DText html << "" elsif tag == "expandable" html << "" + elsif tag == "table" + html << "" end end diff --git a/test/unit/dtext_test.rb b/test/unit/dtext_test.rb index 4fbde0e30..b1eb0f2e5 100644 --- a/test/unit/dtext_test.rb +++ b/test/unit/dtext_test.rb @@ -160,4 +160,8 @@ class DTextTest < ActiveSupport::TestCase def test_complex_links_2 assert_equal("

Tags (Tagging Guidelines | Tag Checklist | Tag Groups)

", p("Tags [b]([[howto:tag|Tagging Guidelines]] | [[howto:tag_checklist|Tag Checklist]] | [[Tag Groups]])[/b]")) end + + def test_table + assert_equal("
header
post #100
", p("[table][thead][tr][th]header[/th][/tr][/thead][tbody][tr][td]post #100[/td][/tr][/tbody][/table]")) + end end