Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@klebervirgilio klebervirgilio commented Oct 2, 2018

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