From 0fcd1584be34b04775f371e8088aab87138f27bf Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 28 May 2017 11:13:36 +0100 Subject: [PATCH 1/4] Move rendered templates out of project dir --- md_pdf/assets/templates/cover-template.html | 2 +- md_pdf/assets/templates/footer-template.html | 4 ++-- md_pdf/assets/templates/header-template.html | 4 ++-- md_pdf/build/context.py | 4 +++- md_pdf/build/css.py | 4 ++-- md_pdf/build/templates.py | 6 ++--- md_pdf/consts.py | 24 ++++++++++++++++---- md_pdf/csl.py | 4 ++-- 8 files changed, 35 insertions(+), 17 deletions(-) diff --git a/md_pdf/assets/templates/cover-template.html b/md_pdf/assets/templates/cover-template.html index cac7519..841d1fa 100644 --- a/md_pdf/assets/templates/cover-template.html +++ b/md_pdf/assets/templates/cover-template.html @@ -1,7 +1,7 @@ - +

{{ title }}

diff --git a/md_pdf/assets/templates/footer-template.html b/md_pdf/assets/templates/footer-template.html index 2ea9fde..731b9fb 100644 --- a/md_pdf/assets/templates/footer-template.html +++ b/md_pdf/assets/templates/footer-template.html @@ -1,7 +1,7 @@ - + @@ -17,6 +17,6 @@
- + diff --git a/md_pdf/assets/templates/header-template.html b/md_pdf/assets/templates/header-template.html index 579edb0..2c211f2 100644 --- a/md_pdf/assets/templates/header-template.html +++ b/md_pdf/assets/templates/header-template.html @@ -1,7 +1,7 @@ - + @@ -15,6 +15,6 @@
- + diff --git a/md_pdf/build/context.py b/md_pdf/build/context.py index 1d43af8..61506ff 100644 --- a/md_pdf/build/context.py +++ b/md_pdf/build/context.py @@ -1,4 +1,4 @@ -from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR, DATE_FORMAT, TIME_FORMAT, DATETIME_FORMAT +from md_pdf.consts import TEMPLATES_DIR, STATIC_DIR, DATE_FORMAT, TIME_FORMAT, DATETIME_FORMAT, INTERNAL_TEMPLATES_DIR, INTERNAL_STATIC_DIR from word_count import word_count from md_pdf.utils import get_plain_text from md_pdf import __version__ @@ -10,6 +10,8 @@ import os EXTRA_CONTEXT = { 'templates_dir': TEMPLATES_DIR, 'static_dir': STATIC_DIR, + 'internal_templates_dir': INTERNAL_TEMPLATES_DIR, + 'internal_static_dir': INTERNAL_STATIC_DIR, 'date': datetime.datetime.now().strftime(DATE_FORMAT), 'time': datetime.datetime.now().strftime(TIME_FORMAT), 'datetime': datetime.datetime.now().strftime(DATETIME_FORMAT), diff --git a/md_pdf/build/css.py b/md_pdf/build/css.py index 0843923..ccbb01a 100644 --- a/md_pdf/build/css.py +++ b/md_pdf/build/css.py @@ -1,10 +1,10 @@ from scss import Compiler -from md_pdf.consts import STATIC_DIR +from md_pdf.consts import INTERNAL_STATIC_DIR from md_pdf.build.pdf import STYLE_FILE import os -STYLE_SRC_FILE = os.path.join(STATIC_DIR, 'style.scss') +STYLE_SRC_FILE = os.path.join(INTERNAL_STATIC_DIR, 'style.scss') def render_css(): diff --git a/md_pdf/build/templates.py b/md_pdf/build/templates.py index e593f5c..0266672 100644 --- a/md_pdf/build/templates.py +++ b/md_pdf/build/templates.py @@ -1,5 +1,5 @@ from jinja2 import Template -from md_pdf.consts import TEMPLATES_DIR +from md_pdf.consts import TEMPLATES_DIR, INTERNAL_TEMPLATES_DIR from md_pdf.build.context import get_context import os import logging @@ -8,7 +8,7 @@ logger = logging.getLogger(__file__) FILE_NAME_FORMAT = os.path.join(TEMPLATES_DIR, "{}.html") -TEMPLATE_FORMAT = os.path.join(TEMPLATES_DIR, "{}-template.html") +TEMPLATE_FORMAT = os.path.join(INTERNAL_TEMPLATES_DIR, "{}-template.html") def render_page(input_file, output_file, context): @@ -30,4 +30,4 @@ def render_templates(config, content): ]: render_page(TEMPLATE_FORMAT.format(template), FILE_NAME_FORMAT.format(template), context) if config.get('toc', False): - render_page(os.path.join(TEMPLATES_DIR, 'toc-template.xsl'), os.path.join(TEMPLATES_DIR, 'toc.xsl'), context) + render_page(os.path.join(INTERNAL_TEMPLATES_DIR, 'toc-template.xsl'), os.path.join(TEMPLATES_DIR, 'toc.xsl'), context) diff --git a/md_pdf/consts.py b/md_pdf/consts.py index 7e3620e..a02f0a7 100644 --- a/md_pdf/consts.py +++ b/md_pdf/consts.py @@ -1,12 +1,23 @@ import os +import shutil PROJECT_DIR = os.path.dirname(__file__) WORKING_DIR = os.getcwd() -ASSET_DIR = os.path.join(PROJECT_DIR, 'assets') -CSL_DIR = os.path.join(ASSET_DIR, 'csl') -TEMPLATES_DIR = os.path.join(ASSET_DIR, 'templates') -STATIC_DIR = os.path.join(ASSET_DIR, 'static') +try: + ASSETS_DIR = os.path.join(os.environ['APPDATA'], '.mdp') +except KeyError: + ASSETS_DIR = os.path.join(os.environ['HOME'], '.mdp') + + +CSL_DIR = os.path.join(ASSETS_DIR, 'csl') +TEMPLATES_DIR = os.path.join(ASSETS_DIR, 'templates') +STATIC_DIR = os.path.join(ASSETS_DIR, 'static') + +INTERNAL_ASSETS_DIR = os.path.join(PROJECT_DIR, 'assets') +INTERNAL_STATIC_DIR = os.path.join(INTERNAL_ASSETS_DIR, 'static') +INTERNAL_TEMPLATES_DIR = os.path.join(INTERNAL_ASSETS_DIR, 'templates') + CONFIG_FILE = os.path.join(WORKING_DIR, 'mdp.yml') @@ -15,3 +26,8 @@ CSL_DOWNLOAD_LINK = "https://github.com/citation-style-language/styles/archive/m DATE_FORMAT = "%d %B %Y" TIME_FORMAT = "%H:%M" DATETIME_FORMAT = "{} {}".format(DATE_FORMAT, TIME_FORMAT) + + +os.makedirs(ASSETS_DIR, exist_ok=True) +os.makedirs(TEMPLATES_DIR, exist_ok=True) +os.makedirs(STATIC_DIR, exist_ok=True) diff --git a/md_pdf/csl.py b/md_pdf/csl.py index 1d45830..a779058 100644 --- a/md_pdf/csl.py +++ b/md_pdf/csl.py @@ -1,4 +1,4 @@ -from md_pdf.consts import CSL_DOWNLOAD_LINK, ASSET_DIR, CSL_DIR +from md_pdf.consts import CSL_DOWNLOAD_LINK, ASSETS_DIR, CSL_DIR from md_pdf.exceptions import PrematureExit import os import urllib @@ -12,7 +12,7 @@ import logging logger = logging.getLogger(__file__) -CSL_TEMP_DIR = os.path.join(ASSET_DIR, 'styles-master') +CSL_TEMP_DIR = os.path.join(ASSETS_DIR, 'styles-master') def check_csl(): From d725d4ee3a9dddffe088ce01a8773f28956bcc1f Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 28 May 2017 11:14:24 +0100 Subject: [PATCH 2/4] Remove old entries from gitignore --- .gitignore | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.gitignore b/.gitignore index edd2d08..b9bab86 100644 --- a/.gitignore +++ b/.gitignore @@ -245,10 +245,3 @@ ENV/ # End of https://www.gitignore.io/api/node,linux,python,jetbrains,archlinuxpackages out/ -md_pdf/assets/templates/cover.html -md_pdf/assets/templates/header.html -md_pdf/assets/templates/footer.html -md_pdf/assets/templates/toc.xsl -md_pdf/assets/static/style.css -md_pdf/assets/csl/ -md_pdf/assets/styles-master/ From 413d888a77d96672b9b67c4b51ba38b80656063a Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 28 May 2017 11:20:02 +0100 Subject: [PATCH 3/4] Use actual variable --- md_pdf/csl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/md_pdf/csl.py b/md_pdf/csl.py index a779058..87f739c 100644 --- a/md_pdf/csl.py +++ b/md_pdf/csl.py @@ -43,7 +43,7 @@ def download_csl(): bar.start(max_value=len(member_list)) for i, member in enumerate(member_list): - csl_zip.extract(member, path=ASSET_DIR) + csl_zip.extract(member, path=ASSETS_DIR) bar.update(i) bar.finish() From 79620e7feaea986a0d3767109e3c566cb33f8313 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 28 May 2017 11:20:41 +0100 Subject: [PATCH 4/4] Remove unused import --- md_pdf/consts.py | 1 - 1 file changed, 1 deletion(-) diff --git a/md_pdf/consts.py b/md_pdf/consts.py index a02f0a7..9a98e4b 100644 --- a/md_pdf/consts.py +++ b/md_pdf/consts.py @@ -1,5 +1,4 @@ import os -import shutil PROJECT_DIR = os.path.dirname(__file__) WORKING_DIR = os.getcwd()