Comments (6)
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.
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.
That worked perfectly! Thanks a ton :D (I'm new to python actually)
from panflute.
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.
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.
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)
- Nav and other HTML5 elements missing
- How to skip a table? HOT 2
- Creating a Table without a Caption produces error HOT 1
- Creating a Panflute filter that interacts with other filters HOT 1
- Reimplement the panflute.stringify with native pandoc writer HOT 2
- Unable to get Panflute filter comments.py to work correctly HOT 1
- Add comparison method to Element class? HOT 2
- Compatibility with Pandoc 2.18 HOT 3
- Using panflute as a writer rather than a filter HOT 8
- Stringifying the terms of a definition list results in AttributeError HOT 7
- Hwo to use the span method? HOT 1
- Applying panflute filter multiple times generates an unexpected result
- Can't convert markdown style citations to LaTeX style using panflute filter HOT 5
- Navigation with next and prev unstable HOT 3
- Add space before footnote in stringify function HOT 4
- Is there solution to add custom style for lists with panflute ? HOT 2
- Avoid side-effects in walk
- Figure, Table: to_json() crashes if caption is None HOT 1
- Reading the value of `--resource-path` inside a panflute filter ? HOT 3
- Trying to create new metadata from metadata
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from panflute.