-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.rb
More file actions
58 lines (47 loc) · 1.52 KB
/
Copy pathhelper.rb
File metadata and controls
58 lines (47 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require 'pry'
# See files in the same directory:
# Given `helper_test.md`, returns `helper_modified.md`.
def number_questions_and_add_answer_templates(text)
# Number the first question
modified = "1." + text[1..-1]
# Number the rest of the questions and add answer templates
number = 1
new_question = /\n# [A-Z]/
answer_template = "\n<details><summary><b>Answer</b></summary>\n<p>\n\n``\n\n</p>\n</details>"
divider = "\n\n---\n\n"
modified.gsub!(new_question) do |str|
number += 1
"#{answer_template}#{divider}#{number}. #{str[-1]}"
end
# Add answer template to the last question
modified + answer_template
end
def label_code_snippets(text, language="ruby")
found = 0
code_snippet = /\n```\n/
modified = text.gsub(code_snippet) do |str|
found += 1
found.odd? ? "#{str[0..-2]}#{language}\n" : str
end
modified
end
def make_multiple_choice_lists(text)
multiple_choice_option = /\n[A-Z]\. /
modified = text.gsub(multiple_choice_option) do |str|
"\n- " + str[1..-1]
end
modified
end
def format_file(filename)
# Read in all of the file as a string
f = File.new("#{filename}.md")
text = f.read
numbered_with_answer_templates = number_questions_and_add_answer_templates(text)
code_labeled = label_code_snippets(numbered_with_answer_templates)
multiple_choice_listed = make_multiple_choice_lists(code_labeled)
# Write file
new_file = File.new("#{filename}_modified.md", 'w')
new_file.puts multiple_choice_listed
end
# EXAMPLE USAGE
# format_file("helper_test")