├── INSTALL ├── README.md └── pre-commit /INSTALL: -------------------------------------------------------------------------------- 1 | curl https://raw.github.com/hybridgroup/GitHub-Wikifier/master/pre-commit > .git/hooks/pre-commit; 2 | chmod +x .git/hooks/pre-commit; 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # What is this? 2 | 3 | GitHub Wikifier helps you organize your Github Wiki with a set of good practices 4 | and autogenerated navigation table. 5 | [Why should I care?](https://github.com/hybridgroup/GitHub-Wikifier/wiki/1.2.-Why-Should-I-Care) 6 | 7 | # Demo 8 | 9 | See it in action in [artoo wiki](https://github.com/hybridgroup/artoo/wiki) 10 | 11 | # Requirements 12 | 13 | 1. Zsh ( Install via `sudo apt-get install zsh` or `brew install zsh` ) 14 | 2. GNU Utilities ( only for Mac ) via `brew install coreutils` 15 | 16 | # How to use 17 | 18 | 1. Clone your wiki git repo 19 | 20 | https://github.com/yourname/reponame.wiki.git 21 | 22 | 2. Create directory structure following these guidelines https://github.com/hybridgroup/Github-Wikifier/wiki/1.1.-Naming-Guidelines 23 | 24 | 3. Run command to install script 25 | 26 | ```curl -Lo- https://goo.gl/C170k | bash``` 27 | 28 | 4. Commit your changes 29 | 30 | ```git commit -m "$message"``` 31 | 32 | The script will be triggered parsing files and directories and add generated files to your commit. 33 | 34 | 5. Push your changes 35 | 36 | * It's important that you use git access for your changes from now on. 37 | 38 | # Contact 39 | 40 | * If you need help or you feel we can improve something, let us know! [Open a new issue](https://github.com/hybridgroup/GitHub-Wikifier/issues/new) 41 | 42 | # Participate 43 | 44 | * Want to contribute making this better? Check out the open issues or send a pull request. 45 | 46 | # Maintainers 47 | 48 | * [Mario Ricalde](http://github.com/marioricalde) 49 | * [Javier Cervantes](http://github.com/solojavier) 50 | -------------------------------------------------------------------------------- /pre-commit: -------------------------------------------------------------------------------- 1 | #!/bin/zsh 2 | # 3 | # Wikify by Mario "Kuroir" Ricalde 4 | # 5 | function wikify() { 6 | 7 | # gnu ls has natural number sorting, useful for sorting 1, 10, 2 into 1, 2, 10 8 | function gls_or_ls() { 9 | if hash gls 2>/dev/null; then 10 | gls -v "$@" 11 | else 12 | ls "$@" 13 | fi 14 | } 15 | 16 | # Filter some files and directories. 17 | function continue_if_filtered() { 18 | if [[ "$@" =~ "images$" ]]; then 19 | continue 20 | elif [[ "$@" =~ "(_sidebar|_footer|_header)" ]]; then 21 | continue 22 | elif [[ "$@" == "." ]]; then 23 | continue 24 | fi 25 | } 26 | 27 | function directory_depth() { 28 | if [[ $initial_depth -ne 0 ]]; then 29 | new_depth=$(echo $@ | grep -o / | wc -l) 30 | # The minus 1, is for the parent subject directory, we don't count it. 31 | echo $(($new_depth - $initial_depth - 1 )) 32 | else 33 | echo $@ | grep -o / | wc -l 34 | fi 35 | } 36 | 37 | # 4 Spaces per level, this is the markdown standard for lists. 38 | function calculate_spaces() { 39 | _spaces="" 40 | if [[ $@ -gt 0 ]]; then 41 | i=1 42 | while [[ $i -le $@ ]]; do 43 | _spaces="$_spaces " 44 | (( i++ )) 45 | done 46 | fi 47 | echo $_spaces 48 | } 49 | 50 | function depth_and_space() { 51 | depth=$(directory_depth $@) 52 | space=$(calculate_spaces $depth) 53 | } 54 | 55 | function to_parent_subject() { 56 | echo "$@" | sed -e 's/\/[^\/]*$//' 57 | } 58 | 59 | function subject_name() { 60 | echo "$@" | sed -e 's/.*\/\([^\/]*\).md$/\1/' 61 | } 62 | 63 | function string_to_name_and_id() { 64 | if [[ "$(file -ib $1)" =~ "directory" ]]; then 65 | 1="$1.md" 66 | fi 67 | identifier=$(echo "$1" | sed -e 's/.*\/\([^\/]*\).md$/\1/') 68 | name=$(echo "$identifier" | sed -e 's/^[0-9\.]*\-//' | sed -e 's/\-/ /g') 69 | } 70 | 71 | function subject_to_list() { 72 | string_to_name_and_id $1 73 | if [[ ! -s $file ]]; then 74 | flag=" ⚑" 75 | fi 76 | echo "${space}0. [[$name|$identifier]]$flag" 77 | } 78 | 79 | function id_to_string() { 80 | echo "${@/-/ }" 81 | } 82 | 83 | function subject_to_header() { 84 | string_to_name_and_id $1 85 | echo "\n${space}### [[☰|$identifier]] $(id_to_string $identifier)" 86 | } 87 | 88 | function recursive_write() { 89 | if [[ "$reverse_pointer_depth" -eq "0" ]]; then 90 | pointer_file=$(to_parent_subject $@) 91 | else 92 | pointer_file=$@ 93 | fi 94 | 95 | if [[ "$@" != "$subjects" ]]; then 96 | if [[ "$reverse_pointer_depth" -ne "0" ]]; then 97 | space=$(calculate_spaces $(($reverse_pointer_depth - 1))) 98 | echo "$(subject_to_list $file)" >> "$pointer_file.md" 99 | fi 100 | (( reverse_pointer_depth++ )) 101 | recursive_write $(to_parent_subject $@) 102 | # if current - 1 = home. Then we're at the bottom! 103 | if [[ "$(to_parent_subject $@)" == "$subjects" ]]; then 104 | echo "$(subject_to_list $file)" >> "$@/_sidebar.md" 105 | fi 106 | else 107 | space=$(calculate_spaces $(($reverse_pointer_depth - 2))) 108 | echo "$(subject_to_list $file)" >> "$subjects/Home.md" 109 | reverse_pointer_depth=0 110 | fi 111 | } 112 | 113 | function parse_subject() { 114 | depth_and_space $@ 115 | echo "${space}Directory: $@" 116 | 117 | # Directory write 118 | if [[ "$depth" -eq "0" ]]; then 119 | echo "$(subject_to_header $@.md)" >> "$subjects/Home.md" 120 | echo "$(subject_to_header $@.md)\n---" > "$@/_sidebar.md" 121 | echo "$@.md" >> /tmp/wikify.txt 122 | echo "$@/_sidebar.md" >> /tmp/wikify.txt 123 | fi 124 | 125 | for file in $(gls_or_ls -l -v $@ | awk '{ print $9 }'); do; 126 | continue_if_filtered $file 127 | file="$@/$file" 128 | 129 | if [[ "$(file -ib $file)" =~ "directory" ]]; then 130 | echo "$file.md" >> /tmp/wikify.txt 131 | recursive_write $file 132 | parse_subject $file 133 | else 134 | depth_and_space $file 135 | echo "${space}File: $file" 136 | echo "$file" >> /tmp/wikify.txt 137 | recursive_write $file 138 | fi 139 | done 140 | } 141 | 142 | LANG=C 143 | subjects=$PWD 144 | initial_depth=0 145 | initial_depth=$(directory_depth $subjects) 146 | reverse_pointer_depth=0 147 | rm -f /tmp/wikify.txt; touch /tmp/wikify.txt 148 | 149 | # Empty Home.md 150 | home_file="$subjects/Home.md" 151 | rm $home_file 152 | 153 | # Cleanup Previous Generated Files. 154 | for subject in $(find . -type d ! \( -path './.*' -o -path '.' \)); do; 155 | rm "$subject.md" 156 | done 157 | 158 | # Begin doing the magic 159 | for subject in $(gls_or_ls -l -d $PWD/* | egrep '^d' | awk '{ print $9}'); do; 160 | continue_if_filtered $subject 161 | parse_subject $subject 162 | done 163 | 164 | echo "$subjects/Home.md" >> /tmp/wikify.txt 165 | while read line 166 | do 167 | [ -f $line ] && git add $line 168 | done < /tmp/wikify.txt 169 | 170 | echo "**Important**: The Tables of Content are generated. Any change will be overridden on the next update.
For more information: [GitHub Wikifier](https://github.com/hybridgroup/GitHub-Wikifier)" > "$subjects/_footer.md" 171 | subjects="$PWD." # hack to restore ps1 172 | } 173 | 174 | wikify . 175 | 176 | --------------------------------------------------------------------------------