Skip to content

Instantly share code, notes, and snippets.

@betaveros
Created September 3, 2020 06:56
Show Gist options
  • Save betaveros/fcf9e54706acc241eb16604debe4901f to your computer and use it in GitHub Desktop.
Save betaveros/fcf9e54706acc241eb16604debe4901f to your computer and use it in GitHub Desktop.
Lektor plugin to work with extensionless URLs. Very hacky! Use at own risk.
# -*- coding: utf-8 -*-
import jinja2
from lektor.pluginsystem import Plugin
from lektor.admin.webui import LektorInfo
from lektor.context import url_to
class ExtensionlessPlugin(Plugin):
name = 'extensionless'
description = u'Monkey patch the Jinja environment to strip .html from URLs and the server to add .html to extensionless URLs'
def on_setup_env(self, **extra):
def extensionless_url_filter(ctx, *a, **kw):
url = url_to(*a, **kw)
if url.endswith('.html'): return url[:-5]
return url
self.env.jinja_env.filters['url'] = jinja2.contextfilter(extensionless_url_filter)
def on_server_spawn(self, **extra):
old_resolve_artifact = LektorInfo.resolve_artifact
def resolve_artifact(self, path, pad=None, redirect_slash=True):
last_token = path.split('/')[-1]
if last_token and '.' not in last_token: path += '.html'
return old_resolve_artifact(self, path, pad, redirect_slash)
LektorInfo.resolve_artifact = resolve_artifact
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment