Module: Converter::FlatUILessConversion
- Includes:
- LessConversion
- Included in:
- Converter
- Defined in:
- lib/tasks/converter/flat_ui_less_conversion.rb
Constant Summary collapse
- FLAT_UI_MIXINS =
Mixins that are added by FlatUI
%w{placeholder-height mask background-clip dropdown-arrow form-controls-corners-reset label-variant navbar-vertical-align social-button-variant}- FLAT_UI_OVERRIDE_MIXINS =
Mixins that FlatUI has modified
%w{placeholder text-hide scale animation horizontal vertical directional horizontal-three-colors vertical-three-colors radial striped button-variant input-size form-control form-control-focus}- FLAT_UI_PRO_MISSING_MODULES =
Modules missing from Flat UI Pro that are in Flat UI Free
%w{}
Instance Method Summary collapse
- #cleanup_whitespace(file) ⇒ Object
-
#convert_arbitrary_less_ampersand(less, selector) ⇒ Object
Based on the original convert_less_ampersand but modified for flat_ui_sass.
- #create_rule(name, *styles) ⇒ Object
- #extract_and_combine_nested_rules(file) ⇒ Object
- #fix_flat_ui_image_assets(file) ⇒ Object
- #fix_relative_asset_url(rule, type) ⇒ Object
-
#fix_variable_declaration_order(file) ⇒ Object
TODO this method is on the brittle side look into make it more robust.
- #flat_ui_less_files ⇒ Object
-
#load_shared ⇒ Object
Set load_shared to read from mixins.less again since bootstrap converted to modular mixins.
-
#parameterize_mixin_parent_selector(file, rule_sel) ⇒ Object
Fix to support replacing mixin definitions with default args github.com/twbs/bootstrap-sass/blob/master/tasks/converter/less_conversion.rb#L293.
-
#process_flat_ui_stylesheet_assets! ⇒ Object
This is similar to the process_stylesheet_assets utilized by bootstrap-sass except it is specific to flatui.
-
#replace_asset_url(rule, type) ⇒ Object
Methods overridden from the bootstrap-sass converter.
- #replace_file_imports(file) ⇒ Object
-
#replace_spin(less) ⇒ Object
Regex will match things like spinner-input-width by default.
Instance Method Details
#cleanup_whitespace(file) ⇒ Object
254 255 256 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 254 def cleanup_whitespace(file) file.gsub(/\r|\t/, "") end |
#convert_arbitrary_less_ampersand(less, selector) ⇒ Object
Based on the original convert_less_ampersand but modified for flat_ui_sass
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 260 def convert_arbitrary_less_ampersand(less, selector) return less unless less.include?(selector) styles = less.dup tmp = "\n" less.scan(/^(\s*&)(-[\w\[\]-]+\s*\{.+?})/m) do |ampersand, css| tmp << "#{selector}#{unindent(css)}\n" styles.gsub! "#{ampersand}#{css}", "" end if tmp.length > 1 styles.gsub!(/\s*#{"\\"+selector}\s*\{\s*}/m, '') styles << tmp else styles = less end styles end |
#create_rule(name, *styles) ⇒ Object
209 210 211 212 213 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 209 def create_rule(name, *styles) rule = "#{unindent(name)} {\n" styles.each {|s| rule += indent "#{s}\n"} rule += "}\n" end |
#extract_and_combine_nested_rules(file) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 184 def extract_and_combine_nested_rules(file) matches = Hash.new {|k,v| k[v] = []} file = file.dup file.scan(/\.[\w-]+&/x) do |selector| #first find the rules, and remove them file = replace_rules(file, "\s*#{selector}") do |rule, pos, css| selector = selector.gsub(/&$/, '') styles = rule.split(/[{}]/).last.strip parent = selector_for_pos(css, pos.begin).split('{').last.strip matches[selector] << create_rule(parent, styles) # Return an empty string to blank out this rule "" end end # Generate the combined nested rules rules = matches.inject("") do |s, (rule, children)| s += create_rule "&#{rule}", *children end close = close_brace_pos file, 0 file.insert(close, indent(rules)) end |
#fix_flat_ui_image_assets(file) ⇒ Object
248 249 250 251 252 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 248 def fix_flat_ui_image_assets(file) file = replace_all file, /\#\{(url\(.*?\).*?)}/, '\1' file = fix_relative_asset_url file, :image file = replace_asset_url file, :image end |
#fix_relative_asset_url(rule, type) ⇒ Object
243 244 245 246 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 243 def fix_relative_asset_url(rule, type) # Use a really naive pluralization replace_all rule, /url\(['"]?\.\.\/#{type}s\/([a-zA-Z0-9\-\/\.\?#]+)['"]?\)/, "url(\"#{@output_dir}/\\1\")" end |
#fix_variable_declaration_order(file) ⇒ Object
TODO this method is on the brittle side look into make it more robust
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 217 def fix_variable_declaration_order(file) # Spinner needs to be moved after Buttons # since it depends on $btn-default-bg # # also spinner-btn-hover-bg needs to be # before spinner-up-btn-border if file.include? 'Spinner' lines = file.split "\n" spinner_start = lines.index {|l| l =~ /Spinner/} spinner_end = lines.index {|l| l =~ /Pagination/} - 1 = lines.index {|l| l =~ /Navs/} - 1 (spinner_end - spinner_start).times do lines.insert(, lines.delete_at(spinner_start)) end spinner_btn_bg = lines.index {|l| l =~ /spinner-btn-bg:/}-1 3.times do lines.insert(spinner_btn_bg+5, lines.delete_at(spinner_btn_bg)) end file = lines.join("\n") end file end |
#flat_ui_less_files ⇒ Object
169 170 171 172 173 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 169 def flat_ui_less_files @flat_ui_less_files ||= Dir.chdir "#{@src_path}/less" do Dir['**/*.less'].reject{|f| f =~ /(?:demo|docs)\.less$/ } end end |
#load_shared ⇒ Object
Set load_shared to read from mixins.less again since bootstrap converted to modular mixins.
177 178 179 180 181 182 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 177 def load_shared @shared_mixins ||= begin log_status ' Reading shared mixins from mixins.less' read_mixins read_files('less', ['mixins.less'])['mixins.less'], nested: NESTED_MIXINS end end |
#parameterize_mixin_parent_selector(file, rule_sel) ⇒ Object
Fix to support replacing mixin definitions with default args github.com/twbs/bootstrap-sass/blob/master/tasks/converter/less_conversion.rb#L293
to:
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 310 def parameterize_mixin_parent_selector(file, rule_sel) log_transform rule_sel param = '$parent' replace_rules(file, '^\s*@mixin\s*' + rule_sel) do |mxn_css| mxn_css.sub! /(?=@mixin)/, "// [converter] $parent hack\n" # insert param into mixin def mxn_css.sub!(/(@mixin [\w-]+)\(([\$\w\-:,\s]*)\)/) { "#{$1}(#{param}#{', ' if $2 && !$2.empty?}#{$2})" } # wrap properties in #{$parent} { ... } replace_properties(mxn_css) { |props| next props if props.strip.empty? spacer = ' ' * indent_width(props) "#{spacer}\#{#{param}} {\n#{indent(props.sub(/\s+\z/, ''), 2)}\n#{spacer}}" } # change nested& rules to nested#{$parent} replace_rules(mxn_css, /.*&[ ,:]/) { |rule| replace_in_selector rule, /&/, "\#{#{param}}" } end end |
#process_flat_ui_stylesheet_assets! ⇒ Object
This is similar to the process_stylesheet_assets utilized by bootstrap-sass except it is specific to flatui
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 21 def process_flat_ui_stylesheet_assets! log_status 'Processing stylesheets...' files = read_files('less', flat_ui_less_files) log_status ' Converting LESS files to Scss:' files.each do |name, file| log_processing name # apply common conversions # icon-font bombs on this so skip it file = convert_less(file) unless name =~ /flat-ui|glyphicons/ file = replace_file_imports(file) file = cleanup_whitespace(file) case name when 'flat-ui.less' lines = file.split "\n" lines.reject! {|line| #kill the fonts lines, those are up to the user #kill variables since those need to be manually imported before bootstrap line =~ /fonts|url|variables/ } # Add a comment for the icon font icon_font_import = lines.index {|line| line =~ /glyphicons/} lines.insert(icon_font_import, '// Flat-UI-Icons') lines.delete_at(icon_font_import+2) file = lines.join "\n" when 'mixins.less' NESTED_MIXINS.each do |selector, prefix| file = flatten_mixins(file, selector, prefix) end file = varargify_mixin_definitions(file, *VARARG_MIXINS) file = deinterpolate_vararg_mixins(file) %w(responsive-(in)?visibility input-size text-emphasis-variant bg-variant).each do |mixin| file = parameterize_mixin_parent_selector file, mixin end file = replace_ms_filters(file) if pro? file = replace_all file, /(?<=[.-])\$state/, '#{$state}' else # calc-color mixin only exists in Flat-UI free file = replace_all file, /-(\$.+-color)/, '-#{\1}' file = replace_all file, /#\{\$\$\{(.+)\}\}/, 'interpolate_variable($\1)' end file = replace_rules(file, ' .list-group-item-') { |rule| extract_nested_rule rule, 'a&' } file = replace_all file, /,\s*\.open \.dropdown-toggle& \{(.*?)\}/m, " {\\1}\n .open & { &.dropdown-toggle {\\1} }" file = replace_all file, '$ratio, $ratio-y', '$scale-args' file = convert_grid_mixins file when 'variables.less' file = insert_default_vars(file) if ::Sass::VERSION >= '3.3.0' file = unindent " // a flag to toggle asset pipeline / compass integration\n $flat-ui-sass-asset-helper: function-exists(flat-ui-font-path) !default;\n\n SCSS\n else\n file = unindent <<-SCSS + file, 14\n // a flag to toggle asset pipeline / compass integration\n // defaults to true if flat-ui-font-path function is present (no function => twbs-font-path('') parsed as string == right side)\n $flat-ui-sass-asset-helper: (flat-ui-font-path(\"\") != unquote('flat-ui-font-path(\"\")')) !default;\n\n SCSS\n end\n file = fix_variable_declaration_order file\n file = replace_all file, /(\\$icon-font-path:\\s+).*(!default)/, '\\1\"'+@output_dir+'/\" \\2'\n when 'modules/buttons.less'\n file = extract_nested_rule file, '.btn-xs&'\n file = extract_nested_rule file, '.btn-hg&'\n when 'modules/forms.less'\n # Fix mixin regex not supporting non-variable arguments\n file.gsub! /@include input-size\\((?:\\$.+)\\);/ do |match|\n match.gsub /; /, ', '\n end\n file = apply_mixin_parent_selector(file, '\\.input-(?:sm|lg|hg)')\n when 'modules/input-groups.less'\n file = replace_rules(file, '.input-group-rounded') do |rule|\n extract_and_combine_nested_rules rule\n end\n when 'modules/glyphicons.less'\n file = replace_vars(file)\n file = replace_escaping(file)\n file = replace_all file, /\\#\\{(url\\(.*?\\))}/, '\\1'\n file = replace_rules(file, '@font-face') { |rule|\n rule = replace_all rule, /(\\$icon-font(?:-\\w+)+)/, '\#{\\1}'\n replace_asset_url rule, :font\n }\n when 'modules/login.less'\n file = fix_flat_ui_image_assets file\n when 'modules/navbar.less'\n # Fix mixin regex not supporting non-variable arguments\n file.gsub! /@include input-size\\((?:\\$.+)\\);/ do |match|\n match.gsub /; /, ', '\n end\n file = apply_mixin_parent_selector(file, '\\.navbar-input')\n when 'modules/palette.less'\n file.gsub! /@include calc-color\\((.+)\\);/ do |match|\n match.gsub /#\\{([\\w\\-]+)\\}/, '\"\\1\"'\n end\n when 'modules/select.less'\n # Fix the include that the converter makes an extend\n file = replace_all file, /@extend \\.caret/, '@include caret'\n when 'modules/spinner.less'\n # Fix mixin regex not supporting non-variable arguments\n file.gsub! /@include spinner-variant\\((?:\\$?.+)\\);/ do |match|\n match.gsub /; /, ', '\n end\n when 'modules/switch.less'\n file = fix_flat_ui_image_assets file\n when 'modules/tile.less'\n file = fix_flat_ui_image_assets file\n when 'modules/todo.less'\n file = fix_flat_ui_image_assets file\n when 'modules/thumbnails.less'\n file = extract_nested_rule file, 'a&'\n when 'modules/type.less'\n # Since .bg-primary has a color associated with it we need to divide it into\n # two selectors\n file = replace_rules(file, '.bg-primary') do |rule|\n parts = rule.split \"\\n\"\n selector = parts.index {|line| line =~ /\\.bg-primary/}\n mixin = parts.index {|line| line =~ /@include/}\n parts.insert(mixin, \"}\\n\#{parts[selector]}\")\n rule = parts.join \"\\n\"\n end\n file = apply_mixin_parent_selector(file, '\\.(text|bg)-(success|primary|info|warning|danger)')\n when 'modules/video.less'\n file = replace_rules(file, /\\s*\\.vjs(?:-(?:control|time))?(?!-\\w+)/) do |rule|\n selector = get_selector(rule).scan(/\\.vjs(?:-(?:control|time))?(?!-\\w+)/).first\n convert_arbitrary_less_ampersand(rule, selector)\n end\n file = fix_flat_ui_image_assets file\n end\n\n name = name.sub(/\\.less$/, '.scss')\n base = File.basename(name)\n name.gsub!(base, \"_\#{base}\") unless base == 'flat-ui.scss'\n path = File.join(@dest_path[:scss], name)\n save_file(path, file)\n log_processed File.basename(path)\n end\n\n manifest = File.join(@dest_path[:scss], '..', \"\#{@output_dir}.scss\")\n save_file(manifest, \"@import \\\"\#{@output_dir}/flat-ui\\\";\")\nend\n" + file, 14 |
#replace_asset_url(rule, type) ⇒ Object
Methods overridden from the bootstrap-sass converter
284 285 286 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 284 def replace_asset_url(rule, type) replace_all rule, /url\((.*?)\)/, "url(if($flat-ui-sass-asset-helper, flat-ui-#{type}-path(\\1), \\1))" end |
#replace_file_imports(file) ⇒ Object
289 290 291 292 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 289 def replace_file_imports(file) file.gsub %r([@\$]import ["|'](.*)["|'];), %Q(@import "#{@output_dir}/\\1";) end |
#replace_spin(less) ⇒ Object
Regex will match things like spinner-input-width by default.
Fix the first lookaround to be a positive lookaround and also check for word chars after the word ‘spin’
300 301 302 |
# File 'lib/tasks/converter/flat_ui_less_conversion.rb', line 300 def replace_spin(less) less.gsub(/(?<![\-$@.])spin(?![\-\w])/, 'adjust-hue') end |