Created
May 9, 2013 14:07
-
-
Save samvasko/5547637 to your computer and use it in GitHub Desktop.
Parse snippets from laravel source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Made by Samuel Vasko ~ [email protected] | |
# Parse functions from Laravel source | |
# and format them into Sublime Text snippets | |
require "awesome_print" | |
replacements = { | |
"App" => "App", | |
"Artisan" => "Art", | |
"Auth" => "Auth", | |
"Blade" => "Blade", | |
"Cache" => "Cache", | |
"Config" => "Config", | |
"Cookie" => "Cookie", | |
"Crypt" => "Crypt", | |
"DB" => "DB", | |
"Event" => "Event", | |
"File" => "File", | |
"Form" => "Form", | |
"Hash" => "Hash", | |
"HTML" => "HTML", | |
"Input" => "Input", | |
"Lang" => "Lang", | |
"Log" => "Log", | |
"Mail" => "Mail", | |
"Paginator" => "Pagin", | |
"Password" => "Pass", | |
"Queue" => "Queue", | |
"Redirect" => "Redir", | |
"Redis" => "Redis", | |
"Request" => "Req", | |
"Response" => "Resp", | |
"Route" => "Route", | |
"Schema" => "Schema", | |
"Session" => "Sess", | |
"URL" => "URL", | |
"Validator" => "Valid", | |
"View" => "View", | |
} | |
# Build the list of files | |
list = File.read('files.txt').lines | |
Dir.mkdir('Snippets') unless Dir.exists?('Snippets') | |
Dir.chdir('Snippets') | |
list = list.map { |e| e.split(' ') } | |
for file in list | |
# One file is scope | |
name = file[0] | |
location = file[1] | |
File.foreach(location) do |line| | |
unless line.match(/public function [a-zA-Z]/) == nil | |
# now some cleanup | |
line = line.strip[16..line.length] | |
# mad regex to separate function from parameters | |
split = line.match(/^(\w+)\((.+)?\)/) | |
fname = split[1] | |
params = '' | |
if split[2] != nil | |
params = split[2].delete(',)').split(' ').select { |it| it.include?('$') } | |
# Sublime snippet generation starts here | |
# Make a ${1:name} around | |
i = 0; | |
params = params.map do |m| | |
i += 1 | |
'${'+i.to_s+':\\'+m+'}' | |
end | |
end | |
params = params.join(', ') unless params.empty? | |
snip = name +'::'+fname+'('+params+')' | |
# Snippet is made, now proceed to creating tab trigger | |
# Theory is that for short strings it will contain whole thing, for combined | |
# statements it have only uppercase charcters like getEngineResolver -> ger | |
filename = replacements[name]+'-'+fname | |
match = fname.scan(/[A-Z]/) | |
if match.size > 0 | |
fname = fname[0] + match.join.downcase | |
end | |
tab = replacements[name] + '-' + fname | |
core = "<snippet>\n"\ | |
" <content><![CDATA["+snip+"]]></content>\n"\ | |
" <tabTrigger>"+tab+"</tabTrigger>\n"\ | |
" <scope>source.php</scope>\n"\ | |
"</snippet>\n" | |
bye = File.new(filename+'.sublime-snippet', 'w+') | |
bye.write(core) | |
bye.close | |
end | |
end | |
end | |
puts 'Done!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment