Skip to content

Instantly share code, notes, and snippets.

@klebervirgilio
Created October 2, 2018 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klebervirgilio/c8a8bf0a11b489f5fc1911e302e582db to your computer and use it in GitHub Desktop.
Save klebervirgilio/c8a8bf0a11b489f5fc1911e302e582db to your computer and use it in GitHub Desktop.
Order awesomo/**/*.md files alphabetically
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
@klebervirgilio
Copy link
Author

it's not a 100% reliable. it will get 97% of the job done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment