Class: Leg::Template::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/leg/template.rb

Instance Method Summary collapse

Constructor Details

#initialize(template_source, tutorial, config, params) ⇒ Context

Returns a new instance of Context.



49
50
51
52
53
54
# File 'lib/leg/template.rb', line 49

def initialize(template_source, tutorial, config, params)
  @template_source = template_source
  @tutorial = tutorial
  @config = config
  @params = params
end

Instance Method Details

#markdown(source) ⇒ Object



73
74
75
# File 'lib/leg/template.rb', line 73

def markdown(source)
  Leg::Markdown.render(source)
end

#pagesObject



77
78
79
# File 'lib/leg/template.rb', line 77

def pages
  @tutorial.pages
end

#render(path) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/leg/template.rb', line 64

def render(path)
  if !path.end_with? ".md"
    raise ArgumentError, "Only .md files are supported by render() at the moment."
  end

  contents = File.read(path)
  Leg::Markdown.render(contents)
end

#render_templateObject



56
57
58
59
60
61
62
# File 'lib/leg/template.rb', line 56

def render_template
  b = binding
  @config.options.merge(@params).each do |name, value|
    b.local_variable_set(name, value)
  end
  ERB.new(@template_source).result(b)
end

#syntax_highlighting_css(scope) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/leg/template.rb', line 81

def syntax_highlighting_css(scope)
  syntax_theme = @config.options[:syntax_theme] || "github"
  if syntax_theme.is_a? String
    theme = Rouge::Theme.find(syntax_theme)
  elsif syntax_theme.is_a? Hash
    theme = Class.new(Rouge::Themes::Base16)
    theme.name "base16.custom"
    theme.palette syntax_theme
  end

  theme.render(scope: scope)
end