Pārlūkot izejas kodu

new autfix task that does nothing

ruduki2 5 gadi atpakaļ
vecāks
revīzija
425fb06af6
1 mainītis faili ar 65 papildinājumiem un 0 dzēšanām
  1. 65 0
      Rakefile

+ 65 - 0
Rakefile

@@ -141,6 +141,71 @@ task :clean do
   # TODO
 end
 
+task :autofix do
+  #check links
+  files.each do |file|
+    twee_source = File.read(file.source_file)
+    twee_source = twee_source.lines.each_with_index.map do |line, lineno|
+      line.chomp!
+      line
+=begin
+      line
+      .scan(%r{\[\[(.+?)\](\[(.+?)\])?\]})
+      .map { |x| x[0] }
+      .each do |link|
+        case link.scan('->').length
+        when 0
+          #link contains no ->, probably not a problem
+          text = dest = link
+          passages[title][:outbound_links] << \
+            { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
+        when 1
+          # no problem
+          text, dest = link.split('->')
+          passages[title][:outbound_links] << \
+            { passage: dest, source_line: "#{file.source_file}:#{lineno}" }
+        else
+          puts "#{file.source_file}:#{lineno} link contains multiple -> definitely a problem".colorize(:red)
+          next
+        end
+        if text.start_with?('\'')
+          if !text.end_with?('\'')
+            puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with single quote despite starting with one".colorize(:red)
+            next
+          end
+          text = text[1..-2]
+          text
+          .to_enum(:scan, '\'')
+          .map {Regexp.last_match.offset(0)[0]}
+          .each do |offset|
+            if text[offset - 1] != '\\'
+              puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted single quote in body".colorize(:red)
+            end
+          end
+        elsif text.start_with?('"')
+          if !text.end_with?('"')
+            puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) does not end with double quote despite starting with one".colorize(:red)
+            next
+          end
+          text = text[1..-2]
+          text
+          .to_enum(:scan, '"')
+          .map {Regexp.last_match.offset(0)[0]}
+          .each do |offset|
+            if text[offset - 1] != '\\'
+              puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains unquoted double quote in body".colorize(:red)
+            end
+          end
+        elsif text =~ /\A[\[\]'"\\]*\z/
+          puts "#{file.source_file}:#{lineno} link text (#{text.inspect}) contains special characters but is not quoted with single or double quotes".colorize(:red)
+        end
+      end
+=end
+    end.join("\n") + "\n"
+    File.write(file.source_file, twee_source)
+  end
+end
+
 task :lint do
   titles = Set.new
   passages = {}