-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Custom code fence #555
Draft
shaedrich
wants to merge
70
commits into
main
Choose a base branch
from
custom-code-fence
base: main
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.
Draft
Custom code fence #555
Changes from all commits
Commits
Show all changes
70 commits
Select commit
Hold shift + click to select a range
58149ab
Enable custom fence with attr_list
shaedrich 21ff237
Modify example
shaedrich 44f84ad
Test
shaedrich 7dad144
Ignore wayback machine timeout for now
shaedrich 857800f
Add keywords to spellcheck exceptions
shaedrich f164518
use attr_list style code fences
shaedrich a0ca842
Use standard pony syntax highlight
shaedrich 7a94619
Custom fences formatter and validator
shaedrich c4a4dca
Add module file
shaedrich 25e25d3
Fix typo
shaedrich 28952fe
Use file in route directory (no module)
shaedrich bbd94c3
Add -m argument to build command
shaedrich d5ac6d4
load local modules
shaedrich f669393
reqs
shaedrich eca6abf
Remove unrecognized option from build command
shaedrich 88b96d5
Install lib module folder as part of build command
shaedrich fee9326
requirements in install command
shaedrich b20f323
PYTHONPATH in build command
shaedrich 1cea0d7
Remove redundant
shaedrich 4cceaa5
static class
shaedrich c34ae9d
Add validation
shaedrich 0ac65f1
use pygments
shaedrich ad23762
Add nav buttons
shaedrich c5ce12a
debug
shaedrich e27f244
use excedption
shaedrich b6229a1
test
shaedrich d1eb814
test2
shaedrich 9dbb2e1
More parameters
shaedrich efd8ff0
Imports
shaedrich 540b3b0
Don't validate
shaedrich baa2d95
Return actual markup
shaedrich 98bcee4
return markup directly
shaedrich 8a78fcb
Use pony lexer
shaedrich 97204f3
Load snippet
shaedrich 3f66f51
debug
shaedrich 09e035a
debug all dicts
shaedrich 8dc6ee7
get snippet lines
shaedrich 17c9281
Simplify
shaedrich 5bfad84
Fix key as string
shaedrich 706ca16
try-catch
shaedrich 7f67cc4
Fix file path
shaedrich cfd7773
use cwd
shaedrich a864c1e
Add import
shaedrich 7110b2f
re-implement line number logic
shaedrich eccd567
debug again
shaedrich d5d775c
convert to number
shaedrich fadf122
destructure tuple
shaedrich e9476e7
inclusive comparison
shaedrich 06d8926
re-implement line join
shaedrich 4a55e86
Simplify
shaedrich 5410c2b
debug
shaedrich 505d1f5
split class in debugging
shaedrich b1e9d7f
config
shaedrich 91ba8c3
debug
shaedrich d3bbcd1
more debug
shaedrich b113123
even more debug
shaedrich 54918e2
debug - why not?
shaedrich de5fc30
dedent_subsections logic
shaedrich b1b788d
debug once again
shaedrich ceb5510
1-indexed
shaedrich 7c08170
Fix debug
shaedrich fca1540
Better example
shaedrich 8aec846
improve debug
shaedrich e490856
Fix snippet path
shaedrich 8f35e48
remove debugging output
shaedrich b8aa275
They already have line breaks
shaedrich 7e97580
Wrap output
shaedrich 89cf63d
Navigation
shaedrich f738e31
Wrap in pre
shaedrich aba7756
Only render run button when runnable
shaedrich 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 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 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 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
Empty file.
This file contains 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,106 @@ | ||
from pygments import highlight | ||
from pygments.lexers import PonyLexer | ||
from pygments.formatters import HtmlFormatter | ||
|
||
from pymdownx.superfences import SuperFencesException | ||
from pymdownx.superfences import _escape | ||
|
||
from mkdocs.exceptions import PluginError | ||
from mkdocs.config import base | ||
from mkdocs.config import config_options as c | ||
from mkdocs.structure.pages import Page, _AbsoluteLinksValidationValue | ||
from mkdocs.utils.yaml import get_yaml_loader, yaml_load | ||
from mkdocs.utils.templates import TemplateContext | ||
|
||
import os | ||
import re | ||
|
||
def format(source, language, css_class, options, md, classes=None, id_value='', attrs=None, **kwargs): | ||
if "snippet" in attrs: #options | ||
#workingDir = os.getcwd().replace('/lib/superfences_ponylang') | ||
try: | ||
snippetPath = attrs.get('snippet') | ||
if ':' in snippetPath: | ||
snippetPath, lineNumbers = snippetPath.split(':', 2) | ||
lines = [] | ||
_lines = {} | ||
if ',' in lineNumbers: | ||
lineNumbers = lineNumbers.split(',') | ||
else: | ||
lineNumbers = [ lineNumbers ] | ||
with open(os.getcwd() + "/code-samples/" + snippetPath, 'r') as f: | ||
for i, line in enumerate(f): | ||
for lineNum in lineNumbers: | ||
if '-' in lineNum: | ||
start, end = lineNum.split('-') | ||
if (i + 1) >= int(start) and (i + 1) <= int(end): | ||
lines.append(line) | ||
# _lines[i] = (lineNumbers, line, True) | ||
elif (i + 1) == int(lineNum): | ||
lines.append(line) | ||
# _lines[i] = (lineNumbers, line, True) | ||
#else: | ||
# _lines[i] = (lineNumbers, line, False) | ||
#source = str(lines) | ||
#source = str(_lines) | ||
|
||
if 'dedent_subsections' in attrs and attrs.get('dedent_subsections'): | ||
p = re.compile('^\s+') | ||
indents = [] | ||
for line in lines: | ||
m = p.match(line) | ||
if m is None: | ||
indents.append(0) | ||
else: | ||
indents.append(m.span()[1]) | ||
indent = min(indents) | ||
if indent > 0: | ||
for i, line in enumerate(lines): | ||
lines[i] = line[indent:None] | ||
|
||
source = ''.join(lines) #'\n' | ||
#source = str(base.Config.user_configs.__dict__) + str(TemplateContext) + str(c) + str(options) + str(attrs) + str(classes) + str(kwargs) | ||
else: | ||
with open(os.getcwd() + "/code-samples/" + snippetPath, 'r') as f: | ||
source = f.read() | ||
except: | ||
raise SuperFencesException('Snippet "%s" does not work' % (snippetPath)) | ||
else: | ||
source = str(options) + str(attrs) + str(classes) + str(kwargs) | ||
|
||
try: | ||
highlighted = highlight(source, PonyLexer(), HtmlFormatter()) | ||
except: | ||
raise SuperFencesException('Could not highlight source code "%s" passed' % (source)) | ||
|
||
return f""" | ||
<pre> | ||
<nav class="md-code__nav"> | ||
<button class="md-code__button" title="Copy to clipboard" data-clipboard-target="#__code_1 > code" data-md-type="copy"></button> | ||
{'<button class="md-code__button" title="Run in playground" data-md-type="run"></button>' if attrs.get('runnable', false) else ''} | ||
{'<button class="md-code__button" title="Run in playground" data-md-type="inline"></button>' if attrs.get('runnable', false) else ''} | ||
</nav> | ||
<code lang="pony">%s</code> | ||
</pre> | ||
""" % highlighted | ||
|
||
return """ | ||
<pre class_name="ponylang %s %s", data-option="%s"> | ||
<nav class="md-code__nav"> | ||
<button class="md-code__button" title="Copy to clipboard" data-clipboard-target="#__code_1 > code" data-md-type="copy"></button> | ||
<button class="md-code__button" title="Run in playground" data-md-type="run"></button> | ||
<button class="md-code__button" title="Run in playground" data-md-type="inline"></button> | ||
</nav> | ||
<code>%s</code> | ||
</pre> | ||
""" % (language, class_name, options['opt'], _escape(highlighted)) # html_escape | ||
|
||
def validate(language: str, options: dict, attrs: dict, md) -> bool: | ||
return True | ||
allowed_options = { "snippet", "dedent_subsections" } #lambda v: v in ENGINES | ||
for opt in options.keys(): | ||
if opt not in allowed_options: | ||
raise SuperFencesException('unknown config key "%s" passed' % (opt)) | ||
return False | ||
print('valid fence') | ||
return True |
This file contains 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 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 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,5 @@ | ||
def format(source, language, css_class, options, md, classes=None, id_value='', attrs=None, **kwargs): | ||
return '<pre class_name="%s %s", data-option="%s"><nav class="md-code__nav"><button class="md-code__button" title="Copy to clipboard" data-clipboard-target="#__code_1 > code" data-md-type="copy"></button></nav><code>%s</code></pre>' % (language, class_name, options['opt'], html_escape(source)) | ||
|
||
def validate(language: str, options: dict) -> bool: | ||
return True # TODO allowed attributes and values |
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.
this appears unrelated.
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.
That's temporary due to https://app.netlify.com/sites/pony-tutorial/deploys/66608a3635679b0008f1cff3#L152