Class: Ro::Template
- Defined in:
- lib/ro/template.rb,
lib/ro/template/rouge_formatter.rb
Defined Under Namespace
Classes: RougeFormatter
Class Method Summary collapse
- .render(*args, &block) ⇒ Object
- .render_erb(content, options = {}) ⇒ Object
- .render_file(path, options = {}) ⇒ Object
- .render_html(html) ⇒ Object
- .render_json(json) ⇒ Object
- .render_markdown(content, options = {}) ⇒ Object
- .render_ruby(code) ⇒ Object
- .render_src(path, options = {}) ⇒ Object
- .render_string(content, options = {}) ⇒ Object
- .render_text(text) ⇒ Object
- .render_yaml(yaml) ⇒ Object
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, = {}) content = String(content).force_encoding('utf-8') = Map.for(.is_a?(Hash) ? : { context: }) context = [: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, = {}) path = File.(path.to_s.strip) = Map.for(.is_a?(Hash) ? : { context: }) content = IO.binread(path).force_encoding('utf-8') engines = File.basename(path).split('.')[1..-1] context = [:context] begin render_string(content, path: path, engines: engines, context: context) rescue Ro::Error => e msg = e. 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, = {}) content = String(content).force_encoding('utf-8') = Map.for(.is_a?(Hash) ? : { context: }) theme = .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, = {}) path = File.(path.to_s.strip) = Map.for(.is_a?(Hash) ? : { context: }) content = IO.binread(path).force_encoding('utf-8') engines = File.basename(path).split('.')[1..-1].reverse context = [:context] theme = .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, = {}) content = String(content).force_encoding('utf-8') = Map.for(.is_a?(Hash) ? : { context: }) engines = Array(.fetch(:engines) { ['md'] }).flatten.compact path = .fetch(:path) { '(string)' } context = [: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 |