|
@@ -147,12 +147,42 @@ task :autofix do
|
|
twee_source = File.read(file.source_file)
|
|
twee_source = File.read(file.source_file)
|
|
twee_source = twee_source.lines.each_with_index.map do |line, lineno|
|
|
twee_source = twee_source.lines.each_with_index.map do |line, lineno|
|
|
line.chomp!
|
|
line.chomp!
|
|
- line
|
|
|
|
|
|
+
|
|
|
|
+ line.gsub(%r{\[\[(?<link>[^\]]*?)\](?<link_mods>\[[^\]]*\])?\]}) do |m|
|
|
|
|
+ link = $~[:link]
|
|
|
|
+ link_mods = $~[:link_mods]
|
|
|
|
+
|
|
|
|
+ case link.scan('->').length
|
|
|
|
+ when 0
|
|
|
|
+ # this style of link doesn't need to be quoted, but it also shouldn't contain special chars
|
|
|
|
+ "[[#{link}]#{link_mods}]"
|
|
|
|
+ when 1
|
|
|
|
+ text, dest = link.split('->')
|
|
|
|
+ if text.start_with?("'")
|
|
|
|
+ text = text.end_with?("'") ? text[1..-2] : text[1..-1]
|
|
|
|
+ text.gsub!(/(?<!\\)'/) { "\\'" }
|
|
|
|
+ text.gsub!(/(?<!\\)\\(?!')/) { "\\\\" }
|
|
|
|
+ text = "'#{text}'"
|
|
|
|
+ elsif link.start_with?('"')
|
|
|
|
+ text = text.end_with?('"') ? text[1..-2] : text[1..-1]
|
|
|
|
+ text.gsub!(/(?<!\\)"/) { "\\\"" }
|
|
|
|
+ text.gsub!(/(?<!\\)\\(?!")/) { "\\\\" }
|
|
|
|
+ text = "\"#{text}\""
|
|
|
|
+ elsif link =~ /\A[\[\]'"\\]*\z/
|
|
|
|
+ text.gsub!(/(?<!\\)'/) { "\\'" }
|
|
|
|
+ text.gsub!(/(?<!\\)"/) { "\\\"" }
|
|
|
|
+ text.gsub!(/(?<!\\)\\(?!['"])/) { "\\\\" }
|
|
|
|
+ text = "'" + (text.gsub("'") { "\\'" }) + "'"
|
|
|
|
+ end
|
|
|
|
+ "[[#{text}->#{dest}]#{link_mods}]"
|
|
|
|
+ else
|
|
|
|
+ # TODO this doesn't really happen very much in practice, so don't worry about an autofix, just passthru
|
|
|
|
+ warn('multiple -> in link')
|
|
|
|
+ "[[#{link}]#{link_mods}]"
|
|
|
|
+ end
|
|
|
|
+ end
|
|
=begin
|
|
=begin
|
|
- line
|
|
|
|
- .scan(%r{\[\[(.+?)\](\[(.+?)\])?\]})
|
|
|
|
- .map { |x| x[0] }
|
|
|
|
- .each do |link|
|
|
|
|
|
|
+ .map do |link|
|
|
case link.scan('->').length
|
|
case link.scan('->').length
|
|
when 0
|
|
when 0
|
|
#link contains no ->, probably not a problem
|
|
#link contains no ->, probably not a problem
|