Commit 503bfee8 by Rocky Duan

methods for extracting @ list

parent d360c8d8
class Content class Content
include Mongoid::Document include Mongoid::Document
AT_NOTIFICATION_REGEX = /(?<=^|\s)(@[A-Za-z0-9_]+)(?!\w)/
def self.get_marked_text(text)
counter = -1
text.gsub AT_NOTIFICATION_REGEX do
counter += 1
"#{$1}_#{counter}"
end
end
def self.get_at_position_list(text)
list = []
text.gsub AT_NOTIFICATION_REGEX do
parts = $1.rpartition('_')
list << [parts.last.to_i, parts.first[1..-1]]
end
list
end
def self.get_valid_at_position_list(text)
html = Nokogiri::HTML(RDiscount.new(self.get_marked_text(text)).to_html)
html.xpath('//code').each do |c|
c.children = ''
end
self.get_at_position_list html.to_s
end
end end
require 'spec_helper'
describe Content do
before :each do
@text =
"""
hi @tom, I have a question from @pi314 about the following code:
```
class A
def set_some_variable
@some_variable = 1
end
end
```
and also the following code
class A
def get_some_variable
@some_variable
end
end
what is the 'at' symbol doing there? @dementrock
"""
end
describe "#get_marked_text(text)" do
it "returns marked at text" do
converted = Content.get_marked_text(@text)
converted.should include "@tom_0"
converted.should include "@pi314_1"
converted.should include "@some_variable_2"
converted.should include "@some_variable_3"
converted.should include "@dementrock_4"
end
end
describe "#get_valid_at_position_list(text)" do
it "returns the list of positions for the valid @ notifications, filtering out the ones in code blocks" do
list = Content.get_valid_at_position_list(@text)
list.should include [0, "tom"]
list.should include [1, "pi314"]
list.should include [4, "dementrock"]
list.length.should == 3
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment