abakan-zz / ablog

ABlog for blogging with Sphinx
ablog.readthedocs.org
Other
122 stars 35 forks source link

`postlist` should render a template so it can be overridden by the user #67

Closed lakhman closed 2 years ago

lakhman commented 8 years ago

Hey, I'm using this for my blog, it's awesome, I even managed to write a plugin which inserts a "bar" under the first header of each section with the date, tags and author.

I wanted to further style the postlist directive, I noticed everything is generated via nodes, could we perhaps replace the node generation with rendering a jinja template?

Like all other templates, this one should be able to be overridden by the user, we'd use this for the homepage most probably, so it would be nice to be able to style it as the user wishes.

https://github.com/abakan/ablog/blob/devel/ablog/post.py#L370

This is a snippet of my plugin, inside of creating new nodes, just render a template.

Could this be done?

with open(fName, 'rb') as f:
    try:
        # Render our template with our context
        template = Template(f.read())
        context = {
            'post': post,
            'ablog': blogObject,
            'post_date_format': blogObject.__getattr__('post_date_format')
        }

        # Add our pathto extension for jinja rendering
        htmlBuilder = StandaloneHTMLBuilder(app)
        def pathto(otheruri, resource=False, baseuri=htmlBuilder.get_target_uri(post.docname)):
            if resource and '://' in otheruri:
                return otheruri
            elif not resource:
                otheruri = htmlBuilder.get_target_uri(otheruri)
                return relative_uri(baseuri, otheruri) or '#'
            else:
                return '/' + posixpath.join(app.virtual_staticdir, otheruri)

        context['pathto'] = pathto
        context['hasdoc'] = lambda name: name in app.env.all_docs
        context['toctree'] = lambda **kw: app._get_local_toctree(post.docname, **kw)

        new_content = template.render(context)

        # Causes error, research what this is used for
        # html = publish_string(source=new_content, writer_name='html')
        markup = jinja2.Markup(new_content)

        attributes = {'format': 'html'}
        node = nodes.raw('', markup, **attributes)

        # exit, we have our template and node
        return node
    except Exception as e:
        msgList = (Fore.RED, sys.exc_info()[0], e.message, post_bar_html_template, Style.RESET_ALL)
        app.warn("%sUnexpected error: %s, %s in %s %s" % msgList)
        return

Thanks! Awesome project! - I love sphinx! I learned a lot from this extension. Thanks! 👍

lakhman commented 8 years ago

or, could we just have it so we can render html in the :format:.

    :format: {title} by {author} on {date} {tags}
    :format: <span class="title">{title}</span> by {author} on {date} {tags}
AlbertMietus commented 8 years ago

I would prefer the template proposal. Keep the data & format/style separate.

--albert An iPhone is great, but for typing mail

Op 24 mei 2016 om 20:46 heeft lakhman notifications@github.com het volgende geschreven:

or, could we just have it so we can render html in the :format:.

:format: {title} by {author} on {date} {tags}
:format: <span class="title">{title}</span> by {author} on {date} {tags}

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub

ericholscher commented 8 years ago

+1 on this. Just ran into this very issue. @lakhman is the work you did to make this into an external plugin go anywhere? Seems like it shouldn't be hard to hook into the doctree-resolved event and update/add the node outside of ablog if needed.

lakhman commented 8 years ago

@ericholscher No, I'm sorry to say I abandoned this library in favour of tinkerer. @Maintainer please close this issue if you feel it's irrelevant.

http://tinkerer.me/

I still have a few issues with tinkerer and am planning to write my own blogging engine merging the good parts of ablog and tinkerer. It'll probably be a few months still I get around to this tho.

Ablog and Tinkerer are very hackable (they're based of Sphinx), you should be able to customize it (with a little learning) very easily to do whatever you want.