Debian Codegraph @

PKG-INFO

raw
1 Metadata-Version: 1.1
2 Name: django-markupfield
3 Version: 2.0.1
4 Summary: Custom Django field for easy use of markup in text fields
5 Home-page: https://github.com/jamesturk/django-markupfield/
6 Author: James Turk
7 Author-email: [email protected]
8 License: BSD License
9 Description: ==================
10 django-markupfield
11 ==================
12
13 .. image:: https://github.com/jamesturk/django-markupfield/workflows/Test/badge.svg
14
15 .. image:: https://img.shields.io/pypi/v/django-markupfield.svg
16 :target: https://pypi.python.org/pypi/django-markupfield
17
18 An implementation of a custom MarkupField for Django. A MarkupField is in
19 essence a TextField with an associated markup type. The field also caches
20 its rendered value on the assumption that disk space is cheaper than CPU
21 cycles in a web application.
22
23 Installation
24 ============
25
26 The recommended way to install django-markupfield is with
27 `pip <https://pypi.python.org/pypi/pip>`_
28
29 It is not necessary to add ``'markupfield'`` to your ``INSTALLED_APPS``, it
30 merely needs to be on your ``PYTHONPATH``. However, to use titled markup you
31 either add ``'markupfield'`` to your ``INSTALLED_APPS`` or add the
32 corresponding translations to your project translation.
33
34 Requirements
35 ------------
36
37 Requires Django >= 2.2 and 3.6+
38
39 * 1.5.x is the last release to officially support Django < 2.2 or Python 2.7
40 * 1.4.x is the last release to officially support Django < 1.11
41 * 1.3.x is the last release to officially support Django 1.4 or Python 3.3
42
43 Settings
44 ========
45
46 To best make use of MarkupField you should define the
47 ``MARKUP_FIELD_TYPES`` setting, a mapping of strings to callables that
48 'render' a markup type::
49
50 import markdown
51 from docutils.core import publish_parts
52
53 def render_rest(markup):
54 parts = publish_parts(source=markup, writer_name="html4css1")
55 return parts["fragment"]
56
57 MARKUP_FIELD_TYPES = (
58 ('markdown', markdown.markdown),
59 ('ReST', render_rest),
60 )
61
62 If you do not define a ``MARKUP_FIELD_TYPES`` then one is provided with the
63 following markup types available:
64
65 html:
66 allows HTML, potentially unsafe
67 plain:
68 plain text markup, calls urlize and replaces text with linebreaks
69 markdown:
70 default `markdown`_ renderer (only if `markdown`_ is installed)
71 restructuredtext:
72 default `ReST`_ renderer (only if `docutils`_ is installed)
73
74 It is also possible to override ``MARKUP_FIELD_TYPES`` on a per-field basis
75 by passing the ``markup_choices`` option to a ``MarkupField`` in your model
76 declaration.
77
78 .. _`ReST`: http://docutils.sourceforge.net/rst.html
79 .. _`markdown`: https://pypi.python.org/pypi/Markdown
80 .. _`docutils`: http://docutils.sourceforge.net/
81
82 Usage
83 =====
84
85 Using MarkupField is relatively easy, it can be used in any model definition::
86
87 from django.db import models
88 from markupfield.fields import MarkupField
89
90 class Article(models.Model):
91 title = models.CharField(max_length=100)
92 slug = models.SlugField(max_length=100)
93 body = MarkupField()
94
95 ``Article`` objects can then be created with any markup type defined in
96 ``MARKUP_FIELD_TYPES``::
97
98 Article.objects.create(title='some article', slug='some-article',
99 body='*fancy*', body_markup_type='markdown')
100
101 You will notice that a field named ``body_markup_type`` exists that you did
102 not declare, MarkupField actually creates two extra fields here
103 ``body_markup_type`` and ``_body_rendered``. These fields are always named
104 according to the name of the declared ``MarkupField``.
105
106 Arguments
107 ---------
108
109 ``MarkupField`` also takes three optional arguments. Either
110 ``default_markup_type`` and ``markup_type`` arguments may be specified but
111 not both.
112
113 ``default_markup_type``:
114 Set a markup_type that the field will default to if one is not specified.
115 It is still possible to edit the markup type attribute and it will appear
116 by default in ModelForms.
117
118 ``markup_type``:
119 Set markup type that the field will always use, ``editable=False`` is set
120 on the hidden field so it is not shown in ModelForms.
121
122 ``markup_choices``:
123 A replacement list of markup choices to be used in lieu of
124 ``MARKUP_FIELD_TYPES`` on a per-field basis.
125
126 ``escape_html``:
127 A flag (False by default) indicating that the input should be regarded
128 as untrusted and as such will be run through Django's ``escape`` filter.
129
130
131 Examples
132 ~~~~~~~~
133
134 ``MarkupField`` that will default to using markdown but allow the user a choice::
135
136 MarkupField(default_markup_type='markdown')
137
138 ``MarkupField`` that will use ReST and not provide a choice on forms::
139
140 MarkupField(markup_type='restructuredtext')
141
142 ``MarkupField`` that will use a custom set of renderers::
143
144 CUSTOM_RENDERERS = (
145 ('markdown', markdown.markdown),
146 ('wiki', my_wiki_render_func)
147 )
148 MarkupField(markup_choices=CUSTOM_RENDERERS)
149
150 .. note::
151 When using ``markdown``, be sure to use ``markdown.markdown`` and not
152 the ``markdown.Markdown`` class, the class requires an explicit ``reset``
153 to function properly in some cases. (See [issue #40](https://github.com/jamesturk/django-markupfield/issues/40)
154 for details.)
155
156
157 Accessing a MarkupField on a model
158 ----------------------------------
159
160 When accessing an attribute of a model that was declared as a ``MarkupField``
161 a special ``Markup`` object is returned. The ``Markup`` object has three
162 parameters:
163
164 ``raw``:
165 The unrendered markup.
166 ``markup_type``:
167 The markup type.
168 ``rendered``:
169 The rendered HTML version of ``raw``, this attribute is read-only.
170
171 This object has a ``__unicode__`` method that calls
172 ``django.utils.safestring.mark_safe`` on ``rendered`` allowing MarkupField
173 objects to appear in templates as their rendered selfs without any template
174 tag or having to access ``rendered`` directly.
175
176 Assuming the ``Article`` model above::
177
178 >>> a = Article.objects.all()[0]
179 >>> a.body.raw
180 u'*fancy*'
181 >>> a.body.markup_type
182 u'markdown'
183 >>> a.body.rendered
184 u'<p><em>fancy</em></p>'
185 >>> print unicode(a.body)
186 <p><em>fancy</em></p>
187
188 Assignment to ``a.body`` is equivalent to assignment to ``a.body.raw`` and
189 assignment to ``a.body_markup_type`` is equivalent to assignment to
190 ``a.body.markup_type``.
191
192 .. important::
193 Keeping in mind that ``body`` is MarkupField instance is particullary important with ``default`` or ``default_if_none`` filter for model that could be blank. If ``body``'s ``rendered`` is ``None`` or empty string (``""``) these filters will *not* evaluate ``body`` as falsy to display default text::
194
195 {{ a.body|default:"<missing body>" }}
196
197 That's because ``body`` is regular non-``None`` MarkupField instance. To let ``default`` or ``default_if_none`` filters to work evaluate ``rendered`` MarkupField attribute instead. To prevent escaping HTML for the case ``rendered`` is truethy, finish chain with ``safe`` filter::
198
199 {{ a.body.rendered|default:"<missing body>"|safe }}
200
201 .. note::
202 a.body.rendered is only updated when a.save() is called
203
204
205 Platform: any
206 Classifier: Development Status :: 5 - Production/Stable
207 Classifier: Intended Audience :: Developers
208 Classifier: License :: OSI Approved :: BSD License
209 Classifier: Natural Language :: English
210 Classifier: Operating System :: OS Independent
211 Classifier: Programming Language :: Python
212 Classifier: Programming Language :: Python :: 3
213 Classifier: Programming Language :: Python :: 3.6
214 Classifier: Programming Language :: Python :: 3.7
215 Classifier: Programming Language :: Python :: 3.8
216 Classifier: Programming Language :: Python :: 3.9
217 Classifier: Programming Language :: Python :: 3.10
218 Classifier: Environment :: Web Environment