class Moodle2CC::CC::CCHelper::HtmlContentExporter

Attributes

course[R]
media_object_flavor[R]
media_object_infos[R]
referenced_files[RW]
used_media_objects[R]
user[R]

Public Class Methods

new(course, user, opts = {}) click to toggle source
# File lib/moodle2cc/cc/cc_helper.rb, line 162
def initialize(course, user, opts = {})
  @media_object_flavor = opts[:media_object_flavor]
  @used_media_objects = Set.new
  @media_object_infos = {}
  @rewriter = UserContent::HtmlRewriter.new(course, user)
  @course = course
  @user = user
  @track_referenced_files = opts[:track_referenced_files]
  @for_course_copy = opts[:for_course_copy]
  @referenced_files = {}

  @rewriter.set_handler('file_contents') do |match|
    if match.url =~ %r{/media_objects/(\d_\w+)}
      # This is a media object referencing an attachment that it shouldn't be
      "/media_objects/#{$1}"
    else
      match.url.gsub(/course( |%20)files/, WEB_CONTENT_TOKEN)
    end
  end
  @rewriter.set_handler('files') do |match|
    obj = match.obj_class.find_by_id(match.obj_id)
    next(match.url) unless obj && @rewriter.user_can_view_content?(obj)
    folder = obj.folder.full_name.gsub(/course( |%20)files/, WEB_CONTENT_TOKEN)
    @referenced_files[obj.id] = CCHelper.create_key(obj) if @track_referenced_files && !@referenced_files[obj.id]
    # for files, turn it into a relative link by path, rather than by file id
    # we retain the file query string parameters
    "#{folder}/#{URI.escape(obj.display_name)}#{CCHelper.file_query_string(match.rest)}"
  end
  @rewriter.set_handler('wiki') do |match|
    "#{WIKI_TOKEN}/#{match.type}#{match.rest}"
  end
  @rewriter.set_default_handler do |match|
    new_url = match.url
    if match.obj_id
      obj = match.obj_class.find_by_id(match.obj_id)
      if obj && @rewriter.user_can_view_content?(obj)
        # for all other types,
        # create a migration id for the object, and use that as the new link
        migration_id = CCHelper.create_key(obj)
        new_url = "#{OBJECT_TOKEN}/#{match.type}/#{migration_id}"
      end
    else
      new_url = "#{COURSE_TOKEN}/#{match.type}#{match.rest}"
    end
    new_url
  end
end

Public Instance Methods

html_content(html) click to toggle source
# File lib/moodle2cc/cc/cc_helper.rb, line 223
def html_content(html)
  html = @rewriter.translate_content(html)
  return html if html.blank? || @for_course_copy

  # keep track of found media comments, and translate them into links into the files tree
  # if imported back into canvas, they'll get uploaded to the media server
  # and translated back into media comments
  doc = Nokogiri::HTML::DocumentFragment.parse(html)
  doc.css('a.instructure_inline_media_comment').each do |anchor|
    next unless anchor['id']
    media_id = anchor['id'].gsub(/^media_comment_/, '')
    obj = course.media_objects.by_media_id(media_id).first
    if obj && obj.context == course && migration_id = CCHelper.create_key(obj)
      @used_media_objects << obj
      info = CCHelper.media_object_info(obj, nil, media_object_flavor)
      @media_object_infos[obj.id] = info
      anchor['href'] = File.join(WEB_CONTENT_TOKEN, MEDIA_OBJECTS_FOLDER, info[:filename])
    end
  end

  return doc.to_s
end
html_page(html, title, meta_fields={}) click to toggle source
# File lib/moodle2cc/cc/cc_helper.rb, line 212
def html_page(html, title, meta_fields={})
  content = html_content(html)
  meta_html = ""
  meta_fields.each_pair do |k, v|
    next unless v.present?
    meta_html += %{<meta name="#{k}" content="#{v}"/>\n}
  end

  %{<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n<title>#{title}</title>\n#{meta_html}</head>\n<body>\n#{content}\n</body>\n</html>}
end