class Moodle2CC::Moodle2Converter::HtmlConverter

Constants

COURSE_TOKEN
MEDIA_TYPES
OBJECT_TOKEN
WEB_CONTENT_TOKEN
WIKI_TOKEN

Public Class Methods

new(canvas_files, moodle_course) click to toggle source
# File lib/moodle2cc/moodle2converter/html_converter.rb, line 11
def initialize(canvas_files, moodle_course)
  @moodle_course = moodle_course
  @file_index = {}
  @moodle_course.files.each do |f|
    @file_index[f.file_path.downcase + f.file_name.downcase] = canvas_files.find { |cc_f| cc_f.identifier == f.content_hash }
  end
end

Public Instance Methods

convert(content) click to toggle source
# File lib/moodle2cc/moodle2converter/html_converter.rb, line 19
def convert(content)
  return "" unless content
  content = update_links(content.gsub('id="main"', ''))
  content = convert_equations(content)
  content
end

Private Instance Methods

convert_equations(content) click to toggle source
# File lib/moodle2cc/moodle2converter/html_converter.rb, line 104
def convert_equations(content)
  # turn moodle equations ( e.g. $$3 * x$$ ) into canvas equations
  content.gsub(/\$\$([^\$]*)\$\$/) do |match|
    latex = $1.to_s.gsub("\"", "\\\"")
    if latex.length > 0
      url = "/equation_images/#{CGI.escape(CGI.escape(latex).gsub("+", "%20"))}"

      "<img class=\"equation_image\" title=\"#{latex}\" alt=\"#{latex}\" src=\"#{url}\">"
    else
      ""
    end
  end
end
lookup_cc_file(link) click to toggle source
# File lib/moodle2cc/moodle2converter/html_converter.rb, line 67
def lookup_cc_file(link)
  if match = link.match(/@@PLUGINFILE@@(.*)/)
    file_link(match.captures.first) || link
  end
end
replace_media_anchor(tag) click to toggle source
# File lib/moodle2cc/moodle2converter/html_converter.rb, line 40
def replace_media_anchor(tag)
  if tag.name == 'a'
    href = tag['href']
    match = href.match(/\.([A-z0-9]+)$/)
    if (match && MEDIA_TYPES.key?(match.captures[0]))
      tag['class'] = 'instructure_inline_media_comment'
    end
  end
end
update_url(link) click to toggle source
# File lib/moodle2cc/moodle2converter/html_converter.rb, line 50
def update_url(link)
  if canvas_link = lookup_cc_file(link)
    canvas_link
  elsif match = link.match(/\/mod\/(page|forum|assignment)\/view\.php\?.*id=(\d*)(#.*)?/)
    lookup_cc_link(match.captures[0], match.captures[1], match.captures[2]) || link
  elsif match = CGI::unescape(link).match(/\$\@ASSIGNVIEWBYID\*(\d*)@\$/)
    lookup_cc_link('assignment', match.captures[0], nil) || link
  elsif match = link.match(/file.php\/\d*(\/.*)/)
    file_link(match.captures.first) || link
  else
    link
  end
rescue => e
  Moodle2CC::OutputLogger.logger.info "invalid url #{link} - #{e.message}"
  link
end