Skip to content

Commit

Permalink
Merge pull request #5 from rzk-lang/fix-svg-rendering
Browse files Browse the repository at this point in the history
Fix SVG rendering
  • Loading branch information
fizruk authored Nov 2, 2023
2 parents e63d3d7 + 142dd82 commit 86622d3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
python-version: '3.12'

- name: Install pypa/build
run: python3 -m pip install build --user
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to the "mkdocs-plugin-rzk" plugin will be documented in this

Check [Keep a Changelog](https://keepachangelog.com/) for recommendations on how to structure this file.

## v0.1.4 - 2023.11.02

### Fixed

- Rendering topes as SVGs was broken by the `v0.6.5` release of Rzk. This patch fixes it while also improving performance.

## v0.1.3 - 2023.09.04

### Added
Expand Down
42 changes: 20 additions & 22 deletions rzk/generate_svgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ class RzkPluginConfig(base.Config):

class RzkPlugin(BasePlugin[RzkPluginConfig]):
def __init__(self):
self.rzk_code_block = re.compile(r'(^```\s*rzk[^\n]*\s+(.*?)\s+^```)', flags=re.MULTILINE | re.DOTALL)
self.define_svg = re.compile(
r'\[ \d+ out of \d+ \] Checking (#def(?:ine)? \S+)' # Match the definition name
r'(?!\s*\[ \d+ out of \d+ \]).+?' # Eat everything up to <svg> or next definition
r'(<svg.*?<\/svg>)', # Match the SVG
flags=re.MULTILINE | re.DOTALL)
self.define_name = re.compile(r'(<span class="nf">(.*?)</span>)')
self.svg_element = re.compile(r'^(<svg.*?</svg>)', flags=re.MULTILINE | re.DOTALL)
self.rzk_installed = True

def on_startup(self, *, command: Literal['build', 'gh-deploy', 'serve'], dirty: bool) -> None:
Expand All @@ -38,27 +41,22 @@ def on_page_markdown(self, md: str, page: Page, config: MkDocsConfig, files: Fil
if not page.file.src_uri.endswith('.rzk.md'): return md
if not self.rzk_installed: return md
logger.info('Inserting SVG diagrams in ' + page.file.src_uri)
# Some snippets can depend on terms defined in previous snippets, so we need to store them all
previous_snippets = ['#lang rzk-1\n#set-option "render" = "svg"\n\n']
# Since each snippet will contain previous ones, the previously printed SVGs should not be repeated
previous_svgs: set[str] = set()
code_blocks = self.rzk_code_block.findall(md)
for (fenced_block, code) in code_blocks:
previous_snippets.append(code.replace('#lang rzk-1', ''))
full_code = '\n'.join(previous_snippets).encode()
process = subprocess.run([self.config.path, 'typecheck'], capture_output=True, input=full_code)
if process.returncode != 0:
logger.debug(process.stderr.decode())
continue
process = subprocess.run([self.config.path, 'typecheck', page.file.abs_src_path], capture_output=True)
output = process.stderr.decode()
if process.returncode != 0:
logger.debug(output)
return md

output = process.stderr.decode()
svgs: list[str] = self.svg_element.findall(output)
# One snippet might have more than one diagram, so we shouldn't just use svgs[-1]
# However, there is probably a more efficient way than iterating over all matches everytime
for svg in svgs:
if svg in previous_svgs: continue
previous_svgs.add(svg)
md = md.replace(fenced_block, svg + '\n\n' + fenced_block)
svgs = self.define_svg.findall(output)
for (name, svg) in svgs:
# Find the code block that includes the given name and prepend it with the svg
fenced_block_pattern = rf'```rzk[^`]*{re.escape(name)}\s[^`]*```'
match = re.search(fenced_block_pattern, md)
if match is None:
logger.warning(f'Failed to find the code block containing "{name}"')
continue
fenced_block = match[0]
md = md.replace(fenced_block, svg + '\n\n' + fenced_block)

return md

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='mkdocs-plugin-rzk',
version='0.1.3',
version='0.1.4',
description='A MkDocs plugin for Literate Rzk',
long_description='This plugin automates the generation of SVG renderings for code snippets in Literate Rzk Markdown files rendered using MkDocs.',
keywords='mkdocs',
Expand Down

0 comments on commit 86622d3

Please sign in to comment.