-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
2,076 additions
and
556 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,115 @@ | ||
desc "Make the sass/scss snippets" | ||
|
||
task :build do | ||
str = File.read('UltiSnips/css.snippets') | ||
str.gsub!(/;/, '') | ||
str.gsub!(/ \{$/, '') | ||
str.gsub!(/^\}$/, '') | ||
str.gsub!('@include ', '+') | ||
|
||
puts "+ UltiSnips/sass.snippets" | ||
File.open('UltiSnips/sass.snippets', 'w') { |f| f.write str } | ||
require 'yaml' | ||
data = YAML::load_file('snips.yml') | ||
snips = Snippets.new(data) | ||
|
||
[:css, :sass, :scss, :stylus].each do |format| | ||
fn = "UltiSnips/#{format}.snippets" | ||
puts fn | ||
File.open(fn, 'w') { |f| f.write snips.to_s(format) } | ||
end | ||
end | ||
|
||
class Snippets | ||
def initialize(snips) | ||
@snips = snips | ||
end | ||
|
||
def to_s(format) | ||
out = [] | ||
|
||
out += @snips['simple'].map do |key, val| | ||
block \ | ||
name: key, | ||
desc: to_desc(val), | ||
snip: to_snippet(val, format) | ||
end | ||
|
||
out += @snips['expressions'].map do |key, val| | ||
block \ | ||
name: key, | ||
desc: to_desc(val), | ||
snip: unplaceholder(val) | ||
end | ||
|
||
if format == :sass || format == :scss | ||
out += @snips['sass-expressions'].map do |key, val| | ||
block \ | ||
name: key, | ||
desc: to_desc(val), | ||
snip: unplaceholder(val) | ||
end | ||
end | ||
|
||
if format == :stylus | ||
out += @snips['stylus-expressions'].map do |key, val| | ||
block \ | ||
name: key, | ||
desc: to_desc(val), | ||
snip: unplaceholder(val) | ||
end | ||
end | ||
|
||
out += @snips['media'].map do |key, val| | ||
block \ | ||
name: key, | ||
desc: val, | ||
snip: (brackety?(format) ? val.gsub(') ', ') { ') : val) | ||
end | ||
|
||
out += @snips['css3'].map do |key, val| | ||
block \ | ||
name: key, | ||
desc: to_desc(val), | ||
snip: to_snippet(val, format, true) | ||
end | ||
|
||
|
||
out.join("\n\n") | ||
end | ||
|
||
private | ||
|
||
def brackety?(format) | ||
format == :css || format == :scss | ||
end | ||
|
||
def to_desc(snippet) | ||
snippet.gsub(/\{(.*?)\}/, '___') | ||
end | ||
|
||
def unplaceholder(snippet) | ||
i = 0 | ||
snippet.gsub(/\{(.*?)\}/) { |placeholder| "${#{i += 1}:#{placeholder[1...-1]}}" } | ||
end | ||
|
||
# Turns a raw snippet into a snippet of a given format | ||
def to_snippet(value, format, mixinify=false) | ||
snippet = value.dup | ||
|
||
snippet.gsub!(/; /, "\n") | ||
unplaceholder snippet | ||
if mixinify | ||
if format == :sass | ||
snippet.gsub!(/^(.*?): (.*?)$/, "+\\1(\\2)") | ||
elsif format == :scss | ||
snippet.gsub!(/^(.*?): (.*?)$/, "@include \\1(\\2)") | ||
end | ||
end | ||
|
||
snippet.gsub!(/$/, ';') if brackety?(format) | ||
|
||
snippet | ||
end | ||
|
||
# Formats a block | ||
def block(options) | ||
[ | ||
"snippet #{options[:name]} \"#{options[:desc]}\"", | ||
options[:snip], | ||
"endsnippet" | ||
].join("\n") | ||
end | ||
end |
Oops, something went wrong.