Skip to content

Commit 58e277e

Browse files
committed
inject format func with jinja
1 parent 1b5d3ea commit 58e277e

File tree

11 files changed

+142
-63
lines changed

11 files changed

+142
-63
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
max-line-length = 88
3+
ignore=E203,W503

.github/workflows/integration.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ on: [push, pull_request]
44

55
jobs:
66

7+
pre-commit:
8+
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: Set up Python 3.7
14+
uses: actions/setup-python@v1
15+
with:
16+
python-version: 3.7
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install --upgrade pip
20+
pip install -e .[code_style]
21+
- name: Run pre-commit
22+
run: |
23+
pre-commit run --all-files || ( git status --short ; git diff ; exit 1 )
24+
725
docs:
826

927
runs-on: ubuntu-latest
@@ -32,7 +50,7 @@ jobs:
3250
publish:
3351

3452
name: Publish to PyPi
35-
needs: [docs]
53+
needs: [pre-commit, docs]
3654
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
3755
runs-on: ubuntu-latest
3856
steps:

.pre-commit-config.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Install pre-commit hooks via
2+
# pre-commit install
3+
4+
exclude: >
5+
(?x)^(
6+
\.vscode/settings\.json
7+
)$
8+
9+
repos:
10+
11+
- repo: git://github.com/pre-commit/pre-commit-hooks
12+
rev: v2.2.3
13+
hooks:
14+
- id: check-json
15+
- id: check-yaml
16+
- id: end-of-file-fixer
17+
- id: trailing-whitespace
18+
- id: flake8
19+
20+
- repo: https://github.com/psf/black
21+
rev: stable
22+
hooks:
23+
- id: black

doc/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ help:
1717
# Catch-all target: route all unknown targets to Sphinx using the new
1818
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
1919
%: Makefile
20-
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
20+
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

doc/conf.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919

2020
# -- Project information -----------------------------------------------------
2121

22-
project = 'Sphinx Copybutton'
23-
copyright = '2018, Chris Holdgraf'
24-
author = 'Chris Holdgraf'
22+
project = "Sphinx Copybutton"
23+
copyright = "2018, Chris Holdgraf"
24+
author = "Chris Holdgraf"
2525

2626
# The short X.Y version
27-
version = ''
27+
version = ""
2828
# The full version, including alpha/beta/rc tags
29-
release = ''
29+
release = ""
3030

3131

3232
# -- General configuration ---------------------------------------------------
@@ -38,19 +38,19 @@
3838
# Add any Sphinx extension module names here, as strings. They can be
3939
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
4040
# ones.
41-
extensions = ['sphinx_copybutton']
41+
extensions = ["sphinx_copybutton"]
4242

4343
# Add any paths that contain templates here, relative to this directory.
44-
templates_path = ['_templates']
44+
templates_path = ["_templates"]
4545

4646
# The suffix(es) of source filenames.
4747
# You can specify multiple suffix as a list of string:
4848
#
4949
# source_suffix = ['.rst', '.md']
50-
source_suffix = '.rst'
50+
source_suffix = ".rst"
5151

5252
# The master toctree document.
53-
master_doc = 'index'
53+
master_doc = "index"
5454

5555
# The language for content autogenerated by Sphinx. Refer to documentation
5656
# for a list of supported languages.
@@ -62,18 +62,18 @@
6262
# List of patterns, relative to source directory, that match files and
6363
# directories to ignore when looking for source files.
6464
# This pattern also affects html_static_path and html_extra_path .
65-
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
65+
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
6666

6767
# The name of the Pygments (syntax highlighting) style to use.
68-
pygments_style = 'sphinx'
68+
pygments_style = "sphinx"
6969

7070

7171
# -- Options for HTML output -------------------------------------------------
7272

7373
# The theme to use for HTML and HTML Help pages. See the documentation for
7474
# a list of builtin themes.
7575
#
76-
html_theme = 'alabaster'
76+
html_theme = "alabaster"
7777
# html_theme = 'sphinx_rtd_theme'
7878

7979
# Theme options are theme-specific and customize the look and feel of a theme
@@ -85,7 +85,7 @@
8585
# Add any paths that contain custom static files (such as style sheets) here,
8686
# relative to this directory. They are copied after the builtin static files,
8787
# so a file named "default.css" will overwrite the builtin "default.css".
88-
html_static_path = ['_static']
88+
html_static_path = ["_static"]
8989

9090
# Custom sidebar templates, must be a dictionary that maps document names
9191
# to template names.
@@ -109,7 +109,7 @@
109109
# -- Options for HTMLHelp output ---------------------------------------------
110110

111111
# Output file base name for HTML help builder.
112-
htmlhelp_basename = 'SphinxCopybuttondoc'
112+
htmlhelp_basename = "SphinxCopybuttondoc"
113113

114114

115115
# -- Options for LaTeX output ------------------------------------------------
@@ -118,15 +118,12 @@
118118
# The paper size ('letterpaper' or 'a4paper').
119119
#
120120
# 'papersize': 'letterpaper',
121-
122121
# The font size ('10pt', '11pt' or '12pt').
123122
#
124123
# 'pointsize': '10pt',
125-
126124
# Additional stuff for the LaTeX preamble.
127125
#
128126
# 'preamble': '',
129-
130127
# Latex figure (float) alignment
131128
#
132129
# 'figure_align': 'htbp',
@@ -136,8 +133,13 @@
136133
# (source start file, target name, title,
137134
# author, documentclass [howto, manual, or own class]).
138135
latex_documents = [
139-
(master_doc, 'SphinxCopybutton.tex', 'Sphinx Copybutton Documentation',
140-
'Chris Holdgraf', 'manual'),
136+
(
137+
master_doc,
138+
"SphinxCopybutton.tex",
139+
"Sphinx Copybutton Documentation",
140+
"Chris Holdgraf",
141+
"manual",
142+
)
141143
]
142144

143145

@@ -146,8 +148,7 @@
146148
# One entry per manual page. List of tuples
147149
# (source start file, name, description, authors, manual section).
148150
man_pages = [
149-
(master_doc, 'sphinxcopybutton', 'Sphinx Copybutton Documentation',
150-
[author], 1)
151+
(master_doc, "sphinxcopybutton", "Sphinx Copybutton Documentation", [author], 1)
151152
]
152153

153154

@@ -157,7 +158,13 @@
157158
# (source start file, target name, title, author,
158159
# dir menu entry, description, category)
159160
texinfo_documents = [
160-
(master_doc, 'SphinxCopybutton', 'Sphinx Copybutton Documentation',
161-
author, 'SphinxCopybutton', 'One line description of project.',
162-
'Miscellaneous'),
161+
(
162+
master_doc,
163+
"SphinxCopybutton",
164+
"Sphinx Copybutton Documentation",
165+
author,
166+
"SphinxCopybutton",
167+
"One line description of project.",
168+
"Miscellaneous",
169+
)
163170
]

doc/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
sphinx
22

33
# Install ourselves
4-
.
4+
.

setup.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33

44
from setuptools import setup, find_packages
55

6-
if (os.path.isdir('clipboard.js') and
7-
not os.path.islink('sphinx_copybutton/_static/clipboard.min.js')):
6+
if os.path.isdir("clipboard.js") and not os.path.islink(
7+
"sphinx_copybutton/_static/clipboard.min.js"
8+
):
89
raise SystemExit("Error: Support for symbolic links is required")
910

10-
if (os.path.isdir('clipboard.js') and
11-
not os.path.isfile('clipboard.js/dist/clipboard.min.js')):
11+
if os.path.isdir("clipboard.js") and not os.path.isfile(
12+
"clipboard.js/dist/clipboard.min.js"
13+
):
1214
raise SystemExit(
1315
"""Error: clipboard.js submodule not available, run
1416
1517
git submodule update --init
16-
""")
18+
"""
19+
)
1720

18-
with open('./README.md', 'r') as ff:
21+
with open("./README.md", "r") as ff:
1922
readme_text = ff.read()
2023

2124
# Parse version
@@ -26,21 +29,27 @@
2629
version = line.split(" = ")[-1].strip('"')
2730

2831
setup(
29-
name='sphinx-copybutton',
32+
name="sphinx-copybutton",
3033
version=version,
3134
description="Add a copy button to each of your code cells.",
3235
long_description=readme_text,
33-
long_description_content_type='text/markdown',
34-
author='Executable Book Project',
36+
long_description_content_type="text/markdown",
37+
author="Executable Book Project",
3538
url="https://github.com/ExecutableBookProject/sphinx-copybutton",
36-
license='MIT License',
39+
license="MIT License",
3740
packages=find_packages(),
38-
package_data={'sphinx_copybutton': ['_static/copybutton.css',
39-
'_static/copybutton.js_t',
40-
'_static/copy-button.svg',
41-
'_static/clipboard.min.js']},
41+
package_data={
42+
"sphinx_copybutton": [
43+
"_static/copybutton.css",
44+
"_static/copybutton_funcs.js",
45+
"_static/copybutton.js_t",
46+
"_static/copy-button.svg",
47+
"_static/clipboard.min.js",
48+
]
49+
},
4250
classifiers=["License :: OSI Approved :: MIT License"],
43-
install_requires=[
44-
"sphinx>=1.8"
45-
]
51+
install_requires=["sphinx>=1.8"],
52+
extras_require={
53+
"code_style": ["flake8<3.8.0,>=3.7.0", "black", "pre-commit==1.17.0"],
54+
},
4655
)

sphinx_copybutton/__init__.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
11
"""A small sphinx extension to add "copy" buttons to code blocks."""
2-
import os
2+
from pathlib import Path
33
from sphinx.util import logging
44

55
__version__ = "0.2.12dev0"
66

77
logger = logging.getLogger(__name__)
88

9+
910
def scb_static_path(app):
10-
static_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '_static'))
11-
app.config.html_static_path.append(static_path)
11+
app.config.html_static_path.append(
12+
str(Path(__file__).parent.joinpath("_static").absolute())
13+
)
14+
1215

1316
def add_to_context(app, config):
1417
# Update the global context
15-
config.html_context.update({'copybutton_prompt_text': config.copybutton_prompt_text})
16-
config.html_context.update({'copybutton_only_copy_prompt_lines': config.copybutton_only_copy_prompt_lines})
17-
config.html_context.update({'copybutton_remove_prompts': config.copybutton_remove_prompts})
18-
config.html_context.update({'copybutton_image_path': config.copybutton_image_path})
19-
config.html_context.update({'copybutton_selector': config.copybutton_selector})
18+
config.html_context.update(
19+
{"copybutton_prompt_text": config.copybutton_prompt_text}
20+
)
21+
config.html_context.update(
22+
{"copybutton_only_copy_prompt_lines": config.copybutton_only_copy_prompt_lines}
23+
)
24+
config.html_context.update(
25+
{"copybutton_remove_prompts": config.copybutton_remove_prompts}
26+
)
27+
config.html_context.update({"copybutton_image_path": config.copybutton_image_path})
28+
config.html_context.update({"copybutton_selector": config.copybutton_selector})
29+
config.html_context.update(
30+
{
31+
"copybutton_format_func": Path(__file__)
32+
.parent.joinpath("_static", "copybutton_funcs.js")
33+
.read_text()
34+
}
35+
)
36+
2037

2138
def setup(app):
22-
logger.verbose('Adding copy buttons to code blocks...')
39+
logger.verbose("Adding copy buttons to code blocks...")
2340
# Add our static path
24-
app.connect('builder-inited', scb_static_path)
41+
app.connect("builder-inited", scb_static_path)
2542

2643
# configuration for this tool
2744
app.add_config_value("copybutton_prompt_text", "", "html")
@@ -34,9 +51,11 @@ def setup(app):
3451
app.connect("config-inited", add_to_context)
3552

3653
# Add relevant code to headers
37-
app.add_css_file('copybutton.css')
38-
app.add_js_file('clipboard.min.js')
54+
app.add_css_file("copybutton.css")
55+
app.add_js_file("clipboard.min.js")
3956
app.add_js_file("copybutton.js")
40-
return {"version": __version__,
41-
"parallel_read_safe": True,
42-
"parallel_write_safe": True}
57+
return {
58+
"version": __version__,
59+
"parallel_read_safe": True,
60+
"parallel_write_safe": True,
61+
}
Lines changed: 1 addition & 1 deletion
Loading

sphinx_copybutton/_static/copybutton.js_t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { formatCopyText } from "./copybutton_funcs";
2-
31
// Localization support
42
const messages = {
53
'en': {
@@ -84,6 +82,8 @@ const addCopyButtonToCodeCells = () => {
8482
codeCell.insertAdjacentHTML('afterend', clipboardButton(id))
8583
})
8684

85+
{{ copybutton_format_func }}
86+
8787
var copyTargetText = (target) => {
8888
var target = document.querySelector(trigger.attributes['data-clipboard-target'].value);
8989
var textContent = target.innerText.split('\n');

sphinx_copybutton/_static/copybutton_funcs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ export function formatCopyText(textContent, copybuttonPromptText, onlyCopyPrompt
3030
textContent = textContent.slice(0, -1)
3131
}
3232
return textContent
33-
}
33+
}

0 commit comments

Comments
 (0)