Module: ApiGuides::MarkdownHelper

Included in:
Views::Example, Views::Reference, Views::Section
Defined in:
lib/api_guides/markdown_helper.rb

Defined Under Namespace

Classes: HTMLwithHighlighting

Instance Method Summary collapse

Instance Method Details

#left_align(string) ⇒ Object

Takes a string and removes trailing whitespace from the beginning of each line. It takes the number of leading whitespace characters from the first line and removes that from every single line in the string. It’s used to normalize strings that may be intended when writing the XML documents.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/api_guides/markdown_helper.rb', line 45

def left_align(string)
  return string unless string.match(/^(\s+)\S/)

  lines = string.gsub("\t", "  ").lines

  first_line = lines.select {|l| l.present?}.first

  if first_line =~ /^(\s+)\S/
    whitespace = $~[1]

    aligned_string = lines.map do |line|
      line.gsub(/^\s{#{whitespace.length}}/, '')
    end.join('')
  else
    string
  end
end

#markdown(string) ⇒ Object

Simple helper to convert a string to markdown using all our custom hax (including syntax highligting). It uses Redcarpert to do the heavy lifting.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/api_guides/markdown_helper.rb', line 28

def markdown(string)
  content = left_align string

  md = ::Redcarpet::Markdown.new HTMLwithHighlighting, :auto_link => true, 
    :no_intra_emphis => true,
    :tables => true, 
    :fenced_code_blocks => true,
    :strikethrough => true

  md.render content
end