-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove dist directory and add .travis.yml
- Loading branch information
1 parent
05f1e73
commit a917953
Showing
6 changed files
with
602 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: python | ||
python: | ||
- "3.6" | ||
- "3.7" | ||
|
||
script: pytest tets/unit-tests/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from pathlib import Path | ||
import yaml | ||
|
||
def created_message(path): | ||
return f'{path} created!' | ||
|
||
def skip_message(path): | ||
return f'{path} exists! Skipping...' | ||
|
||
def main( | ||
template_path='templates', | ||
content_path='content', | ||
config_path='config.yaml', | ||
**kwargs, | ||
): | ||
template_path = Path(template_path) | ||
content_path = Path(content_path) | ||
config_path = Path(config_path) | ||
|
||
if not config_path.exists(): | ||
config_path.write_text( | ||
yaml.dump({'engine_variables': { | ||
'content_path': content_path, | ||
'template_path': template_path, | ||
}}) | ||
) | ||
print(created_message(config_path)) | ||
|
||
else: | ||
print(skip_message(config_path)) | ||
|
||
for path in (template_path, content_path): | ||
if not path.is_dir(): | ||
Path(path).mkdir(exist_ok=True, **kwargs) | ||
print(created_message(path)) | ||
|
||
else: | ||
print(skip_message(str(path))) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
from typing import Type, Union, Sequence | ||
from collections import defaultdict | ||
from itertools import zip_longest | ||
from render_engine.page import Page | ||
from pathlib import Path | ||
import json | ||
import maya | ||
|
||
|
||
PathString = Union[str, Type[Path]] | ||
|
||
class Collection: | ||
"""Create a Collection of Similar Page Objects""" | ||
def __init__( | ||
self, | ||
*, | ||
paginate: bool=False, | ||
name: str, | ||
content_path: PathString='', | ||
route: Union[PathString, Sequence[PathString]]='./', | ||
url_root: str='./', | ||
url_suffix: str='.html', | ||
extension: str='.md', | ||
template: str='page.html', | ||
pages: Sequence[PathString]=None, | ||
content_type=Page, | ||
**kwargs, | ||
): | ||
""" | ||
___________ | ||
- name is used to create a slug object | ||
- content_type the type of pages that you are bundling (Currently all | ||
collections have to be of the same type) | ||
- extension tells collection what types of documents to look at, | ||
usually (HTML or Markdown files) | ||
TODO: Add ignore param that looks are all files that don't contain the | ||
ignored type - (e.g. Collection(ignored=".tmp")) | ||
""" | ||
self.name = name | ||
self.extension = extension | ||
if content_path: | ||
self.content_path = content_path | ||
self.route = Path(route) | ||
self.url_root = url_root | ||
|
||
# Build the URL so that it can be used as reference | ||
if 'url' in kwargs: | ||
self.url = url | ||
|
||
else: | ||
url_stem = str(route) | ||
url_suffix = url_suffix | ||
self.url = f'{url_root}/{url_stem}{url_suffix}' | ||
|
||
# make properties for all attrs | ||
for key, attr in kwargs.items(): | ||
setattr(self, key, attr) | ||
|
||
if not pages: | ||
page_glob = list(Path(self.content_path).glob(f'*{self.extension}')) | ||
pages = [content_type( | ||
route=Path(route).joinpath(Path(content_path.name)), | ||
content_path=content_path, | ||
url_root=self.url_root, | ||
template=template, | ||
) for content_path in page_glob ] | ||
self.pages = sorted( | ||
pages, | ||
key=self.sorter, | ||
reverse=True, | ||
) | ||
|
||
def __iter__(self): | ||
return iter(self.pages) | ||
|
||
@staticmethod | ||
def sorter(page): | ||
if getattr(page, 'date_published', None): | ||
return page.date_published | ||
|
||
if getattr(page, 'date_modified', None): | ||
return page.date_modified | ||
|
||
if getattr(page, 'title', None): | ||
return page.title | ||
|
||
if getattr(page, 'name', None): | ||
return page.name | ||
|
||
if getattr(page, 'slug', None): | ||
return page.slug | ||
|
||
@property | ||
def paginate(self): | ||
"Collect data into fixed-length chunks or blocks" | ||
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx" | ||
args = [iter(self.pages)] * 10 | ||
iterable = zip_longest(*args, fillvalue=None) | ||
return iterable | ||
|
||
@property | ||
def categories(self): | ||
d = defaultdict(list) | ||
for p in self.pages: | ||
d[p._category].append(p) | ||
return d | ||
|
||
def to_json(self, engine, pages=None, next_url=None): | ||
"""Gets/Sets Data for dictionary feed metadata""" | ||
title = engine.FEED_TITLE | ||
description = engine.FEED_DESCRIPTION | ||
home_page_url = engine.SITE_URL | ||
feed_url = f'{self.route}{self.name}.json' | ||
version = 'https://jsonfeed.org.version/1' | ||
icon = getattr(engine, 'FEED_ICON','') | ||
user_comment = getattr(engine, 'user_comment', '') | ||
next_url = next_url # needs pagination | ||
favicon = getattr(engine, 'SITE_FAVICON', '') | ||
author = getattr(engine, 'AUTHOR', '') | ||
expired = getattr('self', 'expired', '') | ||
hubs = getattr(engine, 'JSON_FEED_HUB', '') | ||
|
||
feed_data = { | ||
'title': title, | ||
'home_page_url': home_page_url, | ||
'feed_url': feed_url, | ||
'version': version, | ||
'icon': icon, | ||
'description': description, | ||
'user_comment': user_comment, | ||
'next_url': next_url, | ||
'favicon': favicon, | ||
'author': author, | ||
'expired': expired, | ||
'hubs': hubs, | ||
'items': [], | ||
} | ||
|
||
# if not pages: | ||
# pages = list(map(lambda x:x.to_json(), self.pages)) | ||
# | ||
# feed_data['items'].extend(pages) | ||
# print(feed_data) | ||
return json.dumps(feed_data) | ||
|
||
|
||
def to_rss(self, engine, pages=None, html=True, full_text=True): | ||
"""Applies feed Metadata into a RSS file. | ||
TODO: Move data to jinja2 Template | ||
""" | ||
channel = json.loads(self.to_json(engine=engine)) | ||
channel['items'] = list( | ||
map( | ||
lambda item: item.to_rss( | ||
html=html, | ||
full_text=full_text, | ||
), | ||
self.pages), | ||
) | ||
template = engine.env.get_template('feeds/rss/blog.rss') | ||
template.render(channel=channel) | ||
|
Oops, something went wrong.