Merge pull request #14 from RealOrangeOne/extract-assets

Dont render assets to project dir
This commit is contained in:
Jake Howard 2017-05-28 11:35:12 +01:00 committed by GitHub
commit 253ecb88f1
9 changed files with 35 additions and 25 deletions

7
.gitignore vendored
View file

@ -245,10 +245,3 @@ ENV/
# End of https://www.gitignore.io/api/node,linux,python,jetbrains,archlinuxpackages # End of https://www.gitignore.io/api/node,linux,python,jetbrains,archlinuxpackages
out/ 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/

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<link rel="stylesheet" href="../static/style.css" /> <link rel="stylesheet" href="{{ static_dir }}/style.css" />
</head> </head>
<body class="cover"> <body class="cover">
<h1>{{ title }}</h1> <h1>{{ title }}</h1>

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<link rel="stylesheet" href="../static/style.css" /> <link rel="stylesheet" href="{{ static_dir }}/style.css" />
</head> </head>
<body class="footer"> <body class="footer">
<table> <table>
@ -17,6 +17,6 @@
</td> </td>
</tr> </tr>
</table> </table>
<script type="text/javascript" src="../static/context.js"></script> <script type="text/javascript" src="{{ internal_static_dir }}/context.js"></script>
</body> </body>
</html> </html>

View file

@ -1,7 +1,7 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<link rel="stylesheet" href="../static/style.css" /> <link rel="stylesheet" href="{{ static_dir }}/style.css" />
</head> </head>
<body class="header"> <body class="header">
<table> <table>
@ -15,6 +15,6 @@
</td> </td>
</tr> </tr>
</table> </table>
<script type="text/javascript" src="../static/context.js"></script> <script type="text/javascript" src="{{ internal_static_dir }}/context.js"></script>
</body> </body>
</html> </html>

View file

@ -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 word_count import word_count
from md_pdf.utils import get_plain_text from md_pdf.utils import get_plain_text
from md_pdf import __version__ from md_pdf import __version__
@ -10,6 +10,8 @@ import os
EXTRA_CONTEXT = { EXTRA_CONTEXT = {
'templates_dir': TEMPLATES_DIR, 'templates_dir': TEMPLATES_DIR,
'static_dir': STATIC_DIR, 'static_dir': STATIC_DIR,
'internal_templates_dir': INTERNAL_TEMPLATES_DIR,
'internal_static_dir': INTERNAL_STATIC_DIR,
'date': datetime.datetime.now().strftime(DATE_FORMAT), 'date': datetime.datetime.now().strftime(DATE_FORMAT),
'time': datetime.datetime.now().strftime(TIME_FORMAT), 'time': datetime.datetime.now().strftime(TIME_FORMAT),
'datetime': datetime.datetime.now().strftime(DATETIME_FORMAT), 'datetime': datetime.datetime.now().strftime(DATETIME_FORMAT),

View file

@ -1,10 +1,10 @@
from scss import Compiler 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 from md_pdf.build.pdf import STYLE_FILE
import os 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(): def render_css():

View file

@ -1,5 +1,5 @@
from jinja2 import Template 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 from md_pdf.build.context import get_context
import os import os
import logging import logging
@ -8,7 +8,7 @@ logger = logging.getLogger(__file__)
FILE_NAME_FORMAT = os.path.join(TEMPLATES_DIR, "{}.html") 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): 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) render_page(TEMPLATE_FORMAT.format(template), FILE_NAME_FORMAT.format(template), context)
if config.get('toc', False): 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)

View file

@ -2,11 +2,21 @@ import os
PROJECT_DIR = os.path.dirname(__file__) PROJECT_DIR = os.path.dirname(__file__)
WORKING_DIR = os.getcwd() WORKING_DIR = os.getcwd()
ASSET_DIR = os.path.join(PROJECT_DIR, 'assets')
CSL_DIR = os.path.join(ASSET_DIR, 'csl') try:
TEMPLATES_DIR = os.path.join(ASSET_DIR, 'templates') ASSETS_DIR = os.path.join(os.environ['APPDATA'], '.mdp')
STATIC_DIR = os.path.join(ASSET_DIR, 'static') 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') CONFIG_FILE = os.path.join(WORKING_DIR, 'mdp.yml')
@ -15,3 +25,8 @@ CSL_DOWNLOAD_LINK = "https://github.com/citation-style-language/styles/archive/m
DATE_FORMAT = "%d %B %Y" DATE_FORMAT = "%d %B %Y"
TIME_FORMAT = "%H:%M" TIME_FORMAT = "%H:%M"
DATETIME_FORMAT = "{} {}".format(DATE_FORMAT, TIME_FORMAT) 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)

View file

@ -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 from md_pdf.exceptions import PrematureExit
import os import os
import urllib import urllib
@ -12,7 +12,7 @@ import logging
logger = logging.getLogger(__file__) 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(): def check_csl():
@ -43,7 +43,7 @@ def download_csl():
bar.start(max_value=len(member_list)) bar.start(max_value=len(member_list))
for i, member in enumerate(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.update(i)
bar.finish() bar.finish()