Created
October 2, 2018 13:07
-
-
Save klebervirgilio/c8a8bf0a11b489f5fc1911e302e582db to your computer and use it in GitHub Desktop.
Order awesomo/**/*.md files alphabetically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file = File.open("/path/to/file.md", "r").each_line | |
header = file.take(2) | |
title = header.join.gsub(/\W/,"") | |
groups = file.slice_when { |line| line.chomp == "---" }.to_a | |
sorted = groups.sort_by { |(line, *_)| line.downcase.match(/\[(.*)\]/).to_s.gsub(/\W/,"") } | |
BANNER = proc do |title, vowel| | |
%Q<## Alphabetical index of projects in #{title}: | |
| | | | | | | | | |
|--- |--- |--- |--- |--- |--- | ---| | |
|[A](#a)|[B](#b)|[C](#c)|[D](#d)|[E](#e)|[F](#f)|[G](#g)| | |
|[H](#h)|[I](#i)|[J](#j)|[K](#k)|[L](#l)|[M](#m)|[N](#n)| | |
|[P](#p)|[Q](#q)|[R](#r)|[S](#s)|[T](#t)|[U](#u)|[V](#v)| | |
|[W](#w)|[X](#x)|[Y](#y)|[Z](#z)| | | | | |
| | | | | | | | | |
<br> | |
> | |
end | |
new_lines = [BANNER.call(title)] | |
vowel = nil | |
sorted.flatten.each { |line| | |
if line.start_with?("[**") | |
new_vowel = line[/\[(.*)\]/].gsub(/\W/,"")[0] | |
if vowel&.downcase != new_vowel.downcase | |
vowel = new_vowel | |
if new_lines[-1] == "---\n" | |
new_lines.delete_at(-1) | |
end | |
new_lines << "## #{vowel.upcase}\n\n" | |
end | |
end | |
new_lines << line | |
} | |
File.open("/path/to/file.ordered.md", "w") do |f| | |
new_lines.each { |line| f << line } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it's not a 100% reliable. it will get 97% of the job done!