configurable markdown extensions
meta is always loaded, because the code expects it
This commit is contained in:
parent
fe7d61e1f7
commit
5ca483a904
@ -3,6 +3,7 @@ import logging
|
|||||||
import os
|
import os
|
||||||
from logging.config import dictConfig
|
from logging.config import dictConfig
|
||||||
|
|
||||||
|
import markdown
|
||||||
from flask import Flask, request, send_from_directory
|
from flask import Flask, request, send_from_directory
|
||||||
|
|
||||||
from ._version import get_versions
|
from ._version import get_versions
|
||||||
@ -31,6 +32,10 @@ def create_app(instance_path=None, test_config=None):
|
|||||||
|
|
||||||
logger.debug("instance path: %s", app.instance_path)
|
logger.debug("instance path: %s", app.instance_path)
|
||||||
|
|
||||||
|
# initialize markdown parser from config, but include
|
||||||
|
# extensions our app depends on, like the meta extension
|
||||||
|
app.config['md'] = markdown.Markdown(extensions=app.config['MARKDOWN_EXTENSIONS'] + ['meta'])
|
||||||
|
|
||||||
@app.before_request
|
@app.before_request
|
||||||
def log_request():
|
def log_request():
|
||||||
logger.info("REQUEST: %s %s", request.method, request.path)
|
logger.info("REQUEST: %s %s", request.method, request.path)
|
||||||
|
@ -32,5 +32,7 @@ class Config(object):
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MARKDOWN_EXTENSIONS = ['meta', 'tables']
|
||||||
|
|
||||||
TITLE_SUFFIX = 'incorporeal.org'
|
TITLE_SUFFIX = 'incorporeal.org'
|
||||||
MEDIA_DIR = 'media'
|
MEDIA_DIR = 'media'
|
||||||
|
@ -3,7 +3,6 @@ import datetime
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import markdown
|
|
||||||
from flask import Blueprint, Markup, abort
|
from flask import Blueprint, Markup, abort
|
||||||
from flask import current_app as app
|
from flask import current_app as app
|
||||||
from flask import redirect, render_template
|
from flask import redirect, render_template
|
||||||
@ -12,7 +11,6 @@ from tzlocal import get_localzone
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
bp = Blueprint('pages', __name__, url_prefix='/')
|
bp = Blueprint('pages', __name__, url_prefix='/')
|
||||||
md = markdown.Markdown(extensions=['meta', 'tables'])
|
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/', defaults={'path': 'index'})
|
@bp.route('/', defaults={'path': 'index'})
|
||||||
@ -34,9 +32,9 @@ def display_page(path):
|
|||||||
logger.warning("requested path '%s' (resolved path '%s') not found!", path, resolved_path)
|
logger.warning("requested path '%s' (resolved path '%s') not found!", path, resolved_path)
|
||||||
abort(404)
|
abort(404)
|
||||||
else:
|
else:
|
||||||
content = Markup(md.convert(entry))
|
content = Markup(app.config['md'].convert(entry))
|
||||||
logger.debug("file metadata: %s", md.Meta)
|
logger.debug("file metadata: %s", app.config['md'].Meta)
|
||||||
title = " ".join(md.Meta.get('title')) if md.Meta.get('title') else ""
|
title = " ".join(app.config['md'].Meta.get('title')) if app.config['md'].Meta.get('title') else ""
|
||||||
return render_template('base.html', title=title, content=content, navs=parent_navs,
|
return render_template('base.html', title=title, content=content, navs=parent_navs,
|
||||||
mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z'))
|
mtime=mtime.strftime('%Y-%m-%d %H:%M:%S %Z'))
|
||||||
|
|
||||||
@ -86,6 +84,6 @@ def generate_parent_navs(path):
|
|||||||
else:
|
else:
|
||||||
with app.open_instance_resource(resolved_path, 'r') as entry_file:
|
with app.open_instance_resource(resolved_path, 'r') as entry_file:
|
||||||
entry = entry_file.read()
|
entry = entry_file.read()
|
||||||
_ = Markup(md.convert(entry))
|
_ = Markup(app.config['md'].convert(entry))
|
||||||
page_name = " ".join(md.Meta.get('title')) if md.Meta.get('title') else f'/{path}'
|
page_name = " ".join(app.config['md'].Meta.get('title')) if app.config['md'].Meta.get('title') else f'/{path}'
|
||||||
return generate_parent_navs(parent_path) + [(page_name, f'/{path}')]
|
return generate_parent_navs(parent_path) + [(page_name, f'/{path}')]
|
||||||
|
@ -13,6 +13,32 @@ def test_config():
|
|||||||
assert create_app(instance_path=instance_path, test_config={"TESTING": True}).testing
|
assert create_app(instance_path=instance_path, test_config={"TESTING": True}).testing
|
||||||
|
|
||||||
|
|
||||||
|
def test_markdown_meta_extension_always():
|
||||||
|
"""Test that the markdown meta extension is always loaded, even if not specified."""
|
||||||
|
app = create_app(instance_path=os.path.join(HERE, 'instance'),
|
||||||
|
test_config={'MARKDOWN_EXTENSIONS': []})
|
||||||
|
client = app.test_client()
|
||||||
|
response = client.get('/')
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b'<title>Index - incorporeal.org</title>' in response.data
|
||||||
|
|
||||||
|
|
||||||
|
def test_extra_markdown_extensions_work():
|
||||||
|
"""Test we can load more extensions via config, and that they work."""
|
||||||
|
app = create_app(instance_path=os.path.join(HERE, 'instance'))
|
||||||
|
client = app.test_client()
|
||||||
|
response = client.get('/mdash-or-triple-dash')
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b'word --- word' in response.data
|
||||||
|
|
||||||
|
app = create_app(instance_path=os.path.join(HERE, 'instance'),
|
||||||
|
test_config={'MARKDOWN_EXTENSIONS': ['smarty']})
|
||||||
|
client = app.test_client()
|
||||||
|
response = client.get('/mdash-or-triple-dash')
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b'word — word' in response.data
|
||||||
|
|
||||||
|
|
||||||
def test_title_override():
|
def test_title_override():
|
||||||
"""Test that a configuration with a specific title overrides the default."""
|
"""Test that a configuration with a specific title overrides the default."""
|
||||||
instance_path = os.path.join(HERE, 'instance')
|
instance_path = os.path.join(HERE, 'instance')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user