-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.rb
116 lines (98 loc) · 2.52 KB
/
convert.rb
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
59
60
61
62
63
64
65
66
67
68
69
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env ruby
require 'rspec'
class DokuConvertor
attr_reader :input
private :input
def initialize(input:)
@input = input
end
def call
input.split("\n").map do |line|
replace_headings(
process_links(
line
)
)
end.join("\n")
end
def replace_headings(line)
case line
when /====== (.*) =====/ then "# #{$1}"
when /===== (.*) =====/ then "## #{$1}"
when /==== (.*) ====/ then "### #{$1}"
when /=== (.*) ===/ then "#### #{$1}"
when /== (.*) ==/ then "##### #{$1}"
when /= (.*) =/ then "###### #{$1}"
else line
end
end
def process_links(line)
line.gsub(/\[\[(.*)\]\]/) do |match|
"[#{$1}](/wiki/#{$1.downcase.gsub(' ','_')})"
end
end
end
RSpec.describe DokuConvertor do
subject { described_class.new(input: input).call }
let(:input) { nil }
describe "Processing headers" do
describe "h1" do
let(:input) { "====== Ruby ======" }
it { is_expected.to eql "# Ruby" }
end
describe "h2" do
let(:input) { "===== Ruby =====" }
it { is_expected.to eql "## Ruby" }
end
describe "h3" do
let(:input) { "==== Ruby ====" }
it { is_expected.to eql "### Ruby" }
end
describe "h4" do
let(:input) { "=== Ruby ===" }
it { is_expected.to eql "#### Ruby" }
end
describe "h5" do
let(:input) { "== Ruby ==" }
it { is_expected.to eql "##### Ruby" }
end
describe "h6" do
let(:input) { "= Ruby =" }
it { is_expected.to eql "###### Ruby" }
end
end
describe "Processing links" do
context "that are internal" do
let(:input) { " * [[Useful Java Classes]]" }
it { is_expected.to eql " * [Useful Java Classes](/wiki/useful_java_classes)" }
end
context "that are external" do
let(:input) { " * [[Useful Java Classes]]" }
it { is_expected.to eql " * [Useful Java Classes](/wiki/useful_java_classes)" }
end
end
describe "Processing multiple lines" do
let(:input) { <<~WIKI
====== Ruby ======
===== Rails =====
Ruby on Rails is a web framework
WIKI
}
it { is_expected.to eql <<~MARKDOWN
# Ruby
## Rails
Ruby on Rails is a web framework
MARKDOWN
.strip
}
end
end
Dir.glob("import/wiki/*.md").each do |file|
content = File.open(file).read
new_content = DokuConvertor.new(input: content).call
new_path = file.gsub(/^import/, "content/blog")
f = File.open(new_path, "w")
f.write(new_content)
f.close
puts new_path
end