Ruby on rails 使用Regex-Ruby的CRUD文本
我正在寻找解析和更新文档的解决方案。一个很好的例子是user.js脚本 示例案例: 用户将Ruby on rails 使用Regex-Ruby的CRUD文本,ruby-on-rails,regex,ruby-on-rails-4,xml-parsing,string-parsing,Ruby On Rails,Regex,Ruby On Rails 4,Xml Parsing,String Parsing,我正在寻找解析和更新文档的解决方案。一个很好的例子是user.js脚本 示例案例: 用户将user.js脚本上载到userscripts.org。文件必须具有浏览器特定变量的头。e、 g: // ==UserScript== // @name Fancy Title // @description Fancey Description // @namespace http://example.com // @icon http://example.com/icon
user.js
脚本上载到userscripts.org。文件必须具有浏览器特定变量的头。e、 g:
// ==UserScript==
// @name Fancy Title
// @description Fancey Description
// @namespace http://example.com
// @icon http://example.com/icon.png
// @updateURL http://example.com/user.js
// @downloadURL http://example.com/user.js
// @homepageURL http://example.com
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @include http*://example.com
// @include http://example.com/scripts/*
// @include http://example.com/tags/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_listValues
// @version 1.0
// ==/UserScript==
什么是一个好的解决方案来回溯检查这些变量并修改
或将它们添加到文档中。基本上是从@testing.title
=>@name-title
导入变量,反之亦然
假设元头
不包含变量@udpateURL
和@downloadeURL
,我将分别添加它们
我的第一个猜测是regex
用(@\w+
)扫描文档,这将得到所有变量@
,但从那里我就迷路了:)
我可以用普通的ruby解决这个问题,还是有现成的gem
编辑:
Sam指出:\/\/\s*@(\w+)\s+(.*)
,它准确地捕获了我需要的变量
标识符(@title
)和值(花式标题
)
如何设置、读取或更新它们
@Yoshiji先生为我提供了一个非常棒的regex元阅读器:
raw_metas = file_content.scan( /\A\/\/\s==UserScript==(\w|\W)*\/\/\s==\/UserScript==/ )
metas = {}
raw_metas.split(/\r\n|\n|\r/).each do |line_with_meta|
attribute_name = line_with_data.scan(/@\w+/)
value = line_with_data.sub("// #{attribute_name}", '').strip
if metas[attribute_name.sub('@', '').to_sym].present?
metas[attribute_name.sub('@', '').to_sym] = [ metas[attribute_name.sub('@', '').to_sym], value].flatten
else
metas[attribute_name.sub('@', '').to_sym] = value
end
end
但是我完全不知道如何设置它来与模型的属性交互
我需要更改哪些元数据
这意味着这些属性(:description等)存储在我的模型中,我需要传递它们
// @name => @model.name
// @description => @model.description
// @namespace => Application root_path
// @updateURL => @model show_view url
// @downloadURL => @Model show_view url
// @homepageURL => Application root_path
// @include => Custom url (passed by me)
// @include => Custom url (passed by me)
// @include => Custom url (passed by me)
// @version => @model.version
[编辑:在聊天中,您提到您的输入可能在一行上。这显示了处理该问题的正则表达式,以及重建字符串的一般过程。]
此代码将名称和值存储在两个散列中,将@version
替换为2.0
,然后将其输出(请参见本部分底部的输出):
我不知道Ruby,但这里有一个更完整的表达:那真的很有帮助!!!!谢谢sam。是的,很抱歉我帮不了Ruby..但是也许或者可以帮上忙。我只是在读Regex Ruby文档:),但是很难实现。Thanx对于push tho.FYI,添加了代码以替换令牌:在示例中,@version
从1.0
更改为2.0
,这是一些严重的代码。首先谢谢你!根据我现在的理解,我必须创建两个模型属性::tokens
和:value
对吗?如果我误解了它,我如何从我的模型属性中获取一个字符串并将其传递到那里?我会用这个更新我的问题@实际上,这只是几行简单的Ruby代码,正如您所要求的。因为字符串的缘故,它很长。:)这两个散列被称为标记
,用于@名称
和值
来保存值。如果查看我修改@version
的示例,您会发现修改哈希非常容易。在本例中,我们更改了@version值,但我们也可以将名称@version
更改为@VER
,如下所示:tokens[versionkey]=“@VER”
如果代码中有任何参数,如param
,只需说出类似于values[somekey]=param
@TheMiniJohn嘿,对不起,我离开机器好几个小时了。我没有使用单个散列的原因(例如token[“@version”]=“1.0”
是因为我们会发生冲突(您只能有一个@include
键)。不明白您还需要存储哪些值?它们已经存储在令牌
和值
散列中,您可以随意修改它们。您能和我一起玩两分钟吗?:)
subject = <<-eos
@name Fancy Title
// @description Fancey Description
// @namespace http://example.com
// @icon http://example.com/icon.png
// @updateURL http://example.com/user.js
// @downloadURL http://example.com/user.js
// @homepageURL http://example.com
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @include http*://example.com
// @include http://example.com/scripts/*
// @include http://example.com/tags/*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_listValues
// @version 1.0
eos
regex = /\/\/ (@\w+)\s*([^\n]*)/
# put captures in two hashes
tokens = Hash.new
values = Hash.new
counter = 0
subject.scan(regex) {|m|
tokens[counter] = $1
values[counter] = $2
counter += 1
}
# find hash key for @version
versionkey = tokens.index("@version")
# change version to 2.0
values[versionkey] = "2.0"
# print names and values
i=0
while i < counter do
puts "#{tokens[i]} : #{values[i]}"
i +=1
end
// # '// '
( # group and capture to \1:
@ # '@'
\w+ # word characters (a-z, A-Z, 0-9, _) (1 or
# more times (matching the most amount
# possible))
) # end of \1
\s* # whitespace (\n, \r, \t, \f, and " ") (0 or
# more times (matching the most amount
# possible))
( # group and capture to \2:
[^\n]* # any character except: '\n' (newline) (0
# or more times (matching the most amount
# possible))
) # end of \2