configuring site_tools
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import click
|
||||
import sys
|
||||
import typer
|
||||
|
||||
from enum import Enum
|
||||
from site_tools.tasks import CONFIG
|
||||
from site_tools.content_manager import create
|
||||
|
||||
CONFIG.update(
|
||||
templates_path='markdown-templates',
|
||||
)
|
||||
|
||||
app = typer.Typer()
|
||||
|
||||
|
||||
class ContentType(str, Enum):
|
||||
post = 'post'
|
||||
lore = 'lore'
|
||||
monster = 'monster'
|
||||
region = 'region'
|
||||
page = 'page'
|
||||
|
||||
|
||||
@app.command()
|
||||
def new(
|
||||
content_type: ContentType = typer.Argument(
|
||||
...,
|
||||
help="The type of content to create.",
|
||||
),
|
||||
title: str = typer.Argument(
|
||||
...,
|
||||
help="The title of the content.",
|
||||
),
|
||||
category: str = typer.Argument(
|
||||
None,
|
||||
help='Override the default category; required for "post" content.',
|
||||
),
|
||||
template: str = typer.Argument(
|
||||
None,
|
||||
help="Override the default template for the content_type.",
|
||||
),
|
||||
template_dir: str = typer.Argument(
|
||||
CONFIG['templates_path'],
|
||||
help="Override the default location for markdown templates.",
|
||||
)
|
||||
) -> None:
|
||||
if not category:
|
||||
match content_type:
|
||||
case 'post':
|
||||
print("You must specify a category for 'post' content.")
|
||||
sys.exit()
|
||||
case 'monster':
|
||||
category = 'beastiary'
|
||||
case 'region':
|
||||
category = 'regions'
|
||||
case _:
|
||||
category = content_type
|
||||
click.edit(filename=create(content_type, title, template_dir, category,
|
||||
template or content_type))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app()
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
# dm/some-post.md
|
||||
|
||||
title: some post
|
||||
author: evilchili
|
||||
tags: ContentType.post
|
||||
date: 2022-08-02 17:59:59.899095
|
||||
status: draft
|
||||
---
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
# regions/region-name.md
|
||||
|
||||
title: region name
|
||||
author: evilchili
|
||||
tags: ContentType.region
|
||||
date: 2022-08-02 17:57:41.572598
|
||||
category: regions
|
||||
template: region
|
||||
region:
|
||||
name: region name
|
||||
size: 10
|
||||
terrain: difficult
|
||||
shelter: none
|
||||
water_source: none
|
||||
travel:
|
||||
dc: 10
|
||||
critical_success:
|
||||
critical_fail:
|
||||
forage:
|
||||
dc: 10
|
||||
critical_success:
|
||||
critical_fail:
|
||||
track:
|
||||
dc: 10
|
||||
critical_success:
|
||||
critical_fail:
|
||||
evade:
|
||||
dc: 10
|
||||
critical_success:
|
||||
critical_fail:
|
||||
survey:
|
||||
dc: 10
|
||||
critical_success:
|
||||
critical_fail:
|
||||
encounter_chance: 5
|
||||
special:
|
||||
weather:
|
||||
d1:
|
||||
d2:
|
||||
d3:
|
||||
d4:
|
||||
d5:
|
||||
d6:
|
||||
d7:
|
||||
d8:
|
||||
status: draft
|
||||
---
|
||||
|
||||
Region description goes here.
|
||||
@@ -0,0 +1,44 @@
|
||||
import datetime
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from pathlib import Path
|
||||
from pelican.writers import Writer
|
||||
from pelican.utils import slugify, sanitised_join
|
||||
from site_tools.tasks import SETTINGS
|
||||
|
||||
|
||||
def create(content_type: str, title: str, template_dir: str,
|
||||
category: str = None, template: str = None) -> str:
|
||||
"""
|
||||
Return the path to a new source file.
|
||||
"""
|
||||
base_path = Path(__file__).parent.absolute()
|
||||
|
||||
def _slugify(s):
|
||||
return slugify(s, regex_subs=SETTINGS['SLUG_REGEX_SUBSTITUTIONS'])
|
||||
|
||||
template_path = Path(template_dir)
|
||||
template_name = template + ".md"
|
||||
if not (template_path / template_name).exists():
|
||||
template_name = 'default.md'
|
||||
print("Not found. Using default markdown template.")
|
||||
template_source = Environment(
|
||||
loader=FileSystemLoader(template_path),
|
||||
trim_blocks=True,
|
||||
).get_template(template_name)
|
||||
|
||||
target_filename = _slugify(title) + '.md'
|
||||
|
||||
relpath = Path(slugify(category)) if category else ''
|
||||
|
||||
target_path = base_path / SETTINGS['PATH'] / relpath
|
||||
dest = sanitised_join(str(target_path / target_filename))
|
||||
|
||||
SETTINGS['WRITE_SELECTED'].append(dest)
|
||||
writer = Writer(target_path, settings=SETTINGS)
|
||||
writer.write_file(name=target_filename, template=template_source, context={
|
||||
'title': title,
|
||||
'tags': content_type,
|
||||
'date': datetime.datetime.now(),
|
||||
'filename': str(relpath / target_filename)
|
||||
})
|
||||
return dest
|
||||
@@ -1,19 +1,12 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import inspect
|
||||
import shlex
|
||||
import shutil
|
||||
import datetime
|
||||
|
||||
from pathlib import Path
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
||||
from invoke import task
|
||||
from invoke.main import program
|
||||
from pelican import main as pelican_main
|
||||
from pelican.writers import Writer
|
||||
from pelican.utils import slugify, sanitised_join
|
||||
from pelican.settings import DEFAULT_CONFIG, get_settings_from_file
|
||||
|
||||
OPEN_BROWSER_ON_SERVE = True
|
||||
@@ -135,71 +128,6 @@ def publish(c):
|
||||
**CONFIG))
|
||||
|
||||
|
||||
@task
|
||||
def monster(c, name, t='default'):
|
||||
"""Create a new monster"""
|
||||
return create_new_content(c, name, template=t, category='beastiary')
|
||||
|
||||
|
||||
@task
|
||||
def lore(c, title, t='default'):
|
||||
"""Create a new lore post"""
|
||||
return create_new_content(c, title, template=t, category='lore')
|
||||
|
||||
|
||||
@task
|
||||
def post(c, category, title, t=None, template_dir='markdown-templates'):
|
||||
"""Create a new post in the specified category"""
|
||||
return create_new_content(c, title, template=t or category, template_dir=template_dir, category=category)
|
||||
|
||||
|
||||
@task
|
||||
def page(c, title, t='default', template_dir='markdown-templates'):
|
||||
"""Create a new page"""
|
||||
return create_new_content(c, title, template=t, template_dir=template_dir, output_path='pages')
|
||||
|
||||
|
||||
def create_new_content(c, title, template_dir='markdown-templates', category="", template=None, output_path=''):
|
||||
"""Create new content of a given type and invoke the editor."""
|
||||
|
||||
content_type = template or inspect.stack()[1].function
|
||||
print(f"Creating new '{content_type}' content: \"{category}/{title}\"")
|
||||
|
||||
base_path = Path(__file__).parent.absolute()
|
||||
|
||||
def _slugify(s):
|
||||
return slugify(s, regex_subs=DEFAULT_CONFIG['SLUG_REGEX_SUBSTITUTIONS'])
|
||||
|
||||
template_path = Path(template_dir)
|
||||
template_name = _slugify(content_type) + ".md"
|
||||
if not (template_path / template_name).exists():
|
||||
template_name = 'default.md'
|
||||
print("Using default markdown template.")
|
||||
|
||||
target_filename = _slugify(title) + '.md'
|
||||
target_path = base_path / SETTINGS['PATH']
|
||||
if output_path:
|
||||
target_path = target_path / output_path
|
||||
if category:
|
||||
target_path = target_path / slugify(category)
|
||||
|
||||
template = Environment(
|
||||
loader=FileSystemLoader(template_path),
|
||||
trim_blocks=True,
|
||||
).get_template(template_name)
|
||||
|
||||
dest = sanitised_join(str(target_path / target_filename))
|
||||
SETTINGS['WRITE_SELECTED'].append(dest)
|
||||
writer = Writer(target_path, settings=SETTINGS)
|
||||
writer.write_file(name=target_filename, template=template, context={
|
||||
'title': title,
|
||||
'tags': content_type,
|
||||
'date': datetime.datetime.now(),
|
||||
'filename': dest
|
||||
})
|
||||
c.run(f"$EDITOR {dest}", pty=True)
|
||||
|
||||
|
||||
def pelican_run(cmd):
|
||||
cmd += ' ' + program.core.remainder # allows to pass-through args to pelican
|
||||
pelican_main(shlex.split(cmd))
|
||||
|
||||
Reference in New Issue
Block a user