This repository has been archived on 2023-03-26. You can view files and clone it, but cannot push or open issues or pull requests.
md-pdf/md_pdf/build/context.py

46 lines
1.6 KiB
Python
Raw Permalink Normal View History

from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR, DATE_FORMAT, TIME_FORMAT, DATETIME_FORMAT, INTERNAL_TEMPLATES_DIR, INTERNAL_STATIC_DIR
2017-05-25 17:42:36 +01:00
from word_count import word_count
from md_pdf.utils import get_plain_text
2017-05-27 14:20:39 +01:00
from md_pdf import __version__
2017-05-25 21:33:18 +01:00
from dateutil import parser
import datetime
2017-05-27 14:12:31 +01:00
import os
EXTRA_CONTEXT = {
'templates_dir': TEMPLATES_DIR,
2017-05-25 20:36:23 +01:00
'static_dir': STATIC_DIR,
'internal_templates_dir': INTERNAL_TEMPLATES_DIR,
'internal_static_dir': INTERNAL_STATIC_DIR,
2017-05-25 21:33:18 +01:00
'date': datetime.datetime.now().strftime(DATE_FORMAT),
'time': datetime.datetime.now().strftime(TIME_FORMAT),
2017-05-27 14:20:39 +01:00
'datetime': datetime.datetime.now().strftime(DATETIME_FORMAT),
'mdp_version': __version__
}
2017-06-09 23:07:30 +01:00
def get_context(config: dict, content: str) -> dict:
2017-05-25 20:39:24 +01:00
config = config.copy()
2017-05-28 16:58:58 +01:00
if 'context' in config:
context = config['context'].copy()
del config['context']
else:
context = {}
2017-06-09 23:07:30 +01:00
merged_context = dict(
2017-05-25 20:39:24 +01:00
config,
**EXTRA_CONTEXT,
2017-05-27 14:35:28 +01:00
**context,
2017-05-25 20:39:24 +01:00
**{
2017-05-27 14:12:31 +01:00
'output_dir': os.path.abspath(config['output_dir']),
2017-05-25 20:39:24 +01:00
}
)
if config.get('show_word_count'):
2017-06-09 23:07:30 +01:00
merged_context['word_count'] = word_count(get_plain_text(content))
2017-05-25 21:33:18 +01:00
if config.get('submission_date'):
if type(config['submission_date']) in [datetime.date, datetime.datetime, datetime.time]:
submission_date = config['submission_date']
else:
submission_date = parser.parse(config['submission_date'])
2017-06-09 23:07:30 +01:00
merged_context['submission_date'] = submission_date.strftime(DATE_FORMAT)
return merged_context