github-to-canvas 0.0.58 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ class CourseCreationInterface
2
+
3
+ def initialize(file)
4
+ data = YAML.load(File.read("#{Dir.pwd}/#{file}"))
5
+ CanvasInterface.create_new_course(data["name"])
6
+ end
7
+
8
+
9
+ end
@@ -1,38 +1,14 @@
1
1
  class CreateCanvasLesson
2
2
 
3
- def initialize(course, filepath, file_to_convert, branch, name, type, save_to_github, fis_links, remove_header_and_footer, forkable)
4
- # name = name.split(/[- _]/).map(&:capitalize).join(' ')
5
- begin
6
- markdown = File.read("#{filepath}/#{file_to_convert}")
7
- rescue
8
- puts "#{file_to_convert} not found in current directory. Exiting..."
9
- abort
10
- end
11
- create_canvas_lesson(markdown, course, filepath, branch, name, type, save_to_github, fis_links, remove_header_and_footer, forkable)
12
- end
13
-
14
- def create_canvas_lesson(markdown, course, filepath, branch, name, type, save_to_github, fis_links, remove_header_and_footer, forkable)
15
- GithubInterface.get_updated_repo(filepath, branch)
16
- new_html = RepositoryConverter.convert(filepath, markdown, branch, remove_header_and_footer)
17
- if fis_links
18
- new_html = RepositoryConverter.add_fis_links(filepath, new_html, forkable)
19
- end
20
- response = CanvasInterface.submit_to_canvas(course, type, name, new_html)
3
+ # def initialize(course, filepath, file_to_convert, branch, name, type, save_to_github, fis_links, remove_header_and_footer, forkable)
4
+ # # name = name.split(/[- _]/).map(&:capitalize).join(' ')
21
5
 
22
- puts 'Creating .canvas file'
23
- CanvasDotfile.update_or_create(filepath, response, course, type)
24
-
25
- puts "Canvas lesson created. Lesson available at #{response['html_url']}."
6
+ # create_canvas_lesson(markdown, course, filepath, branch, name, type, save_to_github, fis_links, remove_header_and_footer, forkable)
7
+ # end
26
8
 
27
- # If --save option is used, the .canvas file gets committed and pushed to the remote repo
28
- if save_to_github
29
- puts 'Adding .canvas file'
30
- GithubInterface.git_add(filepath, '.canvas')
31
- puts 'Commiting .canvas file'
32
- GithubInterface.git_commit(filepath, 'AUTO: add .canvas file after migration')
33
- puts 'Pushing .canvas file'
34
- GithubInterface.git_push(filepath, branch)
35
- end
36
- end
9
+ # def create_canvas_lesson(markdown, course, filepath, branch, name, type, save_to_github, fis_links, remove_header_and_footer, forkable)
10
+ # response = CanvasInterface.submit_to_canvas(course, type, name, new_html)
11
+
12
+ # end
37
13
 
38
14
  end
@@ -1,3 +1,5 @@
1
+ require 'json'
2
+ require 'rest-client'
1
3
  class GithubInterface
2
4
 
3
5
  def self.cd_into_and(filepath, command)
@@ -44,4 +46,37 @@ class GithubInterface
44
46
  def self.git_push(filepath, branch)
45
47
  self.cd_into_and(filepath, "git push origin #{branch}")
46
48
  end
49
+
50
+ def self.read_remote(url)
51
+ if url.match(/https:\/\/github.com\//)
52
+ url = url.sub(/https:\/\/github.com\//, 'https://raw.githubusercontent.com/')
53
+ url = url.sub(/blob\//, '')
54
+ end
55
+ if !url.end_with?('.md')
56
+ url_fallback = url + '/main/README.md'
57
+ url = url + '/master/README.md'
58
+ end
59
+ begin
60
+ response = RestClient.get(url)
61
+ rescue
62
+ begin
63
+ response = RestClient.get(url_fallback)
64
+ return response.body
65
+ rescue
66
+ puts 'Error reading ' + url
67
+ end
68
+ end
69
+
70
+
71
+ end
72
+
73
+ def self.save_to_github(filepath, branch)
74
+ puts 'Adding .canvas file'
75
+ self.git_add(filepath, '.canvas')
76
+ puts 'Commiting .canvas file'
77
+ self.git_commit(filepath, 'AUTO: add .canvas file after migration')
78
+ puts 'Pushing .canvas file'
79
+ self.git_push(filepath, branch)
80
+ end
81
+
47
82
  end
@@ -1,36 +1,131 @@
1
1
  require 'redcarpet'
2
+
3
+ class CustomRender < Redcarpet::Render::HTML
4
+ def block_code(code, lang)
5
+ "<pre>" \
6
+ "<code>#{multi_line(code)}</code>" \
7
+ "</pre>"
8
+ end
9
+
10
+ def multi_line(code)
11
+ code.gsub(/\n(?=[^.])/, "<br />")
12
+ end
13
+ end
14
+
2
15
  class RepositoryConverter
16
+ def self.local_file_conversion(options)
17
+ GithubInterface.get_updated_repo(options[:filepath], options[:branch])
18
+ markdown = RepositoryInterface.read_local_file(options[:filepath], options[:file_to_convert])
19
+ raw_remote_url = self.set_raw_image_remote_url(options[:filepath])
20
+ markdown = self.escape_existing_html(markdown) if options[:contains_html]
21
+ markdown = self.fix_local_images(options, markdown, raw_remote_url)
22
+ html = self.convert_to_html(markdown)
23
+ # self.fix_local_html_links(options, html, options[:filepath])
24
+ end
3
25
 
4
- def self.convert(filepath, readme, branch, remove_header_and_footer)
5
- if remove_header_and_footer
6
- readme = self.remove_header(readme)
7
- readme = self.remove_footer(readme)
26
+ def self.remote_file_conversion(options)
27
+ markdown = GithubInterface.read_remote(options[:filepath])
28
+ raw_remote_url = self.set_raw_image_remote_url(options[:filepath])
29
+ if options[:contains_html]
30
+ puts "Contains HTML"
31
+ puts options[:contains_html]
32
+ markdown = self.escape_existing_html(markdown)
8
33
  end
9
- self.fix_local_images(filepath, readme, branch)
10
- self.convert_to_html(filepath, readme)
34
+ markdown = self.fix_local_images(options, markdown, raw_remote_url)
35
+ html = self.convert_to_html(markdown)
36
+ # self.fix_local_html_links(options, html, options[:filepath])
37
+ end
38
+
39
+ def self.convert_to_html(markdown)
40
+ redcarpet = Redcarpet::Markdown.new(CustomRender, options={tables: true, autolink: true, fenced_code_blocks: true})
41
+ html = redcarpet.render(markdown)
42
+ self.remove_line_breaks(html)
43
+ end
44
+
45
+ def self.adjust_converted_html(options, html)
46
+
47
+ if options[:remove_header_and_footer]
48
+ html = self.remove_header_and_footer(html)
49
+ end
50
+
51
+ if options[:fis_links]
52
+ html = self.add_fis_links(options, html)
53
+ end
54
+
55
+ if options[:contains_html]
56
+ html = self.fix_escaped_inline_html_code(html)
57
+ end
58
+
59
+ html
60
+ end
61
+
62
+ def self.fix_escaped_inline_html_code(html)
63
+ html = html.gsub("<code>&amp;lt;", "<code>&lt;")
64
+ html = html.gsub("&amp;gt;</code>", "&gt;</code>")
65
+ end
66
+
67
+
68
+ def self.escape_existing_html(markdown)
69
+ markdown = markdown.gsub(/<\/(?!iframe)/, "&lt;/")
70
+ markdown = markdown.gsub(/<(?!iframe)/, "&lt;")
71
+ markdown = markdown.gsub(/(?<!iframe)>/, "&gt;")
72
+ end
73
+
74
+ def self.remove_header_and_footer(html)
75
+ new_html = self.remove_html_header(html)
76
+ new_html = self.remove_footer(new_html)
77
+ new_html
11
78
  end
12
79
 
13
80
  def self.remove_header(readme)
14
- readme.gsub!(/^# .+?\n\n/,"")
81
+ readme = readme.gsub(/^# .+?\n\n/,"")
15
82
  readme.gsub(/^# .+?\n/,"")
16
83
  end
17
84
 
18
85
  def self.remove_footer(readme)
19
- readme.gsub(/<p (.+?)<\/p>/,"")
86
+ readme.gsub(/<p class='util--hide'(.+?)<\/p>/,"")
20
87
  end
21
88
 
22
- def self.fix_local_images(filepath, readme, branch)
23
- raw_remote_url = self.set_raw_image_remote_url(filepath)
24
- self.adjust_local_markdown_images(readme, raw_remote_url, branch)
25
- self.adjust_local_html_images(readme, raw_remote_url)
89
+ def self.remove_html_header(html)
90
+ html.gsub(/<h1>.*<\/h1>/,"")
26
91
  end
27
92
 
28
- def self.set_raw_image_remote_url(filepath)
93
+ def self.fix_local_html_links(options, html, filepath)
94
+ # fixes relative hyperlinks by appending the github path to the file
95
+ filepath_base = filepath.match(/https:\/\/github.com\/.*?\/.*?\//).to_s
96
+ filepath_base = self.get_github_base_url(filepath)
97
+ html.gsub!(/a href="(?!(http|#)).*?"/) {|local_link|
98
+ local_link[8..-2]
99
+ }
100
+ end
101
+
102
+ def self.fix_local_images(options, markdown, raw_remote_url)
103
+ # fixes markdown images with relative links by appending the raw githubusercontent path to the file
104
+ self.adjust_local_markdown_images(markdown, raw_remote_url, options[:branch])
105
+ self.adjust_local_html_images(markdown, raw_remote_url, options[:branch])
106
+ markdown
107
+ end
108
+
109
+ def self.get_github_base_url(filepath)
29
110
  remote = GithubInterface.git_remote(filepath)
30
- remote.gsub!("git@github.com:","https://raw.githubusercontent.com/")
31
- remote.gsub!("https://github.com/","https://raw.githubusercontent.com/")
111
+ remote.gsub!("git@github.com:","https://github.com/")
32
112
  remote.gsub!(/.git$/,"")
33
- remote.strip!
113
+ remote.strip!
114
+ end
115
+
116
+
117
+ def self.set_raw_image_remote_url(filepath)
118
+ if filepath.include? 'https://github.com/'
119
+ remote = filepath
120
+ else
121
+ remote = GithubInterface.git_remote(filepath)
122
+ end
123
+ raw_remote = remote.gsub("git@github.com:","https://raw.githubusercontent.com/")
124
+ raw_remote = raw_remote.gsub("https://github.com/","https://raw.githubusercontent.com/")
125
+ raw_remote = raw_remote.gsub(/\/blob\/master\/.*$/,"")
126
+ raw_remote = raw_remote.gsub(/\/blob\/main\/.*$/,"")
127
+ raw_remote = raw_remote.gsub(/.git$/,"")
128
+ raw_remote.strip
34
129
  end
35
130
 
36
131
  def self.get_repo_url(filepath)
@@ -53,33 +148,45 @@ class RepositoryConverter
53
148
  }
54
149
  end
55
150
 
56
- def self.adjust_local_html_images(readme, raw_remote_url)
151
+ def self.adjust_local_html_images(readme, raw_remote_url, branch)
57
152
  readme.gsub!(/src=(\'|\")[\s\S]*?(\'|\")/) { |image_source|
58
153
  if !image_source.match?('amazonaws.com') && !image_source.match?('https://') && !image_source.match?('http://') && !image_source.match?('youtube')
59
154
  image_source.gsub!(/(\'|\")/, "")
60
155
  image_source.gsub!(/src=/, '')
61
156
  image_source.strip!
62
- 'src="' + raw_remote_url + '/master/' + image_source + '"'
157
+ 'src="' + raw_remote_url + '/' + branch + '/' + image_source + '"'
63
158
  else
64
159
  image_source
65
160
  end
66
161
  }
67
162
  end
68
163
 
69
- def self.convert_to_html(filepath, readme)
70
- redcarpet = Redcarpet::Markdown.new(Redcarpet::Render::HTML, options={tables: true, autolink: true, fenced_code_blocks: true})
71
- # File.write("#{filepath}/README.html", redcarpet.render(readme))
72
- redcarpet.render(readme)
164
+ def self.remove_line_breaks(html)
165
+ html.gsub("\n",' ')
73
166
  end
74
167
 
75
- def self.add_fis_links(filepath, readme, forkable)
76
- repo_path = self.get_repo_url(filepath)
77
- repo_name = repo_path.split('/')[-1]
78
- repo_org = repo_path.split('/')[-2]
168
+
79
169
 
80
- header = self.create_github_link_header(repo_path, forkable)
81
- data_element = self.create_data_element(repo_org, repo_name)
82
- data_element + header + readme
170
+ def self.get_repo_info(filepath)
171
+ if !filepath.match?('https://github.com')
172
+ repo_path = self.get_repo_url(filepath)
173
+ else
174
+ repo_path = filepath
175
+ end
176
+
177
+ {
178
+ repo_path: repo_path,
179
+ repo_name: repo_path.split('/')[4],
180
+ repo_org: repo_path.split('/')[3]
181
+ }
182
+ end
183
+
184
+ def self.add_fis_links(options, html)
185
+ repo_info = self.get_repo_info(options[:filepath])
186
+ html = html.sub(/<div id="git-data-element.*<header class="fis-header.*<\/header>/,'') # remove existing fis header
187
+ header = self.create_github_link_header(repo_info[:repo_path], options[:forkable])
188
+ data_element = self.create_data_element(repo_info[:repo_org], repo_info[:repo_name], options[:aaq])
189
+ data_element + header + html
83
190
  end
84
191
 
85
192
  def self.create_github_link_header(repo_path, forkable)
@@ -92,14 +199,19 @@ class RepositoryConverter
92
199
  # add link to fork (forking handled by separate Flatiron server, generation of link handled via custom Canvas JS theme file)
93
200
  if (forkable)
94
201
  github_fork_link = "<a class='fis-fork-link' id='fork-link' href='#' target='_blank' rel='noopener'><img id='fork-img' title='Fork This Assignment' alt='Fork This Assignment' /></a>"
95
- "<header class='fis-header' style='visibility: hidden;'>#{github_fork_link}#{github_issue_link}</header>"
202
+ "<header class='fis-header' style='visibility: hidden;'>#{github_fork_link}#{github_repo_link}#{github_issue_link}</header>"
96
203
  else
97
204
  "<header class='fis-header' style='visibility: hidden;'>#{github_repo_link}#{github_issue_link}</header>"
98
205
  end
99
206
  end
100
207
 
101
- def self.create_data_element(repo_org, repo_name)
102
- "<div id='git-data-element' data-org='#{repo_org}' data-repo='#{repo_name}'></div>"
208
+ def self.create_data_element(repo_org, repo_name, aaq)
209
+ if (aaq)
210
+ "<div id='git-data-element' data-aaq='enabled' data-org='#{repo_org}' data-repo='#{repo_name}'></div>"
211
+ else
212
+ "<div id='git-data-element' data-org='#{repo_org}' data-repo='#{repo_name}'></div>"
213
+ end
103
214
  end
104
215
 
216
+
105
217
  end
@@ -0,0 +1,33 @@
1
+ class RepositoryInterface
2
+
3
+ def self.local_repo_post_submission(options, response)
4
+ # Updates or creates a local .canvas file
5
+ CanvasDotfile.update_or_create(options, response)
6
+
7
+ # If --save option is used, the .canvas file gets committed and pushed to the remote repo
8
+ if options[:save_to_github]
9
+ self.save_to_github(options[:filepath], options[:branch])
10
+ end
11
+ end
12
+
13
+ def self.get_name(filepath, html)
14
+ repo_info = RepositoryConverter.get_repo_info(filepath)
15
+ name = html[/<h1>.*<\/h1>/]
16
+ if name
17
+ name = name.sub('<h1>','').sub('</h1>','')
18
+ else
19
+ name = repo_info[:repo_name].split(/[- _]/).map(&:capitalize).join(' ')
20
+ end
21
+ name
22
+ end
23
+
24
+ def self.read_local_file(filepath, file_to_convert)
25
+ begin
26
+ markdown = File.read("#{filepath}/#{file_to_convert}")
27
+ rescue
28
+ puts "#{file_to_convert} not found in current directory. Exiting..."
29
+ abort
30
+ end
31
+ markdown
32
+ end
33
+ end
@@ -1,29 +1,10 @@
1
1
  class UpdateCanvasLesson
2
2
 
3
3
  def initialize(course, filepath, file_to_convert, branch, name, type, save_to_github, fis_links, remove_header_and_footer, only_update_content, id, forkable)
4
- # name = name.split(/[- _]/).map(&:capitalize).join(' ')
5
- begin
6
- markdown = File.read("#{filepath}/#{file_to_convert}")
7
- rescue
8
- puts "#{file_to_convert} not found in current directory. Exiting..."
9
- abort
10
- end
11
4
  update_canvas_lesson(course, markdown, filepath, branch, name, type, save_to_github, fis_links, remove_header_and_footer, only_update_content, id, forkable)
12
5
  end
13
6
 
14
7
  def update_canvas_lesson(course, markdown, filepath, branch, name, type, save_to_github, fis_links, remove_header_and_footer, only_update_content, id, forkable)
15
- # Pulls any updates that exist on GitHub
16
- GithubInterface.get_updated_repo(filepath, branch)
17
-
18
- # Converts markdown to HTML
19
- # Default is README.md. --file <FILENAME> can be used to override default.
20
- new_html = RepositoryConverter.convert(filepath, markdown, branch, remove_header_and_footer)
21
-
22
- # adds Flatiron School specific header and footer
23
- if fis_links
24
- new_html = RepositoryConverter.add_fis_links(filepath, new_html, forkable)
25
- end
26
-
27
8
  # Read the local .canvas file if --id <ID> is not used. Otherwise, use provided ID (--course <COURSE> also required)
28
9
  if !id
29
10
  canvas_data = CanvasDotfile.read_canvas_data
@@ -37,20 +18,7 @@ class UpdateCanvasLesson
37
18
 
38
19
  # Implements update on Canvas
39
20
  response = JSON.parse(CanvasInterface.update_existing_lesson(course, id, info[1], name, new_html, only_update_content))
40
-
41
- # Updates or creates a local .canvas file
42
- CanvasDotfile.update_or_create(filepath, response, course, info[1])
43
- end
44
- # If --save option is used, the .canvas file gets committed and pushed to the remote repo
45
- if save_to_github
46
- puts 'Adding .canvas file'
47
- GithubInterface.git_add(filepath, '.canvas')
48
- puts 'Commiting .canvas file'
49
- GithubInterface.git_commit(filepath, 'AUTO: add .canvas file after migration')
50
- puts 'Pushing .canvas file'
51
- GithubInterface.git_push(filepath, branch)
52
21
  end
53
-
54
22
  end
55
23
 
56
24
 
@@ -1,3 +1,3 @@
1
1
  class GithubToCanvas
2
- VERSION = "0.0.58"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github-to-canvas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.58
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - maxwellbenton
@@ -82,9 +82,11 @@ files:
82
82
  - lib/github-to-canvas.rb
83
83
  - lib/github-to-canvas/canvas_dotfile.rb
84
84
  - lib/github-to-canvas/canvas_interface.rb
85
+ - lib/github-to-canvas/course_creation_interface.rb
85
86
  - lib/github-to-canvas/create_canvas_lesson.rb
86
87
  - lib/github-to-canvas/github_interface.rb
87
88
  - lib/github-to-canvas/repository_converter.rb
89
+ - lib/github-to-canvas/repository_interface.rb
88
90
  - lib/github-to-canvas/update_canvas_lesson.rb
89
91
  - lib/github-to-canvas/version.rb
90
92
  homepage: https://github.com/learn-co-curriculum/github-to-canvas
@@ -106,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
108
  - !ruby/object:Gem::Version
107
109
  version: '0'
108
110
  requirements: []
109
- rubygems_version: 3.1.4
111
+ rubygems_version: 3.0.8
110
112
  signing_key:
111
113
  specification_version: 4
112
114
  summary: github-to-canvas is a tool for migrating and aligning GitHub content with