class Jekyll::DistorteD::ThirteenthStyle

Constants

TAB_SEQUENCE

Public Class Methods

new(tag_name, arguments, liquid_options) click to toggle source
Calls superclass method
# 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

Public Instance Methods

render(context) click to toggle source

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.

# 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) click to toggle source
# File lib/distorted-jekyll/13th-style.rb, line 37
def render_to_output_buffer(context, output)
  css_filename = File.join(File.dirname(__FILE__), '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