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

extend the --theme option to all themes #399

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Python Tests

on:
push:
branches:
- "**" # This will trigger on any branch push
pull_request:
branches:
- "**" # This will trigger on PRs targeting any branch

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.x" # This will use the latest stable Python 3

- name: Install dependencies
run: |
pip install -r requirements-test.txt

- name: Run tests
run: |
pytest -vv
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ To customize Grip, create `~/.grip/settings.py`, then add one or more of the fol

- `HOST`: The host to use when not provided as a CLI argument, `localhost` by default
- `PORT`: The port to use when not provided as a CLI argument, `6419` by default
- `THEME`: The github theme to use for styling. Choose one of:
- `light` -- the default
- `dark`
- `dark_colorblind`
- `dark_dimmed`
- `dark_high_contrast`
- `dark_tritanopia`
- `light_colorblind`
- `light_high_contrast`
- `light_tritanopia`
- `DEBUG`: Whether to use Flask's debugger when an error happens, `False` by default
- `DEBUG_GRIP`: Prints extended information when an error happens, `False` by default
- `API_URL`: Base URL for the github API, for example that of a Github Enterprise instance. `https://api.github.com` by default
Expand Down
8 changes: 4 additions & 4 deletions grip/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def create_app(path=None, user_content=False, context=None, username=None,
password=None, render_offline=False, render_wide=False,
render_inline=False, api_url=None, title=None, text=None,
autorefresh=None, quiet=None, theme='light', grip_class=None):
autorefresh=None, quiet=None, theme=None, grip_class=None):
"""
Creates a Grip application with the specified overrides.
"""
Expand Down Expand Up @@ -49,7 +49,7 @@ def create_app(path=None, user_content=False, context=None, username=None,
def serve(path=None, host=None, port=None, user_content=False, context=None,
username=None, password=None, render_offline=False,
render_wide=False, render_inline=False, api_url=None, title=None,
autorefresh=True, browser=False, quiet=None, theme='light', grip_class=None):
autorefresh=True, browser=False, quiet=None, theme=None, grip_class=None):
"""
Starts a server to render the specified file or directory containing
a README.
Expand All @@ -72,7 +72,7 @@ def clear_cache(grip_class=None):
def render_page(path=None, user_content=False, context=None,
username=None, password=None,
render_offline=False, render_wide=False, render_inline=False,
api_url=None, title=None, text=None, quiet=None, theme='light',
api_url=None, title=None, text=None, quiet=None, theme=None,
grip_class=None):
"""
Renders the specified markup text to an HTML page and returns it.
Expand All @@ -97,7 +97,7 @@ def render_content(text, user_content=False, context=None, username=None,
def export(path=None, user_content=False, context=None,
username=None, password=None, render_offline=False,
render_wide=False, render_inline=True, out_filename=None,
api_url=None, title=None, quiet=False, theme='light', grip_class=None):
api_url=None, title=None, quiet=False, theme=None, grip_class=None):
"""
Exports the rendered HTML to a file.
"""
Expand Down
26 changes: 18 additions & 8 deletions grip/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from .browser import start_browser_when_ready
from .constants import (
DEFAULT_GRIPHOME, DEFAULT_GRIPURL, STYLE_ASSET_URLS_INLINE_FORMAT)
from .exceptions import AlreadyRunningError, ReadmeNotFoundError
from .exceptions import AlreadyRunningError, ConfigurationError, ReadmeNotFoundError
from .readers import DirectoryReader
from .renderers import GitHubRenderer, ReadmeRenderer

Expand All @@ -43,7 +43,7 @@ class Grip(Flask):
"""
def __init__(self, source=None, auth=None, renderer=None,
assets=None, render_wide=None, render_inline=None, title=None,
autorefresh=None, quiet=None, theme='light', grip_url=None,
autorefresh=None, quiet=None, theme=None, grip_url=None,
static_url_path=None, instance_path=None, **kwargs):
# Defaults
if source is None or isinstance(source, str_type):
Expand Down Expand Up @@ -92,6 +92,8 @@ def __init__(self, source=None, auth=None, renderer=None,
password = self.config['PASSWORD']
if username or password:
auth = (username or '', password or '')
if theme is None:
theme = self.config['THEME']

# Thread-safe event to signal to the polling threads to exit
self._run_mutex = threading.Lock()
Expand Down Expand Up @@ -213,14 +215,22 @@ def _render_page(self, subpath=None):
if self.autorefresh
else None)

if self.theme == 'dark':
# the default values
data_color_mode = 'auto'
data_light_theme = 'light'
data_dark_theme = 'dark'

if self.theme.startswith('dark'):
data_color_mode = 'dark'
data_light_theme = 'light'
data_dark_theme = 'dark'
else:
data_dark_theme = self.theme
elif self.theme.startswith('light'):
data_color_mode = 'light'
data_light_theme = 'light'
data_dark_theme = 'dark'
data_light_theme = self.theme
elif self.theme == 'auto':
data_color_mode = self.theme
else:
raise ConfigurationError("unexpected theme: %r" % self.theme)


return render_template(
'index.html', filename=self.reader.filename_for(subpath),
Expand Down
42 changes: 28 additions & 14 deletions grip/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,44 @@
the file changes.
--quiet Do not print to the terminal.
--theme=<theme> Theme to view markdown file (light mode or dark mode).
Valid options ("light", "dark"). Default: "light"
Default: "auto" Valid options: (as of writing)
- `auto` -- defaults to `light`
- `light`
- `dark`
- `dark_colorblind`
- `dark_dimmed`
- `dark_high_contrast`
- `dark_tritanopia`
- `light_colorblind`
- `light_high_contrast`
- `light_tritanopia`
"""

from __future__ import annotations
from __future__ import print_function

import sys
import errno
import mimetypes
import socket
import errno
import sys
from getpass import getpass

from docopt import docopt
from getpass import getpass
from path_and_address import resolve, split_address
from path_and_address import resolve
from path_and_address import split_address

from . import __version__
from .api import clear_cache, export, serve
from .api import clear_cache
from .api import export
from .api import serve
from .exceptions import ReadmeNotFoundError


usage = '\n\n\n'.join(__doc__.split('\n\n\n')[1:])
version = 'Grip ' + __version__

# Note: GitHub supports more than light mode and dark mode (exp: light-high-constrast, dark-high-constrast).
VALID_THEME_OPTIONS = ['light', 'dark']
VALID_THEME_PREFIXES = ['auto', 'light', 'dark']


def main(argv=None, force_utf8=True, patch_svg=True):
"""
Expand Down Expand Up @@ -106,14 +120,14 @@ def main(argv=None, force_utf8=True, patch_svg=True):
password = getpass()

# Parse theme argument
if args['--theme']:
if args['--theme'] in VALID_THEME_OPTIONS:
theme: str = args['--theme']
theme: str | None = args["--theme"]
if theme:
for prefix in VALID_THEME_PREFIXES:
if theme.startswith(prefix):
break
else:
print('Error: valid options for theme argument are "light", "dark"')
print('Error: invalid value for theme: %r', theme)
return 1
else:
theme = 'light'

# Export to a file instead of running a server
if args['--export']:
Expand Down
3 changes: 3 additions & 0 deletions grip/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ def __str__(self):
return 'No README found at {0}'.format(self.path)

return self.strerror

class ConfigurationError(ValueError):
pass
1 change: 1 addition & 0 deletions grip/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
CACHE_DIRECTORY = 'cache-{version}'
AUTOREFRESH = True
QUIET = False
THEME = 'auto'


# Note: For security concerns, please don't save your GitHub password in your
Expand Down
Empty file added pyproject.toml
Empty file.
2 changes: 1 addition & 1 deletion tests/output/app/gfm-test-user-content.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/output/app/gfm-test-user-context.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading