getpelican / pelican

Static site generator that supports Markdown and reST syntax. Powered by Python.
https://getpelican.com
GNU Affero General Public License v3.0
12.47k stars 1.81k forks source link

Get pelican data like categories, tags via command-line #2540

Closed tuvokki closed 5 years ago

tuvokki commented 5 years ago

Hi, like in issue #1410 I have been working on something similar, based on PyInquirer to gather data for article generation. For this I would like to have a list of categories and tags so I can present the user with a nice list of options that are already in the content. Is there a way to ask pelican for this data? Something like:

$ pelican --gettags
> ['tag1','tag2','tag3',....etc]

Which I can use to populate a list of options (see: list.py example)

Btw. it can be a use-case for #2539 When using tasks.py the pelican-api could be used to do this:

    @task
    def new_article(c):
        tags = pelican.get_tags()
        categories = pelican.get_categories()

        questions = [
            {
                'type': 'input',
                'name': 'title',
                'message': 'What\'s the title?',
            },
            {
                'type': 'list',
                'name': 'category',
                'message': 'What category is the article for?',
                'choices': categories,
            },
            {
                'type': 'checkbox',
                'qmark': '😃',
                'message': 'Select tags',
                'name': 'tags',
                'choices': tags,
            }
        ]
        answers = prompt(questions)
        _generate_new_article(answers)
avaris commented 5 years ago

It is certainly doable, but pelican wasn't designed to be used as API. So, you'd need a controller script doing the hard work. I can give some pointers though.

All you need is pretty much an ArticlesGenerator doing generate_context with the right settings. That should give you tags, categories etc.... It's probably obvious that this requires reading whole content, so doing this every time you run might not be feasible. You might want to read once and cache values in a JSON or such. And possibly refresh based on a command occasionally.

tuvokki commented 5 years ago

With help of your comment and some debugging I came up with a result, based on the loc_context which is a trial-and-error bogus variable since it does not seem to be used in my use-case. It needs some polishing. 😁

This snippet seems to produce the desired results:

from pelican import ArticlesGenerator, settings

loc_context = {
    'generated_content': {'location': ''},
    'static_links': set(),
    'NEWEST_FIRST_ARCHIVES': True
}

@task
def yolo(c):
    loc_settings = settings.read_settings(path=os.path.join('.', 'pelicanconf.py'))
    gen = ArticlesGenerator(loc_context, loc_settings, 'content', 'themes', 'output')
    gen.generate_context()
    print(gen.tags.keys())
justinmayer commented 5 years ago

@tuvokki: Glad to hear you found a solution. ✨