class IframesController < ApplicationController
  layout false
  skip_before_filter :verify_authenticity_token, :only => [:html_content]

  include ApplicationHelper
  def show
    render :layout => false
  end

  # 渲染实训代码
  # educodercss: 字符串以 ‘,’分隔,存储的是版本库css的路径
  # educoderscript: 字符串以 ‘,’分隔,存储的是版本库js的路径
  # contents: html实训的整体内容
  def html_content
    @contents = params[:contents] || ""
    gpid = params[:gpid]
    edu_css = params[:educodercss]
    edu_js = params[:educoderscript]
    @g = Gitlab.client
    @rev = "master"
    if @contents.present?
      @contents = @contents.gsub("w3equalsign", "=").gsub("w3scrw3ipttag", "script").gsub("edulink", "link").html_safe
    end
    # css
    if edu_css.present?
      css_path = edu_css.split(",")
      css_path.each do |path|
        file_content = @g.files(gpid, path, @rev).content
        file_content = tran_base64_decode64(file_content) unless file_content.blank?
        @contents = @contents.sub(/EDUCODERCSS/, "<style>#{file_content}</style>")
      end
    end
    # js
    if edu_js.present?
      js_path = edu_js.split(",")
      js_path.each do |path|
        file_content = @g.files(gpid, path, @rev).content
        file_content = tran_base64_decode64(file_content) unless file_content.blank?
        @contents = @contents.sub(/EDUCODERJS/, "<script>#{file_content}</script>")
      end
    end
    respond_to do |format|
      format.js
      format.html
    end
  end
end