Class: RepositoryConverter
- Inherits:
-
Object
- Object
- RepositoryConverter
- Defined in:
- lib/github-to-canvas/repository_converter.rb
Class Method Summary collapse
- .add_fis_links(options, html) ⇒ Object
- .adjust_converted_html(options, html) ⇒ Object
- .adjust_local_html_images(readme, raw_remote_url, branch) ⇒ Object
- .adjust_local_markdown_images(readme, raw_remote_url, branch) ⇒ Object
- .convert_to_html(markdown) ⇒ Object
- .create_data_element(repo_org, repo_name, aaq, prework) ⇒ Object
- .create_github_link_header(repo_path, options) ⇒ Object
- .escape_existing_html(markdown) ⇒ Object
- .fix_escaped_inline_html_code(html) ⇒ Object
- .fix_local_html_links(options, html, filepath) ⇒ Object
- .fix_local_images(options, markdown, raw_remote_url) ⇒ Object
- .get_github_base_url(filepath) ⇒ Object
- .get_repo_info(filepath) ⇒ Object
- .get_repo_url(filepath) ⇒ Object
- .local_file_conversion(options) ⇒ Object
- .remote_file_conversion(options) ⇒ Object
- .remove_footer(readme) ⇒ Object
- .remove_header(readme) ⇒ Object
- .remove_header_and_footer(html) ⇒ Object
- .remove_html_header(html) ⇒ Object
- .remove_line_breaks(html) ⇒ Object
- .set_raw_image_remote_url(filepath) ⇒ Object
Class Method Details
.add_fis_links(options, html) ⇒ Object
246 247 248 249 250 251 252 |
# File 'lib/github-to-canvas/repository_converter.rb', line 246 def self.add_fis_links(, html) repo_info = self.get_repo_info([:filepath]) html = html.sub(/<div id="git-data-element.*<header class="fis-header.*<\/header>/,'') # remove existing fis header header = self.create_github_link_header(repo_info[:repo_path], ) data_element = self.create_data_element(repo_info[:repo_org], repo_info[:repo_name], [:aaq], [:prework]) data_element + header + html end |
.adjust_converted_html(options, html) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/github-to-canvas/repository_converter.rb', line 53 def self.adjust_converted_html(, html) if [:remove_header_and_footer] html = self.(html) end if [:fis_links] || [:git_links] html = self.add_fis_links(, html) end if [:contains_html] html = self.fix_escaped_inline_html_code(html) end html end |
.adjust_local_html_images(readme, raw_remote_url, branch) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/github-to-canvas/repository_converter.rb', line 207 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) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/github-to-canvas/repository_converter.rb', line 194 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) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/github-to-canvas/repository_converter.rb', line 45 def self.convert_to_html(markdown) renderer = CustomRender.new(escape_html: true, prettify: true, hard_wrap: true) redcarpet = Redcarpet::Markdown.new(CustomRender, ={tables: true, autolink: true, fenced_code_blocks: true, disable_indented_code_blocks: true}) html = redcarpet.render(markdown) puts "Markdown converted to HTML" self.remove_line_breaks(html) end |
.create_data_element(repo_org, repo_name, aaq, prework) ⇒ Object
273 274 275 |
# File 'lib/github-to-canvas/repository_converter.rb', line 273 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 |
.create_github_link_header(repo_path, options) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/github-to-canvas/repository_converter.rb', line 254 def self.create_github_link_header(repo_path, ) # add link to associated repository github_repo_link = "<a class='fis-git-link' href='#{repo_path}' target='_blank' rel='noopener'><img id='repo-img' title='Open GitHub Repo' alt='GitHub Repo' /></a>" # add link to new issue form github_issue_link = "<a class='fis-git-link' href='#{repo_path}/issues/new' target='_blank' rel='noopener'><img id='issue-img' title='Create New Issue' alt='Create New Issue' /></a>" # add link to fork (forking handled by separate Flatiron server, generation of link handled via custom Canvas JS theme file) if ([:forkable]) github_fork_link = "<a class='fis-fork-link' id='fork-link' href='#{repo_path}/fork' target='_blank' rel='noopener'><img id='fork-img' title='Fork This Assignment' alt='Fork This Assignment' /></a>" "<header class='fis-header' style='visibility: hidden;'>#{github_fork_link}#{github_repo_link}#{github_issue_link}</header>" elsif [:git_links] "<header class='fis-header'>#{github_repo_link}#{github_issue_link}</header>" else "<header class='fis-header' style='visibility: hidden;'>#{github_repo_link}#{github_issue_link}</header>" end end |
.escape_existing_html(markdown) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/github-to-canvas/repository_converter.rb', line 102 def self.escape_existing_html(markdown) markdown = markdown.gsub(/```(\n|.)*```/) { |code| # all blocks code = code.gsub("<", "<") code = code.gsub(">", ">") } # markdown = markdown.gsub(/(`)(.+?)(`)/) { |code| # # all backticks # if code.match?(/<(.*?)>/) # code = code.gsub("<", "<") # code = code.gsub(">", ">") # end # code # } # markdown = markdown.gsub(/<(?!iframe)/, "<") # markdown = markdown.gsub(/(?<!iframe)>/, ">") # markdown = markdown.gsub(/```(.*?)```/) {|s| # byebug # } markdown end |
.fix_escaped_inline_html_code(html) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/github-to-canvas/repository_converter.rb', line 70 def self.fix_escaped_inline_html_code(html) # stops HTML/JSX code blocks from rendering as HTML in Canvas # html = html.gsub("&gt;</code>", "></code>") # html = html.gsub("&gt;</code>", "></code>") # fixes < and > code snippets # html = html.gsub(/<pre><code>(.*?)<\/code><\/pre>/) { |string| # byebug # } # html = html.gsub("&gt;", ">") # # fixes blockquotes # html = html.gsub(/\n<p>>\;(.*)\n>\;/) { |bq| # bq.delete_prefix!("\n<p>>") # "\n<blockquote>" + bq # } # html = html.gsub(/\n>\;(.*)\n>\;/) { |bq| # bq.delete_prefix!("\n>") # " " + bq # } # html = html.gsub(/\n>\;(.*)<\/p>/) { |bq| # bq.delete_prefix!("\n>\;") # bq.delete_suffix!("</p>") # " " + bq + "</blockquote>" # } html end |
.fix_local_html_links(options, html, filepath) ⇒ Object
149 150 151 152 153 154 155 156 |
# File 'lib/github-to-canvas/repository_converter.rb', line 149 def self.fix_local_html_links(, html, filepath) # fixes relative hyperlinks by appending the github path to the file filepath_base = filepath.match(/https:\/\/github.com\/.*?\/.*?\//).to_s filepath_base = self.get_github_base_url(filepath) html.gsub!(/a href="(?!(http|#)).*?"/) {|local_link| local_link[8..-2] } end |
.fix_local_images(options, markdown, raw_remote_url) ⇒ Object
158 159 160 161 162 163 |
# File 'lib/github-to-canvas/repository_converter.rb', line 158 def self.fix_local_images(, 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, [:branch]) self.adjust_local_html_images(markdown, raw_remote_url, [:branch]) markdown end |
.get_github_base_url(filepath) ⇒ Object
165 166 167 168 169 170 |
# File 'lib/github-to-canvas/repository_converter.rb', line 165 def self.get_github_base_url(filepath) remote = GithubInterface.git_remote(filepath) remote.gsub!("[email protected]:","https://github.com/") remote.gsub!(/.git$/,"") remote.strip! end |
.get_repo_info(filepath) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/github-to-canvas/repository_converter.rb', line 232 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) ⇒ Object
187 188 189 190 191 192 |
# File 'lib/github-to-canvas/repository_converter.rb', line 187 def self.get_repo_url(filepath) remote = GithubInterface.git_remote(filepath) remote.gsub!("[email protected]:","https://github.com/") remote.gsub!(/.git$/,"") remote.strip! end |
.local_file_conversion(options) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/github-to-canvas/repository_converter.rb', line 16 def self.local_file_conversion() # GithubInterface.get_updated_repo(options[:filepath], options[:branch]) markdown = RepositoryInterface.read_local_file([:filepath], [:file_to_convert]) raw_remote_url = self.set_raw_image_remote_url([:filepath]) markdown = self.escape_existing_html(markdown) if [:contains_html] markdown = self.fix_local_images(, markdown, raw_remote_url) html = self.convert_to_html(markdown) # self.fix_local_html_links(options, html, options[:filepath]) end |
.remote_file_conversion(options) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/github-to-canvas/repository_converter.rb', line 26 def self.remote_file_conversion() markdown = GithubInterface.read_remote([:filepath]) raw_remote_url = self.set_raw_image_remote_url([:filepath]) if [:contains_html] begin markdown = self.escape_existing_html(markdown) rescue puts "Error reading remote markdown" abort end end if (![:branch]) [:branch] = 'master' end markdown = self.fix_local_images(, markdown, raw_remote_url) html = self.convert_to_html(markdown) # self.fix_local_html_links(options, html, options[:filepath]) end |
.remove_footer(readme) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/github-to-canvas/repository_converter.rb', line 138 def self.(readme) readme.gsub(/<p class='util--hide'(.+?)<\/p>/,"") readme.gsub(/<p data-visibility='hidden'(.+?)<\/p>/,"") readme.gsub(/<p><\;p data-visibility='\;hidden'(.+?)<\/p>/,"") readme.gsub(/<p><\;p class='util--hide'\;(.+?)<\/p>/,"") end |
.remove_header(readme) ⇒ Object
133 134 135 136 |
# File 'lib/github-to-canvas/repository_converter.rb', line 133 def self.remove_header(readme) readme = readme.gsub(/^# .+?\n\n/,"") readme.gsub(/^# .+?\n/,"") end |
.remove_header_and_footer(html) ⇒ Object
127 128 129 130 131 |
# File 'lib/github-to-canvas/repository_converter.rb', line 127 def self.(html) new_html = self.remove_html_header(html) new_html = self.(new_html) new_html end |
.remove_html_header(html) ⇒ Object
145 146 147 |
# File 'lib/github-to-canvas/repository_converter.rb', line 145 def self.remove_html_header(html) html.gsub(/<h1>.*<\/h1>/,"") end |
.remove_line_breaks(html) ⇒ Object
226 227 228 |
# File 'lib/github-to-canvas/repository_converter.rb', line 226 def self.remove_line_breaks(html) html.gsub("\n",' ') end |
.set_raw_image_remote_url(filepath) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/github-to-canvas/repository_converter.rb', line 173 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("[email protected]:","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 |