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

How to write a filter that runs all filters that are written in code chunks inside markdown? #72

Closed
kiwi0fruit opened this issue Nov 5, 2017 · 5 comments

Comments

@kiwi0fruit
Copy link
Contributor

kiwi0fruit commented Nov 5, 2017

How to write a filter (meta-filter) that runs all filters that are written in code chunks inside markdown?

Some code chunk can have attributes like panflute-name=filter_x. And metadata can have:

panflute-filters: [meta-filter, filter_x, filter_y]
panflute-path: 'C:/meta-filter'

I know how to find all such chunks. Then that meta-filter writes code from each chunk to it's own file like C:/meta-filter/filter_x.py before it's main() function.

The question: would filter_x run in this scenario or reading from that file or checking if it's exist would occure before first meta-filter run?

@sergiocorreia
Copy link
Owner

Could you give me an example of how the markdown would look like? If you just want to run the filters already in the markdown file, I don't think you need to even save them as files. I would i) make a list of al the filters in the document, ii) and run eval() on them, while passing them the document.

@kiwi0fruit
Copy link
Contributor Author

kiwi0fruit commented Nov 6, 2017

Thank you for help.

Markdown code with a chunk would be like:

``` {panflute-name=filter_x}
# standard filter with main()
```

So you mean I should have a meta-filter like this (looks like this would work):

def prepare(doc):
    # add to the doc the temporary list
    # with found code chunks.
    # Now it's empty.

def action(elem, doc):
    # if the element is a code chunk
    # and has kwarg `panflute-name`
    # then add this code chunk to the
    # temp list.

def finalize(doc):
    # Loop over temp list. On each item:
        eval(item)
        main(doc)
    # Then delete temp list


def main(doc=None):
    return run_filter(action, prepare=prepare, finalize=finalize, doc=doc) 

@sergiocorreia
Copy link
Owner

I think that will work. I was a bit concerned about evaling functions with the same names (prepare, etc.) but since you are evaling within finalize, you could eval() it with a different namespace (if any issue arises)

@kiwi0fruit
Copy link
Contributor Author

kiwi0fruit commented Nov 6, 2017 via email

@kiwi0fruit
Copy link
Contributor Author

Now I think that it's a bad idea :-) Closing.

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