|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +def beginEndRegex(name) |
| 4 | + /\\begin{#{name}}(\[(?<options>.*?)\])?({(?<title>[^}]+)})?(?<content>.*?)\\end{#{name}}/m |
| 5 | +end |
| 6 | + |
| 7 | +replacements = { |
| 8 | + /^%.*$/ => -> { '' }, |
| 9 | + "\\`E" => -> { "È" }, |
| 10 | + /\\section{([^}]*)}/ => -> { "\n# #{$1}\n\n---\n" }, |
| 11 | + /\\alert\s*(\{([^}{]+|\g<1>)*\})/ => -> { "*__#{$2}__*" }, |
| 12 | + /``(.*)(''|")/ => -> { "\"#{$1}\"" }, |
| 13 | + /"(.*)('')/ => -> { "\"#{$1}\"" }, |
| 14 | + /\\texttt\s*(\{([^}{]+|\g<1>)*\})/ => -> { "`#{$2}`" }, |
| 15 | + /\\cil\s*(\{([^}{]+|\g<1>)*\})/ => -> { "`#{$2}`" }, |
| 16 | + /\\Cil\s*(\{([^}{]+|\g<1>)*\})/ => -> { "`#{$2}`" }, |
| 17 | + /\\(text)?bf\s*(?<content>\{([^}{]+|\g<content>)*\})/ => -> { "**#{$~[:content][1...-1]}**" }, |
| 18 | + /\\textit\s*(\{([^}{]+|\g<1>)*\})/ => -> { "*#{$2}*" }, |
| 19 | + /\\(my)?url\s*(\{([^}{]+|\g<1>)*\})/ => -> { "#{$3}" }, |
| 20 | + /\\emph\s*(\{([^}{]+|\g<1>)*\})/ => -> { "*#{$2}*" }, |
| 21 | + /\\item\s*(\[(.*)\])?\s+/ => -> { "* #{$2} " }, |
| 22 | + /\\(iz|en)\s*(?<content>\{([^}{]+|\g<content>)*\})/ => -> { "\n#{$~[:content][1...-1]}\n" }, |
| 23 | + beginEndRegex('itemize') => -> { "\n#{$~[:content][1...-1]}\n" }, |
| 24 | + beginEndRegex('quote') => -> { |
| 25 | + "\n> #{$~[:content][1...-1]}\n\n" |
| 26 | + }, |
| 27 | + /\\bl\s*(?<title>\{([^}{]+|\g<title>)*\})\s*(?<content>\{([^}{]+|\g<content>)*\})/m => -> { |
| 28 | + title = $~[:title] |
| 29 | + title = if title.nil? then "\n" else "\n## #{title}\n" end |
| 30 | + "\n### #{$~[:title][1...-1]}\n\n#{$~[:content][1...-1]}\n" |
| 31 | + }, |
| 32 | + beginEndRegex('block') => -> { |
| 33 | + title = $~[:title] |
| 34 | + title = if title.nil? then "\n" else "\n### #{title}\n" end |
| 35 | + "#{title}\n\n#{$~[:content][1...-1]}\n" |
| 36 | + }, |
| 37 | + /\\bx\s*(?<content>\{([^}{]+|\g<content>)*\})/m => -> { "\n#{$~[:content][1...-1]}\n" }, |
| 38 | + /\\fr(s\{[^}]*\})?\s*(?<title>\{([^}{]+|\g<title>)*\})\s*(?<content>\{([^}{]+|\g<content>)*\})/m => -> { |
| 39 | + "\n## #{$~[:title][1...-1]}\n\n#{$~[:content][1...-1]}\n\n---" |
| 40 | + }, |
| 41 | + beginEndRegex('frame') => -> { |
| 42 | + title = $~[:title] |
| 43 | + title = if title.nil? then "\n" else "\n## #{title}\n" end |
| 44 | + "#{title}\n#{$~[:content][1...-1]}\n\n---" |
| 45 | + }, |
| 46 | + /\{\\bf\s+(?<content>[^}]*)}/ => -> { "**#{$~[:content]}**" }, |
| 47 | + '\\\\' => -> { '<br>' }, |
| 48 | + '\\%' => -> { '%' }, |
| 49 | + '\\_' => -> { '_' }, |
| 50 | + /\\fg\s*(?<options>\{([^}{]+|\g<options>)*\})?\s*(?<path>\{([^}{]+|\g<path>)*\})?/ => -> { |
| 51 | + "" |
| 52 | + }, |
| 53 | + /\\sizedrangedcodet?{[^}]*}{(?<from>\d+)}{(?<to>\d+)}{(?<path>[^}]*)}/ => -> { |
| 54 | + "\n```java\n"\ |
| 55 | + "{{% import-raw from=#{$~[:from]} to=#{$~[:to]} path=\"#{$~[:path]}\" %}}\n"\ |
| 56 | + "```\n" |
| 57 | + }, |
| 58 | +} |
| 59 | +files = ARGV |
| 60 | +for file_name in files do |
| 61 | + old_text = File.read(file_name) |
| 62 | + loop do |
| 63 | + new_text = old_text |
| 64 | + for matcher, replacement in replacements do |
| 65 | + puts "replacement" |
| 66 | + new_text = new_text.gsub(matcher) { replacement.call } |
| 67 | + end |
| 68 | + break if old_text == new_text |
| 69 | + old_text = new_text |
| 70 | + end |
| 71 | + File.open(file_name, "w") {|file| file.puts old_text } |
| 72 | +end |
0 commit comments