new-horizons/docs/lib/Content/AbstractTemplatedItem.py
Ben C 5f4980e49e
Added Table Of Contents To Docs (#64)
* Add Bootstrap Extension

* Rename main.yml

* Artifact Upload

* Fix Bootstrap Reference Error

* BootstrapTreeProcessor

* getiterator removed

* keys function

* Style Images

* Update docs_build.yml

* Added Meta Files

* Template Get

* Fix Page Ref

* Update BASE_URL

* Sort Schemas

* Add Sitemaps

* Add favicons, open-graph, and setup guide

* Update Setup.md

* Update .gitignore

* Update Setup.md

* Use _blank on external links

* Restructured Docs

* Fix Links

* Added XML Schemas

* Name XML Schemas

* Improved Best Practices

* Add Logs For Static Files

* Make docs build happen on PR

* Responsive Footer

* Started On Table OF Contents

* Added Table Of Contents

* Fix <code> bg colors

* Remove unused CSS

* Add canonical link

* Fix OGP Image

* Try ScrollSpy on TOC

* Added ScrollSpy

* Improve TOC Spacing

* Custom Scroll Bars

* Fix Formatting in ship_log.md
2022-03-08 15:08:56 -08:00

52 lines
1.7 KiB
Python

from abc import ABC
from itertools import chain
from pathlib import Path
from jinja2 import Template, Environment
from lib.Content.AbstractItem import AbstractItem, MinifyMixin
class AbstractTemplatedItem(MinifyMixin, AbstractItem, ABC):
extensions: tuple[str]
title: str = None
description: str | None = None
sort_priority: int = None
render_toc: bool = False
table_of_contents: dict = {}
def load_metadata(self):
if self.title is None:
self.title = self.in_path.stem
if self.description is None:
self.description = None
if self.sort_priority is None:
self.sort_priority = 10
@classmethod
def initialize(cls, env: Environment):
pages = []
file_paths = list(chain(*[Path('content/', cls.root_dir).glob(f'**/*.{ext}') for ext in cls.extensions]))
for path in file_paths:
new_page = cls(path)
new_page.env = env
new_page.load_metadata()
pages.append(new_page)
return pages
def inner_render(self, template: Template, **context):
return template.render(**context)
def render(self, **context):
container = self.env.get_template('base/page_template.jinja2')
template = self.env.get_template(str(self.in_path.relative_to(Path('content/')).as_posix()))
context.update({
'page': self,
'rendered': self.inner_render(template, **context),
'render_toc': self.render_toc
})
return container.render(**context)
def add_route(self, router, out_dir):
router[self.title.lower()] = out_dir + str(self.out_path.relative_to('out/'))