-
Notifications
You must be signed in to change notification settings - Fork 316
Page context plugin #141
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
jammon
wants to merge
5
commits into
eudicots:master
Choose a base branch
from
jammon:page_context-plugin
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
Page context plugin #141
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e960c2a
Parsing of page context factored out into a plugin
jammon 36b228a
More integration tests
jammon 6e2e3d0
Added documentation
jammon 7cb0068
Following many suggestions of @krallin
jammon ed84669
renamed plugin/builtin/pagecontext.py to page_context.py
jammon 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| #coding:utf-8 | ||
| from __future__ import unicode_literals | ||
| import logging | ||
| import re | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class PageContextPlugin(object): | ||
| """ | ||
| A plugin to extract context variables from the top of the file | ||
|
|
||
| TODO: | ||
| - read style of metadata from config | ||
| - read splitChar from config | ||
| """ | ||
| ORDER = 0 | ||
|
|
||
| def preBuildPage(self, page, context, data): | ||
| """ | ||
| Update the page context with the context variables from the file | ||
| and delete them | ||
| """ | ||
| if not page.is_html() or not data: | ||
| return context, data | ||
|
|
||
| lines = data.splitlines() | ||
|
|
||
| # Look into the file to decide on the style of metadata | ||
| # TODO: make that configurable | ||
| if lines[0].strip() == '---': | ||
| page_context, lines = self.jekyll_style(lines, page) | ||
| else: | ||
| page_context, lines = self.simple_style(lines) | ||
| context.update(page_context) | ||
|
|
||
| return context, '\n'.join(lines) | ||
|
|
||
| def jekyll_style(self, lines, page): | ||
| """ | ||
| metadata as defined in http://jekyllrb.com/docs/frontmatter/ | ||
|
|
||
| `lines` should not be empty | ||
| `page` is required only for the error message | ||
| """ | ||
| if lines[0].strip() != '---': # no metadata | ||
| return {}, lines | ||
|
|
||
| context = {} | ||
| splitChar = ':' | ||
| for i, line in enumerate(lines[1:]): | ||
| if line.strip() == '---': # end of metadata | ||
| i += 1 | ||
| break | ||
|
|
||
| try: | ||
| key, value = line.split(splitChar, 1) | ||
| except ValueError: | ||
| # splitChar not in line | ||
| logger.warning('Page context data in file %s seem to end in line %d', | ||
| page.source_path, i) | ||
| break | ||
|
|
||
| context[key.strip()] = value.strip() | ||
|
|
||
| return context, lines[i+1:] | ||
|
|
||
| def simple_style(self, lines): | ||
| """ | ||
| Only lines at the top of the file with a colon are metadata. | ||
| No multiline metadata | ||
|
|
||
| `lines` should not be empty | ||
| """ | ||
| context = {} | ||
| splitChar = ':' | ||
|
|
||
| for i, line in enumerate(lines): | ||
|
|
||
| if splitChar not in line: | ||
| break | ||
|
|
||
| key, value = line.split(splitChar, 1) | ||
| context[key.strip()] = value.strip() | ||
|
|
||
| return context, lines[i:] | ||
|
|
||
| def multimarkdown_style(self, lines): | ||
| """ | ||
| metadata as defined in http://fletcher.github.io/MultiMarkdown-4/metadata | ||
|
|
||
| `lines` should not be empty | ||
| """ | ||
| context = {} | ||
| splitChar = ':' | ||
| fenced = False | ||
| key = None | ||
| whitespace = re.compile(r'\s') | ||
|
|
||
| if splitChar not in lines[0] and lines[0].strip() != '---': | ||
| return {}, lines # no metadata | ||
|
|
||
| for i, line in enumerate(lines): | ||
| # fencing is allowed but not required in multimarkdown | ||
| if line.strip() == '---': | ||
| if i == 0: | ||
| fenced = True | ||
| continue | ||
| elif fenced: | ||
| i += 1 | ||
| break | ||
|
|
||
| # a blank line triggers the beginning of the rest of the document | ||
| if not line.strip(): | ||
| # if the file starts with a blank line, nothing should be changed | ||
| if i != 0: | ||
| i += 1 | ||
| break | ||
|
|
||
| # indented lines are the continuation of the previous value | ||
| # but multiline values don't have to be indented | ||
| if key and (whitespace.match(line) or not splitChar in line): | ||
| context[key] = ' '.join( | ||
| (context[key], line.lstrip())) | ||
| continue | ||
|
|
||
| key, value = line.split(splitChar, 1) | ||
|
|
||
| # keys are lower cased and stripped of all whitespace | ||
| key = re.sub(r'\s', '', key.lower()) | ||
| context[key] = value.strip() | ||
|
|
||
| return context, lines[i:] |
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
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.
I think we should refactor this method.
datais passed, but we never use it.extrais never passed. How about we remove those altogether?Cheers,
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.
Sure, I had left it in because somebody else might rely on them being there. But that seems overly cautious.