Class: UCss

Inherits:
Object
  • Object
show all
Defined in:
lib/ucss.rb

Constant Summary collapse

REGEX =
/[^<>"'`\s]*[^<>"'`\s:]/.freeze
CLASS_MAP =
{
  'bg' => colors('background'),
  'p' => spacing(['padding']),
  'py' => spacing(%w[padding-top padding-bottom]),
  'px' => spacing(%w[padding-left padding-right]),
  'm' => spacing(['margin']),
  'my' => spacing(%w[margin-top margin-bottom]),
  'mx' => spacing(%w[margin-left margin-right]),
  'text' => colors('color').merge(align('text-align')),
  'justify' => flex_align('justify-content').merge(
    { 'items' => flex_align('justify-items') },
    { 'self' => flex_align('justify-self') }
  ),
  'self' => flex_align('align-self'),
  'items' => flex_align('align-items'),
  'content' => flex_align('align-content'),
  'place' => {
    'content' => flex_align('place-content'),
    'items' => flex_align('place-items'),
    'self' => flex_align('self')
  },
  'gap' => spacing(%w[gap]).merge(
    { 'x' => spacing(%w[column-gap]) },
    { 'y' => spacing(%w[row-gap]) }
  ),
  'flex' => {
    'DEFAULT' => 'display: flex',
    'col' => 'flex-direction: column',
    'row' => 'flex-direction: row',
    '1' => 'flex: 1 1 0%',
    'auto' => 'flex: 1 1 auto',
    'initial' => 'flex: 0 1 auto',
    'none' => 'flex: none'
  }
}.merge(COLORS).freeze
@@classes =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUCss

Returns a new instance of UCss.



85
86
87
88
89
# File 'lib/ucss.rb', line 85

def initialize
  @output = './assets/css/utility.css'
  @input  = './views/**/*.*'
  @matches = @@classes || []
end

Class Method Details

.align(property) ⇒ Object



23
24
25
26
27
# File 'lib/ucss.rb', line 23

def self.align(property)
  %w[left right center justify].map do |v|
    [v, "#{property}: #{v}"]
  end.to_h
end

.colors(property) ⇒ Object



17
18
19
20
21
# File 'lib/ucss.rb', line 17

def self.colors(property)
  COLORS.deep_transform_values do |v|
    "#{property}: #{v}"
  end
end

.css(str) ⇒ Object



80
81
82
83
# File 'lib/ucss.rb', line 80

def self.css(str)
  @@classes << str.split(' ').compact
  @@classes.flatten!.uniq!
end

.flex_align(property) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ucss.rb', line 29

def self.flex_align(property)
  {
    'start' => 'flex-start',
    'center' => 'center',
    'between' => 'space-between',
    'around' => 'space-around',
    'evenly' => 'space-evenly',
    'stretch' => 'stretch',
    'auto' => 'auto',
    'baseline' => 'baseline'
  }.transform_values { |v| "#{property}: #{v}" }
end

.spacing(properties) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/ucss.rb', line 9

def self.spacing(properties)
  SPACING.transform_values do |v|
    properties.map do |p|
      "#{p}: #{v}"
    end.join('; ')
  end
end

Instance Method Details

#body(name) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ucss.rb', line 91

def body(name)
  parts = name.split('-')

  content = CLASS_MAP.dig(*parts)

  content = content['DEFAULT'] if content.is_a?(Hash)

  return unless content

  ".#{name} { #{content}; }"
end

#content(classes = @matches) ⇒ Object



103
104
105
# File 'lib/ucss.rb', line 103

def content(classes = @matches)
  classes.map { |name| body(name) }.compact.join("\n")
end

#read(from: @input) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/ucss.rb', line 107

def read(from: @input)
  files = Dir[from]

  files.each do |f|
    s = File.read f
    @matches += s.scan(REGEX)
  end

  @matches.uniq!
end

#write(to: @output) ⇒ Object



118
119
120
# File 'lib/ucss.rb', line 118

def write(to: @output)
  File.write to, content(@matches)
end