Skip to content

Instantly share code, notes, and snippets.

@fabiokr
Created June 11, 2015 11:41
Show Gist options
  • Save fabiokr/5c51f4e3c76d3fbaa3f2 to your computer and use it in GitHub Desktop.
Save fabiokr/5c51f4e3c76d3fbaa3f2 to your computer and use it in GitHub Desktop.
require 'erb'
class Ansible
class TemplateParams
attr_accessor :content, :helper
# Public: Initializes the Template Params context.
#
# content - the Ansible content
def initialize(content)
@content = content
@helper = Helper.new
end
# Public: Gets the ERB binding.
#
# Returns a Binding instance.
def get_binding
binding
end
end
class Helper
# Public: Applies html format to a line.
#
# line - a String
#
# Returns a String.
def format_line(line)
# replaces whitespaces with the html version
line = line.gsub(" ", " ")
# replaces ok=123
line = line.gsub(/(\w+)=(\d+)/, "<span class=\"\\1\">\\1=\\2</span>")
# replaces ok:...
if status = line.split(":").first
line = "<span class=\"#{status}\">#{line}</span>"
end
line
end
end
# Public: Applies the syntax highlight.
#
# content - a String
#
# Returns a String.
def parse(content)
source = ERB.new(File.read(File.dirname(__FILE__) + "/ansible.html.erb", nil))
source.result(TemplateParams.new(content).get_binding)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment