Skip to content

Commit

Permalink
fix node shared regex (#3815)
Browse files Browse the repository at this point in the history
* fix node shared regex

* add test
  • Loading branch information
kaustubh-nair authored and jywarren committed Oct 31, 2018
1 parent fc10d69 commit af295d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
20 changes: 10 additions & 10 deletions app/models/concerns/node_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def liked_by(uid)

# rubular regex: http://rubular.com/r/hBEThNL4qd
def self.graph_grid(body, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[graph\:(\S+)\]/) do |_tagname|
body.gsub(/(?<![\>`])(\<p\>)?\[graph\:(\S+)\]/) do |_tagname|
url = Regexp.last_match(2)
a = ActionController::Base.new
randomSeed = rand(1000).to_s
Expand All @@ -29,7 +29,7 @@ def self.graph_grid(body, _page = 1)

# rubular regex: http://rubular.com/r/hBEThNL4qd
def self.notes_grid(body, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[notes\:(\S+)\]/) do |_tagname|
body.gsub(/(?<![\>`])(\<p\>)?\[notes\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2)
exclude = nil
if tagname.include?('!')
Expand Down Expand Up @@ -68,7 +68,7 @@ def self.notes_grid(body, _page = 1)

# rubular regex: http://rubular.com/r/hBEThNL4qd
def self.questions_grid(body, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[questions\:(\S+)\]/) do |_tagname|
body.gsub(/(?<![\>`])(\<p\>)?\[questions\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2)
exclude = nil
if tagname.include?('!')
Expand Down Expand Up @@ -104,7 +104,7 @@ def self.questions_grid(body, _page = 1)
end

def self.activities_grid(body)
body.gsub(/[^\>`](\<p\>)?\[activities\:(\S+)\]/) do |_tagname|
body.gsub(/(?<![\>`])(\<p\>)?\[activities\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2)
exclude = nil
if tagname.include?('!')
Expand Down Expand Up @@ -137,7 +137,7 @@ def self.activities_grid(body)
end

def self.upgrades_grid(body)
body.gsub(/[^\>`](\<p\>)?\[upgrades\:(\S+)\]/) do |_tagname|
body.gsub(/(?<![\>`])(\<p\>)?\[upgrades\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2)
exclude = nil
if tagname.include?('!')
Expand Down Expand Up @@ -172,7 +172,7 @@ def self.upgrades_grid(body)

# Blank map loaded only , markers will be loaded using API call .
def self.notes_map(body)
body.gsub(/[^\>`](\<p\>)?\[map\:content\:(\S+)\:(\S+)\]/) do |_tagname|
body.gsub(/(?<![\>`])(\<p\>)?\[map\:content\:(\S+)\:(\S+)\]/) do |_tagname|
lat = Regexp.last_match(2)
lon = Regexp.last_match(3)
a = ActionController::Base.new
Expand All @@ -187,7 +187,7 @@ def self.notes_map(body)
end

def self.notes_map_by_tag(body)
body.gsub(/[^\>`](\<p\>)?\[map\:tag\:(\S+)\:(\S+)\:(\S+)\]/) do |_tagname|
body.gsub(/(?<![\>`])(\<p\>)?\[map\:tag\:(\S+)\:(\S+)\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2)
lat = Regexp.last_match(3)
lon = Regexp.last_match(4)
Expand Down Expand Up @@ -218,7 +218,7 @@ def self.notes_map_by_tag(body)

# in our interface, "users" are known as "people" because it's more human
def self.people_map(body, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[map\:people\:(\S+)\:(\S+)\]/) do |_tagname|
body.gsub(/(?<![\>`])(\<p\>)?\[map\:people\:(\S+)\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2)
lat = Regexp.last_match(2)
lon = Regexp.last_match(3)
Expand All @@ -238,7 +238,7 @@ def self.people_map(body, _page = 1)

# in our interface, "users" are known as "people" because it's more human
def self.people_grid(body, current_user = nil, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[people\:(\S+)\]/) do |_tagname|
body.gsub(/(?<![\>`])(\<p\>)?\[people\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2)
exclude = nil
if tagname.include?('!')
Expand Down Expand Up @@ -276,7 +276,7 @@ def self.people_grid(body, current_user = nil, _page = 1)
end

def self.wikis_grid(body, _page = 1)
body.gsub(/[^\>`](\<p\>)?\[wikis\:(\S+)\]/) do |_tagname|
body.gsub(/(?<![\>`])(\<p\>)?\[wikis\:(\S+)\]/) do |_tagname|
tagname = Regexp.last_match(2)
exclude = nil
if tagname.include?('!')
Expand Down
17 changes: 17 additions & 0 deletions test/unit/node_shared_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ class NodeSharedTest < ActiveSupport::TestCase
assert html.scan('<td class="title">').length > 1
end

test 'that NodeShared works if code starts at the beginning of the line' do
before = "[wikis:foo]"
html = NodeShared.wikis_grid(before)
assert html
assert_equal 1, html.scan('<table class="table inline-grid wikis-grid wikis-grid-foo wikis-grid-foo-').length
assert_equal 1, html.scan('<table').length
end

test 'that NodeShared does not replace characters before codes like [wikis:foo]' do
before = "Here is a code a[wikis:foo]"
html = NodeShared.wikis_grid(before)
assert html
assert_equal 1, html.scan('<table class="table inline-grid wikis-grid wikis-grid-foo wikis-grid-foo-').length
assert_equal 1, html.scan('<table').length
assert_equal 1, html.scan('Here is a code a').length
end

test 'that NodeShared does not convert short codes like [notes:foo] into tables which list notes, when inside `` marks' do
before = "This shouldn't actually produce a table:\n\n`[notes:tagname]`\n\nOr this:\n\n `[notes:tagname]`"
html = NodeShared.notes_grid(before)
Expand Down

0 comments on commit af295d7

Please sign in to comment.