github-to-canvas 0.0.58 → 0.1.0

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,6 @@
1
+ require 'byebug'
2
+ require 'json'
3
+ require 'rest-client'
1
4
  class GithubInterface
2
5
 
3
6
  def self.cd_into_and(filepath, command)
@@ -44,4 +47,36 @@ class GithubInterface
44
47
  def self.git_push(filepath, branch)
45
48
  self.cd_into_and(filepath, "git push origin #{branch}")
46
49
  end
50
+
51
+ def self.read_remote(url)
52
+ if url.match(/https:\/\/github.com\//)
53
+ url = url.sub(/https:\/\/github.com\//, 'https://raw.githubusercontent.com/')
54
+ url = url.sub(/blob\//, '')
55
+ end
56
+ if !url.end_with?('.md')
57
+ url_fallback = url + '/main/README.md'
58
+ url = url + '/master/README.md'
59
+ end
60
+ begin
61
+ response = RestClient.get(url)
62
+ rescue
63
+ begin
64
+ response = RestClient.get(url_fallback)
65
+ rescue
66
+ puts 'Error reading ' + url
67
+ end
68
+ end
69
+
70
+ response.body
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,13 +1,59 @@
1
1
  require 'redcarpet'
2
+ require 'byebug'
3
+
4
+ class CustomRender < Redcarpet::Render::HTML
5
+ def block_code(code, lang)
6
+ "<pre>" \
7
+ "<code>#{multi_line(code)}</code>" \
8
+ "</pre>"
9
+ end
10
+
11
+ def multi_line(code)
12
+ code.gsub(/\n(?=[^.])/, "<br />")
13
+ end
14
+ end
15
+
2
16
  class RepositoryConverter
17
+ def self.local_file_conversion(options)
18
+ GithubInterface.get_updated_repo(options[:filepath], options[:branch])
19
+ markdown = RepositoryInterface.read_local_file(options[:filepath], options[:file_to_convert])
20
+ raw_remote_url = self.set_raw_image_remote_url(options[:filepath])
21
+
22
+ markdown = self.fix_local_images(options, markdown, raw_remote_url)
23
+ html = self.convert_to_html(markdown)
24
+ # self.fix_local_html_links(options, html, options[:filepath])
25
+ end
26
+
27
+ def self.remote_file_conversion(options)
28
+ markdown = GithubInterface.read_remote(options[:filepath])
29
+ raw_remote_url = self.set_raw_image_remote_url(options[:filepath])
30
+ markdown = self.fix_local_images(options, markdown, raw_remote_url)
31
+ html = self.convert_to_html(markdown)
32
+ # self.fix_local_html_links(options, html, options[:filepath])
33
+ end
3
34
 
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)
35
+ def self.convert_to_html(markdown)
36
+ redcarpet = Redcarpet::Markdown.new(CustomRender, options={tables: true, autolink: true, fenced_code_blocks: true})
37
+ html = redcarpet.render(markdown)
38
+ self.remove_line_breaks(html)
39
+ end
40
+
41
+ def self.adjust_converted_html(options, html)
42
+
43
+ if options[:remove_header_and_footer]
44
+ html = self.remove_header_and_footer(html)
45
+ end
46
+
47
+ if options[:fis_links]
48
+ html = self.add_fis_links(options, html)
8
49
  end
9
- self.fix_local_images(filepath, readme, branch)
10
- self.convert_to_html(filepath, readme)
50
+ html
51
+ end
52
+
53
+ def self.remove_header_and_footer(html)
54
+ new_html = self.remove_html_header(html)
55
+ new_html = self.remove_footer(new_html)
56
+ new_html
11
57
  end
12
58
 
13
59
  def self.remove_header(readme)
@@ -16,21 +62,49 @@ class RepositoryConverter
16
62
  end
17
63
 
18
64
  def self.remove_footer(readme)
19
- readme.gsub(/<p (.+?)<\/p>/,"")
65
+ readme.gsub(/<p class='util--hide'(.+?)<\/p>/,"")
20
66
  end
21
67
 
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)
68
+ def self.remove_html_header(html)
69
+ html.gsub(/<h1>.*<\/h1>/,"")
26
70
  end
27
71
 
28
- def self.set_raw_image_remote_url(filepath)
72
+ def self.fix_local_html_links(options, html, filepath)
73
+ # fixes relative hyperlinks by appending the github path to the file
74
+ filepath_base = filepath.match(/https:\/\/github.com\/.*?\/.*?\//).to_s
75
+ filepath_base = self.get_github_base_url(filepath)
76
+ html.gsub!(/a href="(?!(http|#)).*?"/) {|local_link|
77
+ local_link[8..-2]
78
+ }
79
+ end
80
+
81
+ def self.fix_local_images(options, markdown, raw_remote_url)
82
+ # fixes markdown images with relative links by appending the raw githubusercontent path to the file
83
+ self.adjust_local_markdown_images(markdown, raw_remote_url, options[:branch])
84
+ self.adjust_local_html_images(markdown, raw_remote_url, options[:branch])
85
+ markdown
86
+ end
87
+
88
+ def self.get_github_base_url(filepath)
29
89
  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/")
90
+ remote.gsub!("git@github.com:","https://github.com/")
32
91
  remote.gsub!(/.git$/,"")
33
- remote.strip!
92
+ remote.strip!
93
+ end
94
+
95
+
96
+ def self.set_raw_image_remote_url(filepath)
97
+ if filepath.include? 'https://github.com/'
98
+ remote = filepath
99
+ else
100
+ remote = GithubInterface.git_remote(filepath)
101
+ end
102
+ raw_remote = remote.gsub("git@github.com:","https://raw.githubusercontent.com/")
103
+ raw_remote = raw_remote.gsub("https://github.com/","https://raw.githubusercontent.com/")
104
+ raw_remote = raw_remote.gsub(/\/blob\/master\/.*$/,"")
105
+ raw_remote = raw_remote.gsub(/\/blob\/main\/.*$/,"")
106
+ raw_remote = raw_remote.gsub(/.git$/,"")
107
+ raw_remote.strip
34
108
  end
35
109
 
36
110
  def self.get_repo_url(filepath)
@@ -53,33 +127,45 @@ class RepositoryConverter
53
127
  }
54
128
  end
55
129
 
56
- def self.adjust_local_html_images(readme, raw_remote_url)
130
+ def self.adjust_local_html_images(readme, raw_remote_url, branch)
57
131
  readme.gsub!(/src=(\'|\")[\s\S]*?(\'|\")/) { |image_source|
58
132
  if !image_source.match?('amazonaws.com') && !image_source.match?('https://') && !image_source.match?('http://') && !image_source.match?('youtube')
59
133
  image_source.gsub!(/(\'|\")/, "")
60
134
  image_source.gsub!(/src=/, '')
61
135
  image_source.strip!
62
- 'src="' + raw_remote_url + '/master/' + image_source + '"'
136
+ 'src="' + raw_remote_url + '/' + branch + '/' + image_source + '"'
63
137
  else
64
138
  image_source
65
139
  end
66
140
  }
67
141
  end
68
142
 
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)
143
+ def self.remove_line_breaks(html)
144
+ html.gsub("\n",' ')
73
145
  end
74
146
 
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]
147
+
148
+
149
+ def self.get_repo_info(filepath)
150
+ if !filepath.match?('https://github.com')
151
+ repo_path = self.get_repo_url(filepath)
152
+ else
153
+ repo_path = filepath
154
+ end
79
155
 
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
156
+ {
157
+ repo_path: repo_path,
158
+ repo_name: repo_path.split('/')[4],
159
+ repo_org: repo_path.split('/')[3]
160
+ }
161
+ end
162
+
163
+ def self.add_fis_links(options, html)
164
+ repo_info = self.get_repo_info(options[:filepath])
165
+ html = html.sub(/<div id="git-data-element.*<header class="fis-header.*<\/header>/,'') # remove existing fis header
166
+ header = self.create_github_link_header(repo_info[:repo_path], options[:forkable])
167
+ data_element = self.create_data_element(repo_info[:repo_org], repo_info[:repo_name])
168
+ data_element + header + html
83
169
  end
84
170
 
85
171
  def self.create_github_link_header(repo_path, forkable)
@@ -92,7 +178,7 @@ class RepositoryConverter
92
178
  # add link to fork (forking handled by separate Flatiron server, generation of link handled via custom Canvas JS theme file)
93
179
  if (forkable)
94
180
  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>"
181
+ "<header class='fis-header' style='visibility: hidden;'>#{github_fork_link}#{github_repo_link}#{github_issue_link}</header>"
96
182
  else
97
183
  "<header class='fis-header' style='visibility: hidden;'>#{github_repo_link}#{github_issue_link}</header>"
98
184
  end
@@ -102,4 +188,5 @@ class RepositoryConverter
102
188
  "<div id='git-data-element' data-org='#{repo_org}' data-repo='#{repo_name}'></div>"
103
189
  end
104
190
 
191
+
105
192
  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.0"
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.0
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