示例#1
0
 def test_bodyfinder(self):
     self.assertEqual(bodyfinder(FAUX_HTML_LEADING_TEXT),
                      '\n  <h1>Not a lot here</h1>\n ')
     self.assertEqual(bodyfinder(SIMPLE_HTML),
                      '\n  <h1>Not a lot here</h1>\n ')
     self.assertEqual(bodyfinder(SIMPLE_STRUCTUREDTEXT),
                      SIMPLE_STRUCTUREDTEXT)
     self.assertEqual(bodyfinder(SIMPLE_XHTML),
                      '\n  <h1>Not a lot here</h1>\n ')
     self.assertEqual(bodyfinder(STX_WITH_HTML),
                      '<p>Hello world, I am Bruce.</p>')
示例#2
0
    def test_bodyfinder(self):
        from Products.CMFDefault.utils import bodyfinder

        self.assertEqual(bodyfinder(FAUX_HTML_LEADING_TEXT),
                         '\n  <h1>Not a lot here</h1>\n ')
        self.assertEqual(bodyfinder(SIMPLE_HTML),
                         '\n  <h1>Not a lot here</h1>\n ')
        self.assertEqual(bodyfinder(SIMPLE_STRUCTUREDTEXT),
                         SIMPLE_STRUCTUREDTEXT)
        self.assertEqual(bodyfinder(SIMPLE_XHTML),
                         '\n  <h1>Not a lot here</h1>\n ')
        self.assertEqual(bodyfinder(STX_WITH_HTML),
                         '<p>Hello world, I am Bruce.</p>')
示例#3
0
    def setFormat(self, format):
        """ Set text format and Dublin Core resource format.
        """
        value = str(format)
        old_value = self.text_format
        text_formats = ('structured-text', 'plain', 'restructured-text')

        if value == 'text/html' or value == 'html':
            self.text_format = 'html'
        elif value == 'text/plain':
            if self.text_format not in text_formats:
                self.text_format = 'structured-text'
        elif value == 'plain':
            self.text_format = 'plain'
        elif value == 'restructured-text':
            self.text_format = 'restructured-text'
        else:
            self.text_format = 'structured-text'

        # Did the format change? We might need to re-cook the content.
        if value != old_value:
            if html_headcheck(self.text) and value != 'plain':
                self.text = bodyfinder(self.text)

            self._edit(self.text)
示例#4
0
    def edit(self, text_format, text, file='', safety_belt=''):
        """ Update the document.

        To add webDav support, we need to check if the content is locked, and if
        so return ResourceLockedError if not, call _edit.

        Note that this method expects to be called from a web form, and so
        disables header processing
        """
        self.failIfLocked()
        if not self._safety_belt_update(safety_belt=safety_belt):
            msg = _(u'Intervening changes from elsewhere detected. '
                    u'Please refetch the document and reapply your changes. '
                    u'(You may be able to recover your version using the '
                    u"browser 'back' button, but will have to apply them to "
                    u'a freshly fetched copy.)')
            raise EditingConflict(msg)
        if file and (type(file) is not type('')):
            contents = file.read()
            if contents:
                text = contents
        if html_headcheck(text) and text_format.lower() != 'plain':
            text = bodyfinder(text)
        self.setFormat(text_format)
        self._edit(text)
        self.reindexObject()
示例#5
0
    def convert(self, data, cache, **kwargs):
        kwargs['filename'] = 'unknow.rtf'

        tmpdir, fullname = self.initialize_tmpdir(data, **kwargs)
        html = self.invokeCommand(tmpdir, fullname)
        path, images = self.subObjects(tmpdir)
        objects = {}
        if images:
            self.fixImages(path, images, objects)
        self.cleanDir(tmpdir)
        cache.setData(bodyfinder(html))
        cache.setSubObjects(objects)
        return cache
示例#6
0
 def handleText(self, text, format=None):
     """ Handles the raw text, returning headers, body, format """
     headers = {}
     if not format:
         format = self.guessFormat(text)
     if format == 'text/html':
         parser = SimpleHTMLParser()
         parser.feed(text)
         headers.update(parser.metatags)
         if parser.title:
             headers['title'] = parser.title
         body = bodyfinder(text)
     else:
         headers, body = parseHeadersBody(text, headers)
     return headers, text, format
示例#7
0
文件: Event.py 项目: bendavis78/zope
    def handleText(self, text, format=None):
        """ Handles the raw text, returning headers, body, cooked, format """
        headers = {}
        if format == 'html':
            parser = SimpleHTMLParser()
            parser.feed(text)
            headers.update(parser.metatags)
            if parser.title:
                headers['Title'] = parser.title
            bodyfound = bodyfinder(text)
            if bodyfound:
                body = bodyfound
        else:
            headers, body = parseHeadersBody(text, headers)

        return headers, body, format
示例#8
0
    def convert(self, data, cache, **kwargs):
        kwargs['filename'] = 'unknown.pdf'

        ## tmpdir, fullname = self.initialize_tmpdir(data, **kwargs)

        tmpdir, fullname, tex_fd = self.initialize_tmpdir()
        html = self.invokeCommand(data, tmpdir, fullname, tex_fd)
        htmldir = fullname[:-4] + '/'
        path, images = self.subObjects(htmldir)
        objects = {}
        if images:
            self.fixImages(path, images, objects)
        self.cleanDir(tmpdir)
        cache.setData(bodyfinder(html))
        cache.setSubObjects(objects)
        return cache
示例#9
0
    def convert(self, data, cache, **kwargs):
        if 'filename' not in kwargs or not kwargs['filename']:
            kwargs['filename'] = 'unknown.pdf'

        tmpdir, fullname = self.initialize_tmpdir(data, **kwargs)
        html = self.invokeCommand(tmpdir, fullname)
        html = fixBrokenStyles(html)
        if process_double_encoding:
            html = noDoubleEncoding(html)

        path, images = self.subObjects(tmpdir)
        objects = {}
        if images:
            self.fixImages(path, images, objects)
        self.cleanDir(tmpdir)
        cache.setData(
            bodyfinder(html).decode('utf-8', 'replace').encode('utf-8'))
        cache.setSubObjects(objects)
        return cache
示例#10
0
def setPageText(portal, page, viewName):
    """
    Sets text of a Plone document if it exists and reindex the document
    The text is coming from a browser view template <body> tag
    """
    if page is None:
        return
    request = getattr(portal, 'REQUEST', None)
    if request is not None:
        view = queryMultiAdapter((portal, request), name=viewName)
        if view is not None:
            text = bodyfinder(view.index()).strip()
            try:
                page.setText(text, mimetype='text/html')
            except:
                from plone.app.textfield.value import RichTextValue
                rtv = RichTextValue(text)
                page.text = rtv
            page.reindexObject()
示例#11
0
 def getData(self, couterr):
     return bodyfinder(couterr.read())
示例#12
0
def setupPortalContent(p):
    """
    Import default plone content
    """
    existing = p.keys()
    wftool = getToolByName(p, "portal_workflow")

    language = p.Language()
    parts = (language.split('-') + [None, None])[:3]
    locale = locales.getLocale(*parts)
    target_language = base_language = locale.id.language

    # If we get a territory, we enable the combined language codes
    use_combined = False
    if locale.id.territory:
        use_combined = True
        target_language += '_' + locale.id.territory

    # As we have a sensible language code set now, we disable the
    # start neutral functionality
    tool = getToolByName(p, "portal_languages")
    pprop = getToolByName(p, "portal_properties")
    sheet = pprop.site_properties

    tool.manage_setLanguageSettings(language, [language],
                                    setUseCombinedLanguageCodes=use_combined,
                                    startNeutral=False)

    # Set the first day of the week, defaulting to Sunday, as the
    # locale data doesn't provide a value for English. European
    # languages / countries have an entry of Monday, though.
    calendar = getToolByName(p, "portal_calendar", None)
    if calendar is not None:
        first = 6
        gregorian = locale.dates.calendars.get(u'gregorian', None)
        if gregorian is not None:
            first = gregorian.week.get('firstDay', None)
            # on the locale object we have: mon : 1 ... sun : 7
            # on the calendar tool we have: mon : 0 ... sun : 6
            if first is not None:
                first = first - 1

        calendar.firstweekday = first

    # Enable visible_ids for non-latin scripts

    # See if we have an url normalizer
    normalizer = queryUtility(IURLNormalizer, name=target_language)
    if normalizer is None:
        normalizer = queryUtility(IURLNormalizer, name=base_language)

    # If we get a script other than Latn we enable visible_ids
    if locale.id.script is not None:
        if locale.id.script.lower() != 'latn':
            sheet.visible_ids = True

    # If we have a normalizer it is safe to disable the visible ids
    if normalizer is not None:
        sheet.visible_ids = False

    request = getattr(p, 'REQUEST', None)
    # The front-page
    if 'front-page' not in existing:
        front_title = u'Welcome to Plone'
        front_desc = u'Congratulations! You have successfully installed Plone.'
        front_text = None
        _createObjectByType('Document',
                            p,
                            id='front-page',
                            title=front_title,
                            description=front_desc)
        fp = p['front-page']
        if wftool.getInfoFor(fp, 'review_state') != 'published':
            wftool.doActionFor(fp, 'publish')

        if base_language != 'en':
            util = queryUtility(ITranslationDomain, 'plonefrontpage')
            if util is not None:
                front_title = util.translate(u'front-title',
                                             target_language=target_language,
                                             default="Welcome to Plone")
                front_desc = util.translate(
                    u'front-description',
                    target_language=target_language,
                    default="Congratulations! You have successfully installed "
                    "Plone.")
                translated_text = util.translate(
                    u'front-text', target_language=target_language)
                if translated_text != u'front-text':
                    front_text = translated_text

        if front_text is None and request is not None:
            view = queryMultiAdapter((p, request),
                                     name='plone-frontpage-setup')
            if view is not None:
                front_text = bodyfinder(view.index()).strip()

        fp.setTitle(front_title)
        fp.setDescription(front_desc)
        fp.setLanguage(language)
        fp.setText(front_text, mimetype='text/html')

        # Show off presentation mode
        fp.setPresentation(True)

        # Mark as fully created
        fp.unmarkCreationFlag()

        p.setDefaultPage('front-page')
        fp.reindexObject()

    # News topic
    if 'news' not in existing:
        news_title = 'News'
        news_desc = 'Site News'
        if base_language != 'en':
            util = queryUtility(ITranslationDomain, 'plonefrontpage')
            if util is not None:
                news_title = util.translate(u'news-title',
                                            target_language=target_language,
                                            default='News')
                news_desc = util.translate(u'news-description',
                                           target_language=target_language,
                                           default='Site News')

        _createObjectByType('Folder',
                            p,
                            id='news',
                            title=news_title,
                            description=news_desc)
        _createObjectByType('Collection',
                            p.news,
                            id='aggregator',
                            title=news_title,
                            description=news_desc)

        folder = p.news
        folder.setOrdering('unordered')
        folder.setConstrainTypesMode(constraintypes.ENABLED)
        folder.setLocallyAllowedTypes(['News Item'])
        folder.setImmediatelyAddableTypes(['News Item'])
        folder.setDefaultPage('aggregator')
        folder.unmarkCreationFlag()
        folder.setLanguage(language)

        if wftool.getInfoFor(folder, 'review_state') != 'published':
            wftool.doActionFor(folder, 'publish')

        topic = p.news.aggregator
        topic.setLanguage(language)

        query = [{
            'i': 'portal_type',
            'o': 'plone.app.querystring.operation.selection.is',
            'v': ['News Item']
        }, {
            'i': 'review_state',
            'o': 'plone.app.querystring.operation.selection.is',
            'v': ['published']
        }]
        topic.setQuery(query)

        topic.setSort_on('effective')
        topic.setSort_reversed(True)
        topic.setLayout('folder_summary_view')
        topic.unmarkCreationFlag()

        if wftool.getInfoFor(topic, 'review_state') != 'published':
            wftool.doActionFor(topic, 'publish')

    # Events topic
    if 'events' not in existing:
        events_title = 'Events'
        events_desc = 'Site Events'
        if base_language != 'en':
            util = queryUtility(ITranslationDomain, 'plonefrontpage')
            if util is not None:
                events_title = util.translate(u'events-title',
                                              target_language=target_language,
                                              default='Events')
                events_desc = util.translate(u'events-description',
                                             target_language=target_language,
                                             default='Site Events')

        _createObjectByType('Folder',
                            p,
                            id='events',
                            title=events_title,
                            description=events_desc)
        _createObjectByType('Collection',
                            p.events,
                            id='aggregator',
                            title=events_title,
                            description=events_desc)
        folder = p.events
        folder.setOrdering('unordered')
        folder.setConstrainTypesMode(constraintypes.ENABLED)
        folder.setLocallyAllowedTypes(['Event'])
        folder.setImmediatelyAddableTypes(['Event'])
        folder.setDefaultPage('aggregator')
        folder.unmarkCreationFlag()
        folder.setLanguage(language)

        if wftool.getInfoFor(folder, 'review_state') != 'published':
            wftool.doActionFor(folder, 'publish')

        topic = folder.aggregator
        topic.unmarkCreationFlag()
        topic.setLanguage(language)

        query = [{
            'i': 'portal_type',
            'o': 'plone.app.querystring.operation.selection.is',
            'v': ['Event']
        }, {
            'i': 'start',
            'o': 'plone.app.querystring.operation.date.afterToday',
            'v': ''
        }, {
            'i': 'review_state',
            'o': 'plone.app.querystring.operation.selection.is',
            'v': ['published']
        }]
        topic.setQuery(query)
        topic.setSort_on('start')
    else:
        topic = p.events

    if wftool.getInfoFor(topic, 'review_state') != 'published':
        wftool.doActionFor(topic, 'publish')

    # configure Members folder
    members_title = 'Users'
    members_desc = "Site Users"
    if 'Members' not in existing:
        _createObjectByType('Folder',
                            p,
                            id='Members',
                            title=members_title,
                            description=members_desc)

    if 'Members' in p.keys():
        if base_language != 'en':
            util = queryUtility(ITranslationDomain, 'plonefrontpage')
            if util is not None:
                members_title = util.translate(u'members-title',
                                               target_language=target_language,
                                               default='Users')
                members_desc = util.translate(u'members-description',
                                              target_language=target_language,
                                              default="Site Users")

        members = getattr(p, 'Members')
        members.setTitle(members_title)
        members.setDescription(members_desc)
        members.setOrdering('unordered')
        members.unmarkCreationFlag()
        members.setLanguage(language)
        members.reindexObject()

        if wftool.getInfoFor(members, 'review_state') != 'published':
            wftool.doActionFor(members, 'publish')

        # add index_html to Members area
        if 'index_html' not in members.objectIds():
            addPy = members.manage_addProduct['PythonScripts'] \
                        .manage_addPythonScript
            addPy('index_html')
            index_html = getattr(members, 'index_html')
            index_html.write(member_indexhtml)
            index_html.ZPythonScript_setTitle('User Search')

        # Block all right column portlets by default
        manager = queryUtility(IPortletManager, name='plone.rightcolumn')
        if manager is not None:
            assignable = queryMultiAdapter((members, manager),
                                           ILocalPortletAssignmentManager)
            assignable.setBlacklistStatus('context', True)
            assignable.setBlacklistStatus('group', True)
            assignable.setBlacklistStatus('content_type', True)
示例#13
0
 def convert(self, orig, data, **kwargs):
     body = bodyfinder(orig)
     data.setData(body)
     return data