Last active
November 14, 2019 14:55
-
-
Save cdw9/65212d1ae7ac8b854948d2fa616dc7e5 to your computer and use it in GitHub Desktop.
Plone 5.2 - HTML Dublin Core Behavior for Plone. Meant to replace plone.dublincore. Honors HTML filtering, and saves a plain text version of the value to the default Title and Description, so you don't have to override dozens of templates in your site
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
import re | |
import six | |
from plone.app.dexterity import _ | |
from plone.app.dexterity.behaviors.metadata import ( | |
MetadataBase, | |
ICategorization, | |
Categorization, | |
IPublication, | |
Publication, | |
IOwnership, | |
Ownership) | |
from plone.autoform import directives | |
from plone.autoform.interfaces import IFormFieldProvider | |
from plone.supermodel import model | |
from Products.PortalTransforms.transforms.safe_html import SafeHTML | |
from z3c.form.interfaces import IAddForm, IEditForm | |
from zope import schema | |
from zope.interface import provider | |
@provider(IFormFieldProvider) | |
class IHTMLBasic(model.Schema): | |
html_title = schema.TextLine( | |
title=_(u'label_title', default=u'Title'), | |
required=True | |
) | |
html_description = schema.Text( | |
title=_(u'label_description', default=u'Summary'), | |
description=_( | |
u'help_description', | |
default=u'Used in item listings and search results.' | |
), | |
required=False, | |
missing_value=u'', | |
) | |
directives.order_before(html_description='*') | |
directives.order_before(html_title='*') | |
directives.no_omit(IEditForm, 'html_title', 'html_description') | |
directives.no_omit(IAddForm, 'html_title', 'html_description') | |
class HTMLBasic(MetadataBase): | |
"""Clean the HTML title and description | |
and set regular Title and Description without HTML | |
""" | |
def _get_html_title(self): | |
return self.context.html_title | |
def cleanhtml(self, raw_html): | |
cleanr = re.compile('<.*?>') | |
cleanhtml = re.compile('&.*?;') | |
cleantext = re.sub(cleanr, '', raw_html) | |
cleantext = re.sub(cleanhtml, '', cleantext) | |
return cleantext | |
def _set_html_title(self, value): | |
if not value: | |
self.context.title = '' | |
return | |
if not isinstance(value, six.text_type): | |
raise ValueError('Title must be text.') | |
scrubbed_value = self.transform.scrub_html(value) | |
self.context.html_title = scrubbed_value | |
self.context.title = self.cleanhtml(scrubbed_value) | |
html_title = property(_get_html_title, _set_html_title) | |
def _get_html_description(self): | |
return self.context.html_description | |
def _set_html_description(self, value): | |
if not value: | |
self.context.description = '' | |
return | |
if not isinstance(value, six.text_type): | |
raise ValueError('Description must be text.') | |
scrubbed_value = self.transform.scrub_html(value) | |
self.context.html_description = scrubbed_value | |
self.context.description = self.cleanhtml(scrubbed_value) | |
html_description = property(_get_html_description, _set_html_description) | |
def __init__(self, *args, **kwargs): | |
super(HTMLBasic, self).__init__(*args, **kwargs) | |
self.transform = SafeHTML() | |
@provider(IFormFieldProvider) | |
class IHTMLDublinCore(IOwnership, IPublication, ICategorization, IHTMLBasic): | |
""" Metadata behavior providing all the DC fields | |
""" | |
pass | |
class HTMLDublinCore(HTMLBasic, Categorization, Publication, Ownership): | |
pass |
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
<div class="documentDescription description" | |
tal:define="html_descrip context/html_description | nothing; | |
plain_descrip context/Description" | |
tal:condition="plain_descrip"> | |
<tal:html tal:condition="html_descrip" | |
tal:content="structure html_descrip"> | |
Description | |
</tal:html> | |
<tal:plain tal:condition="not: html_descrip" | |
tal:content="plain_descrip"> | |
Description | |
</tal:plain> | |
</div> |
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
<h1 class="documentFirstHeading" | |
tal:define="html_title context/html_title | nothing; | |
plain_title context/Title" | |
tal:condition="plain_title"> | |
<tal:html tal:condition="html_title" | |
tal:content="structure html_title"> | |
Title or id | |
</tal:html> | |
<tal:plain tal:condition="not: html_title" | |
tal:content="plain_title"> | |
Title or id | |
</tal:plain> | |
</h1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment