Class: Ro::Template

Inherits:
Object show all
Defined in:
lib/ro/template.rb,
lib/ro/template/rouge_formatter.rb

Defined Under Namespace

Classes: RougeFormatter

Class Method Summary collapse

Class Method Details

.render(*args, &block) ⇒ Object



11
12
13
# File 'lib/ro/template.rb', line 11

def Template.render(*args, &block)
  render_file(*args, &block)
end

.render_erb(content, options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ro/template.rb', line 98

def Template.render_erb(content, options = {})
  content = String(content).force_encoding('utf-8')
  options = Map.for(options.is_a?(Hash) ? options : { context: options })
  context = options[:context]

  erb = ERB.new(content, trim_mode: '%<>')

  html =
    if context.respond_to?(:to_hash)
      hash = context.to_hash
      erb.result_with_hash(hash)
    else
      binding = context ? context.instance_eval{ binding } : ::Kernel.binding
      erb.result(binding)
    end

  HTML.new(html)
end

.render_file(path, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ro/template.rb', line 15

def Template.render_file(path, options = {})
  path = File.expand_path(path.to_s.strip)
  options = Map.for(options.is_a?(Hash) ? options : { context: options })

  content = IO.binread(path).force_encoding('utf-8')
  engines = File.basename(path).split('.')[1..-1]
  context = options[:context]

  begin
    render_string(content, path: path, engines: engines, context: context)
  rescue Ro::Error => e
    msg = e.message
    Ro.error! "failed to render #{ path } with `#{ msg }`"
  end
end

.render_html(html) ⇒ Object



69
70
71
# File 'lib/ro/template.rb', line 69

def Template.render_html(html)
  HTML.new(html)
end

.render_json(json) ⇒ Object



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

def Template.render_json(json)
  data = JSON.parse(json)
  Ro.mapify(data)
end

.render_markdown(content, options = {}) ⇒ Object



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
# File 'lib/ro/template.rb', line 117

def Template.render_markdown(content, options = {})
  content = String(content).force_encoding('utf-8')
  options = Map.for(options.is_a?(Hash) ? options : { context: options })

  theme = options.fetch(:theme) { Ro.config.md_theme }

  parsed = FrontMatterParser::Parser.new(:md).call(content)
  front_matter = parsed.front_matter
  content = parsed.content

  opts = {
    input: 'GFM',
    hard_wrap: false,
    syntax_highlighter_opts: { formatter: RougeFormatter, theme: theme }
  }

  div =
    "      <div class=\"ro markdown\">\n        \#{ ::Kramdown::Document.new(content, opts).to_html }\n      </div>\n    _____\n\n  html = Rinku.auto_link(div, :all, 'target=\"_blank\"')\n\n  HTML.new(html, front_matter:)\nend\n"

.render_ruby(code) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/ro/template.rb', line 83

def Template.render_ruby(code)
  string = IO.popen('ruby', 'w+'){|ruby| ruby.puts(code); ruby.close_write; ruby.read}

  if $? == 0
    string
  else
    Ro.error!("ruby:\n\n#{ code }")
  end
end

.render_src(path, options = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ro/template.rb', line 145

def Template.render_src(path, options = {})
  path = File.expand_path(path.to_s.strip)
  options = Map.for(options.is_a?(Hash) ? options : { context: options })

  content = IO.binread(path).force_encoding('utf-8')
  engines = File.basename(path).split('.')[1..-1].reverse
  context = options[:context]

  theme = options.fetch(:theme) { Ro.config.md_theme }
  formatter = RougeFormatter.new(theme: theme)

  language = engines.shift

  lexer = Rouge::Lexer.find(language) || Ro.error!('no lexer found for ')

  content = render_string(content, path: path, engines: engines, context: context) if engines.size.nonzero?

  HTML.new(
    "      <div class=\"ro markdown src\">\n        \#{ formatter.format(lexer.lex(content)) }\n      </div>\n    _____\n  )\nend\n"

.render_string(content, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ro/template.rb', line 31

def Template.render_string(content, options = {})
  content = String(content).force_encoding('utf-8')
  options = Map.for(options.is_a?(Hash) ? options : { context: options })
  engines = Array(options.fetch(:engines) { ['md'] }).flatten.compact
  path = options.fetch(:path) { '(string)' }
  context = options[:context]

  loop do
    break if engines.empty?

    engine = engines.shift.to_s.strip.downcase

    content =
      case engine
        when 'html'
          render_html(content)
        when 'txt', 'text'
          render_text(content)
        when 'erb'
          render_erb(content, context:)
        when 'md', 'markdown'
          render_markdown(content)
        when 'yml', 'yaml'
          render_yaml(content)
        when 'json'
          render_json(content)
        when 'rb'
          render_ruby(content)
        when 'txt', 'text'
          render_text(content)
        else
          Ro.error!("no engine found for engine=#{ engine.inspect } engines=#{ engines.inspect }")
      end
  end

  content
end

.render_text(text) ⇒ Object



93
94
95
96
# File 'lib/ro/template.rb', line 93

def Template.render_text(text)
  html = Text.render(text)
  HTML.new(html)
end

.render_yaml(yaml) ⇒ Object



78
79
80
81
# File 'lib/ro/template.rb', line 78

def Template.render_yaml(yaml)
  data = YAML.load(yaml)
  Ro.mapify(data)
end