class SimpleFAQ::Formatter

Constants

FORMATTING
MARKDOWN
REGEXES

Public Class Methods

new(body) click to toggle source
# File lib/simple_faq.rb, line 33
def initialize(body)
  @body = body.strip.gsub("\r", "")
  @section_list = ""
  format_section_titles
  format_qa_pairs
  format_intro
  format_section_list
  @body.gsub!(/(\n){1,}/, "\n")
end

Public Instance Methods

html() click to toggle source
# File lib/simple_faq.rb, line 43
def html
  FORMATTING[:body_wrap].sub("{body}", @body)
end

Private Instance Methods

format_intro() click to toggle source
# File lib/simple_faq.rb, line 107
def format_intro
  intro_tag_match = @body.scan(REGEXES[:intro]).flatten
  return unless intro_tag_match.size > 0

  intro_sub_string = intro_tag_match[0]
  intro_body = intro_sub_string.strip

  @body.sub!(intro_sub_string, MARKDOWN.render(intro_body))
end
format_qa_pairs() click to toggle source
# File lib/simple_faq.rb, line 67
def format_qa_pairs
  qas = @body.scan(REGEXES[:qa])
  qas.each_with_index do |qa_pair, qa_index|
    qa_sub_string = qa_pair[0]
    q = qa_pair[1]
    a = qa_pair[2]
    q_formatting = if a.match(REGEXES[:has_video])
      :q_with_video
    else
      :q
    end

    qa_id = "q#{qa_index}"

    a_with_markdown = MARKDOWN.render(a.strip)
    a_with_vimeo = process_vimeo_tags(a_with_markdown)

    q_formatted = FORMATTING[q_formatting].gsub('{q}', q.strip).gsub('{i}', qa_id)
    a_formatted = FORMATTING[:a].gsub('{a}', a_with_vimeo).gsub('{i}', qa_id)

    qa_html = q_formatted + "\n" + a_formatted

    @body.sub!(qa_sub_string, qa_html)
  end

end
format_section_list() click to toggle source
# File lib/simple_faq.rb, line 49
def format_section_list
  @body.gsub!('{section-list}', FORMATTING[:section_list].sub("{section-list}", @section_list))
end
format_section_titles() click to toggle source
# File lib/simple_faq.rb, line 53
def format_section_titles
  title_matches = @body.scan(REGEXES[:section_title])
  title_matches.each_with_index do |title_match, i|
    title_sub_string = title_match[0]
    title = title_match[1]

    title_anchor = FORMATTING[:title_anchor].gsub('{i}', i.to_s).gsub('{title}', title)
    title_link = FORMATTING[:title_link].gsub('{i}', i.to_s).gsub('{title}', title)

    @body.sub!(title_sub_string, title_anchor)
    @section_list << title_link
  end
end
process_vimeo_tags(s) click to toggle source
# File lib/simple_faq.rb, line 94
def process_vimeo_tags s
  r = s.dup
  streams = s.scan(REGEXES[:vimeo])

  streams.each do |stream|
    stream_tag = stream[0]
    stream_id = stream[1]
    stream_html = FORMATTING[:vimeo].sub('{stream_id}', stream_id)
    r.sub!(stream_tag, stream_html)
  end
  r
end