Skip to content

Commit

Permalink
fix: accept only lang#copyable in mako template
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarnajjar committed Apr 15, 2020
1 parent d8ded43 commit 15aea1b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 57 deletions.
8 changes: 4 additions & 4 deletions index.mako
Original file line number Diff line number Diff line change
Expand Up @@ -1501,27 +1501,27 @@ you use pre-commit!
- Markdown:
```md # copyable
```md#copyable
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
```
- HTML:
```html # copyable
```html#copyable
<a href="https://github.com/pre-commit/pre-commit"><img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white" alt="pre-commit" style="max-width:100%;"></a>
```
- reStructuredText:
```rst # copyable
```rst#copyable
.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white
:target: https://github.com/pre-commit/pre-commit
:alt: pre-commit
```
- AsciiDoc:
``` # copyable
``` #copyable
image:https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white[pre-commit, link=https://github.com/pre-commit/pre-commit]
```
Expand Down
6 changes: 4 additions & 2 deletions scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ h1,

// vs.css: https://github.com/richleland/pygments-css
.highlight {
img { cursor: pointer; }

.hll { background-color: #ffc; }
.c { color: #008000; } /* Comment */
.err { border: 1px solid #ff0; } /* Error */
Expand Down Expand Up @@ -204,6 +202,10 @@ h1,
.s2 { color: #8ae234; }
}

.copyable {
img { cursor: pointer; }
}

@media (max-width: $screen-md-max) {
.table-bordered td:first-child {
background: #f5f5f5;
Expand Down
66 changes: 15 additions & 51 deletions template_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import shlex
import subprocess
import sys
from typing import Any
from typing import Type

import markdown_code_blocks
import markupsafe
import mistune


ID_RE = re.compile(r'\[\]\(#([a-z0-9-]+)\)')
SPECIAL_CHARS_RE = re.compile('[^a-z0-9 _-]')
Expand Down Expand Up @@ -88,46 +86,6 @@ def _render_cmd(code: str) -> str:
return str(md(f'```pre-commit\n$ {cmdstr}\n{output}```'))


class Lexer(mistune.BlockLexer):

def parse_fences(self, m: Any) -> None:
self.tokens.append({
'type': 'code',
'lang': m.group(3),
'text': m.group(6),
})


class Grammar(mistune.BlockGrammar):
fences = re.compile(
r'^( *)(`{3,}|~{3,}) *(([^`\s]+]?)?( +#\s+[^`\s]+?)?)? *\n'
r'([\s\S]*?)\n'
r'\1\2 *(?:\n+|$)',
)


def _highlight(
doc: str,
Renderer: Type[mistune.Renderer],
) -> str:
return mistune.Markdown(
Renderer(escape=True, hard_wrap=False),
block=Lexer(Grammar())
)(doc)


def _with_class(original: str, new_class: str) -> str:
new_class_already_there = False
pattern = re.compile(r'<div class="([^"]+)"')
classes_found = pattern.search(original)
if classes_found:
new_class_already_there = new_class in classes_found.group(1).split()
if new_class_already_there:
return original
else:
return re.sub(pattern, fr'<div class="\1 {new_class}"', original)


class Renderer(markdown_code_blocks.CodeRenderer):
def header(self, text: str, level: int, raw: str) -> str:
match = ID_RE.search(raw)
Expand All @@ -142,19 +100,25 @@ def header(self, text: str, level: int, raw: str) -> str:
)

def block_code(self, code: str, lang: str) -> str:
if lang and '#' in lang:
lang, new_class = [c.strip() for c in lang.split('#')]
original_output = self.block_code(code, lang)
return _with_class(original_output, new_class)
copyable = False
if lang:
copyable_s = '#copyable'
copyable = lang.endswith(copyable_s)
if copyable:
lang, _, _ = lang.rpartition(copyable_s)
if lang == 'table':
return _render_table(code)
ret = _render_table(code)
elif lang == 'cmd':
return _render_cmd(code)
ret = _render_cmd(code)
else:
ret = super().block_code(code, lang)
if copyable:
return f'<div class="copyable">{ret}</div>'
else:
return super().block_code(code, lang)
return ret


def md(s: str) -> str:
html = _highlight(s, Renderer=Renderer)
html = markdown_code_blocks.highlight(s, Renderer=Renderer)
# manually bless the highlighted output.
return markupsafe.Markup(html)

0 comments on commit 15aea1b

Please sign in to comment.