diff --git a/.spelling-wordlist.txt b/.spelling-wordlist.txt index 9b45ba8d..1e6c08e1 100644 --- a/.spelling-wordlist.txt +++ b/.spelling-wordlist.txt @@ -10,6 +10,7 @@ booleans capybaras combinators compilable +dedent dereference deserialise deserialised @@ -52,6 +53,7 @@ polymorphism ponyc PonyCheck Ponylang +ponylang PonyTest POSIX preprocessor diff --git a/docs/getting-started/hello-world.md b/docs/getting-started/hello-world.md index 09cdcc1b..68144103 100644 --- a/docs/getting-started/hello-world.md +++ b/docs/getting-started/hello-world.md @@ -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 diff --git a/docs/getting-started/how-it-works.md b/docs/getting-started/how-it-works.md index 5f0c069c..65bc6d83 100644 --- a/docs/getting-started/how-it-works.md +++ b/docs/getting-started/how-it-works.md @@ -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. diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/lib/superfences_ponylang/__init__.py b/lib/superfences_ponylang/__init__.py new file mode 100644 index 00000000..8e7e9d66 --- /dev/null +++ b/lib/superfences_ponylang/__init__.py @@ -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""" +
+            
+            %s
+        
+ """ % highlighted + + return """ +
+        
+        %s
+    
+ """ % (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 \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index caed243a..ef6a0c7c 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -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 @@ -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/*"] theme: name: material diff --git a/netlify.toml b/netlify.toml index 1b6bb27d..ac671ccc 100644 --- a/netlify.toml +++ b/netlify.toml @@ -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" diff --git a/ponylang_custom_fence.py b/ponylang_custom_fence.py new file mode 100644 index 00000000..cf59eff2 --- /dev/null +++ b/ponylang_custom_fence.py @@ -0,0 +1,5 @@ +def format(source, language, css_class, options, md, classes=None, id_value='', attrs=None, **kwargs): + return '
%s
' % (language, class_name, options['opt'], html_escape(source)) + +def validate(language: str, options: dict) -> bool: + return True # TODO allowed attributes and values \ No newline at end of file