Module: CS50::Mixins

Included in:
Block, Tag
Defined in:
lib/jekyll-theme-cs50.rb

Instance Method Summary collapse

Instance Method Details

#initialize(tag_name, markup, options) ⇒ Object



104
105
106
107
108
# File 'lib/jekyll-theme-cs50.rb', line 104

def initialize(tag_name, markup, options)
  @tag_name = tag_name
  @markup = markup
  super
end

#render(context) ⇒ Object



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
# File 'lib/jekyll-theme-cs50.rb', line 110

def render(context)

  # Interpolate any variables, a la render_variable in
  # https://github.com/jekyll/jekyll/blob/master/lib/jekyll/tags/include.rb
  output = @markup.gsub(/\{\{.*?\}\}/) do |s|
    Liquid::Template.parse(s).render(context)
  end

  # Quote unquoted URLs
  output = output.gsub(/(\S+\s*=\s*".*"|\S+\s*=\s*'.*'|".*"|'.*'|\S+)/) do |s|
      if s =~ /^#{URI::regexp}$/
        "\"#{s}\""
      else
        s
      end
  end

  # Parse any arguments
  @args, @kwargs = [], {}
  output.scan(/"[^"]*"|'[^']'|\S+\s*=\s*"[^"]*"|\S+\s*=\s*'[^']*'|\S+\s*=\s*\S+|\S+/).each do |s|
    if s.start_with?("'")
      @args.push(s.gsub(/^'|'$/, ""))
    elsif s.start_with?('"')
      @args.push(s.gsub(/^"|"$/, ""))
    else
      key , value = s.split("=", 2)
      if value.nil?
        @args.push(key)
      else
        @kwargs[key] = value
      end
    end
  end

  # Return any content
  super
end