module SciolyFF::Interpreter::HTML

Grants ability to convert a SciolyFF file into stand-alone HTML and other formats (YAML, JSON)

Public Instance Methods

to_html(hide_raw: false, color: ' click to toggle source
# File lib/sciolyff/interpreter/html.rb, line 11
def to_html(hide_raw: false, color: '#000000')
  helpers = Interpreter::HTML::Helpers.new
  src = Erubi::Engine.new(helpers.template).src
  helpers.eval_with_binding(src, self, hide_raw, color)
         .gsub(/^\s*$/, '')   # remove empty lines
         .gsub(/\s+$/, '')    # remove trailing whitespace
end
to_json(hide_raw: false, pretty: false) click to toggle source
# File lib/sciolyff/interpreter/html.rb, line 24
def to_json(hide_raw: false, pretty: false)
  rep = hide_raw ? hidden_raw_rep : @rep
  return JSON.pretty_generate(rep) if pretty

  rep.to_json
end
to_yaml(hide_raw: false) click to toggle source
# File lib/sciolyff/interpreter/html.rb, line 19
def to_yaml(hide_raw: false)
  rep = hide_raw ? hidden_raw_rep : @rep
  stringify_keys(rep).to_yaml
end

Private Instance Methods

hidden_raw_rep() click to toggle source
# File lib/sciolyff/interpreter/html.rb, line 33
def hidden_raw_rep
  @hidden_raw_rep || begin
    @hidden_raw_rep = Marshal.load(Marshal.dump(@rep))
    @hidden_raw_rep[:Placings].each { |p| hide_and_replace_raw(p) }
    @hidden_raw_rep
  end
end
hide_and_replace_raw(placing_rep) click to toggle source
# File lib/sciolyff/interpreter/html.rb, line 41
def hide_and_replace_raw(placing_rep)
  placing = placings.find do |p|
    p.event.name == placing_rep[:event] &&
      p.team.number == placing_rep[:team]
  end
  placing_rep.delete :raw
  placing_rep[:tie] = true if placing.tie?
  placing_rep[:place] = placing.place if placing.place
end
stringify_keys(hash) click to toggle source
# File lib/sciolyff/interpreter/html.rb, line 51
def stringify_keys(hash)
  return hash unless hash.instance_of? Hash

  hash.map do |k, v|
    new_k = k.to_s
    new_v = case v
            when Array then v.map { |e| stringify_keys(e) }
            when Hash then stringify_keys(v)
            else v
            end
    [new_k, new_v]
  end.to_h
end