Skip to content

Commit

Permalink
First version of the plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaverde committed Mar 9, 2017
1 parent 90c80c1 commit b13349c
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions lektor_tipue_search.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
# -*- coding: utf-8 -*-
import os
import json
from collections import defaultdict

from lektor.pluginsystem import Plugin
from lektor.db import Page


class TipueSearchPlugin(Plugin):
name = u'Lektor Tipue Search'
description = u'Add your description here.'
description = u'Serialize models contents for using tipue search.'

def on_setup_env(self, **extra):
#TODO load configurations

self.tipue_search = defaultdict(list)

self.output_path = 'tipue_search'

if not os.path.exists(self.output_path):
os.makedirs(self.output_path)

def on_before_build_all(self, builder, **extra):
self.tipue_search.clear()

def on_before_build(self, source, prog, **extra):
if isinstance(source, Page):
#FIXME at the moment only support blog-post
if source.datamodel.id == 'blog-post':
item = {'title': source['title'],
'text': source['summary'],
'tags': source['tags'], }

self.tipue_search[source.alt].append(item)

def on_after_build_all(self, builder, **extra):
for alt, pages in self.tipue_search.items():
filename = os.path.join(builder.env.root_path, self.output_path,
'tipue_search_{}.json'.format(alt))

with open(filename, 'r') as f:
contents = f.read()

def on_process_template_context(self, context, **extra):
def test_function():
return 'Value from plugin %s' % self.name
context['test_function'] = test_function
if contents != json.dumps(pages):
with open(filename, 'w') as f:
f.write(json.dumps(pages))

0 comments on commit b13349c

Please sign in to comment.