├── .gitignore ├── LICENSE ├── README.md ├── jekyll-random.gemspec └── lib └── jekyll-random.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | /.config 4 | /coverage/ 5 | /InstalledFiles 6 | /pkg/ 7 | /spec/reports/ 8 | /spec/examples.txt 9 | /test/tmp/ 10 | /test/version_tmp/ 11 | /tmp/ 12 | .idea/ 13 | 14 | # Used by dotenv library to load environment variables. 15 | # .env 16 | 17 | ## Specific to RubyMotion: 18 | .dat* 19 | .repl_history 20 | build/ 21 | *.bridgesupport 22 | build-iPhoneOS/ 23 | build-iPhoneSimulator/ 24 | 25 | ## Specific to RubyMotion (use of CocoaPods): 26 | # 27 | # We recommend against adding the Pods directory to your .gitignore. However 28 | # you should judge for yourself, the pros and cons are mentioned at: 29 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 30 | # 31 | # vendor/Pods/ 32 | 33 | ## Documentation cache and generated files: 34 | /.yardoc/ 35 | /_yardoc/ 36 | /doc/ 37 | /rdoc/ 38 | 39 | ## Environment normalization: 40 | /.bundle/ 41 | /vendor/bundle 42 | /lib/bundler/man/ 43 | 44 | # for a library or gem, you might want to ignore these files since the code is 45 | # intended to run in multiple environments; otherwise, check them in: 46 | # Gemfile.lock 47 | # .ruby-version 48 | # .ruby-gemset 49 | 50 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: 51 | .rvmrc 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Paweł Kuna 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Jekyll-Random 2 | 3 | A Jekyll plugin that generates _pseudo-random_ data. Very useful when you want to generate a large amount of random data. The plugin is prepared to work with other Jekyll plugins like `jekyll-timeago` or `jekyll-humanize`. 4 | 5 | **The data is generated based on index, so that every time when you run `jekyll build`, the generated data is the same.** 6 | 7 | [](https://badge.fury.io/rb/jekyll-random) 8 | 9 | 10 | ## Installation 11 | 12 | 1. Add the following to your site's `Gemfile`: 13 | 14 | ```ruby 15 | gem 'jekyll-random' 16 | ``` 17 | 18 | 2. Add the following to your site's `_config.yml`: 19 | 20 | ```yml 21 | plugins: 22 | - jekyll-random 23 | ``` 24 | 25 | If you are using a Jekyll version less than `3.5.0`, use the `gems` key instead of `plugins`. 26 | 27 | ### Manual installation 28 | 29 | Simply download the `lib/jekyll-random.rb` file and place it in the `_plugins` directory of your Jekyll site. 30 | 31 | 32 | ## Usage 33 | 34 | Each of the methods in the file presents an available Fluid type filter to be used in Jekyll templates. Thanks to this you can manipulate the generated data. 35 | 36 | ### random_number(_index_, _min=0_, _max=100_, _round=0_) 37 | 38 | Return a number between _min_ and _max_ based on _index_. By default it returns a number between 0 and 100. 39 | 40 | ```ruby 41 | {% for i in (1..100) %} 42 | {{ i }} - {{ forloop.index | random_number: 0, 10 }} 43 | {% endfor %} 44 | ``` 45 | 46 | The code above returns random numbers like: 47 | 48 | ``` 49 | 1 - 6 50 | 2 - 1 51 | 3 - 6 52 | 4 - 8 53 | 5 - 4 54 | 6 - 7 55 | 7 - 1 56 | ... 57 | ``` 58 | 59 | You can also change the `round` parameter to generate fractions: 60 | 61 | ```ruby 62 | {% for i in (1..100) %} 63 | {{ i }} - {{ forloop.index | random_number: 0, 100, 2 }} 64 | {% endfor %} 65 | ``` 66 | 67 | The result of code above: 68 | 69 | ``` 70 | 1 - 42.98 71 | 2 - 0.08 72 | 3 - 96.59 73 | 4 - 6.81 74 | 5 - 36.91 75 | 6 - 7.06 76 | 7 - 80.38 77 | ... 78 | ``` 79 | 80 | ### random_item(_index_, _items_) 81 | 82 | Return random item from _items_ array based on _index_. _items_ can be array, collection, or data file. 83 | 84 | ```ruby 85 | {% assign colors = 'red|green|blue|yellow|orange' | split: '|' %} 86 | {% for i in (1..100) %} 87 | {{ i }} - {{ forloop.index | random_item: colors }} 88 | {% endfor %} 89 | ``` 90 | 91 | The results: 92 | 93 | ``` 94 | 1 - blue 95 | 2 - blue 96 | 3 - red 97 | 4 - yellow 98 | 5 - yellow 99 | 6 - red 100 | 7 - red 101 | 8 - yellow 102 | ... 103 | ``` 104 | 105 | ### random_date(_index_, _start_date=false_, _end_date=false_) 106 | 107 | Return random date between _start_date_ and _end_date_. By default, it returns the date between 100 days ago and now. Returned date you can format by `date` filter to get expected result. This filter is useful when you generate birth date or register date. 108 | 109 | ```ruby 110 | {% for i in (1..100) %} 111 | {{ i }} - {{ forloop.index | random_date: "2010-01-01", "2018-01-01" | date: '%B %d, %Y' }} 112 | {% endfor %} 113 | ``` 114 | 115 | ``` 116 | 1 - May 23, 2016 117 | 2 - January 31, 2017 118 | 3 - August 10, 2014 119 | 4 - December 08, 2016 120 | 5 - January 22, 2016 121 | 6 - November 16, 2015 122 | 7 - June 09, 2013 123 | ... 124 | ``` 125 | 126 | ### random_date_ago(_index_, _days_ago=100_) 127 | 128 | This filter works similar to `random_date`, but returns random date between today and date _days_ago_ ago. By default return date between now and 100 days ago. It is helpful to generate random data like last login date. If you additionally use the `jekyll-timeago` filter you can get date in _2 days ago_ format. 129 | 130 | ```ruby 131 | {% for i in (1..100) %} 132 | {{ i }} - {{ forloop.index | random_date_ago: 10 | timeago }} 133 | {% endfor %} 134 | ``` 135 | 136 | Results: 137 | 138 | ``` 139 | 1 - 6 days ago 140 | 2 - yesterday 141 | 3 - 6 days ago 142 | 4 - 1 week ago 143 | 5 - 4 days ago 144 | 6 - 1 week ago 145 | 7 - yesterday 146 | ... 147 | ``` 148 | 149 | You can also change _days_ago_ parameter to negative number like `{{ forloop.index | random_date_ago: -10 | timeago }}` to get date in future: 150 | 151 | ``` 152 | 1 - today 153 | 2 - in 1 week 154 | 3 - tomorrow 155 | 4 - tomorrow 156 | 5 - in 2 days 157 | 6 - in 2 days 158 | 7 - in 4 days 159 | 8 - in 5 days 160 | 9 - in 1 week 161 | ... 162 | ``` 163 | 164 | ## Generate data based on the same index 165 | 166 | Sometimes you want to generate data in the same row with the same parameters. Because this plugin generate pseudo-random data every returned number will be the same: 167 | 168 | ```ruby 169 | {% for i in (1..5) %} 170 | {{ i }} - {{ forloop.index | random_number }}, {{ forloop.index | random_number }}, {{ forloop.index | random_number }} 171 | {% endfor %} 172 | ``` 173 | 174 | The above code returns a code that does not meet our expectations, because every time returns the same data: 175 | 176 | ``` 177 | 1 - 42, 42, 42 178 | 2 - 0, 0, 0 179 | 3 - 96, 96, 96 180 | 4 - 6, 6, 6 181 | 5 - 36, 36, 36 182 | ... 183 | ``` 184 | 185 | To fix it, simply add a random number to `index` parameter: `{{ forloop.index | plus: 156 | random_number }}`. Thanks to this the results will be different to every index. 186 | 187 | ``` 188 | 1 - 42 - 47 - 55 189 | 2 - 0 - 87 - 96 190 | 3 - 96 - 15 - 81 191 | 4 - 6 - 12 - 59 192 | 5 - 36 - 8 - 20 193 | ... 194 | ``` 195 | 196 | ## Sample code 197 | 198 | To see how useful this plugin is, try to run the code below. Remember to set few random data in `_config.yml` file: 199 | 200 | ```yml 201 | users: 202 | - name: Tate 203 | surname: Nesfield 204 | email: tnesfield0@scribd.com 205 | - name: Thelma 206 | surname: Muirden 207 | email: tmuirden1@google.com 208 | ... 209 | 210 | commits: 211 | - Push poorly written test can down the road another ten years 212 | - This will definitely break in 2020 (TODO) 213 | ... 214 | ``` 215 | 216 | _index.html_ 217 | 218 | ```html 219 | {% assign colors = 'red|green|blue|yellow|orange' | split: '|' %} 220 |
ID | 223 |Name | 224 |Percentage | 226 |Age | 227 |Status | 228 |Register date | 229 |Last login | 230 |Commit | 231 ||
---|---|---|---|---|---|---|---|---|
{{ forloop.index }} | 235 |{{ user.name }} {{ user.surname }} | 236 |{{ user.email }} | 237 |{{ forloop.index | random_number }}% | 238 |{{ forloop.index | random_number: 25, 55 }} | 239 |{{ forloop.index | random_item: colors }} | 240 |{{ forloop.index | random_date: "2010-01-01", "2018-01-01" | date: '%B %d, %Y' }} | 241 |{{ forloop.index | random_date_ago: 10 | timeago }} | 242 |{{ site.commits[forloop.index] }} | 243 |