Ruduki2 6 年 前
コミット
0253f13cc9
1 ファイル変更58 行追加0 行削除
  1. 58 0
      Rakefile

+ 58 - 0
Rakefile

@@ -57,4 +57,62 @@ end
 
 task :clean do
   # TODO
+end
+
+task :lint do
+  FILES.each do |short_name, long_name|
+    filename = "#{short_name}.tw2"
+    twee_source = IO.read(filename)
+    twee_source.lines.each_with_index do |line, lineno|
+      lineno += 1
+      line
+      .scan(%r{\[\[([^\]]*)\]\]})
+      .map { |x| x[0] }
+      .each do |link|
+        case link.scan('->').length
+        when 0
+          #puts("link contains no ->, probably not a problem")
+          next
+        when 1
+          # no problem
+        else
+          puts("link contains multiple -> definitely a problem")
+          next
+        end
+        text, dest = link.split('->')
+        if text.start_with?('\'')
+          if !text.end_with?('\'')
+            puts("#{filename}:#{lineno} link text (#{text.inspect}) does not end with single quote despite starting with one")
+            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 "#{filename}:#{lineno} link text (#{text.inspect}) contains unquoted single quote in body"
+            end
+          end
+        elsif text.start_with?('"')
+          if !text.end_with?('"')
+            puts("#{filename}:#{lineno} link text (#{text.inspect}) does not end with double quote despite starting with one")
+            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 "#{filename}:#{lineno} link text (#{text.inspect}) contains unquoted double quote in body"
+            end
+          end
+        elsif text =~ /\A[\[\]'"\\]*\Z/
+          puts "#{filename}:#{lineno} link text (#{text.inspect}) contains special characters but is not quoted with single or double quotes"
+        end
+        #puts(dest)
+      end
+    end
+  end
 end