- Notifications
You must be signed in to change notification settings - Fork 84
‼️ BREAKING: Change Token.attrs to a dict #144
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 3 commits
Commits
Show all changes
6 commits Select commit Hold shift + click to select a range
dcaca4a ‼️ BREAKING: Change `Token.attrs` to a dict
chrisjsewell bfca4f9 Update token.py
chrisjsewell 1a3eb7d Update port.yaml
chrisjsewell 69e6743 Apply suggestions from code review
chrisjsewell b64e16f Update renderer.py
chrisjsewell cc75081 remove Any types
chrisjsewell 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
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 |
|---|---|---|
| | @@ -5,6 +5,7 @@ class Renderer | |
| copy of rules. Those can be rewritten with ease. Also, you can add new | ||
| rules if you create plugin and adds new token types. | ||
| """ | ||
| import copy | ||
| import inspect | ||
| from typing import Optional, Sequence | ||
| | ||
| | @@ -153,19 +154,10 @@ def renderToken( | |
| @staticmethod | ||
| def renderAttrs(token: Token) -> str: | ||
| """Render token attributes to string.""" | ||
| if not token.attrs: | ||
| return "" | ||
| | ||
| result = "" | ||
| | ||
| for token_attr in token.attrs: | ||
| result += ( | ||
| " " | ||
| + escapeHtml(str(token_attr[0])) | ||
| + '="' | ||
| + escapeHtml(str(token_attr[1])) | ||
| + '"' | ||
| ) | ||
| for key, value in token.attrItems(): | ||
| result += " " + escapeHtml(str(key)) + '="' + escapeHtml(str(value)) + '"' | ||
| | ||
| return result | ||
| | ||
| | @@ -241,17 +233,9 @@ def fence(self, tokens: Sequence[Token], idx: int, options, env) -> str: | |
| # May be, one day we will add .deepClone() for token and simplify this part, but | ||
| # now we prefer to keep things local. | ||
| if info: | ||
| i = token.attrIndex("class") | ||
| tmpAttrs = token.attrs[:] if token.attrs else [] | ||
| | ||
| if i < 0: | ||
| tmpAttrs.append(["class", options.langPrefix + langName]) | ||
| else: | ||
| tmpAttrs[i] = tmpAttrs[i][:] | ||
| tmpAttrs[i][1] += " " + options.langPrefix + langName | ||
| | ||
| # Fake token just to render attributes | ||
| tmpToken = Token(type="", tag="", nesting=0, attrs=tmpAttrs) | ||
| tmpToken = Token(type="", tag="", nesting=0, attrs=copy.copy(token.attrs)) | ||
| ||
| tmpToken.attrJoin("class", options.langPrefix + langName) | ||
| | ||
| return ( | ||
| "<pre><code" | ||
| | @@ -271,16 +255,17 @@ def fence(self, tokens: Sequence[Token], idx: int, options, env) -> str: | |
| | ||
| def image(self, tokens: Sequence[Token], idx: int, options, env) -> str: | ||
| token = tokens[idx] | ||
| assert token.attrs is not None, '"image" token\'s attrs must not be `None`' | ||
| | ||
| # "alt" attr MUST be set, even if empty. Because it's mandatory and | ||
| # should be placed on proper position for tests. | ||
| # | ||
| | ||
| assert ( | ||
| token.attrs and "alt" in token.attrs | ||
| ), '"image" token\'s attrs must contain `alt`' | ||
| | ||
| # Replace content with actual value | ||
| | ||
| token.attrs[token.attrIndex("alt")][1] = self.renderInlineAsText( | ||
| token.children, options, env | ||
| ) | ||
| token.attrSet("alt", self.renderInlineAsText(token.children, options, env)) | ||
| | ||
| return self.renderToken(tokens, idx, options, env) | ||
| | ||
| | ||
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.
Uh oh!
There was an error while loading. Please reload this page.