-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateReadme.ps1
105 lines (86 loc) · 2.46 KB
/
CreateReadme.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
$header = @"
# TIL
> Today I Learned
A collection of concise write-ups on small things I learn day to day across a
variety of languages and technologies.
"@
$footer = @"
## About
I shamelessly stole this idea from
[thoughtbot/til](https://github.com/thoughtbot/til).
## Other TIL Collections
* [Today I Learned by Hashrocket](https://til.hashrocket.com)
* [jwworth/til](https://github.com/jwworth/til)
* [thoughtbot/til](https://github.com/thoughtbot/til)
"@
function Get-Categories{
$dirs = Get-ChildItem -Path . -Directory -Exclude 'Media'
return $dirs.Name
}
function Get-Title($tilFile){
$file = Get-content $tilFile
foreach($line in $file){
if($line.startsWith('#')){
return $line.trim("#").Trim()
}
}
}
function Get-Tils($category){
$tilFiles = Get-ChildItem -Path .\$category
$titles = @()
foreach($filename in $tilFiles){
if($filename.Extension -eq '.md'){
$title = Get-Title $filename.FullName
$titles += [PSCustomObject]@{
title = $title
filename = "$($filename.Directory.Name)/$($filename.Name)"
}
}
}
return $titles
}
function Get-CategoryHash($categoryNames){
$categories = @{}
$count = 0
foreach($category in $categoryNames){
$titles = Get-Tils $category
$categories[$category] = $titles
$count += $titles.title.count
}
return [PSCustomObject]@{
count = $count
categories = $categories
}
}
function Print-File($categoryNames, $count, $categories){
$output = $header
$output += "`n`n"
$output += "_$count TILs and counting..._"
$output += "`n`n"
$output += @"
---
### Categories
"@
foreach($category in ($categoryNames | Sort-Object)){
$output += "* [$((Get-Culture).TextInfo.ToTitleCase($category))](#$category)`n"
}
$output += @"
---
"@
foreach($category in ($categoryNames | Sort-Object)){
$output += "### $((Get-Culture).TextInfo.ToTitleCase($category))`n`n"
$tils = $categories[$category]
Foreach($i in ($tils | Sort-Object -Property title)){
$output += "- [$($i.title)]($($i.filename))`n"
}
$output += "`n"
}
$output += $footer
$output | Out-File 'README.md'
}
function Create-Readme(){
$categoryNames = Get-Categories
$categories = Get-CategoryHash $categoryNames
Print-File $categoryNames $categories.Count $categories.categories
}
Create-Readme