Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapping Links with Divs #73

Closed
HaoZeke opened this issue Nov 10, 2017 · 6 comments
Closed

Wrapping Links with Divs #73

HaoZeke opened this issue Nov 10, 2017 · 6 comments

Comments

@HaoZeke
Copy link

HaoZeke commented Nov 10, 2017

Hi, I've attempted to iterate over Links and tried Div.content.extend(Link) but I can't seem to get a link wrapped in a div outputted....

Any ideas?

@sergiocorreia
Copy link
Owner

There are roughly two types of Pandoc elements: block elements (paragraphs, lists, etc.) and inline elements (strings, spaces, bold text, etc.).

A Div is a block element that can only contain other block elements (see the part that says "args (Block) – contents of the div")).

On the other hand, a link is an inline element, so it cannot be contained directly inside a div. What you need to do is put the link in a block that can contain inlines (Para, Plain, etc.) and put that in a Div.

For instance:

import panflute as pf
div = pf.Div()
link = pf.Link(pf.Str('SomeText'), url='http://www.example.com')
# This fails:
div.content.append(link)
# This works:
div.content.append(pf.Plain(link))

@HaoZeke
Copy link
Author

HaoZeke commented Nov 10, 2017

Thanks! I was also wondering if there was a canonical way to insert a list of elements...

replace_keyword doesn't work for lists (NotImplementedError)

I noted that the documentation suggests wrapping them in another div,

ie.

div = Div(*IamLegion)
doc = doc.replace_keyword('$spooky', div)

However that's throwing my layout off... is there a way to remove the empty enclosing div?

@sergiocorreia
Copy link
Owner

Would it work if you use a span instead of a div? They are kinda the same, except that Span is an inline that contains inlines and Div is a block that contains blocks.

@HaoZeke
Copy link
Author

HaoZeke commented Nov 10, 2017

I shall name the div and unstyle it purposefully in my css I guess...

def prepare(doc):
    doc.hLinks = []
    doc.depth = int(doc.get_metadata('toc-depth', default=1))


def action(elem, doc):
    if isinstance(elem, Header) and elem.level <= doc.depth:
        plain = stringify(elem)
        item = Link(*elem.content,url="#"+stringify(elem))
        doc.hLinks.append(item)
        return []


def finalize(doc):
    div = []
    for x in range(len(doc.hLinks)):
       div.append(Div(classes=['navItem']).content.append(Plain(doc.hLinks[x])))
    divR = Div(*div, classes=['noLove'])
    doc = doc.replace_keyword('$hLinks', divR)
    del doc.hLinks, doc.depth, div

The code above errors out with TypeError: received NoneType but expected <class 'panflute.base.Block'>
, if I try *doc.hLinks I get the same error and with plain do.hLinks I get a list error...

Any ideas?

@sergiocorreia
Copy link
Owner

BTW you don't need the del statement.. the objects will automatically be deleted at the end.

Also, the for x in range.. iteration might be more Pythonic if you just write it as a list comprehension:

divs = [Div(Plain(x), classes=['navItem']) for x in doc.hLinks]

This also helps in solving the error, which is that you are creating Div() without any content, and then appending to it ("received NoneType"). Instead, here you create Div with Plain(x) as content directly.

@HaoZeke
Copy link
Author

HaoZeke commented Nov 10, 2017

That worked perfectly! Thanks a ton :D (I'm new to python actually)

@HaoZeke HaoZeke changed the title Wrapping Link with Div Wrapping Links with Divs Nov 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants