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 regexp prompt matching (+ style and testing) #82

Merged
merged 12 commits into from
Jun 17, 2020
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
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 88
ignore=E203,W503
41 changes: 40 additions & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,45 @@ on: [push, pull_request]

jobs:

pre-commit:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7
- name: Install dependencies
run: |
python -m pip install --upgrade pip
git submodule update --init
pip install -e .[code_style]
- name: Run pre-commit
run: |
pre-commit run --all-files || ( git status --short ; git diff ; exit 1 )


test_js:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
env:
CI: true

docs:

runs-on: ubuntu-latest
Expand Down Expand Up @@ -32,7 +71,7 @@ jobs:
publish:

name: Publish to PyPi
needs: [docs]
needs: [pre-commit, docs]
if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,5 @@ _build/

# VS code config
.vscode

node_modules/
23 changes: 23 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Install pre-commit hooks via
# pre-commit install

exclude: >
(?x)^(
\.vscode/settings\.json
)$

repos:

- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v2.2.3
hooks:
- id: check-json
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- id: flake8

- repo: https://github.com/psf/black
rev: stable
hooks:
- id: black
2 changes: 1 addition & 1 deletion doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ help:
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
58 changes: 33 additions & 25 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

# -- Project information -----------------------------------------------------

project = 'Sphinx Copybutton'
copyright = '2018, Chris Holdgraf'
author = 'Chris Holdgraf'
project = "Sphinx Copybutton"
copyright = "2018, Executable Books Project"
author = "Executable Books Project"

# The short X.Y version
version = ''
version = ""
# The full version, including alpha/beta/rc tags
release = ''
release = ""


# -- General configuration ---------------------------------------------------
Expand All @@ -38,19 +38,19 @@
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['sphinx_copybutton']
extensions = ["sphinx_copybutton", "IPython.sphinxext.ipython_directive"]

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

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'
source_suffix = ".rst"

# The master toctree document.
master_doc = 'index'
master_doc = "index"

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

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
pygments_style = "sphinx"


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'
html_theme = "alabaster"
# html_theme = 'sphinx_rtd_theme'

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

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
Expand All @@ -98,7 +98,8 @@
# html_sidebars = {}

# CopyButton configuration
copybutton_prompt_text = ">>> "
copybutton_prompt_text = ">>> |\\\\$ |\\[\\d*\\]: |\\.\\.\\.: "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thought - could we simplify this a bit for people by allowing for a list instead of a single regexp? We could even allow for two configs like:

copybutton_prompt_start_strings = [">>>", "..."]
copybutton_prompt_start_regexes = ["\\[\\d*\\]"]

I suggest this because I think these should probably be the "defaults" anyway, and then if
people wanted to add a new "starting prompt" they can do so without needing to use regexes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I can see some merit, but I guess the main thing I don't like is that it breaks back compatibility: I like that the current solution uses the original copybutton_prompt_text, then just adds a flag to turn on regexes.
Also it may not be as a compatible with what I think could be a next step, to improve robustness (as mentioned in #82 (comment)); to have a mapping of selectors to prompts, e.g something like.

copybutton_prompt_text = {
   "div higlight-console": "\\\\$ ",
   "div higlight-python": ">>> ",
   "div highlight-ipython": "\\[\\d*\\]: |\\.\\.\\.: "
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm good point about backwards compatibility - we could keep the same original configuration and just add the regex one on top of it, and change behavior so that both will accept either a string (in which case it'll become a one-string list) or a list of strings?

for the dictionary extension, I agree that'd give us more precision. Do you think people will run into clashes between languages and starting characters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could keep the same original configuration and just add the regex one on top of it, and change behavior so that both will accept either a string (in which case it'll become a one-string list) or a list of strings?

This feels more complicated than just using regexes lol? Especially given its only something they have to setup once, then never need to think about again.

Do you think people will run into clashes between languages and starting characters?

Well its probably not massively likely; really depends on people's own documentation. But I personally would rather take the time to add this in my/our repos, just to be sure

copybutton_prompt_is_regexp = True
# Switches for testing but shouldn't be activated in the live docs
# copybutton_only_copy_prompt_lines = False
# copybutton_remove_prompts = False
Expand All @@ -109,7 +110,7 @@
# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'SphinxCopybuttondoc'
htmlhelp_basename = "SphinxCopybuttondoc"


# -- Options for LaTeX output ------------------------------------------------
Expand All @@ -118,15 +119,12 @@
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',

# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
Expand All @@ -136,8 +134,13 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'SphinxCopybutton.tex', 'Sphinx Copybutton Documentation',
'Chris Holdgraf', 'manual'),
(
master_doc,
"SphinxCopybutton.tex",
"Sphinx Copybutton Documentation",
"Executable Books Project",
"manual",
)
]


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


Expand All @@ -157,7 +159,13 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'SphinxCopybutton', 'Sphinx Copybutton Documentation',
author, 'SphinxCopybutton', 'One line description of project.',
'Miscellaneous'),
(
master_doc,
"SphinxCopybutton",
"Sphinx Copybutton Documentation",
author,
"SphinxCopybutton",
"One line description of project.",
"Miscellaneous",
)
]
86 changes: 86 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ use the following configuration:

copybutton_prompt_text = ">>> "

Using regexp prompt identifiers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If your prompts are more complex than a single string, then you can use a regexp to match with.

.. code-block:: python

copybutton_prompt_text = "\\[\\d*\\]: |\\.\\.\\.: "
copybutton_prompt_is_regexp = True

For example when using ipython prompts with continuations:

.. code-block:: restructuredtext

.. code-block:: ipython

[1]: first
...: continuation
output
[2]: second

.. code-block:: ipython

[1]: first
...: continuation
output
[2]: second

Configure whether *only* lines with prompts are copied
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -210,6 +238,64 @@ pull to your computer and install locally with ``pip``::

**Pull requests** and **Issues** are absolutely welcome!

The package is tested for three things (see ``.github/workflows/integration.yml``):

code style
----------

To adhere to this code style install the package with `pre-commit <https://pre-commit.com/>`__:

.. code-block:: console

$ pip install .[code_style]

Then you can run:

.. code-block:: console

$ pre-commit run --all

Or setup pre-commit to run on code commits:

.. code-block:: console

$ pre-commit install

JavaScript unit testing
-----------------------

Install the test dependencies with `npm <https://www.npmjs.com/>`__:

.. code-block:: console

$ npm install ci

Then run the tests:

.. code-block:: console

$ npm test

.. note::

NodeJS >= 12 is required

Documentation builds
--------------------

Install the package:

.. code-block:: console

$ pip install .

Then run the docs build:

.. code-block:: console

$ cd doc
$ make html

.. toctree::
:maxdepth: 1

Expand Down
3 changes: 2 additions & 1 deletion doc/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
sphinx
ipython

# Install ourselves
.
.
Loading