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 support for border watermarks #16

Merged
merged 1 commit into from
Sep 16, 2016
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
2 changes: 1 addition & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
# built documents.
#
# The short X.Y version.
version = '0.1.11'
version = '0.1.12'
# The full version, including alpha/beta/rc tags.
# release = 'beta'

Expand Down
31 changes: 30 additions & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,35 @@ sphinxmark_div (string)

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

sphinxmark_border (string)
- ``left`` - place watermark on left border
- ``right`` - place watermark on right border
- setting the ``sphinxmark_border`` option overrides the
``sphinxmark_repeat`` and ``sphinxmark_fixed`` options.
- when using a text watermark, adjust the ``sphinxmark_text`` options
to achieve the desired appearance.
- Default = None
- Example:

``sphinxmark_border = 'left'``

sphinxmark_repeat (bool)
- ``True`` - image repeats down the page
- ``False`` - image appears once at top of page
- Default = True
- Example:

``sphinxmark_repeat = True``

``sphinxmark_repeat = True``
sphinxmark_fixed (bool)
- ``True`` - watermark does not scroll with content
- ``False`` - watermark scrolls with content
- This option centers the watermark in the viewport, not the div
specified by ``sphinxmark_div``.
- Default = False
- Example:

``sphinxmark_fixed = False``

sphinxmark_image (string)
- image file in ``html_static_path`` directory to use as watermark
Expand Down Expand Up @@ -138,6 +161,12 @@ sphinxmark_text_spacing (int)

``sphinxmark_text_spacing = 400``

sphinxmark_text_rotation (int)
- Text watermark rotation
- Default = 0
- Example:

``sphinxmark_text_rotation = 90``

Troubleshooting
~~~~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions doc/spelling_wordlist.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
css
png
sphinxmark
viewport
5 changes: 3 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.1.11',
version='0.1.12',
description='A Sphinx extension that enables watermarks for HTML output.',
long_description=long_description,
url='https://github.com/kallimachos/sphinxmark',
Expand All @@ -40,7 +40,8 @@
packages=['sphinxmark'],

package_data={
'sphinxmark': ['watermark-draft.png', 'watermark.tpl', 'arial.ttf'],
'sphinxmark': ['watermark-draft.png', 'border.tpl',
'watermark.tpl', 'arial.ttf'],
},

install_requires=['bottle', 'Pillow'],
Expand Down
144 changes: 86 additions & 58 deletions sphinxmark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@
from shutil import copy


def buildcss(app, buildpath, imagefile):
"""Create CSS file."""
# set default values
div = 'body'
repeat = 'repeat-y'
position = 'center'
attachment = 'scroll'

if app.config.sphinxmark_div != 'default':
div = app.config.sphinxmark_div

if app.config.sphinxmark_repeat is False:
repeat = 'no-repeat'

if app.config.sphinxmark_fixed is True:
attachment = 'fixed'

border = app.config.sphinxmark_border
if border == 'left' or border == 'right':
css = template('border', div=div, image=imagefile, side=border)
else:
css = template('watermark', div=div, image=imagefile, repeat=repeat,
position=position, attachment=attachment)
app.debug('[sphinxmark] Template: ' + css)
cssname = 'sphinxmark.css'
cssfile = os.path.join(buildpath, cssname)

with open(cssfile, 'w') as f:
f.write(css)

return(cssname)


def createimage(app, srcdir, buildpath):
"""Create PNG image from string."""
text = app.config.sphinxmark_text
Expand All @@ -31,7 +64,7 @@ def createimage(app, srcdir, buildpath):
xsize, ysize = d.textsize(text, font)
app.debug('[sphinxmark] x = ' + str(xsize) + '\ny = ' + str(ysize))
x = (width / 2) - (xsize / 2)
y = 50
y = (height / 2) - (ysize / 2)

# add text to image
color = app.config.sphinxmark_text_color
Expand All @@ -40,6 +73,9 @@ def createimage(app, srcdir, buildpath):
# set opacity
img.putalpha(app.config.sphinxmark_text_opacity)

# rotate image
img = img.rotate(app.config.sphinxmark_text_rotation)

# save image
imagefile = 'textmark_' + text + '.png'
imagepath = os.path.join(buildpath, imagefile)
Expand All @@ -49,69 +85,58 @@ def createimage(app, srcdir, buildpath):
return(imagefile)


def watermark(app, env):
"""Add watermark."""
app.info('adding watermark...', nonl=True)
def getimage(app, env):
"""Get image file."""
# append source directory to TEMPLATE_PATH so template is found
srcdir = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_PATH.append(srcdir)
staticbase = '_static'
buildpath = os.path.join(app.outdir, staticbase)
try:
os.makedirs(buildpath)
except OSError:
if not os.path.isdir(buildpath):
raise

if app.config.sphinxmark_image == 'default':
imagefile = 'watermark-draft.png'
imagepath = os.path.join(srcdir, imagefile)
copy(imagepath, buildpath)
app.debug('[sphinxmark] Using default image: ' + imagefile)
elif app.config.sphinxmark_image == 'text':
imagefile = createimage(app, srcdir, buildpath)
app.debug('[sphinxmark] Image: ' + imagefile)
else:
imagefile = app.config.sphinxmark_image

if app.config.html_static_path:
staticpath = app.config.html_static_path[0]
else:
staticpath = '_static'

app.debug('[sphinxmark] static path: ' + staticpath)
imagepath = os.path.join(app.confdir, staticpath, imagefile)
app.debug('[sphinxmark] Imagepath: ' + imagepath)

if app.config.sphinxmark_enable is True:
# append source directory to TEMPLATE_PATH so template is found
srcdir = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_PATH.append(srcdir)
staticbase = '_static'
buildpath = os.path.join(app.outdir, staticbase)
try:
os.makedirs(buildpath)
except OSError:
if not os.path.isdir(buildpath):
raise

if app.config.sphinxmark_image == 'default':
imagefile = 'watermark-draft.png'
imagepath = os.path.join(srcdir, imagefile)
copy(imagepath, buildpath)
app.debug('[sphinxmark] Using default image: ' + imagefile)
elif app.config.sphinxmark_image == 'text':
imagefile = createimage(app, srcdir, buildpath)
app.debug('[sphinxmark] Image: ' + imagefile)
else:
imagefile = app.config.sphinxmark_image

if app.config.html_static_path:
staticpath = app.config.html_static_path[0]
else:
staticpath = '_static'

app.debug('[sphinxmark] static path: ' + staticpath)
imagepath = os.path.join(app.confdir, staticpath, imagefile)
app.debug('[sphinxmark] Imagepath: ' + imagepath)

try:
copy(imagepath, buildpath)
except:
message = ("Cannot find '" + imagefile + "'. Put watermark " +
"images in the '_static' directory or " +
"specify the location using 'html_static_path'.")
app.warn(message)
app.warn('Failed to add watermark.')
return

if app.config.sphinxmark_div == 'default':
div = 'body'
else:
div = app.config.sphinxmark_div
except:
message = ("Cannot find '" + imagefile + "'. Put watermark " +
"images in the '_static' directory or " +
"specify the location using 'html_static_path'.")
app.warn(message)
app.warn('Failed to add watermark.')
return

if app.config.sphinxmark_repeat is True:
repeat = 'repeat-y'
else:
repeat = 'no-repeat'
return(buildpath, imagefile)

css = template('watermark', div=div, image=imagefile, repeat=repeat)
app.debug('[sphinxmark] Template: ' + css)
cssname = 'sphinxmark.css'
cssfile = os.path.join(buildpath, cssname)

with open(cssfile, 'w') as f:
f.write(css)
def watermark(app, env):
"""Add watermark."""
if app.config.sphinxmark_enable is True:
app.info('adding watermark...', nonl=True)
buildpath, imagefile = getimage(app, env)
cssname = buildcss(app, buildpath, imagefile)
app.add_stylesheet(cssname)
app.info(' done')

Expand All @@ -121,13 +146,16 @@ def setup(app):
try:
app.add_config_value('sphinxmark_enable', False, 'html')
app.add_config_value('sphinxmark_div', 'default', 'html')
app.add_config_value('sphinxmark_border', None, 'html')
app.add_config_value('sphinxmark_repeat', True, 'html')
app.add_config_value('sphinxmark_fixed', False, 'html')
app.add_config_value('sphinxmark_image', 'default', 'html')
app.add_config_value('sphinxmark_text', 'default', 'html')
app.add_config_value('sphinxmark_text_color', (255, 0, 0), 'html')
app.add_config_value('sphinxmark_text_size', 100, 'html')
app.add_config_value('sphinxmark_text_opacity', 20, 'html')
app.add_config_value('sphinxmark_text_spacing', 400, 'html')
app.add_config_value('sphinxmark_text_rotation', 0, 'html')
app.connect('env-updated', watermark)
except:
app.warn('Failed to add watermark.')
Expand Down
7 changes: 7 additions & 0 deletions sphinxmark/border.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
div.{{div}} {
border-{{side}}: 100px solid transparent;
padding: 15px;
-webkit-border-image: url({{image}}) 20% round;
-o-border-image: url({{image}}) 20% round;
border-image: url({{image}}) 20% 100% repeat;
}
9 changes: 5 additions & 4 deletions sphinxmark/watermark.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
div.{{div}} {
background-image: url("{{ image }}") !important;
background-repeat: {{ repeat }} !important;
background-position: center top !important;
}
background-image: url("{{image}}") !important;
background-repeat: {{repeat}} !important;
background-position: {{position}} top !important;
background-attachment: {{attachment}} !important;
}
15 changes: 9 additions & 6 deletions tests/marktest/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@

# Options for sphinxmark
sphinxmark_enable = True
# sphinxmark_div = 'default'
# sphinxmark_repeat = True
# sphinxmark_image = 'default'
# sphinxmark_text = 'default'
# sphinxmark_div = 'docs-body'
# sphinxmark_border = 'left'
# sphinxmark_repeat = False
# sphinxmark_fixed = True
# sphinxmark_image = 'text'
# sphinxmark_text = 'Mitaka'
# sphinxmark_text_color = (255, 0, 0)
# sphinxmark_text_size = 100
# sphinxmark_text_opacity = 20
# sphinxmark_text_spacing = 400
# sphinxmark_text_opacity = 50
# sphinxmark_text_spacing = 600
# sphinxmark_text_rotation = 90
Loading