-
Notifications
You must be signed in to change notification settings - Fork 59
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
Comments
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)) |
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? |
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. |
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'> Any ideas? |
BTW you don't need the Also, the 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. |
That worked perfectly! Thanks a ton :D (I'm new to python actually) |
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?
The text was updated successfully, but these errors were encountered: