Skip to content

Commit

Permalink
Fix text watermark opacity issue
Browse files Browse the repository at this point in the history
Closes #35
  • Loading branch information
kallimachos committed Mar 2, 2021
1 parent ccdf1ec commit 5d9e026
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dist: xenial
language: python
python: "3.7"
python: "3.9"
install: pip install tox
script: tox
addons:
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sphinxmark
.. image:: https://img.shields.io/pypi/v/sphinxmark.svg?style=flat
:target: https://pypi.python.org/pypi/sphinxmark

.. image:: https://img.shields.io/badge/Python-3.7-brightgreen.svg?style=flat
.. image:: https://img.shields.io/badge/Python-3.9-brightgreen.svg?style=flat
:target: http://python.org

.. image:: http://img.shields.io/badge/license-apache-blue.svg?style=flat
Expand Down
2 changes: 1 addition & 1 deletion doc/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sphinxmark_div (string)
sphinx_rtd_theme -> ``sphinxmark_div = 'document'``

openstackdocstheme -> ``sphinxmark_div = 'docs-body'``

sphinx_book_theme -> ``sphinxmark_div = 'row#main-content'``

sphinxmark_border (string)
Expand Down
12 changes: 10 additions & 2 deletions doc/relnotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@
Release Notes
=============

Release 0.2.1
~~~~~~~~~~~~~

- `#36 <https://github.com/kallimachos/sphinxmark/pull/36>`_: update option documentation to
include ``sphinx_book_theme``. Thanks `@rscohn2 <https://github.com/rscohn2>`_.
- `#35 <https://github.com/kallimachos/sphinxmark/issues/35>`_: fix text watermark opacity issue.
Thanks `@chad-iris <https://github.com/chad-iris>`_.

Release 0.2.0
~~~~~~~~~~~~~

- `#34 <https://github.com/kallimachos/sphinxmark/pull/34>`_: Add support for
- `#34 <https://github.com/kallimachos/sphinxmark/pull/34>`_: add support for
Sphinx 2.0. Remove support for Python 2.7.


Release 0.1.19
~~~~~~~~~~~~~~

- `#29 <https://github.com/kallimachos/sphinxmark/pull/29>`_: Fix isort test
- `#29 <https://github.com/kallimachos/sphinxmark/pull/29>`_: fix isort test
error.
- `#28 <https://github.com/kallimachos/sphinxmark/pull/28>`_: remove invalid
argument to ``ImageDraw.text()`` method.
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

setup(
name='sphinxmark',
version='0.2.0',
version='0.2.1',
description='A Sphinx extension that enables watermarks for HTML output.',
long_description=long_description,
url='https://github.com/kallimachos/sphinxmark',
Expand All @@ -30,7 +30,7 @@
'Framework :: Sphinx :: Extension',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.9',
],

keywords='sphinx documentation watermark',
Expand Down
33 changes: 17 additions & 16 deletions sphinxmark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
https://github.com/kallimachos/sphinxmark
Copyright 2019 Brian Moss
Copyright 2021 Brian Moss
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -24,9 +24,8 @@
from shutil import copy

from bottle import TEMPLATE_PATH, template
from sphinx.util import logging

from PIL import Image, ImageDraw, ImageFont
from sphinx.util import logging

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -89,7 +88,9 @@ def createimage(app, srcdir, buildpath):
d.text((x, y), text, font=font, fill=color)

# set opacity
img.putalpha(app.config.sphinxmark_text_opacity)
img2 = img.copy()
img2.putalpha(app.config.sphinxmark_text_opacity)
img.paste(img2, img)

# rotate image
img = img.rotate(app.config.sphinxmark_text_rotation)
Expand Down Expand Up @@ -138,13 +139,9 @@ def getimage(app):

try:
copy(imagepath, buildpath)
except Exception:
message = ("Cannot find '%s'. Put watermark images in the "
"'_static' directory or specify the location using "
"'html_static_path'." % imagefile)
LOG.warning(message)
LOG.warning('Failed to add watermark.')
return
except FileNotFoundError:
LOG.info(' fail')
raise

return(buildpath, imagefile)

Expand All @@ -153,10 +150,14 @@ def watermark(app, env):
"""Add watermark."""
if app.config.sphinxmark_enable is True:
LOG.info('adding watermark...', nonl=True)
buildpath, imagefile = getimage(app)
cssname = buildcss(app, buildpath, imagefile)
app.add_css_file(cssname)
LOG.info(' done')
try:
buildpath, imagefile = getimage(app)
cssname = buildcss(app, buildpath, imagefile)
app.add_css_file(cssname)
LOG.info(' done')
except Exception as e:
LOG.warning(f'Failed to add watermark: {e}')
return


def setup(app):
Expand All @@ -181,7 +182,7 @@ def setup(app):
app.connect('env-updated', watermark)

return {
'version': '0.1.18',
'version': '0.2.1',
'parallel_read_safe': True,
'parallel_write_safe': True,
}
File renamed without changes
3 changes: 2 additions & 1 deletion tests/marktest/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
source_suffix = '.rst'
master_doc = 'index'
project = 'testmark'
copyright = '2019, Brian'
copyright = '2021, Brian'
author = 'Brian'
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
pygments_style = 'sphinx'
Expand All @@ -18,6 +18,7 @@
# html_theme = 'openstackdocs'
# html_theme_path = [openstackdocstheme.get_html_theme_path()]
htmlhelp_basename = 'testmarkdoc'
# html_static_path = ["alt_static"]

# -- Options for sphinxmark -----------------------------------------------
sphinxmark_enable = True
Expand Down
8 changes: 8 additions & 0 deletions tests/test-requirements.txt → tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
black
doc8
flake8-broken-line
flake8-bugbear
flake8-comprehensions
flake8-debugger
flake8-docstrings
flake8-isort
flake8-quotes
mypy
pyenchant
pytest
pytest-cov
Expand Down
32 changes: 16 additions & 16 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_fixed():


def test_image():
"""Test image."""
"""Test image in default `_static` path."""
image = 'new.png'
app = MakeApp(srcdir='tests/marktest', copy_srcdir_to_tmpdir=True,
confoverrides={'sphinxmark_image': image})
Expand All @@ -142,7 +142,7 @@ def test_imagefail():
app = MakeApp(srcdir='tests/marktest', copy_srcdir_to_tmpdir=True,
confoverrides={'sphinxmark_image': image})
assert app.config.sphinxmark_image == image
with pytest.raises(TypeError):
with pytest.raises(FileNotFoundError):
app.builder.build_all()


Expand All @@ -162,10 +162,10 @@ def test_textmark():
assert ('url("textmark_Mitaka.png")') in css


def test_static():
"""Test static."""
def test_alt_static_path():
"""Test static path override."""
image = 'sample.png'
htmlpath = ['static']
htmlpath = ['alt_static']
app = MakeApp(srcdir='tests/marktest', copy_srcdir_to_tmpdir=True,
confoverrides={'sphinxmark_image': image,
'html_static_path': htmlpath})
Expand All @@ -181,17 +181,17 @@ def test_static():
assert cssresult in css


def test_staticfail():
"""Test static not found raises TypeError."""
image = 'new.png'
htmlpath = ['static']
app = MakeApp(srcdir='tests/marktest', copy_srcdir_to_tmpdir=True,
confoverrides={'sphinxmark_image': image,
'html_static_path': htmlpath})
assert app.config.sphinxmark_image == image
assert app.config.html_static_path == htmlpath
with pytest.raises(TypeError):
app.builder.build_all()
# def test_alt_static_path_fail():
# """Test static path not found raises TypeError."""
# image = 'sample.png'
# htmlpath = ['fail']
# app = MakeApp(srcdir='tests/marktest', copy_srcdir_to_tmpdir=True,
# confoverrides={'sphinxmark_image': image,
# 'html_static_path': htmlpath})
# assert app.config.sphinxmark_image == image
# assert app.config.html_static_path == htmlpath
# with pytest.raises(TypeError):
# app.builder.build_all()


if __name__ == '__main__':
Expand Down
54 changes: 33 additions & 21 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
[tox]
minversion = 3.0
envlist = doc, bashate, isort, py37
; envlist = doc, bashate, isort, py3
envlist = py3
skipsdist = True

[testenv]
deps =
-r{toxinidir}/requirements.txt
-r{toxinidir}/tests/test-requirements.txt
-r{toxinidir}/tests/requirements.txt

whitelist_externals =
bash
make

commands = py.test
commands =
black -l 99 {toxinidir}/sphinxmark {toxinidir}/tests
mypy {toxinidir}/sphinxmark --config-file {toxinidir}/tox.ini
pytest {toxinidir}/sphinxmark {toxinidir}/tests

[testenv:bashate]
deps = bashate
Expand All @@ -26,10 +30,6 @@ commands = doc8 doc
make spelling -C {toxinidir}/doc
make html -C {toxinidir}/doc

[testenv:isort]
deps = isort
commands = isort -c -s .tox

[doc8]
# Ignore target directories
ignore-path = doc/_build*,.tox
Expand All @@ -39,12 +39,28 @@ extensions = .rst
max-line-length = 79

[flake8]
show-source = True
# B306 skipped because aiohttp uses BaseException.message
# H803 skipped (commit subject must not end with period)
# E123, E125 skipped as they are invalid PEP-8.
ignore = E123,E125,H803
# E123, E125, W503 skipped as they are invalid PEP-8.
# N802 skipped because we match method names to mixed case CAB API endpoints
# N803 skipped because some modules use mixed case arguments
# Ideal line length is < 80, but allow < 100
show-source = True
ignore = B306,E123,E125,H803,N802,N803,W503
builtins = _
exclude=.venv,.git,.tox,dist,*lib/python*,*egg,*figures/*
exclude = .venv,.git,.tox,dist,*lib/python*,*egg
max-line-length = 99
inline-quotes = double

[isort]
include_trailing_comma=True
line_length=99
multi_line_output=3
skip=.tox

[mypy]
ignore_missing_imports = True
disallow_untyped_defs = True

[pytest]
norecursedirs =
Expand All @@ -54,16 +70,15 @@ norecursedirs =
python_files=
*.py
addopts =
-vv
--cov-config tox.ini
--cov-report term-missing
--cov sphinxmark/
--doctest-modules
--flake8
-ra
--show-capture=no
--doctest-modules
--tb short
--flake8
--cov=sphinxmark/
--cov-report term-missing
--cov-config tox.ini
--ignore=setup.py
# -vv
filterwarnings =
ignore:invalid escape sequence*:DeprecationWarning
ignore:Using or importing the ABCs*:DeprecationWarning
Expand All @@ -74,8 +89,5 @@ filterwarnings =
[report]
# pytest-cov configuration
exclude_lines =
# Regexes for lines to exclude from consideration
pragma: no cover
# Have to re-enable the standard pragma
if __name__ == .__main__.:
# Don't include __main__ statements in coverage report

0 comments on commit 5d9e026

Please sign in to comment.