Generate some lorem ipsum for the template.
(n=5, html=True, min=20, max=100)
| 236 | |
| 237 | |
| 238 | def generate_lorem_ipsum(n=5, html=True, min=20, max=100): |
| 239 | """Generate some lorem ipsum for the template.""" |
| 240 | from jinja2.constants import LOREM_IPSUM_WORDS |
| 241 | from random import choice, randrange |
| 242 | words = LOREM_IPSUM_WORDS.split() |
| 243 | result = [] |
| 244 | |
| 245 | for _ in range(n): |
| 246 | next_capitalized = True |
| 247 | last_comma = last_fullstop = 0 |
| 248 | word = None |
| 249 | last = None |
| 250 | p = [] |
| 251 | |
| 252 | # each paragraph contains out of 20 to 100 words. |
| 253 | for idx, _ in enumerate(range(randrange(min, max))): |
| 254 | while True: |
| 255 | word = choice(words) |
| 256 | if word != last: |
| 257 | last = word |
| 258 | break |
| 259 | if next_capitalized: |
| 260 | word = word.capitalize() |
| 261 | next_capitalized = False |
| 262 | # add commas |
| 263 | if idx - randrange(3, 8) > last_comma: |
| 264 | last_comma = idx |
| 265 | last_fullstop += 2 |
| 266 | word += ',' |
| 267 | # add end of sentences |
| 268 | if idx - randrange(10, 20) > last_fullstop: |
| 269 | last_comma = last_fullstop = idx |
| 270 | word += '.' |
| 271 | next_capitalized = True |
| 272 | p.append(word) |
| 273 | |
| 274 | # ensure that the paragraph ends with a dot. |
| 275 | p = u' '.join(p) |
| 276 | if p.endswith(','): |
| 277 | p = p[:-1] + '.' |
| 278 | elif not p.endswith('.'): |
| 279 | p += '.' |
| 280 | result.append(p) |
| 281 | |
| 282 | if not html: |
| 283 | return u'\n\n'.join(result) |
| 284 | return Markup(u'\n'.join(u'<p>%s</p>' % escape(x) for x in result)) |
| 285 | |
| 286 | |
| 287 | def unicode_urlencode(obj, charset='utf-8', for_qs=False): |