Skip to content
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
wants to merge 70 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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 Jun 5, 2024
21ff237
Modify example
shaedrich Jun 5, 2024
44f84ad
Test
shaedrich Jun 5, 2024
7dad144
Ignore wayback machine timeout for now
shaedrich Jun 5, 2024
857800f
Add keywords to spellcheck exceptions
shaedrich Jun 5, 2024
f164518
use attr_list style code fences
shaedrich Jun 5, 2024
a0ca842
Use standard pony syntax highlight
shaedrich Jun 5, 2024
7a94619
Custom fences formatter and validator
shaedrich Jun 5, 2024
c4a4dca
Add module file
shaedrich Jun 5, 2024
25e25d3
Fix typo
shaedrich Jun 5, 2024
28952fe
Use file in route directory (no module)
shaedrich Jun 5, 2024
bbd94c3
Add -m argument to build command
shaedrich Jun 5, 2024
d5ac6d4
load local modules
shaedrich Jun 5, 2024
f669393
reqs
shaedrich Jun 5, 2024
eca6abf
Remove unrecognized option from build command
shaedrich Jun 5, 2024
88b96d5
Install lib module folder as part of build command
shaedrich Jun 5, 2024
fee9326
requirements in install command
shaedrich Jun 5, 2024
b20f323
PYTHONPATH in build command
shaedrich Jun 5, 2024
1cea0d7
Remove redundant
shaedrich Jun 5, 2024
4cceaa5
static class
shaedrich Jun 5, 2024
c34ae9d
Add validation
shaedrich Jun 5, 2024
0ac65f1
use pygments
shaedrich Jun 5, 2024
ad23762
Add nav buttons
shaedrich Jun 5, 2024
c5ce12a
debug
shaedrich Jun 5, 2024
e27f244
use excedption
shaedrich Jun 5, 2024
b6229a1
test
shaedrich Jun 5, 2024
d1eb814
test2
shaedrich Jun 5, 2024
9dbb2e1
More parameters
shaedrich Jun 5, 2024
efd8ff0
Imports
shaedrich Jun 5, 2024
540b3b0
Don't validate
shaedrich Jun 5, 2024
baa2d95
Return actual markup
shaedrich Jun 5, 2024
98bcee4
return markup directly
shaedrich Jun 5, 2024
8a78fcb
Use pony lexer
shaedrich Jun 5, 2024
97204f3
Load snippet
shaedrich Jun 5, 2024
3f66f51
debug
shaedrich Jun 5, 2024
09e035a
debug all dicts
shaedrich Jun 5, 2024
8dc6ee7
get snippet lines
shaedrich Jun 5, 2024
17c9281
Simplify
shaedrich Jun 5, 2024
5bfad84
Fix key as string
shaedrich Jun 5, 2024
706ca16
try-catch
shaedrich Jun 5, 2024
7f67cc4
Fix file path
shaedrich Jun 5, 2024
cfd7773
use cwd
shaedrich Jun 5, 2024
a864c1e
Add import
shaedrich Jun 5, 2024
7110b2f
re-implement line number logic
shaedrich Jun 5, 2024
eccd567
debug again
shaedrich Jun 5, 2024
d5d775c
convert to number
shaedrich Jun 5, 2024
fadf122
destructure tuple
shaedrich Jun 5, 2024
e9476e7
inclusive comparison
shaedrich Jun 5, 2024
06d8926
re-implement line join
shaedrich Jun 5, 2024
4a55e86
Simplify
shaedrich Jun 5, 2024
5410c2b
debug
shaedrich Jun 5, 2024
505d1f5
split class in debugging
shaedrich Jun 5, 2024
b1e9d7f
config
shaedrich Jun 5, 2024
91ba8c3
debug
shaedrich Jun 5, 2024
d3bbcd1
more debug
shaedrich Jun 5, 2024
b113123
even more debug
shaedrich Jun 5, 2024
54918e2
debug - why not?
shaedrich Jun 6, 2024
de5fc30
dedent_subsections logic
shaedrich Jun 6, 2024
b1b788d
debug once again
shaedrich Jun 6, 2024
ceb5510
1-indexed
shaedrich Jun 6, 2024
7c08170
Fix debug
shaedrich Jun 6, 2024
fca1540
Better example
shaedrich Jun 6, 2024
8aec846
improve debug
shaedrich Jun 6, 2024
e490856
Fix snippet path
shaedrich Jun 6, 2024
8f35e48
remove debugging output
shaedrich Jun 6, 2024
b8aa275
They already have line breaks
shaedrich Jun 6, 2024
7e97580
Wrap output
shaedrich Jun 6, 2024
89cf63d
Navigation
shaedrich Jun 6, 2024
f738e31
Wrap in pre
shaedrich Jun 6, 2024
aba7756
Only render run button when runnable
shaedrich Jun 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .spelling-wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ booleans
capybaras
combinators
compilable
dedent
dereference
deserialise
deserialised
Expand Down Expand Up @@ -52,6 +53,7 @@ polymorphism
ponyc
PonyCheck
Ponylang
ponylang
PonyTest
POSIX
preprocessor
Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ __Does the name of the file matter?__ Not to the compiler, no. Pony doesn't care

In your file, put the following code:

```pony
--8<-- "hello-world-main.pony"
```{ .ponylang snippet="hello-world-main.pony" dedent_subsections=false runnable=false }
Gibberish
```

## Compiling the program
Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started/how-it-works.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Let's go through that line by line.

## Line 1

```pony
--8<-- "hello-world-main.pony::1"
```{ .ponylang snippet="classes-wombat.pony:1-3,6" dedent_subsections=false runnable=true }
Lorem ipsum
```

This is a __type declaration__. The keyword `actor` means we are going to define an actor, which is a bit like a class in Python, Java, C#, C++, etc. Pony has classes too, which we'll see later.
Expand Down
Empty file added lib/__init__.py
Empty file.
106 changes: 106 additions & 0 deletions lib/superfences_ponylang/__init__.py
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 &gt; 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 &gt; 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
6 changes: 6 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ markdown_extensions:
- name: mermaid
class: mermaid-experimental
format: !!python/name:pymdownx.superfences.fence_code_format
- name: ponylang
class: pony
format: !!python/name:lib.superfences_ponylang.format
#validator: !!python/name:lib.superfences_ponylang.validate
- pymdownx.snippets:
base_path: ['code-samples']
check_paths: true
- smarty
- toc:
permalink: true
- attr_list

plugins:
- search
Expand All @@ -37,6 +42,7 @@ plugins:
raise_error_excludes:
429: ["https://github.com/ponylang", "https://web.archive.org/*"]
404: ["https://github.com/ponylang"]
504: ["https://web.archive.org/*"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this appears unrelated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


theme:
name: material
Expand Down
2 changes: 1 addition & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ID = "Your_Site_ID"
[build]
base = ""
publish = "site/"
command = "mkdocs build"
command = "export PYTHONPATH=$PYTHONPATH:`pwd` && mkdocs build"

[build.environment]
PYTHON_VERSION = "3.8"
Expand Down
5 changes: 5 additions & 0 deletions ponylang_custom_fence.py
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
Loading