Git Product home page Git Product logo

Comments (6)

sergiocorreia avatar sergiocorreia commented on July 21, 2024 1

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))

from panflute.

sergiocorreia avatar sergiocorreia commented on July 21, 2024 1

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.

from panflute.

HaoZeke avatar HaoZeke commented on July 21, 2024 1

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

from panflute.

HaoZeke avatar HaoZeke commented on July 21, 2024

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?

from panflute.

sergiocorreia avatar sergiocorreia commented on July 21, 2024

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.

from panflute.

HaoZeke avatar HaoZeke commented on July 21, 2024

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?

from panflute.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.