-
Notifications
You must be signed in to change notification settings - Fork 0
/
pp
executable file
·102 lines (83 loc) · 2.07 KB
/
pp
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
#!/bin/bash
directory="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
file="$directory/The-Pragmatic-Programmer/readme.md"
readme () {
less $file
}
toc () {
sed -n '/# Table of Contents/,/\[Quick Reference\]/p' ${file} |
remove "\[Table of Contents\]" |
remove "\[Quick Reference\]" |
grep -E -w "\t|-\s\[.*\]" |
cut -d'(' -f1 |
sed -e 's/\[//g' -e 's/\]//g' -e 's/-\s//g'
}
tips () {
sed -n '/## Tips/,/## CheckList/p' ${file} |
sed -e 's/**//g' |
remove "## CheckList"
}
checklist () {
sed -n '/## CheckList/,/Content from The Pragmatic Programmer/p' ${file}
}
print_usage () {
echo 'Usage:'
echo -e '\thelp \tHelp'
echo -e "\tread \tRead $file"
echo -e '\ttoc \tPrint the table of contents, used to find chapters and sections.'
echo -e "\tx \tPrint a chapter. E.g. chapter 1 'A Pragmatic Philosophy'; 1"
echo -e "\tx.y \tPrint a section of a chapter. E.g. chapter 1, section 2 'Software Entropy'; 1.2"
echo -e '\ttips \tPrint tips'
echo -e '\tls \tPrint checklist'
}
get_chapter () {
sed -n "/# Chapter $1./,/# Chapter $2./p" ${file} | remove "# Chapter $2."
}
get_section () {
sed -n "/## $1./,/## $2./p" | remove "## $2."
}
remove () {
sed "/$1/d"
}
if [[ $# == 0 ]] || [[ $@ =~ 'help' ]]; then
print_usage
exit 0
fi
if [[ $@ =~ 'read' ]]; then
readme
exit 0
fi
if [[ $@ =~ 'toc' ]]; then
toc
exit 0
fi
if [[ $@ =~ 'tips' ]]; then
tips
exit 0
fi
if [[ $@ =~ 'ls' ]]; then
checklist
exit 0
fi
# parse arguments in form of Chapter.Section e.g. 1.2
arg=$1
IFS='.' read -ra args <<< "$arg"
if [ ${#args[@]} == 1 ]; then
# print chapter, e.g. 1 for 'A Pragmatic Philosophy'
chapter=${args[0]}
next_chapter=$((chapter+1))
get_chapter $chapter $next_chapter
elif [ ${#args[@]} == 2 ]; then
# print section from a chapter, e.g. 1.2 for 'Software Entropy'
chapter=${args[0]}
section=${args[1]}
next_chapter=$((chapter+1))
next_section=$((section+1))
get_chapter $chapter $next_chapter |
get_section $section $next_section
else
echo "Invalid arguments... here's help"
print_usage
exit 1
fi
exit 0