commit 91530fefc45b68d6c0d5866cc2eb7f346900af8b
parent 4e8c95efea596b95cb5d4d6544291a5f445dfab9
Author: David Eisinger <[email protected]>
Date: Mon, 30 Oct 2023 23:42:26 -0400
Update renumber script to lint
Diffstat:
| M | bin/renumber | | | 63 | +++++++++++++++++++++++++++++++++++++++------------------------ |
1 file changed, 39 insertions(+), 24 deletions(-)
diff --git a/bin/renumber b/bin/renumber
@@ -1,27 +1,42 @@
#!/usr/bin/env ruby
-filename = ARGV.last
-
-unless filename
- warn "Please supply a filename"
- exit 1
-end
-
-content = File.read(filename)
-
-refs = content.scan(/\[\d+\]:/)
-
-unless refs == refs.uniq
- warn "Error: duplicate refs detected"
- exit
+fix = ARGV.shift if ARGV.first == "--fix"
+files = Array(ARGV.shift || Dir.glob("./**/*.md"))
+
+files.each do |file|
+ content = File.read(file)
+
+ refs = content.scan(/^\[\d+\]:/)
+
+ unless refs == refs.uniq
+ warn "Error in #{file}: duplicate refs detected"
+ exit 1
+ end
+
+ idx = 0
+ code_block = false
+ refs = {}
+
+ updated = content.gsub(/\[\d+\]|```|`/) do |token|
+ case token
+ when "```", "`"
+ code_block = !code_block
+ token
+ else
+ if code_block
+ token
+ else
+ refs[token] ||= "[$$#{idx += 1}]"
+ end
+ end
+ end
+
+ updated.gsub!("$$", "")
+
+ if fix
+ File.write(file, updated)
+ elsif content != updated
+ warn "Links in #{file} are not in order"
+ exit 1
+ end
end
-
-links = content.scan(/\[\d+\]/).uniq
-
-links.zip(1..).each do |old_id, new_id|
- content.gsub! old_id, "[$$#{new_id}]"
-end
-
-content.gsub!("$$", "")
-
-File.write(filename, content)