From 1b3f7859fc0bd2b7ca94221db1ae5c62fe895f48 Mon Sep 17 00:00:00 2001 From: Brian Moss Date: Thu, 15 Sep 2016 17:13:51 +1000 Subject: [PATCH] Add support for border watermarks Left or right border can be used for watermark. Add fixed image option. Add text rotation option. Closes #14 --- doc/conf.py | 2 +- doc/index.rst | 31 +++++++- doc/spelling_wordlist.txt | 1 + setup.py | 5 +- sphinxmark/__init__.py | 144 +++++++++++++++++++++++--------------- sphinxmark/border.tpl | 7 ++ sphinxmark/watermark.tpl | 9 +-- tests/marktest/conf.py | 15 ++-- tests/marktest/index.rst | 61 ++++++++-------- tests/test.py | 84 +++++++++++++++------- 10 files changed, 232 insertions(+), 127 deletions(-) create mode 100644 sphinxmark/border.tpl diff --git a/doc/conf.py b/doc/conf.py index 72833d5..70dcce0 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -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' diff --git a/doc/index.rst b/doc/index.rst index 2c0df20..d7252a8 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -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 @@ -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 ~~~~~~~~~~~~~~~ diff --git a/doc/spelling_wordlist.txt b/doc/spelling_wordlist.txt index cbf9b8f..3653500 100644 --- a/doc/spelling_wordlist.txt +++ b/doc/spelling_wordlist.txt @@ -1,3 +1,4 @@ css png sphinxmark +viewport diff --git a/setup.py b/setup.py index aab4d9d..41891f8 100644 --- a/setup.py +++ b/setup.py @@ -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', @@ -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'], diff --git a/sphinxmark/__init__.py b/sphinxmark/__init__.py index 7727e48..89716da 100644 --- a/sphinxmark/__init__.py +++ b/sphinxmark/__init__.py @@ -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 @@ -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 @@ -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) @@ -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') @@ -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.') diff --git a/sphinxmark/border.tpl b/sphinxmark/border.tpl new file mode 100644 index 0000000..3a8ee52 --- /dev/null +++ b/sphinxmark/border.tpl @@ -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; +} \ No newline at end of file diff --git a/sphinxmark/watermark.tpl b/sphinxmark/watermark.tpl index 83697a6..91c17f4 100644 --- a/sphinxmark/watermark.tpl +++ b/sphinxmark/watermark.tpl @@ -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; +} \ No newline at end of file diff --git a/tests/marktest/conf.py b/tests/marktest/conf.py index 1c6eb40..63bd77c 100644 --- a/tests/marktest/conf.py +++ b/tests/marktest/conf.py @@ -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 diff --git a/tests/marktest/index.rst b/tests/marktest/index.rst index 6806f83..6ca2819 100644 --- a/tests/marktest/index.rst +++ b/tests/marktest/index.rst @@ -20,54 +20,55 @@ Usage #. Install sphinxmark: - .. code:: - - $ pip install sphinxmark + ``$ pip install sphinxmark`` #. Open ``conf.py`` and import sphinxmark: - .. code:: - - import sphinxmark + ``import sphinxmark`` #. Add watermark to the list of extensions in ``conf.py``: - .. code:: - - extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.doctest', - 'sphinxmark', - ] + ``extensions = ['sphinxmark']`` #. Specify a static directory in ``conf.py`` for your watermark files. If the path does not exist, the watermark extension creates the directory and populates it with ``watermark.css`` and ``watermark-draft.png``: - .. code:: + ``html_static_path = ['_static']`` - html_static_path = ['_static'] +#. Configure watermark in ``conf.py`` as required: - .. note:: + ``watermark_enable = True`` - ``watermark.css`` is recreated on each Sphinx run. Edits to this file - are not retained. +#. Put images in your static directory and edit ``watermark_image`` + to use custom watermarks. -#. Configure watermark in ``conf.py`` as required: - .. code:: +More paragraphs +~~~~~~~~~~~~~~~ - watermark_enable = True - watermark_image = 'default' - watermark_debug = False +Lorem ipsum dolor sit amet, per te enim fabulas invidunt, electram vulputate +cotidieque an has. Mei pericula maluisset concludaturque id, hinc quaeque +ullamcorper ei eam. Ius ne quot nihil iriure, legere alterum in cum. Cu sit +error altera eligendi, an omnes sapientem eam. Ipsum aeque in cum. Eum habeo +tacimates ut. -#. Put images in your static directory and edit ``watermark_image`` - to use custom watermarks. +Ex dolorem temporibus ius. Ad commodo repudiare evertitur eam, eirmod labores +an cum. Ne lorem affert vel, atqui omnes complectitur his et. Ex pri elitr +deleniti concludaturque, te nam choro molestie necessitatibus. +Eu vis eius aliquid. Idque quidam mea at, mea eu inani dolorum consequuntur. +Dolore scribentur est eu, pro in suas aperiam consequat, tale molestie +urbanitas ei ius. Ea mollis suscipiantur nec, qui cu delenit perfecto, sea +prima sonet soleat ad. Nam ea wisi recusabo, eu probo legimus vim. -.. important:: +Ea vero constituam vix. Cum at aliquip vituperatoribus. Ius enim simul ne, mel +ad tantas consectetuer. Debitis delectus percipitur has no, quo ad eius eripuit +mandamus. Ne quaeque disputationi his. - The extension provides a template css file that uses the specified image - as the watermark for any area defined as ``div.body``. Watermark will not - work with themes that do not place body content in ``div.body``. For this - reason, watermark does not work with ``sphinx_rtd_theme``. +Tollit mentitum vituperatoribus sea at. Cu his dicam voluptua temporibus, id +usu modus regione. Ea saperet principes abhorreant has, audiam denique +dissentiunt in eos. Te paulo soleat vituperatoribus pro, ut veri ridens vis. +Mei modus deleniti eleifend eu, veri maiorum oportere at ius, in diam facilis +nusquam ius. Ea noster omnesque recusabo mei, te utamur commune omnesque pri, +pro ei fabellas lucilius torquatos. diff --git a/tests/test.py b/tests/test.py index 579131a..021d8a9 100644 --- a/tests/test.py +++ b/tests/test.py @@ -9,13 +9,16 @@ # defaults = {'sphinxmark_enable': True, # 'sphinxmark_div': 'default', +# 'sphinxmark_border': None, # 'sphinxmark_repeat': True, +# 'sphinxmark_fixed': False, # 'sphinxmark_image': 'default', # 'sphinxmark_text': 'default', # 'sphinxmark_text_color': (255, 0, 0), # 'sphinxmark_text_size': 100, -# 'sphinxmark_text_opacity': 20 -# 'sphinxmark_text_spacing': 400} +# 'sphinxmark_text_opacity': 20, +# 'sphinxmark_text_spacing': 400, +# 'sphinxmark_text_rotation': 0} htmlfile = 'index.html' htmlresult = ('