-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #3799 -- Added django.contrib.webdesign and moved 'lorem' templ…
…ate tag into there git-svn-id: http://code.djangoproject.com/svn/django/trunk@4857 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
1 parent
587abaa
commit d22e39c
Showing
8 changed files
with
198 additions
and
107 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
""" | ||
Utility functions for generating "lorem ipsum" Latin text. | ||
""" | ||
|
||
import random | ||
|
||
COMMON_P = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' | ||
WORDS = ('exercitationem', 'perferendis', 'perspiciatis', 'laborum', 'eveniet', 'sunt', 'iure', 'nam', 'nobis', 'eum', 'cum', 'officiis', 'excepturi', 'odio', 'consectetur', 'quasi', 'aut', 'quisquam', 'vel', 'eligendi', 'itaque', 'non', 'odit', 'tempore', 'quaerat', 'dignissimos', 'facilis', 'neque', 'nihil', 'expedita', 'vitae', 'vero', 'ipsum', 'nisi', 'animi', 'cumque', 'pariatur', 'velit', 'modi', 'natus', 'iusto', 'eaque', 'sequi', 'illo', 'sed', 'ex', 'et', 'voluptatibus', 'tempora', 'veritatis', 'ratione', 'assumenda', 'incidunt', 'nostrum', 'placeat', 'aliquid', 'fuga', 'provident', 'praesentium', 'rem', 'necessitatibus', 'suscipit', 'adipisci', 'quidem', 'possimus', 'voluptas', 'debitis', 'sint', 'accusantium', 'unde', 'sapiente', 'voluptate', 'qui', 'aspernatur', 'laudantium', 'soluta', 'amet', 'quo', 'aliquam', 'saepe', 'culpa', 'libero', 'ipsa', 'dicta', 'reiciendis', 'nesciunt', 'doloribus', 'autem', 'impedit', 'minima', 'maiores', 'repudiandae', 'ipsam', 'obcaecati', 'ullam', 'enim', 'totam', 'delectus', 'ducimus', 'quis', 'voluptates', 'dolores', 'molestiae', 'harum', 'dolorem', 'quia', 'voluptatem', 'molestias', 'magni', 'distinctio', 'omnis', 'illum', 'dolorum', 'voluptatum', 'ea', 'quas', 'quam', 'corporis', 'quae', 'blanditiis', 'atque', 'deserunt', 'laboriosam', 'earum', 'consequuntur', 'hic', 'cupiditate', 'quibusdam', 'accusamus', 'ut', 'rerum', 'error', 'minus', 'eius', 'ab', 'ad', 'nemo', 'fugit', 'officia', 'at', 'in', 'id', 'quos', 'reprehenderit', 'numquam', 'iste', 'fugiat', 'sit', 'inventore', 'beatae', 'repellendus', 'magnam', 'recusandae', 'quod', 'explicabo', 'doloremque', 'aperiam', 'consequatur', 'asperiores', 'commodi', 'optio', 'dolor', 'labore', 'temporibus', 'repellat', 'veniam', 'architecto', 'est', 'esse', 'mollitia', 'nulla', 'a', 'similique', 'eos', 'alias', 'dolore', 'tenetur', 'deleniti', 'porro', 'facere', 'maxime', 'corrupti') | ||
COMMON_WORDS = ('lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipisicing', 'elit', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt', 'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua') | ||
|
||
def sentence(): | ||
""" | ||
Returns a randomly generated sentence of lorem ipsum text. | ||
The first word is capitalized, and the sentence ends in either a period or | ||
question mark. Commas are added at random. | ||
""" | ||
# Determine the number of comma-separated sections and number of words in | ||
# each section for this sentence. | ||
sections = [' '.join(random.sample(WORDS, random.randint(3, 12))) for i in range(random.randint(1, 5))] | ||
s = ', '.join(sections) | ||
# Convert to sentence case and add end punctuation. | ||
return '%s%s%s' % (s[0].upper(), s[1:], random.choice('?.')) | ||
|
||
def paragraph(): | ||
""" | ||
Returns a randomly generated paragraph of lorem ipsum text. | ||
The paragraph consists of between 1 and 4 sentences, inclusive. | ||
""" | ||
return ' '.join([sentence() for i in range(random.randint(1, 4))]) | ||
|
||
def paragraphs(count, common=True): | ||
""" | ||
Returns a list of paragraphs as returned by paragraph(). | ||
If `common` is True, then the first paragraph will be the standard | ||
'lorem ipsum' paragraph. Otherwise, the first paragraph will be random | ||
Latin text. Either way, subsequent paragraphs will be random Latin text. | ||
""" | ||
paras = [] | ||
for i in range(count): | ||
if common and i == 0: | ||
paras.append(COMMON_P) | ||
else: | ||
paras.append(paragraph()) | ||
return paras | ||
|
||
def words(count, common=True): | ||
""" | ||
Returns a string of `count` lorem ipsum words separated by a single space. | ||
If `common` is True, then the first 19 words will be the standard | ||
'lorem ipsum' words. Otherwise, all words will be selected randomly. | ||
""" | ||
if common: | ||
word_list = list(COMMON_WORDS) | ||
else: | ||
word_list = [] | ||
c = len(word_list) | ||
if count > c: | ||
count = min(count - c, len(WORDS)) | ||
word_list += random.sample(WORDS, count - c) | ||
else: | ||
word_list = word_list[:count] | ||
return ' '.join(word_list) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from django.contrib.webdesign.lorem_ipsum import words, paragraphs | ||
from django import template | ||
|
||
register = template.Library() | ||
|
||
class LoremNode(template.Node): | ||
def __init__(self, count, method, common): | ||
self.count, self.method, self.common = count, method, common | ||
|
||
def render(self, context): | ||
try: | ||
count = int(self.count.resolve(context)) | ||
except (ValueError, TypeError): | ||
count = 1 | ||
if self.method == 'w': | ||
return words(count, common=self.common) | ||
else: | ||
paras = paragraphs(count, common=self.common) | ||
if self.method == 'p': | ||
paras = ['<p>%s</p>' % p for p in paras] | ||
return '\n\n'.join(paras) | ||
|
||
#@register.tag | ||
def lorem(parser, token): | ||
""" | ||
Creates random Latin text useful for providing test data in templates. | ||
Usage format:: | ||
{% lorem [count] [method] [random] %} | ||
``count`` is a number (or variable) containing the number of paragraphs or | ||
words to generate (default is 1). | ||
``method`` is either ``w`` for words, ``p`` for HTML paragraphs, ``b`` for | ||
plain-text paragraph blocks (default is ``b``). | ||
``random`` is the word ``random``, which if given, does not use the common | ||
paragraph (starting "Lorem ipsum dolor sit amet, consectetuer..."). | ||
Examples: | ||
* ``{% lorem %}`` will output the common "lorem ipsum" paragraph | ||
* ``{% lorem 3 p %}`` will output the common "lorem ipsum" paragraph | ||
and two random paragraphs each wrapped in HTML ``<p>`` tags | ||
* ``{% lorem 2 w random %}`` will output two random latin words | ||
""" | ||
bits = list(token.split_contents()) | ||
tagname = bits[0] | ||
# Random bit | ||
common = bits[-1] != 'random' | ||
if not common: | ||
bits.pop() | ||
# Method bit | ||
if bits[-1] in ('w', 'p', 'b'): | ||
method = bits.pop() | ||
else: | ||
method = 'b' | ||
# Count bit | ||
if len(bits) > 1: | ||
count = bits.pop() | ||
else: | ||
count = '1' | ||
count = parser.compile_filter(count) | ||
if len(bits) != 1: | ||
raise TemplateSyntaxError("Incorrect format for %r tag" % tagname) | ||
return LoremNode(count, method, common) | ||
lorem = register.tag(lorem) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
======================== | ||
django.contrib.webdesign | ||
======================== | ||
|
||
The ``django.contrib.webdesign`` package, part of the `"django.contrib" add-ons`_, | ||
provides various Django helpers that are particularly useful to Web *designers* | ||
(as opposed to developers). | ||
|
||
At present, the package contains only a single template tag. If you have ideas | ||
for Web-designer-friendly functionality in Django, please `suggest them`_. | ||
|
||
.. _"django.contrib" add-ons: ../add_ons/ | ||
.. _suggest them: ../contributing/ | ||
|
||
Template tags | ||
============= | ||
|
||
To use these template tags, add ``'django.contrib.webdesign'`` to your | ||
``INSTALLED_APPS`` setting. Once you've done that, use | ||
``{% load webdesign %}`` in a template to give your template access to the tags. | ||
|
||
|
||
lorem | ||
===== | ||
|
||
Displays random "lorem ipsum" Latin text. This is useful for providing sample | ||
data in templates. | ||
|
||
Usage:: | ||
|
||
{% lorem [count] [method] [random] %} | ||
|
||
The ``{% lorem %}`` tag can be used with zero, one, two or three arguments. | ||
The arguments are: | ||
|
||
=========== ============================================================= | ||
Argument Description | ||
=========== ============================================================= | ||
``count`` A number (or variable) containing the number of paragraphs or | ||
words to generate (default is 1). | ||
``method`` Either ``w`` for words, ``p`` for HTML paragraphs or ``b`` | ||
for plain-text paragraph blocks (default is ``b``). | ||
``random`` The word ``random``, which if given, does not use the common | ||
paragraph ("Lorem ipsum dolor sit amet...") when generating | ||
text. | ||
|
||
Examples: | ||
|
||
* ``{% lorem %}`` will output the common "lorem ipsum" paragraph. | ||
* ``{% lorem 3 p %}`` will output the common "lorem ipsum" paragraph | ||
and two random paragraphs each wrapped in HTML ``<p>`` tags. | ||
* ``{% lorem 2 w random %}`` will output two random Latin words. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters