module EvilFront::Typograph

Abstract module to be used in `Russian` and `English` typographs.

Public Instance Methods

typograph(text) click to toggle source

Insert non-break spaces and mark quotes to have nice text.

EvilFront::Russian.typograph(article)
# File lib/evil-front/typograph.rb, line 18
def typograph(text)
  return text if text.nil? or text.empty?

  text.gsub! '"', '"'
  text = use_right_symbols(text)
  tiny_words.each { |regexp| text.gsub! regexp, "\\1\\2 " }

  text
end
typograph_html(html) click to toggle source

Like `typograph`, but process only text nodes in HTML.

EvilFront::Russian.typograph_html(article.html)
# File lib/evil-front/typograph.rb, line 11
def typograph_html(html)
  process_html(html) { |text| typograph(text) }
end

Private Instance Methods

process_html(html) { |html| ... } click to toggle source

Parse HTML and run block only on texts

# File lib/evil-front/typograph.rb, line 31
def process_html(html, &block)
  return html if html.nil? or html.empty?

  if html.include? '<'
    nodes = Nokogiri::HTML::DocumentFragment.parse(html, 'utf-8')
    process_node!(nodes, &block)
    nodes.to_html
  else
    yield(html)
  end
end
process_node!(node) { |text| ... } click to toggle source

Run block on every text node recursively

# File lib/evil-front/typograph.rb, line 44
def process_node!(node, &block)
  return if %w(pre code kbd script style math).include? node.name

  node.children.each do |child|
    if child.is_a? Nokogiri::XML::Text
      text = EvilFront.escape(child.content)
      child.replace( yield(text) )
    else
      process_node! child, &block
    end
  end
end