コード例 #1
0
    def generate_html(self):
        icon_url = None
        if self.target_avatar:
            icon_url = self.target_avatar

        return html.div(class_='autocomplete-block', *[
            html.div(class_='content ellipsis', *[
                html.div(class_='media', *[
                    html.img(src=icon_url, width='40', height='40') if icon_url else html.span()
                ]),
                html.span(*[self.target_username]),
                html.br(),
                html.small(class_='muted', *['Account'])
            ])
        ])
コード例 #2
0
def build_tree_from_text_entity_pack(
        request,
        text_entity_pack,
        itemscope='https://join.app.net/schemas/Post',
        convert_new_lines=False):
    # adapted from omo models TextEntityPack.html
    def entity_text(e):
        return text_entity_pack['text'][e['pos']:e['pos'] + e['len']]

    mention_builder = lambda m: html.a(itemprop='mention',
                                       data={
                                           'mention-name': m['name'],
                                           'mention-id': m['id']
                                       },
                                       href=smart_reverse(request,
                                                          'user_detail_view',
                                                          args=[m['name']],
                                                          force_qualified=True
                                                          ),
                                       *[entity_text(m)])
    hashtag_builder = lambda h: html.a(
        itemprop='hashtag',
        data={'hashtag-name': h['name']},
        href=smart_reverse(
            request, 'hashtags', args=[h['name']], force_qualified=True),
        *[entity_text(h)])
    link_builder = lambda l: html.a(
        href=l['url'], target="_blank", rel='nofollow', *[entity_text(l)])

    # map starting position, length of entity placeholder to the replacement html
    entity_map = {}
    for entity_key, builder in [('mentions', mention_builder),
                                ('hashtags', hashtag_builder),
                                ('links', link_builder)]:
        for entity in text_entity_pack.get('entities', {}).get(entity_key, []):
            try:
                entity_map[(entity['pos'], entity['len'])] = builder(entity)
            except NoReverseMatch:
                logger.warning(
                    'Could not build link for entity=%s in Pau path %s.',
                    entity.get('name'), request.path)

    # replace strings with html
    html_pieces = []
    text_idx = 0  # our current place in the original text string
    for entity_start, entity_len in sorted(entity_map.keys()):
        if text_idx != entity_start:
            # if our current place isn't the start of an entity, bring in text until the next entity
            html_pieces.append(
                text_entity_pack.get('text', "")[text_idx:entity_start])

        # pull out the entity html
        entity_html = entity_map[(entity_start, entity_len)]
        html_pieces.append(entity_html)

        # move past the entity we just added
        text_idx = entity_start + entity_len

    # clean up any remaining text
    html_pieces.append(text_entity_pack.get('text', "")[text_idx:])
    if convert_new_lines:
        new_html_pieces = []
        for piece in html_pieces:
            if isinstance(piece, basestring) and '\n' in piece:
                new_html_pieces += list(
                    intersperse(html.br(), piece.split('\n')))
            else:
                new_html_pieces.append(piece)

        html_pieces = new_html_pieces

    # TODO: link to schema
    return html.span(itemscope=itemscope, *html_pieces)
コード例 #3
0
ファイル: feed.py プロジェクト: 0xMF/alpha
def build_tree_from_text_entity_pack(request, text_entity_pack, itemscope='https://join.app.net/schemas/Post', convert_new_lines=False):
    # adapted from omo models TextEntityPack.html
    def entity_text(e):
        return text_entity_pack['text'][e['pos']:e['pos'] + e['len']]

    mention_builder = lambda m: html.a(
        itemprop='mention',
        data={
            'mention-name': m['name'], 'mention-id': m['id']
        },
        href=smart_reverse(request, 'user_detail_view', args=[m['name']], force_qualified=True),
        *[entity_text(m)]
    )
    hashtag_builder = lambda h: html.a(
        itemprop='hashtag',
        data={
            'hashtag-name': h['name']
        },
        href=smart_reverse(request, 'hashtags', args=[h['name']], force_qualified=True),
        *[entity_text(h)]
    )
    link_builder = lambda l: html.a(href=l['url'], target="_blank", rel='nofollow', *[entity_text(l)])

    # map starting position, length of entity placeholder to the replacement html
    entity_map = {}
    for entity_key, builder in [('mentions', mention_builder), ('hashtags', hashtag_builder), ('links', link_builder)]:
        for entity in text_entity_pack.get('entities', {}).get(entity_key, []):
            try:
                entity_map[(entity['pos'], entity['len'])] = builder(entity)
            except NoReverseMatch:
                logger.warning('Could not build link for entity=%s in Pau path %s.', entity.get('name'), request.path)

    # replace strings with html
    html_pieces = []
    text_idx = 0  # our current place in the original text string
    for entity_start, entity_len in sorted(entity_map.keys()):
        if text_idx != entity_start:
            # if our current place isn't the start of an entity, bring in text until the next entity
            html_pieces.append(text_entity_pack.get('text', "")[text_idx:entity_start])

        # pull out the entity html
        entity_html = entity_map[(entity_start, entity_len)]
        html_pieces.append(entity_html)

        # move past the entity we just added
        text_idx = entity_start + entity_len

    # clean up any remaining text
    html_pieces.append(text_entity_pack.get('text', "")[text_idx:])
    if convert_new_lines:
        new_html_pieces = []
        for piece in html_pieces:
            if isinstance(piece, basestring) and '\n' in piece:
                new_html_pieces += list(intersperse(html.br(), piece.split('\n')))
            else:
                new_html_pieces.append(piece)

        html_pieces = new_html_pieces

    # TODO: link to schema
    return html.span(itemscope=itemscope, *html_pieces)