Class: Moodle2CC::CC::CCHelper::HtmlContentExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/moodle2cc/cc/cc_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(course, user, opts = {}) ⇒ HtmlContentExporter

Returns a new instance of HtmlContentExporter.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# 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

Instance Attribute Details

#courseObject (readonly)

Returns the value of attribute course.



210
211
212
# File 'lib/moodle2cc/cc/cc_helper.rb', line 210

def course
  @course
end

#media_object_flavorObject (readonly)

Returns the value of attribute media_object_flavor.



159
160
161
# File 'lib/moodle2cc/cc/cc_helper.rb', line 159

def media_object_flavor
  @media_object_flavor
end

#media_object_infosObject (readonly)

Returns the value of attribute media_object_infos.



159
160
161
# File 'lib/moodle2cc/cc/cc_helper.rb', line 159

def media_object_infos
  @media_object_infos
end

#referenced_filesObject

Returns the value of attribute referenced_files.



160
161
162
# File 'lib/moodle2cc/cc/cc_helper.rb', line 160

def referenced_files
  @referenced_files
end

#used_media_objectsObject (readonly)

Returns the value of attribute used_media_objects.



159
160
161
# File 'lib/moodle2cc/cc/cc_helper.rb', line 159

def used_media_objects
  @used_media_objects
end

#userObject (readonly)

Returns the value of attribute user.



210
211
212
# File 'lib/moodle2cc/cc/cc_helper.rb', line 210

def user
  @user
end

Instance Method Details

#html_content(html) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# 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 = {}) ⇒ Object



212
213
214
215
216
217
218
219
220
221
# 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