-
Notifications
You must be signed in to change notification settings - Fork 4
/
replacer.rb
executable file
·72 lines (70 loc) · 2.93 KB
/
replacer.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
#!/usr/bin/env ruby
def beginEndRegex(name)
/\\begin{#{name}}(\[(?<options>.*?)\])?({(?<title>[^}]+)})?(?<content>.*?)\\end{#{name}}/m
end
replacements = {
/^%.*$/ => -> { '' },
"\\`E" => -> { "È" },
/\\section{([^}]*)}/ => -> { "\n# #{$1}\n\n---\n" },
/\\alert\s*(\{([^}{]+|\g<1>)*\})/ => -> { "*__#{$2}__*" },
/``(.*)(''|")/ => -> { "\"#{$1}\"" },
/"(.*)('')/ => -> { "\"#{$1}\"" },
/\\texttt\s*(\{([^}{]+|\g<1>)*\})/ => -> { "`#{$2}`" },
/\\cil\s*(\{([^}{]+|\g<1>)*\})/ => -> { "`#{$2}`" },
/\\Cil\s*(\{([^}{]+|\g<1>)*\})/ => -> { "`#{$2}`" },
/\\(text)?bf\s*(?<content>\{([^}{]+|\g<content>)*\})/ => -> { "**#{$~[:content][1...-1]}**" },
/\\textit\s*(\{([^}{]+|\g<1>)*\})/ => -> { "*#{$2}*" },
/\\(my)?url\s*(\{([^}{]+|\g<1>)*\})/ => -> { "#{$3}" },
/\\emph\s*(\{([^}{]+|\g<1>)*\})/ => -> { "*#{$2}*" },
/\\item\s*(\[(.*)\])?\s+/ => -> { "* #{$2} " },
/\\(iz|en)\s*(?<content>\{([^}{]+|\g<content>)*\})/ => -> { "\n#{$~[:content][1...-1]}\n" },
beginEndRegex('itemize') => -> { "\n#{$~[:content][1...-1]}\n" },
beginEndRegex('quote') => -> {
"\n> #{$~[:content][1...-1]}\n\n"
},
/\\bl\s*(?<title>\{([^}{]+|\g<title>)*\})\s*(?<content>\{([^}{]+|\g<content>)*\})/m => -> {
title = $~[:title]
title = if title.nil? then "\n" else "\n## #{title}\n" end
"\n### #{$~[:title][1...-1]}\n\n#{$~[:content][1...-1]}\n"
},
beginEndRegex('block') => -> {
title = $~[:title]
title = if title.nil? then "\n" else "\n### #{title}\n" end
"#{title}\n\n#{$~[:content][1...-1]}\n"
},
/\\bx\s*(?<content>\{([^}{]+|\g<content>)*\})/m => -> { "\n#{$~[:content][1...-1]}\n" },
/\\fr(s\{[^}]*\})?\s*(?<title>\{([^}{]+|\g<title>)*\})\s*(?<content>\{([^}{]+|\g<content>)*\})/m => -> {
"\n## #{$~[:title][1...-1]}\n\n#{$~[:content][1...-1]}\n\n---"
},
beginEndRegex('frame') => -> {
title = $~[:title]
title = if title.nil? then "\n" else "\n## #{title}\n" end
"#{title}\n#{$~[:content][1...-1]}\n\n---"
},
/\{\\bf\s+(?<content>[^}]*)}/ => -> { "**#{$~[:content]}**" },
'\\\\' => -> { '<br>' },
'\\%' => -> { '%' },
'\\_' => -> { '_' },
/\\fg\s*(?<options>\{([^}{]+|\g<options>)*\})?\s*(?<path>\{([^}{]+|\g<path>)*\})?/ => -> {
"![](#{$~[:path][1...-1]})"
},
/\\sizedrangedcodet?{[^}]*}{(?<from>\d+)}{(?<to>\d+)}{(?<path>[^}]*)}/ => -> {
"\n```java\n"\
"{{% import-raw from=#{$~[:from]} to=#{$~[:to]} path=\"#{$~[:path]}\" %}}\n"\
"```\n"
},
}
files = ARGV
for file_name in files do
old_text = File.read(file_name)
loop do
new_text = old_text
for matcher, replacement in replacements do
puts "replacement"
new_text = new_text.gsub(matcher) { replacement.call }
end
break if old_text == new_text
old_text = new_text
end
File.open(file_name, "w") {|file| file.puts old_text }
end