Class: Jekyll::DistorteD::ThirteenthStyle

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/distorted-jekyll/13th-style.rb

Constant Summary collapse

TAB_SEQUENCE =

two spaces

'  '.freeze

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, arguments, liquid_options) ⇒ ThirteenthStyle

Returns a new instance of ThirteenthStyle.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/distorted-jekyll/13th-style.rb', line 16

def initialize(tag_name, arguments, liquid_options)
  super

  # Liquid leaves argument parsing totally up to us.
  # Use the envygeeks/liquid-tag-parser library to wrangle them.
  parsed_arguments = Liquid::Tag::Parser.new(arguments)

  # Specify how many levels to indent printed output.
  # Indentation will apply to all lines after the first,
  # because the first line's output will fall at the same
  # place as our Liquid tag invocation.
  @tabs = parsed_arguments[:tabs] || 0
end

Instance Method Details

#render(context) ⇒ Object

This is going to go away in a future Liquid version and render_to_output_buffer will be the standard approach. I’m going ahead and using it since we are building strings here.



33
34
35
# File 'lib/distorted-jekyll/13th-style.rb', line 33

def render(context)
  return render_to_output_buffer(context, '')
end

#render_to_output_buffer(context, output) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/distorted-jekyll/13th-style.rb', line 37

def render_to_output_buffer(context, output)
  css_filename = File.join(File.dirname(__FILE__), 'template'.freeze, '13th-style.css'.freeze)

  # Use IO.foreach() to call a block on each line of our template file
  # without slurping the entire file into memory like File.read() / File.readlines()
  File.foreach(css_filename).with_index do |line, line_num|
    # Don't indent the first line of the CSS file, because the first line
    # will print starting at the position of our {% 13thStyle %} Liquid tag.
    unless line_num == 0
      output << TAB_SEQUENCE * @tabs
    end
    output << line
  end
  # Remove CSS comments from output so I can leave notes there
  # without bloating up my output.
  # Based on C-shebang-style comment regex from MRE3
  return output.gsub(/\/\*[^*]*\*+(?:[^*\/][^*]*\*+)*\//, '')
end