class RepositoryConverter

Public Class Methods

adjust_converted_html(options, html) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 43
def self.adjust_converted_html(options, html)
  if options[:remove_header_and_footer]
    html = self.remove_header_and_footer(html)
  end
  
  if options[:fis_links] || options[:git_links]
    html = self.add_fis_links(options, html)
  end

  html
end
adjust_local_html_images(readme, raw_remote_url, branch) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 135
def self.adjust_local_html_images(readme, raw_remote_url, branch)
  readme.gsub(/src=(\'|\")[\s\S]*?(\'|\")/) { |image_source|
    
    if !image_source.match?('amazonaws.com') && !image_source.match?('https://') && !image_source.match?('http://') && !image_source.match?('youtube') && !image_source.match(/src=(\'|\")(?=<%)/)
      image_source = image_source.gsub(/(\'|\")/, "")
      image_source = image_source.gsub(/src=/, '')
      image_source = image_source.strip

      begin
        'src="' + raw_remote_url + '/' + branch + '/' + image_source + '"'
      rescue
        puts "Error adjust HTML images - check images in Canvas"
      end
    else
      image_source
    end
  }
end
adjust_local_markdown_images(readme, raw_remote_url, branch) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 122
def self.adjust_local_markdown_images(readme, raw_remote_url, branch)
  readme.gsub(/\!\[.+\]\(.+\)/) {|image_markdown|
    if !image_markdown.match?('amazonaws.com') && !image_markdown.match?('https://') && !image_markdown.match?('http://') && !image_markdown.match?('youtube')
      image_markdown.gsub!(/\(.+\)/) { |path|
        path.delete_prefix!("(")
        path.delete_suffix!(")")
        "(" + raw_remote_url + "/#{branch}/" + path + ")"
      }
    end
    image_markdown
  }
end
convert_to_html(markdown) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 30
def self.convert_to_html(markdown)
  options = {
    tables: true,
    autolink: true,
    fenced_code_blocks: true,
    no_intra_emphasis: true
  }
  redcarpet = Redcarpet::Markdown.new(CustomRender, options)
  html = redcarpet.render(markdown)
  puts "Markdown converted to HTML"
  html
end
create_data_element(repo_org, repo_name, aaq, prework) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 201
def self.create_data_element(repo_org, repo_name, aaq, prework)
  "<div id='git-data-element' #{prework ? "data-prework='true'" : ""} #{aaq ? "data-aaq='enabled'" : ""} data-org='#{repo_org}' data-repo='#{repo_name}'></div>"
end
fix_local_images(options, markdown, raw_remote_url) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 86
def self.fix_local_images(options, markdown, raw_remote_url)
  # fixes markdown images with relative links by appending the raw githubusercontent path to the file
  self.adjust_local_markdown_images(markdown, raw_remote_url, options[:branch])
  self.adjust_local_html_images(markdown, raw_remote_url, options[:branch])
  markdown
end
get_github_base_url(filepath) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 93
def self.get_github_base_url(filepath)
  remote = GithubInterface.git_remote(filepath)
  remote.gsub!("git@github.com:","https://github.com/")
  remote.gsub!(/.git$/,"")
  remote.strip! 
end
get_repo_info(filepath) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 160
def self.get_repo_info(filepath)
  if !filepath.match?('https://github.com')
    repo_path = self.get_repo_url(filepath)
  else
    repo_path = filepath
  end

  {
    repo_path: repo_path,
    repo_name: repo_path.split('/')[4],
    repo_org: repo_path.split('/')[3]
  }
end
get_repo_url(filepath) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 115
def self.get_repo_url(filepath)
  remote = GithubInterface.git_remote(filepath)
  remote.gsub!("git@github.com:","https://github.com/")
  remote.gsub!(/.git$/,"")
  remote.strip!
end
local_file_conversion(options) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 10
def self.local_file_conversion(options)
  # GithubInterface.get_updated_repo(options[:filepath], options[:branch])
  markdown = RepositoryInterface.read_local_file(options[:filepath], options[:file_to_convert])
  raw_remote_url = self.set_raw_image_remote_url(options[:filepath])
  markdown = self.fix_local_images(options, markdown, raw_remote_url)
  html = self.convert_to_html(markdown)
  # self.fix_local_html_links(options, html, options[:filepath])
end
remote_file_conversion(options) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 19
def self.remote_file_conversion(options)
  markdown = GithubInterface.read_remote(options[:filepath])
  raw_remote_url = self.set_raw_image_remote_url(options[:filepath])
  if (!options[:branch])
    options[:branch] = 'master'
  end
  markdown = self.fix_local_images(options, markdown, raw_remote_url)
  html = self.convert_to_html(markdown)
  # self.fix_local_html_links(options, html, options[:filepath])
end
remove_header(readme) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 61
def self.remove_header(readme)
  readme = readme.gsub(/^# .+?\n\n/,"")
  readme.gsub(/^# .+?\n/,"")
end
remove_html_header(html) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 73
def self.remove_html_header(html)
  html.gsub(/<h1>.*?<\/h1>/,"")
end
remove_line_breaks(html) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 154
def self.remove_line_breaks(html)
  html.gsub("\n",' ')
end
set_raw_image_remote_url(filepath) click to toggle source
# File lib/github-to-canvas/repository_converter.rb, line 101
def self.set_raw_image_remote_url(filepath)
  if filepath.include? 'https://github.com/'
    remote = filepath
  else
    remote = GithubInterface.git_remote(filepath)
  end
  raw_remote = remote.gsub("git@github.com:","https://raw.githubusercontent.com/")
  raw_remote = raw_remote.gsub("https://github.com/","https://raw.githubusercontent.com/")
  raw_remote = raw_remote.gsub(/\/blob\/master\/.*$/,"")
  raw_remote = raw_remote.gsub(/\/blob\/main\/.*$/,"")
  raw_remote = raw_remote.gsub(/.git$/,"")
  raw_remote.strip
end