require 'rouge' class FrenchPress class Post class Code < Generic def lang case @file_type when 'rb' 'ruby' when 'c' 'c' when 'css' 'css' when 'py' 'python' when 'yml' 'yaml' when 'swift' 'swift' when 'rs' 'rust' when 'go' 'go' when 'cpp' 'cpp' else '' end end def lexer case lang when 'ruby' Rouge::Lexers::Ruby.new when 'c' Rouge::Lexers::C.new when 'css' Rouge::Lexers::Css.new when 'python' Rouge::Lexers::Python.new when 'yaml' Rouge::Lexers::Yaml.new when 'swift' Rouge::Lexers::Swift.new when 'rust' Rouge::Lexers::Rust.new when 'go' Rouge::Lexers::Go.new when 'cpp' Rouge::Lexers::Cpp.new end end def render return "#{@content}" unless lexer renderer = Rouge::Formatters::HTML.new(css_class: 'highlight') "
#{renderer.format(lexer.lex(@content))}
" end end end end