Skip to content

Commit

Permalink
Improve config options and Readme
Browse files Browse the repository at this point in the history
- Add output_path option
- Add 'model' prefix to model options
- Generalice loading of options, and add defaults
  • Loading branch information
rlaverde committed Mar 10, 2017
1 parent 3dceee9 commit 0bc319b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
53 changes: 48 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,59 @@ git clone [email protected]:rlaverde/lektor-tipue-search.git

## Configurations

You should add an entry for any model taht you want to be generated into de json file
There are some globals configurations:

`configs/tipue-search.ini:`

[blog-post]
title = title
text = summary
tags = tags
output_directory = tipue_search


Also you should add an entry for any model that you want to be generated into de json file (it should start by `model`)

`configs/tipue-search.ini:`

```ini
[model.blog-post]
title = title
text = summary
tags = tags
```

The first part is the json key and the sepcond the model key, i.e the previous configuration correspod to a model:

`models/blog-post.ini:`

```ini
[model]
name = Blog Post

[fields.title]
label = Title
type = string

[fields.summary]
label = Summary
type = string

fields.tags]
label = Tags
type = checkboxes
choices = some_tag, another_tag
```

and will generate a json file (for each alternative):

`tipue_search/tipue_search_en.ini:`

```json
[{"url": "/blog/example",
"text": "This is the blog Summary",
"title": "Blog Example",
"tags": ["example", "some_tag"]},

]
```

## Usage

```bash
Expand Down
27 changes: 20 additions & 7 deletions lektor_tipue_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def __init__(self, *args, **kwargs):
Plugin.__init__(self, *args, **kwargs)
self.tipue_search = defaultdict(list)
self.enabled = False
self.models = None
self.options = {'output_path': 'tipue-search', }

def check_enabled(func):
def func_wrapper(self, *args, **kwargs):
Expand All @@ -34,17 +36,27 @@ def on_setup_env(self, **extra):
self.models = defaultdict(dict)

for key, item in self.get_config().items():
model, field = key.split('.')
self.models[model][field] = item
config_option = key.split('.')

# Load models configurations
if config_option[0] == 'model':
model, field = config_option[1:]
self.models[model][field] = item

for option in self.options.keys():
value = self.get_config().get(option)

if value is not None:
self.options[option] = value

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

self.output_path = 'tipue_search'

if not os.path.exists(self.output_path):
os.makedirs(self.output_path)
output_path = os.path.join(builder.env.root_path,
self.options['output_path'])
if not os.path.exists(output_path):
os.makedirs(output_path)

@check_enabled
def on_before_build(self, source, prog, **extra):
Expand All @@ -61,7 +73,8 @@ def on_before_build(self, source, prog, **extra):
@check_enabled
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,
filename = os.path.join(builder.env.root_path,
self.options['output_path'],
'tipue_search_{}.json'.format(alt))

try:
Expand Down

0 comments on commit 0bc319b

Please sign in to comment.