Skip to content

Commit

Permalink
Super refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Jun 12, 2013
1 parent 6eb062d commit 4c0e26a
Show file tree
Hide file tree
Showing 7 changed files with 2,076 additions and 556 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ margin: 0;
float: left;
```

See [css.snippets][css] for a full list of snippets.
See [this list][list] for a full list of snippets.

[css]: https://github.com/rstacruz/vim-ultisnips-css/blob/master/UltiSnips/css.snippets
[list]: https://github.com/rstacruz/vim-ultisnips-css/blob/master/snips.yml

You'll need
-----------
Expand Down Expand Up @@ -49,6 +49,11 @@ Installation via Vim without Vundle/Pathogen

It's possible but I won't even tell you how to. Do yourself a favor and start using Vundle or Pathogen.

Contributing
------------

Just edit the YML file, don't edit the snippet files themselves.

Acknowledgements
----------------

Expand Down
120 changes: 112 additions & 8 deletions Rakefile
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
Loading

0 comments on commit 4c0e26a

Please sign in to comment.