def test_it_detects_simple_interpolations():

    assert parse_interpolations('chips $fish') == [
        'chips ', Interpolation('$fish', 'fish')
    ]

    assert parse_interpolations('chips $fish[0]') == [
        'chips ', Interpolation('$fish[0]', 'fish[0]')
    ]

    assert parse_interpolations('chips $fish[::-1]') == [
        'chips ', Interpolation('$fish[::-1]', 'fish[::-1]')
    ]

    assert parse_interpolations('chips $fish[1:-1]') == [
        'chips ', Interpolation('$fish[1:-1]', 'fish[1:-1]')
    ]

    assert parse_interpolations('chips $eggs[0].sausages mash') == [
        'chips ',
        Interpolation('$eggs[0].sausages', 'eggs[0].sausages'), ' mash'
    ]
コード例 #2
0
def _interpolated_str_to_ast_value(source):
    items = List(
        [(Str(item)
          if isinstance(item, (str, ustr))
          else parse_and_strip(u'x = ({})'.format(item.value))[0].value)
         for item in interpolate.parse_interpolations(source)],
        Load()
    )
    return Call(func=Attribute(Str(''), 'join', Load()),
                args=[items],
                starargs=None,
                kwargs=None,
                keywords=[])
def _compile_attr(a, container):
    """
    Compile a single parse.Attribute
    """
    intro_text = im.TextNode.factory(
        '{0.name}{0.space1}={0.space2}{0.quote}'.format(a), pos=a.pos)
    outro_text = im.TextNode.factory('{0.quote}{0.space3}'.format(a),
                                     pos=a.value_pos.advance(a.value))

    items = interpolate.parse_interpolations(a.value)

    if a.name in HTML_EMPTY_ATTRS and \
       len(items) == 1 and \
       isinstance(items[0], interpolate.Interpolation):
        interp = items[0]
        container.append(
            im.WithNode(vars=[('__piglet_tmp', interp.value)],
                        pos=a.pos,
                        children=[
                            im.IfNode(test='__piglet_tmp is not None',
                                      pos=a.pos,
                                      children=[
                                          intro_text,
                                          im.InterpolateNode(
                                              value='__piglet_tmp',
                                              pos=a.value_pos), outro_text
                                      ])
                        ]))
    else:
        container.append(intro_text)
        pos = a.value_pos
        for i in items:
            if isinstance(i, (str, ustr)):
                container.append(im.TextNode.factory(i, pos))
                pos = pos.advance(i)
            else:
                container.append(
                    im.InterpolateNode(value=unescape(i.value), pos=pos))
                pos = pos.advance(i.source)
        outro_text.pos = pos
        container.append(outro_text)
def _compile_text(node):
    """
    Compile :class:`piglet.parse.Text` object ``node`` into intermediate Text
    and Interpolation nodes.

    :param node: the :class:`piglet.parse.Text` object
    """
    items = interpolate.parse_interpolations(node.content)
    container = im.ContainerNode()
    pos = node.pos
    for i in items:
        if isinstance(i, (str, ustr)):
            container.append(im.TextNode(i, pos=pos))
            pos = pos.advance(i)
        elif isinstance(i, interpolate.Interpolation):
            container.append(
                im.InterpolateNode.factory(value=unescape(i.value),
                                           pos=pos,
                                           autoescape=i.autoescape
                                           and (not node.cdata)))
            pos = pos.advance(i.source)
    return container
def test_it_detects_delimited_interpolations():
    assert parse_interpolations('fish ${{"foo": "bar"}[item]} chips') == [
        'fish ',
        Interpolation('${{"foo": "bar"}[item]}', '{"foo": "bar"}[item]'),
        ' chips'
    ]
def test_it_handles_escaped_dollars():
    assert parse_interpolations('$${') == ['$', '{']
def test_it_handles_dollars_in_other_contexts():
    assert parse_interpolations('$11') == ['$', '11']
    assert parse_interpolations('1$') == ['1', '$']
    assert parse_interpolations('$$$') == ['$', '$']
    assert parse_interpolations('$$el') == ['$', 'el']
def test_it_parses_text():
    assert parse_interpolations('xyzzy') == ['xyzzy']