class Bookpress::Utility

Public Class Methods

articlize(tree, parent = nil) click to toggle source
# File lib/bookpress.rb, line 101
def self.articlize(tree, parent = nil)
  unless tree.is_a?(String)
    article = ""
    tree.each do |key, value|
      html = ""
      if Utility.idify(key) == Utility.idify(parent)
        if value.is_a?(String)
          html << value
        else
          html << Utility.articlize(tree[key], key)
        end
      else
        html = "<article id='#{Utility.idify(key)}'>"
        html << ("<header><h1>#{Utility.titleize(key)}</h1></header>")
        if value.is_a?(String)
          html << value
        else
          html << Utility.articlize(tree[key], key)
        end
        html << "</article>"
      end
      article << html
    end
    article
  else
    tree
  end
end
idify(title) click to toggle source
# File lib/bookpress.rb, line 81
def self.idify(title)
  if title
    new_title           = title.sub(/(\d*_)/, '')
    really_new_title    = new_title.sub(/(\.\w*)/, '')
    extremely_new_title = really_new_title.sub(/ /, '_')
    extremely_new_title.downcase!
    extremely_new_title
  else
    ''
  end
end
markdown_renderer() click to toggle source
# File lib/bookpress.rb, line 130
def self.markdown_renderer
   Redcarpet::Markdown.new(Bookpress::HTMLRenderer, {
    autolink:                     true,
    disable_indented_code_blocks: true,
    fenced_code_blocks:           true,
    space_after_headers:          true
  })
end
orderify(title) click to toggle source
# File lib/bookpress.rb, line 93
def self.orderify(title)
  if title
    /(\d*)_/.match(title)[1]
  else
    ''
  end
end
titleize(title) click to toggle source
# File lib/bookpress.rb, line 63
def self.titleize(title)
  if title
    new_title        = title.sub(/(\d*_)/, '')
    really_new_title = new_title.sub(/(\.\w*)/, '')
    words            = really_new_title.to_s.split('_')
    words.each do |word|
      if word.length <3
        word.downcase!
      elsif word.length >3
        word.capitalize!
      end
    end
    words.join ' '
  else
    ''
  end
end