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

add copy-icon top-left badges code blocks #331

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ repos:
hooks:
- id: mypy
exclude: ^install-local.py$
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v7.0.0-alpha.3
hooks:
- id: eslint
args: [--fix]
- repo: local
hooks:
- id: scss-lint
Expand Down
1 change: 1 addition & 0 deletions assets/copy-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions assets/copyable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(function () {
function copyTextToClipboard(text) {
var textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.left = '-1';
textArea.style.top = '-1';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
}
var codeBlockElements = document.getElementsByClassName('copyable');
for (var i = 0; i < codeBlockElements.length; i++) {
var block = codeBlockElements[i];
var copyIcon = new Image(16, 16);
copyIcon.setAttribute('src', './assets/copy-icon.svg');
copyIcon.setAttribute('alt', 'copy');
copyIcon.setAttribute('title', 'copy to clipboard');
block.insertBefore(copyIcon, block.children[0]);
copyIcon.addEventListener('click', function(block) {
var text = block.getElementsByTagName('pre')[0].innerText;
copyTextToClipboard(text);
}.bind(null, block));
}
})();
1 change: 1 addition & 0 deletions base.mako
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@
ga('create', 'UA-104682927-1', 'auto');
ga('send', 'pageview');
</script>
<script src="assets/copyable.js"></script>
</body>
</html>
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
```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
```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
```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
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
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,28 @@
"license": "MIT",
"dependencies": {
"bootstrap-sass": "3.4.1"
},
"eslintConfig": {
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
4
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
},
"parserOptions": {
"ecmaVersion": 6
},
"env": {
"browser": true
}
}
}
2 changes: 2 additions & 0 deletions scss/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ h1,
.s2 { color: #8ae234; }
}

.copyable img { cursor: pointer; }

@media (max-width: $screen-md-max) {
.table-bordered td:first-child {
background: #f5f5f5;
Expand Down
18 changes: 14 additions & 4 deletions template_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shlex
import subprocess
import sys
from typing import Optional

import markdown_code_blocks
import markupsafe
Expand Down Expand Up @@ -99,13 +100,22 @@ def header(self, text: str, level: int, raw: str) -> str:
f'</h{level}> '
)

def block_code(self, code: str, lang: str) -> str:
def block_code(self, code: str, lang: Optional[str]) -> str:
copyable = False
if lang is not None:
copyable_s = '#copyable'
copyable = lang.endswith(copyable_s)
lang, _, _ = lang.partition(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:
return super().block_code(code, lang)
ret = super().block_code(code, lang)
if copyable:
return f'<div class="copyable">{ret}</div>'
else:
return ret


def md(s: str) -> str:
Expand Down