Class: Forem::Formatters::Kramdown

Inherits:
Object
  • Object
show all
Defined in:
lib/forem/formatters/kramdown.rb

Overview

Public: Various methods useful for formatting text with Kramdown.

Class Method Summary collapse

Class Method Details

.blockquote(text) ⇒ Object



16
17
18
# File 'lib/forem/formatters/kramdown.rb', line 16

def self.blockquote(text)
  text.to_s.split("\n").map { |line| "> #{line}" }.join("\n")
end

.format(text) ⇒ Object



11
12
13
14
# File 'lib/forem/formatters/kramdown.rb', line 11

def self.format(text)
  renderer = ::Kramdown::Document.new(text.to_s)
  syntax_highlight(renderer.to_html).html_safe
end

.syntax_highlight(html) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/forem/formatters/kramdown.rb', line 20

def self.syntax_highlight(html)
  doc = ::Nokogiri::HTML::DocumentFragment.parse(html.to_s)
  doc.css("pre code[@class]").each do |code|
    line_numbers = { :line_numbers => :inline }
    language = code[:class].to_sym
    syntax = CodeRay.scan(code.text.rstrip, language).div(line_numbers)
    code.replace %(<div class="forem_highlight">#{syntax}</div>)
  end
  doc.to_s.html_safe
end