class Slideoff::Markdown

Constants

PARSER_OPTIONS

Public Class Methods

render(text) click to toggle source
# File lib/slideoff/markdown.rb, line 22
def self.render(text)
  text ||= ""
  markdown = Redcarpet::Markdown.new(self, PARSER_OPTIONS)
  markdown.render(text)
end

Public Instance Methods

block_code(code, lang) click to toggle source
# File lib/slideoff/markdown.rb, line 125
def block_code(code, lang)
  colorized = Pygments.highlight(code, :lexer => lang || "text", :options => {:nowrap => true})
  code_lines = colorized.split("\n")
  code_lines.map! do |line|
    line = %{<span>&nbsp;</span>} if line.empty?
    %{<code>#{line}</code>}
  end
  lang = "data-lang=\"#{lang}\"" if !lang.nil? && !lang.empty?
  %{<pre #{lang}>#{code_lines.join}</pre>}
end
codespan(code) click to toggle source
# File lib/slideoff/markdown.rb, line 136
def codespan(code)
  %{<code class="inline">#{code}</code>}
end
normal_text(text) click to toggle source
# File lib/slideoff/markdown.rb, line 148
def normal_text(text)
  text.gsub!('« ', '«&nbsp;')
  text.gsub!(/ ([:;»!?])/, '&nbsp;\1')
  text.gsub!(' -- ', '—')
  text.gsub!('...', '…')
  text
end
parse_box(text) click to toggle source
# File lib/slideoff/markdown.rb, line 39
def parse_box(text)
  text.gsub!(/(\+\+\+)(.*?)\n\1(.+?)\n\1(.+?)(\n|$)/m) do
    %{<div class="box #{$2}"><div>#{$3}</div><div>#{$4}</div></div>}
  end
end
parse_colorizing(text) click to toggle source
# File lib/slideoff/markdown.rb, line 51
def parse_colorizing(text)
  text.gsub!(/(__)(.*?)\1(.*?)\1/) do
    %{<span class="text-#{$2}">#{$3}</span>}
  end
end
parse_description(text) click to toggle source
# File lib/slideoff/markdown.rb, line 57
def parse_description(text)
  item = /([^\n]+?)\n/
  separator = /\s*:\s+/
  text.gsub!(/(#{item}#{separator}#{item})+/m) do |m|
    scanned = m.scan(/#{item}#{separator}#{item}/m)
    html_list = scanned.map { |(word, desc)| %{<dt>#{word}</dt><dd>#{desc}</dd>} }.join
    %Q{<dl>#{html_list}</dl>}
  end
end
parse_flickr_image(text) click to toggle source
# File lib/slideoff/markdown.rb, line 67
def parse_flickr_image(text)
  text.gsub!(/!F\[(.+?)\](?:\[(.+?)\])?/) do
    id = $1
    size = $2 || :b
    begin
      flickr_image = FlickrImage.new(id)
      src = flickr_image.image_src #(size)
      author = flickr_image.author
      license = flickr_image.license["name"]
      license_url = flickr_image.license["url"]
      cc_attributes = flickr_image.license["cc_attributes"]
      alt = flickr_image.title
      page = flickr_image.page
    rescue Exception => e
      title = "Specify Flickr API key!"
      src = "http://www.placehold.it/1024x807&text=#{CGI.escape(title)}"
      author = "No author"
      license = "All Rights Reserved"
      license_url = ""
      cc_attributes = nil
      alt = title
      page = src
    end

    html = "<figure>"
    html << %{<img alt="#{alt}" src="#{src}"/>}
    html << "<figcaption>"
    html << %{<span class="flickr"></span>}
    html << %{<a href="#{page}" alt="#{author} on Flickr">#{author}</a>}
    html << %{<a href="#{license_url}" alt="#{license}">}
    if cc_attributes && cc_attributes.any?
      html << %{<span class="license license-cc"></span>}
      #cc_attributes.each do |cc|
        #html << %{<span class="license license-#{cc}"></span>}
      #end
    end
    html << %{</a>}
    html << "</figcaption>"
    html << "</figure>"
    html
  end
end
parse_highlight(text) click to toggle source
# File lib/slideoff/markdown.rb, line 45
def parse_highlight(text)
  text.gsub!(/(==)(.*?)\1(.*?)\1/) do
    %{<mark class="#{$2}">#{$3}</mark>}
  end
end
parse_interactive_steps(text) click to toggle source
# File lib/slideoff/markdown.rb, line 114
def parse_interactive_steps(text)
  text.gsub!(/!STEPS\[(.*)\]/) do
    step_count = $1.to_i
    html = ""
    step_count && step_count.times do |step|
      html << %{<div data-step="#{step}" class="step step-#{step} inactive" style="display: none;"></div>}
    end
    html
  end
end
parse_pause(text) click to toggle source
# File lib/slideoff/markdown.rb, line 110
def parse_pause(text)
  text.gsub!(/^!PAUSE\s*$/) { %{<p class="pause"></p>} }
end
preprocess(text) click to toggle source
# File lib/slideoff/markdown.rb, line 28
def preprocess(text)
  parse_highlight(text)
  parse_box(text)
  parse_colorizing(text)
  parse_description(text)
  parse_flickr_image(text)
  parse_pause(text)
  parse_interactive_steps(text)
  text
end
strikethrough(text) click to toggle source
# File lib/slideoff/markdown.rb, line 144
def strikethrough(text)
  "<s>#{text}</s>"
end
table(header, body) click to toggle source
# File lib/slideoff/markdown.rb, line 140
def table(header, body)
  %{<table class="striped"><thead>#{header}</thead><tbody>#{body}</tbody></table>}
end