Beispiel #1
0
def revisions_feed(request=None, pagename=None, feedtype="atom"):
    if pagename is None:
        pagename ='Home'
    page = get_page(request.site._id, pagename)
    if not page:
        raise NotFound
    all_revisions = [page] + page.revisions()
    if feedtype == "atom":
        feed = AtomFeed(
                    title="%s: Latest revisions of %s" % (request.site.cname, page.title),
                    subtitle=request.site.subtitle,
                    updated = page.updated,
                    feed_url = request.url
        )
        for rev in all_revisions:
            title = ''
            _url="%s%s" % (request.host_url, url_for("revision_page", 
                pagename=pagename, 
                nb_revision=rev.nb_revision
            ))
            for change in rev.changes:
                if change['type'] != "unmod":
                    title = "\n".join(change['changed']['lines'])
                    title = do_truncate(do_striptags(title), 60)
            title = title and title or "Edited."
            feed.add(title, convert_markdown(rev.content), 
                updated=rev.updated,
                url=_url,
                id=_url,
                author=rev.title.replace(' ', '_')
            )
        return feed.get_response()
    else:
        json = {
            'title': "%s: Latest revisions of %s" % (request.site.cname, page.title),
            'subtitle': request.site.subtitle,
            'updated':datetime_tojson(page.updated),
            'feed_url': request.url,
            'revisions': []
        }
        for rev in all_revisions:
            title = ''
            for change in rev.changes:
                if change['type'] != "unmod":
                    title = "\n".join(change['changed']['lines'])
                    title = do_truncate(do_striptags(title), 60)
                    
            title = title and title or "Edited."
            url = "%s%s" % (request.host_url, url_for("revision_page", 
                        cname=request.site.cname, pagename=pagename, 
                        nb_revision=rev.nb_revision
            ))
            json['revisions'].append({
                'title': title,
                'content': rev.content,
                'url':  url,
                'updated':datetime_tojson(rev.updated),
                'id':rev.nb_revision
            })
        return send_json(json)
def shorten(env, text, *args, **kwargs):
    if isinstance(text, Markdown):
        text.source = do_truncate(env, text.source, *args, **kwargs)
    elif isinstance(text, Markup):
        # text = Markup(do_truncate(env, text, *args, **kwargs))
        pass
    else:
        text = do_truncate(env, text, *args, **kwargs)
    return text
Beispiel #3
0
def truncate(value, length=255, killwords=None, end='...'):
    ''' A more clever truncate. If killwords is supplied we use the default
    truncate.  Otherwise we try to truncate using killwords=False, if this
    truncates the whole value we try again with killwords=True '''
    if killwords is not None:
        return do_truncate(value, length=length, killwords=killwords, end=end)
    result = do_truncate(value, length=length, killwords=False, end=end)
    if result != end:
        return result
    return do_truncate(value, length=length, killwords=True, end=end)
Beispiel #4
0
def _get_recent_questions(mandate, limit):
    recent_questions_query = (
        Question.query
        .order_by(Question.date.desc())
    )

    if mandate is not None:
        recent_questions_query = (
            recent_questions_query
            .join(Question.asked)
            .filter(Ask.mandate == mandate)
        )

    if limit is not None:
        recent_questions_query = recent_questions_query.limit(limit)

    return [
        {
            'date': q.date,
            'text': filters.do_truncate(q.title),
            'type': q.type,
            'question_id': q.id,
        }
        for q in recent_questions_query
    ]
Beispiel #5
0
    def get_recent_activity(self, limit=None, limit_each=None):
        recent_transcripts_query = (
            self.mandate.transcripts
            .order_by(Transcript.serial.desc())
            .options(joinedload('chapter'))
        )
        if limit_each is not None:
            recent_transcripts_query = (
                recent_transcripts_query
                .limit(limit_each)
            )
        recent_transcripts = [
            {
                'date': t.chapter.date,
                'text': filters.do_truncate(t.text, 200),
                'type': 'speech',
                'chapter_serial': t.chapter.serial,
                'serial_id': t.serial_id,
            }
            for t in recent_transcripts_query
        ]

        recent_questions = _get_recent_questions(self.mandate, limit_each)
        recent_proposals = _get_recent_proposals(self.mandate, limit_each)

        rv = recent_transcripts + recent_questions + recent_proposals
        rv.sort(key=lambda r: r['date'], reverse=True)
        if limit is not None:
            rv = rv[:limit]

        return rv
Beispiel #6
0
    def __init__(self, user, response_format, instance, *args, **kwargs):

        super(ObjectLinksForm, self).__init__(*args, **kwargs)

        queryset = Object.filter_permitted(user, Object.objects)
        self.fields['links'].queryset = queryset

        if 'ajax' not in response_format:
            if instance:
                queryset = queryset.exclude(pk__in=instance.links.all())

            choices = []
            for obj in queryset:
                human_type = obj.get_human_type()
                name = do_truncate(
                    do_striptags(unicode(obj.object_name)), 20, True)
                if human_type:
                    name += u" (" + human_type + u")"
                choices.append((obj.id, name))
            self.fields['links'].choices = choices

        self.fields['links'].label = ""
        self.fields['links'].initial = ""
        self.fields['links'].widget.attrs.update({'class': 'autocomplete',
                                                  'callback': reverse('core_ajax_object_lookup')})
Beispiel #7
0
def rss():    
    config = public_app.config['feed']
    fg = FeedGenerator()
    fg.id('%s/blog' % Config.BASE_URL)
    fg.title(config['title'])
    fg.author( {'name': config['author'],'email': config['email']} )
    fg.description(config['desc'])
    fg.link( href=Config.BASE_URL, rel='alternate' )
    query = {
        'id': { '$regex': 'blog' },
        'current': True,
        'meta.hide': { '$ne': True }
    }
    posts = db.pages.find(query).sort('meta.created', -1)[:20]
    for post in posts:
        fe = fg.add_entry()
        fe.title(post['meta']['title'])
        if 'author' in post['meta']:
            fe.author( {'name': post['meta']['author'],'email': config['email']} )
        else:
            fe.author( {'name': config['author'],'email': config['email']} )
        fe.description(do_truncate(post['content'], 300))
        fe.link(href="%s/%s" % (Config.BASE_URL, post['id']), rel='alternate')
        fe.pubdate(utc.localize(post['meta']['created']))
        fe.content(post['content'])    
    response.headers['Content-Type'] = 'application/rss+xml'
    return fg.rss_str(pretty=True)
Beispiel #8
0
def build_access_request_data(registration, event, generate_code):
    """Return a dictionary with data required by ADaMS API."""
    from indico_cern_access.plugin import CERNAccessPlugin
    start_dt, end_dt = get_access_dates(get_last_request(event))
    tz = timezone('Europe/Zurich')
    title = do_truncate(None,
                        unicode_to_ascii(remove_accents(event.title)),
                        100,
                        leeway=0)
    if generate_code:
        reservation_code = get_random_reservation_code()
    else:
        reservation_code = registration.cern_access_request.reservation_code
    data = {
        '$id': generate_access_id(registration.id),
        '$rc': reservation_code,
        '$gn': title,
        '$fn': unicode_to_ascii(remove_accents(registration.first_name)),
        '$ln': unicode_to_ascii(remove_accents(registration.last_name)),
        '$sd': start_dt.astimezone(tz).strftime('%Y-%m-%dT%H:%M'),
        '$ed': end_dt.astimezone(tz).strftime('%Y-%m-%dT%H:%M')
    }

    if registration.cern_access_request and registration.cern_access_request.license_plate:
        data['$lp'] = registration.cern_access_request.license_plate

    checksum = ';;'.join('{}:{}'.format(key, value)
                         for key, value in sorted(data.viewitems()))
    signature = hmac.new(str(CERNAccessPlugin.settings.get('secret_key')),
                         checksum, hashlib.sha256)
    data['$si'] = signature.hexdigest()
    return data
Beispiel #9
0
    def __init__(self, user, response_format, instance, *args, **kwargs):

        super(ObjectLinksForm, self).__init__(*args, **kwargs)

        queryset = Object.filter_permitted(user, Object.objects)
        self.fields['links'].queryset = queryset

        if not 'ajax' in response_format:
            if instance:
                queryset = queryset.exclude(pk__in=instance.links.all())

            choices = []
            for obj in queryset:
                human_type = obj.get_human_type()
                name = do_truncate(do_striptags(unicode(obj.object_name)), 20,
                                   True)
                if human_type:
                    name += u" (" + human_type + u")"
                choices.append((obj.id, name))
            self.fields['links'].choices = choices

        self.fields['links'].label = ""
        self.fields['links'].initial = ""
        self.fields['links'].widget.attrs.update({
            'class':
            'autocomplete',
            'callback':
            reverse('core_ajax_object_lookup')
        })
Beispiel #10
0
 def label_from_instance(self, obj):
     "Label From Instance"
     name = unicode(obj)
     obj_type = obj.get_human_type()
     label = filters.do_truncate(filters.do_striptags(name), 30)
     if obj_type:
         label += " (" + obj_type + ")" 
     return label
Beispiel #11
0
 def label_from_instance(self, obj):
     "Label From Instance"
     name = unicode(obj)
     obj_type = obj.get_human_type()
     label = filters.do_truncate(filters.do_striptags(name), 30)
     if obj_type:
         label += " (" + obj_type + ")"
     return label
Beispiel #12
0
def post_details(year, slug):
    post = post_from_year_and_slug_or_404(year, slug)
    description = do_truncate(current_app.jinja_env,
                              do_striptags(post.rendered_content))
    title = post.title
    return render_template('post_details.html',
                           post=post,
                           description=description,
                           title=title)
Beispiel #13
0
    def _truncate(author_str, separator=None):

        # If we have a separator, split string on it
        if separator:
            shortened = ';'.join(author_str.split(separator)[0:4])
        else:
            # Otherwise use the jinja truncate function (may split author name)
            shortened = do_truncate(author_str, length=AUTHOR_MAX_LENGTH, end='')

        return literal('%s <abbr title="%s">et al.</abbr>' % (shortened, author_str))
Beispiel #14
0
def truncate_string(s: str, length: int) -> str:
    """Truncate string at word boundary.

    :param s: string to be truncated
    :type s: str
    :param length: max length of result string
    :type length: int
    :return: truncated text
    :rtype: str
    """
    return do_truncate(None, s, length, leeway=5)
Beispiel #15
0
    def preview(self):
        """HTML representing a short preview of the post.

        Contains the first 200 characters of the post's content, followed by a
        link to the post itself.

        """
        preview_text = do_striptags(self._content)
        link = '... <a href="{}">Continue&rarr;</a>'.format(self.url)
        preview_html = do_truncate(preview_text, length=200, end=link)
        return preview_html
Beispiel #16
0
 def __init__(self, test, item, error, message=None):
     self.item = item
     self.error = error
     if not message:
         if isinstance(error, JSONSchemaValidationError):
             error = error.message  # Avoid the huge "verbose" dump
         message = '{error} (in {item})'.format(
             error=error,
             item=do_truncate(str(item), 40),
         )
     super(ValidationException, self).__init__(test=test, message=message)
Beispiel #17
0
 def __init__(self, test, item, error, message=None):
     self.item = item
     self.error = error
     if not message:
         if isinstance(error, JSONSchemaValidationError):
             error = error.message  # Avoid the huge "verbose" dump
         message = '{error} (in {item})'.format(
             error=error,
             item=do_truncate(str(item), 40),
         )
     super(ValidationException, self).__init__(test=test, message=message)
Beispiel #18
0
    def _get_recent_questions(self, mandate, limit):
        recent_questions_query = (Question.query.order_by(
            Question.date.desc()))

        if mandate is not None:
            recent_questions_query = (recent_questions_query.join(
                Question.asked).filter(Ask.mandate == mandate))

        return [{
            'date': q.date,
            'text': filters.do_truncate(q.title),
            'type': q.type,
            'question_id': q.id,
        } for q in recent_questions_query.limit(limit)]
Beispiel #19
0
    def mdstrip(value, length=None):
        '''
        Truncate and strip tags from a markdown source

        The markdown source is truncated at the excerpt if present and smaller than the required length.
        Then, all html tags are stripped.
        '''
        if not value:
            return ''
        if EXCERPT_TOKEN in value:
            value = value.split(EXCERPT_TOKEN, 1)[0]
        if length > 0:
            value = do_truncate(value, length)
        rendered = md(value)
        return do_striptags(rendered)
Beispiel #20
0
    def mdstrip(value, length=None):
        '''
        Truncate and strip tags from a markdown source

        The markdown source is truncated at the excerpt if present and smaller than the required length.
        Then, all html tags are stripped.
        '''
        if not value:
            return ''
        if EXCERPT_TOKEN in value:
            value = value.split(EXCERPT_TOKEN, 1)[0]
        if length > 0:
            value = do_truncate(value, length)
        rendered = md(value)
        return do_striptags(rendered)
Beispiel #21
0
    def _get_recent_activity(self, mandate):
        recent_transcripts_query = (mandate.transcripts.order_by(
            Transcript.serial.desc()).limit(5).options(joinedload('chapter')))
        recent_transcripts = [{
            'date': t.chapter.date,
            'text': filters.do_truncate(t.text, 200),
            'type': 'speech',
        } for t in recent_transcripts_query]

        recent_questions = self._get_recent_questions(mandate, 5)
        recent_proposals = self._get_recent_proposals(mandate, 5)

        rv = recent_transcripts + recent_questions + recent_proposals
        rv.sort(key=lambda r: r['date'], reverse=True)
        return rv[:10]
Beispiel #22
0
def mdstrip(value, length=None, end='…'):
    '''
    Truncate and strip tags from a markdown source

    The markdown source is truncated at the excerpt if present and
    smaller than the required length. Then, all html tags are stripped.
    '''
    if not value:
        return ''
    if EXCERPT_TOKEN in value:
        value = value.split(EXCERPT_TOKEN, 1)[0]
    rendered = md(value, wrap=False)
    text = do_striptags(rendered)
    if length and length > 0:
        text = do_truncate(None, text, length, end=end, leeway=2)
    return text
Beispiel #23
0
def mdstrip(value, length=None, end='…'):
    '''
    Truncate and strip tags from a markdown source

    The markdown source is truncated at the excerpt if present and
    smaller than the required length. Then, all html tags are stripped.
    '''
    if not value:
        return ''
    if EXCERPT_TOKEN in value:
        value = value.split(EXCERPT_TOKEN, 1)[0]
    rendered = md(value, wrap=False)
    text = do_striptags(rendered)
    text = bleach_clean(text)
    if length and length > 0:
        text = do_truncate(None, text, length, end=end, leeway=2)
    return text
Beispiel #24
0
def myhtmlstrip(astring, n=3):
    '''
        strip the article.body to n <p> elements in index.html
    '''
    # match the contents of style in img tags
    imgstyle = re.compile(
        r'(?P<imgtag>\s*img[^>].*?style=)(?P<quote>[\'\"]).*?(?P=quote)', re.I)
    # sub the contents of style in img tags with "width:100%;"
    s = imgstyle.sub('\g<imgtag>\g<quote>max-width:100%;\g<quote>', astring)
    # find all the <p> elements except <p>&nbsp;</p>
    s = re.sub(r'<p>\&nbsp;</p>', '', s, re.M)
    para = re.compile(r'<\s*p[^>]*>.*?<\s*/\s*p\s*>', re.I)
    P = re.findall(para, s)
    # remove all html tags for safe
    P = map(do_striptags, P)
    # join the first n items
    P = "</p><p>".join(P[:n])
    # if no <p> elements, do_truncate
    P = do_mark_safe("<p>%s</p>" %
                     P) if P else do_truncate(do_striptags(s), 255, True)
    return P
Beispiel #25
0
    def _get_recent_activity(self, mandate):
        recent_transcripts_query = (
            mandate.transcripts
            .order_by(Transcript.serial.desc())
            .limit(5)
            .options(joinedload('chapter'))
        )
        recent_transcripts = [
            {
                'date': t.chapter.date,
                'text': filters.do_truncate(t.text, 200),
                'type': 'speech',
            }
            for t in recent_transcripts_query
        ]

        recent_questions = self._get_recent_questions(mandate, 5)
        recent_proposals = self._get_recent_proposals(mandate, 5)

        rv = recent_transcripts + recent_questions + recent_proposals
        rv.sort(key=lambda r: r['date'], reverse=True)
        return rv[:10]
Beispiel #26
0
def mycreatedstrip(astring):
    '''
        strip article.created to YYYY-MM-DD
    '''
    return do_truncate(astring, 10, True, '')
Beispiel #27
0
 def _format(_html):
     return do_truncate(do_striptags(_html), length=200)
Beispiel #28
0
import re
import time

from jinja2.filters import escape, do_truncate, do_mark_safe

from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name

try:
    import simplejson as json
except:
    import json

trunc = lambda s, length=255, end='...': do_truncate(s, length, True, end)
safe = do_mark_safe

tbre = re.compile('(?P<tb>Traceback \(most recent call last\):.*)', re.MULTILINE | re.DOTALL)
url_re = re.compile(r"""(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))""")

class SimpleTimer(object):
    def __init__(self):
        self.timers = {}

    def start(self, name):
        self.timers[name] = time.time()

    def end(self, name):
        if name in self.timers:
            self.timers[name] = time.time() - self.timers[name]
Beispiel #29
0
 def _format(_html):
     return do_truncate(do_striptags(_html), True, length=150)
 def summary(self) -> str:
     if self.description is None:
         return ""
     else:
         return do_truncate(None, html_re.sub("", self.description), 80,
                            False, "&hellip;", 5)
Beispiel #31
0
def format_highlight(highlights):
    return Markup(' [...] '.join(do_truncate(h) for h in highlights))