-
-
Notifications
You must be signed in to change notification settings - Fork 811
Fix tag issues/indentation, axe most of Beeware plugin, update edit links #663
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
50f958e
Fixed tag issues and tweaked some indendation
HalfWhitt bf371c6
Remove Beeware package, update edit links
HalfWhitt 2a2b65c
Update string *name* too
HalfWhitt 1ae10cd
Other cleanups...
HalfWhitt 574d2c9
Curse you, indentation
HalfWhitt 8fedb26
Changed "translate" str, undid rest of .ini changes, brought back "to…
HalfWhitt 010232c
Remove unneeded dependencies
HalfWhitt b582915
Fix preexisting typo in BeeWare plugin
HalfWhitt 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
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
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
195 changes: 2 additions & 193 deletions
195
packages/lektor_beeware_plugin/lektor_beeware_plugin.py
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 |
|---|---|---|
| @@ -1,205 +1,14 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """This is a custom local plugin to ad extra functionality to BeeWare site.""" | ||
| """This is a custom local plugin to add extra functionality to BeeWare site.""" | ||
|
|
||
| # Standard library imports | ||
| from datetime import datetime, date | ||
| import os | ||
| import subprocess | ||
| import sys | ||
| from urllib.parse import quote_plus | ||
| from datetime import date | ||
|
|
||
| # Third party imports | ||
| from lektor.pluginsystem import Plugin | ||
| from lektor.db import Record | ||
| from markupsafe import Markup | ||
| from pygments import highlight | ||
| from pygments import lexers | ||
| from pygments.formatters import HtmlFormatter | ||
| from pygments.styles import get_all_styles | ||
|
|
||
|
|
||
| PY3 = sys.version_info[0] == 3 | ||
|
|
||
|
|
||
| class BeeWarePlugin(Plugin): | ||
| name = 'BeeWare Custom Lektor Plugin' | ||
| description = 'This is a custom local plugin to add extra functionality.' | ||
|
|
||
| DATE_FORMAT = "%Y-%m-%d %H:%M:%S" | ||
| EXECUTE_CACHE = {} | ||
|
|
||
| def on_setup_env(self, **extra): | ||
|
|
||
| # --- Helper methods | ||
| # --------------------------------------------------------------------- | ||
| def execute(cmd): | ||
| """Execute a cmd (list) and return the stdout and stderr.""" | ||
| joined_cmd = ' '.join(cmd) | ||
| if joined_cmd in self.EXECUTE_CACHE: | ||
| stdout, stderr = self.EXECUTE_CACHE[joined_cmd] | ||
| else: | ||
| stdout, stderr = None, None | ||
| try: | ||
| p = subprocess.Popen(cmd, stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE) | ||
| stdout, stderr = p.communicate() | ||
|
|
||
| if PY3: | ||
| if stdout: | ||
| stdout = stdout.decode() | ||
| if stderr: | ||
| stderr = stderr.decode() | ||
|
|
||
| except OSError: | ||
| print('\nCommand not found!\n') | ||
| print(joined_cmd) | ||
| print('\n') | ||
| except Exception as e: | ||
| print(e) | ||
| print('\n') | ||
|
|
||
| self.EXECUTE_CACHE[joined_cmd] = (stdout, stderr) | ||
|
|
||
| return stdout, stderr | ||
|
|
||
| def git_commits(filepath, since): | ||
| """Get git commit hashes for `filepath` `since` a given date.""" | ||
| cmd = ('git', 'log', '--since="'+since+'"', '--pretty=format:%H', | ||
| '--', filepath) | ||
| stdout, stderr = execute(cmd) | ||
|
|
||
| commits = [] | ||
| if stdout: | ||
| commits = [c for c in stdout.split('\n') if c] | ||
|
|
||
| return commits | ||
|
|
||
| def git_diff(filepath, since): | ||
| """Get git diff for a given `filepath` `since` a given date.""" | ||
| html_diff = None | ||
| commits = git_commits(filepath, since) | ||
| if commits: | ||
| cmd = ('git', '--no-pager', 'diff', commits[-1]+'^', '--', | ||
| filepath) | ||
| stdout, stderr = execute(cmd) | ||
|
|
||
| if stdout: | ||
| html_diff = highlight(stdout, lexers.DiffLexer(), | ||
| HtmlFormatter()) | ||
|
|
||
| # print(' '.join(cmd)) | ||
| # print(diff) | ||
| # print('\n') | ||
|
|
||
| return html_diff | ||
|
|
||
| def git_modified_date(filepath): | ||
| """Get last modified date of `filepath` from git.""" | ||
| git_mod_date = None | ||
|
|
||
| if os.path.isfile(filepath): | ||
| cmd = ('git', 'log', '-1', | ||
| '--date=format:"'+self.DATE_FORMAT+'"', | ||
| '--format="%ad"', filepath) | ||
| stdout, stderr = execute(cmd) | ||
|
|
||
| if stdout: | ||
| stdout = stdout.split('\n')[0] | ||
| stdout = stdout.replace('"', '') | ||
| stdout = stdout.replace("'", '') | ||
|
|
||
| if stdout: | ||
| git_mod_date = datetime.strptime(stdout, self.DATE_FORMAT) | ||
|
|
||
| # print(' '.join(cmd)) | ||
| # print(git_mod_date) | ||
| # print('\n') | ||
|
|
||
| return git_mod_date | ||
|
|
||
| # --- Exposed methods | ||
| # --------------------------------------------------------------------- | ||
| def get_pygments_css_styles(style='default'): | ||
| """Get pygments CSS.""" | ||
| class_name = '.highlight' | ||
| css = None | ||
| if style in get_all_styles(): | ||
| cmd = ('pygmentize', '-S', style, '-f', 'html', '-a', | ||
| class_name) | ||
| stdout, stderr = execute(cmd) | ||
| css = '<style>{}</style>'.format(stdout) | ||
|
|
||
| return css | ||
|
|
||
| def is_alt_outdated(record): | ||
| """Check if record alternative is outdated.""" | ||
| return _alt_outdated_diff(record, return_diff=False) | ||
|
|
||
| def alt_outdated_diff(record): | ||
| """Check if record alternative is outdated and returns the diff.""" | ||
| return _alt_outdated_diff(record, return_diff=True) | ||
|
|
||
| def _alt_outdated_diff(record, return_diff=True): | ||
| """Check if record alternative is outdated and returns the diff.""" | ||
| if not isinstance(record, Record): | ||
| raise ValueError('Must provide a `Record` object') | ||
|
|
||
| config = self.env.jinja_env.globals['config'] | ||
| site = self.env.jinja_env.globals['site'] | ||
| primary_path = site.get(record.path, alt="_primary").contents.filename | ||
| primary_path = primary_path.replace( | ||
| '+{0}.lr'.format(config.primary_alternative), '.lr') | ||
| try: | ||
| alt_path = record.contents.filename | ||
| alt_text = '+{0}.lr'.format(config.primary_alternative) | ||
| if alt_text in alt_path: | ||
| alt_path = alt_path.replace(alt_text, '.lr') | ||
| except IOError: | ||
| # This means the alt content file does not exist | ||
| alt_path = None | ||
|
|
||
| primary_modtime = None | ||
| if os.path.isfile(primary_path): | ||
| # primary_modtime = os.path.getmtime(primary_path) | ||
| primary_modtime = git_modified_date(primary_path) | ||
|
|
||
| alt_modtime = None | ||
| if os.path.isfile(alt_path): | ||
| # alt_modtime = os.path.getmtime(alt_path) | ||
| alt_modtime = git_modified_date(alt_path) | ||
|
|
||
| diff = None | ||
| if alt_modtime and primary_modtime > alt_modtime and return_diff: | ||
| diff = git_diff(primary_path, | ||
| alt_modtime.strftime(self.DATE_FORMAT)) | ||
|
|
||
| if not return_diff: | ||
| diff = alt_modtime and primary_modtime > alt_modtime | ||
|
|
||
| return diff | ||
|
|
||
| def urlencode_limit(string, limit=6000): | ||
| """Encodes url for uri but if over limit returns None.""" | ||
| if type(string) == 'Markup': | ||
| string = string.unescape() | ||
|
|
||
| if len(string) <= limit: | ||
| string = string.encode('utf8') | ||
| string = quote_plus(string) | ||
| string = Markup(string) | ||
| else: | ||
| string = None | ||
|
|
||
| return string | ||
|
|
||
| # Add additional methods to the environment globlas | ||
| self.env.jinja_env.globals.update(is_alt_outdated=is_alt_outdated) | ||
| self.env.jinja_env.globals.update(alt_outdated_diff=alt_outdated_diff) | ||
| self.env.jinja_env.globals.update(urlencode_limit=urlencode_limit) | ||
| self.env.jinja_env.globals.update( | ||
| get_pygments_css_styles=get_pygments_css_styles) | ||
|
|
||
| self.env.jinja_env.globals['today'] = date.today() | ||
|
|
||
| # Add some python builtins that are good for debugging | ||
| self.env.jinja_env.globals.update(dir=dir) | ||
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
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| {% if this.title %}<h3>{{ this.title }}</h3>{% endif %} | ||
| <p>{% if this.image %}<img src="{{ this.record.path }}{{ this.image }}" class="hidden-sm-down p-3 float-{{ this.image_alignment }}">{% endif %} {{ this.text }}</p> | ||
| <p>{% if this.image %}<img src="{{ this.record.path }}{{ this.image }}" class="hidden-sm-down p-3 float-{{ this.image_alignment }}">{% endif %}</p> | ||
| {{ this.text }} |
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
Oops, something went wrong.
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.
No objection to having this if there's no other option - but is there no pre-existing plugin that will give "today's date" as a context variable? It seems like one of those things that is so common it must exist...
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.
It may very well... That said, I've also been working, in parallel, on a plugin to provide a more convenient filter replacing our current translation macro. Might as well feed two birds with one scone.
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.
Why are we wasting perfectly good scones on the birds? 🤣
Did you want to roll that new plugin into this, or land this version of a vastly-minimised plugin, and replace it in the next pass?
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.
Well it's much nicer than stones : )
I think it would be a much simpler PR to review (and verify nothing is out of order) if done separately.