Skip to content

Instantly share code, notes, and snippets.

@Theminijohn
Last active August 29, 2015 14:01
Show Gist options
  • Save Theminijohn/84b08b0bb0e7bdf30869 to your computer and use it in GitHub Desktop.
Save Theminijohn/84b08b0bb0e7bdf30869 to your computer and use it in GitHub Desktop.
class Logic::JavascriptParser
def initialize(content)
@raw_content = content
JavascriptParser.new(params[:code_content])
end
def execute
self.parse
self.persist!
end
def parse
raw_metas = @raw_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
@metas = metas
end
def persist!
Script.new(@metas).save!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment