|
|
1
0
我用得不彻底,但我的测试通过了。我想这已经足够让我产生这样的想法了:)我花了几个小时的学习,然后又花了几个小时来实现它。 顺便说一下,我没有在这里做任何优化。对我来说,不需要很快 查看我的规范文件: require 'spec_helper'
describe StyleParser do
describe "given properly formatted input" do
it "should set and return variables properly" do
text = %{# @name Masthead Background Image
# @kind file
# @description Background image.
$mbc2: "http://someurl.com/image.jpg";
# @name Masthead BG Color
# @kind color
# @description Background color.
$mbc: #555;}
@s = StyleParser.new(text)
@s.mbc.name.should == "Masthead BG Color"
@s.mbc.kind.should == "color"
@s.mbc.description.should == "Background color."
@s.mbc.value.should == "#555"
@s.mbc2.name.should == "Masthead Background Image"
@s.mbc2.kind.should == "file"
@s.mbc2.description.should == "Background image."
@s.mbc2.value.should == %Q("http://someurl.com/image.jpg")
end
end
describe "when assigning values" do
it "should update its values" do
text = %{# @name Masthead Background Image
# @kind file
# @description Background image.
$mbc2: "http://someurl.com/image.jpg";}
@s = StyleParser.new(text)
@s.mbc2.value = %Q("Another URL")
@s.mbc2.value.should == %Q("Another URL")
rendered_text = @s.render
rendered_text.should_not match(/http:\/\/someurl\.com\/image\.jpg/)
rendered_text.should match(/\$mbc2: "Another URL";/)
@s.mbc2.value = %Q("Some third URL")
@s.mbc2.value.should == %Q("Some third URL")
rendered_text = @s.render
rendered_text.should_not match(/\$mbc2: "Another URL";/)
rendered_text.should match(/\$mbc2: "Some third URL";/)
end
it "should render the correct values" do
text_old = %{# @name Masthead Background Image
# @kind file
# @description Background image.
$mbc2: "http://someurl.com/image.jpg";}
text_new = %{# @name Masthead Background Image
# @kind file
# @description Background image.
$mbc2: "Another URL";}
@s = StyleParser.new(text_old)
@s.mbc2.value = %Q("Another URL")
@s.render.should == text_new
end
end
end
然后是以下两个文件: # Used to parse through an scss stylesheet to make editing of that stylesheet simpler
# Ex. Given a file called style.scss
#
# // @name Masthead Background Color
# // @type color
# // @description Background color of the masthead.
# $masthead_bg_color: #444;
#
# sp = StyleParser.new(contents of style.scss)
#
# # Reading
# sp.masthead_bg_color.value # returns "#444"
# sp.masthead_bg_color.name # returns "Masthead Background Color"
# sp.masthead_bg_color.type # returns "color"
# sp.masthead_bg_color.description # returns "Background color of the masthead."
#
# # Writing
# sp.masthead_bg_color.value = "#555"
# sp.render # returns all the text above except masthead_bg_color is now #555;
class StyleParser
def initialize(text)
@text = text
@variables = {}
@eol = '\n'
@context_lines = 3
@context = "((?:.*#{@eol}){#{@context_lines}})"
end
# Works this way: http://rubular.com/r/jWSYvfVrjj
# Derived from http://stackoverflow.com/questions/2760759/ruby-equivalent-to-grep-c-5-to-get-context-of-lines-around-the-match
def get_context(s)
regexp = /.*\${1}#{s}:.*;[#{@eol}]*/
@text =~ /^#{@context}(#{regexp})/
before, match = $1, $2
"#{before}#{match}"
end
def render
@variables.each do |key, var|
@text.gsub!(/^\$#{key}: .+;/, %Q($#{key}: #{var.value};))
end
@text
end
def method_missing(method_name)
if method_name.to_s =~ /[\w]+/
context = get_context(method_name)
@variables[method_name] ||= StyleVariable.new(method_name, context)
end
end
end
class StyleVariable
METADATA = %w(name kind description)
def initialize(var, text)
@var = var
@text = text
end
def method_missing(method_name)
if METADATA.include? method_name.to_s
content_of(method_name.to_s)
end
end
def value
@text.each do |string|
string =~ /^\${1}#{@var}: (.+);/
return $1 if $1
end
end
def value=(val)
@text.gsub!(/^\$#{@var}: .+;/, "$#{@var}: #{val};")
end
private
def content_of(variable)
@text.each do |string|
string =~ /^# @([\w]+[^\s]) (.+)/
return $2 if $1 == variable
end
end
end
|
|
|
David542 · 任何语言都允许函数名中有空格吗? 1 年前 |
|
Andy · 将LENGTH OF移动到COMP字段解析失败 2 年前 |
|
|
Chris Geo · 如何找到LR0项目的FOLLOW集合? 2 年前 |
|
|
Yash Singhal · 在reactjs中解析Pdf中的文本 2 年前 |
|
|
i33SoDA · 如何将逗号分隔的数字字符串解析为int数组? 2 年前 |