Skip to content

Instantly share code, notes, and snippets.

@Kwpolska

Kwpolska/README.md

Created Feb 15, 2015
Embed
What would you like to do?
Fix index hack for Nikola

This hacky plugin adds a post list to a file in post-processing, useful when you want post text out of a post list.

Use with care.

Usage:

Here is my hacky post list:

.. listofpostsgoeshere
[Core]
Name = fix_index
Module = fix_index
[Documentation]
Author = Chris Warrick
Version = 1.0
Website = http://getnikola.com
Description = fix indexes
# -*- coding: utf-8 -*-
# Copyright © 2015 Chris Warrick and others.
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import unicode_literals
from nikola.plugin_categories import Task
from nikola.utils import config_changed
import io
import os
class FixIndex(Task):
"""Fix index in output."""
name = "fix_index"
def gen_tasks(self):
"""Build final pages from metadata and HTML fragments."""
kw = {
"post_pages": self.site.config["post_pages"],
"translations": self.site.config["TRANSLATIONS"],
"filters": self.site.config["FILTERS"],
"show_untranslated_posts": self.site.config['SHOW_UNTRANSLATED_POSTS'],
"demote_headers": self.site.config['DEMOTE_HEADERS'],
'output_folder': self.site.config['OUTPUT_FOLDER'],
'date_format': self.site.GLOBAL_CONTEXT.get('date_format'),
# SETTINGS #
'lang': self.site.config['DEFAULT_LANG'],
'max_posts': 5,
'template': 'index_sub_postlist.tmpl',
'inpath': 'index_partial.html',
'outpath': 'index.html',
}
self.site.scan_posts()
infile = os.path.join(kw['output_folder'], kw['inpath'])
outfile = os.path.join(kw['output_folder'], kw['outpath'])
file_dep = [infile]
ft = self.site.posts[:kw['max_posts']]
for post in ft:
file_dep += post.deps(kw['lang'])
yield {'basename': self.name,
'name': outfile,
'task_dep': ['render_pages'],
'file_dep': file_dep,
'targets': [outfile],
'actions': [(fix_things, (infile, outfile, self.site.template_system, ft, kw))],
'uptodate': [config_changed(kw, 'fix_index')]
}
def fix_things(infile, outfile, template_system, ft, kw):
with io.open(infile, 'r', encoding='utf-8') as fh:
data = fh.read()
template_data = {'posts': ft,
'lang': kw['lang'],
'date_format': kw['date_format'],
'post_list_id': 'fixindex_postlist',
}
output = template_system.render_template(kw['template'], None, template_data)
# in rest: .. listofpostsgoeshere
data = data.replace('<!-- listofpostsgoeshere -->', output)
with io.open(outfile, 'w', encoding='utf-8') as fh:
fh.write(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment