-
Notifications
You must be signed in to change notification settings - Fork 12
Serve CodeMirror CSS and JS locally. #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BasementCat
wants to merge
1
commit into
j0ack:master
Choose a base branch
from
BasementCat:serve-local
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,11 @@ | |
for Flask | ||
""" | ||
|
||
import os | ||
import requests | ||
import warnings | ||
from jinja2 import Markup | ||
from flask import current_app | ||
from flask import current_app, url_for | ||
try: | ||
from urllib.parse import urljoin | ||
except ImportError: | ||
|
@@ -50,7 +51,9 @@ class CodeMirrorHeaders(object): | |
THEME_KEY = 'CODEMIRROR_THEME' | ||
ADDONS_KEY = 'CODEMIRROR_ADDONS' | ||
VERSION_KEY = 'CODEMIRROR_VERSION' | ||
SERVE_LOCAL_KEY = 'CODEMIRROR_SERVE_LOCAL' | ||
CDN_URL = '//cdnjs.cloudflare.com/ajax/libs/codemirror/{0}/' | ||
LOCAL_PATH = '/codemirror/{0}' | ||
LANGUAGE_REL_URL = 'mode/{0}/{0}.js' | ||
THEME_REL_URL = 'theme/{0}.css' | ||
ADDON_REL_URL = 'addon/{0}/{1}.js' | ||
|
@@ -62,8 +65,12 @@ def __init__(self, config): | |
self.languages = config.get(self.__class__.LANGUAGES_KEY, []) | ||
self.addons = config.get(self.__class__.ADDONS_KEY, None) | ||
self.version = config.get(self.__class__.VERSION_KEY, '4.12.0') | ||
self.serve_local = config.get(self.__class__.SERVE_LOCAL_KEY, False) | ||
# construct base url | ||
self.base_url = self.__class__.CDN_URL.format(self.version) | ||
if self.serve_local: | ||
self.base_url = None | ||
else: | ||
self.base_url = self.__class__.CDN_URL.format(self.version) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not mandatory since you check |
||
if not self.languages: | ||
error = '{0} is required'.format(self.__class__.LANGUAGES_KEY) | ||
raise CodeMirrorConfigException(error) | ||
|
@@ -75,19 +82,27 @@ def _get_tag(self, url, tag, print_warn=True): | |
:param print_warn: if True print warn when url is unavailable | ||
""" | ||
# construct complete url | ||
complete_url = urljoin(self.base_url, url) | ||
# check if exists | ||
if requests.get('http:' + complete_url).ok: | ||
# construct tag | ||
if tag == 'script': | ||
return '<script src="{0}"></script>'.format(complete_url) | ||
elif tag == 'stylesheet': | ||
return '<link rel="stylesheet" href="{0}">'.format(complete_url) | ||
else: | ||
warnings.warn('Given tag is not valid') | ||
elif print_warn: | ||
warnings.warn('Url {0} not valid'.format(complete_url)) | ||
return None | ||
if self.serve_local: | ||
complete_url = url_for( | ||
'static', | ||
filename=os.path.join(self.__class__.LOCAL_PATH.format(self.version), url).lstrip('/') | ||
) | ||
else: | ||
complete_url = urljoin(self.base_url, url) | ||
# check if exists | ||
if not requests.get('http:' + complete_url).ok: | ||
if print_warn: | ||
warnings.warn('Url {0} not valid'.format(complete_url)) | ||
return None | ||
|
||
# construct tag | ||
if tag == 'script': | ||
return '<script src="{0}"></script>'.format(complete_url) | ||
elif tag == 'stylesheet': | ||
return '<link rel="stylesheet" href="{0}">'.format(complete_url) | ||
else: | ||
warnings.warn('Given tag is not valid') | ||
return None | ||
|
||
def include_codemirror(self): | ||
"""Include resources in pages""" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
""" | ||
from setuptools import setup | ||
|
||
__version__ = '1.0' | ||
__version__ = '1.1' | ||
__author__ = 'TROUVERIE Joachim' | ||
__contact__ = '[email protected]' | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be better if there was a config key for this path, since it's local users could host the scripts where they want.