| 1 |
"""A small sphinx extension to add "copy" buttons to code blocks.""" |
| 2 |
from pathlib import Path |
| 3 |
from sphinx.util import logging |
| 4 |
|
| 5 |
__version__ = "0.5.2" |
| 6 |
|
| 7 |
logger = logging.getLogger(__name__) |
| 8 |
|
| 9 |
|
| 10 |
def scb_static_path(app): |
| 11 |
app.config.html_static_path.append( |
| 12 |
str(Path(__file__).parent.joinpath("_static").absolute()) |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
def add_to_context(app, config): |
| 17 |
# Update the global context |
| 18 |
config.html_context.update( |
| 19 |
{"copybutton_prompt_text": config.copybutton_prompt_text} |
| 20 |
) |
| 21 |
config.html_context.update( |
| 22 |
{"copybutton_prompt_is_regexp": config.copybutton_prompt_is_regexp} |
| 23 |
) |
| 24 |
config.html_context.update( |
| 25 |
{"copybutton_only_copy_prompt_lines": config.copybutton_only_copy_prompt_lines} |
| 26 |
) |
| 27 |
config.html_context.update( |
| 28 |
{"copybutton_remove_prompts": config.copybutton_remove_prompts} |
| 29 |
) |
| 30 |
config.html_context.update( |
| 31 |
{"copybutton_copy_empty_lines": config.copybutton_copy_empty_lines} |
| 32 |
) |
| 33 |
config.html_context.update( |
| 34 |
{ |
| 35 |
"copybutton_line_continuation_character": ( |
| 36 |
config.copybutton_line_continuation_character |
| 37 |
) |
| 38 |
} |
| 39 |
) |
| 40 |
config.html_context.update( |
| 41 |
{"copybutton_here_doc_delimiter": config.copybutton_here_doc_delimiter} |
| 42 |
) |
| 43 |
|
| 44 |
# Old image path deprecation |
| 45 |
# REMOVE after next release |
| 46 |
if config.copybutton_image_path: |
| 47 |
path = Path(app.srcdir) / config.copybutton_image_path |
| 48 |
logger.warning("copybutton_image_path is deprecated, use copybutton_image_svg") |
| 49 |
if not path.exists(): |
| 50 |
raise ValueError("copybutton_img_path does not exist") |
| 51 |
if not path.suffix == ".svg": |
| 52 |
raise ValueError("copybutton_img_path must be an SVG") |
| 53 |
config.copybutton_image_svg = path.read_text() |
| 54 |
|
| 55 |
config.html_context.update({"copybutton_image_svg": config.copybutton_image_svg}) |
| 56 |
config.html_context.update({"copybutton_selector": config.copybutton_selector}) |
| 57 |
config.html_context.update( |
| 58 |
{ |
| 59 |
"copybutton_format_func": Path(__file__) |
| 60 |
.parent.joinpath("_static", "copybutton_funcs.js") |
| 61 |
.read_text() |
| 62 |
.replace("export function", "function") |
| 63 |
} |
| 64 |
) |
| 65 |
config.html_context.update({"copybutton_exclude": config.copybutton_exclude}) |
| 66 |
|
| 67 |
|
| 68 |
def setup(app): |
| 69 |
logger.verbose("Adding copy buttons to code blocks...") |
| 70 |
# Add our static path |
| 71 |
app.connect("builder-inited", scb_static_path) |
| 72 |
|
| 73 |
# configuration for this tool |
| 74 |
app.add_config_value("copybutton_prompt_text", "", "html") |
| 75 |
app.add_config_value("copybutton_prompt_is_regexp", False, "html") |
| 76 |
app.add_config_value("copybutton_only_copy_prompt_lines", True, "html") |
| 77 |
app.add_config_value("copybutton_remove_prompts", True, "html") |
| 78 |
app.add_config_value("copybutton_copy_empty_lines", True, "html") |
| 79 |
app.add_config_value("copybutton_line_continuation_character", "", "html") |
| 80 |
app.add_config_value("copybutton_here_doc_delimiter", "", "html") |
| 81 |
app.add_config_value("copybutton_image_svg", "", "html") |
| 82 |
app.add_config_value("copybutton_selector", "div.highlight pre", "html") |
| 83 |
app.add_config_value("copybutton_exclude", ".linenos", "html") |
| 84 |
|
| 85 |
# DEPRECATE THIS AFTER THE NEXT RELEASE |
| 86 |
app.add_config_value("copybutton_image_path", "", "html") |
| 87 |
|
| 88 |
# Add configuration value to the template |
| 89 |
app.connect("config-inited", add_to_context) |
| 90 |
|
| 91 |
# Add relevant code to headers |
| 92 |
app.add_css_file("copybutton.css") |
| 93 |
app.add_js_file("clipboard.min.js") |
| 94 |
app.add_js_file("copybutton.js") |
| 95 |
return { |
| 96 |
"version": __version__, |
| 97 |
"parallel_read_safe": True, |
| 98 |
"parallel_write_safe": True, |
| 99 |
} |