Commits

wuhan88  committed 7855529 Draft

add youku plugin for sphinx

  • Participants

Comments (0)

Files changed (9)

+include README
+include LICENSE
+include CHANGES.*
+sphinxcontrib.youku
+=====================
+
+This module defines a directive, `youku`.  It takes a single, required
+argument, a YouKu video ID::
+
+    ..  youku:: XOTc1ODUyODA
+
+The referenced video will be embedded into HTML output.  By default, the
+embedded video will be sized for 720p content.  To control this, the
+parameters "aspect", "width", and "height" may optionally be provided::
+
+    ..  youku:: XOTc1ODUyODA
+        :width: 640
+        :height: 480
+
+    ..  youku:: XOTc1ODUyODA
+        :aspect: 4:3
+
+    ..  youku:: XOTc1ODUyODA
+        :width: 100%
+
+    ..  youku:: XOTc1ODUyODA
+        :height: 200px
+
+..  -*- mode: rst; fill-column: 72 -*-

File build/lib/sphinxcontrib/__init__.py

+# -*- coding: utf-8 -*-
+"""
+    sphinxcontrib
+    ~~~~~~~~~~~~~
+
+    This package is a namespace package that contains all extensions
+    distributed in the ``sphinx-contrib`` distribution.
+
+    :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+    :license: BSD, see LICENSE for details.
+"""
+
+__import__('pkg_resources').declare_namespace(__name__)
+

File build/lib/sphinxcontrib/youku.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from __future__ import division
+
+import re
+from docutils import nodes
+from docutils.parsers.rst import directives
+from sphinx.util.compat import Directive
+
+CONTROL_HEIGHT = 30
+
+def get_size(d, key):
+    if key not in d:
+        return None
+    m = re.match("(\d+)(|%|px)$", d[key])
+    if not m:
+        raise ValueError("invalid size %r" % d[key])
+    return int(m.group(1)), m.group(2) or "px"
+
+def css(d):
+    return "; ".join(sorted("%s: %s" % kv for kv in d.iteritems()))
+
+class youku(nodes.General, nodes.Element): pass
+
+def visit_youku_node(self, node):
+    aspect = node["aspect"]
+    width = node["width"]
+    height = node["height"]
+
+    if aspect is None:
+        aspect = 16, 9
+
+    if (height is None) and (width is not None) and (width[1] == "%"):
+        style = {
+            "padding-top": "%dpx" % CONTROL_HEIGHT,
+            "padding-bottom": "%f%%" % (width[0] * aspect[1] / aspect[0]),
+            "width": "%d%s" % width,
+            "position": "relative",
+        }
+        self.body.append(self.starttag(node, "div", style=css(style)))
+        style = {
+            "position": "absolute",
+            "top": "0",
+            "left": "0",
+            "width": "100%",
+            "height": "100%",
+            "border": "0",
+        }
+        attrs = {
+                "src": "http://player.youku.com/embed/%s" % node["id"],
+            "style": css(style),
+        }
+        self.body.append(self.starttag(node, "iframe", **attrs))
+        self.body.append("</iframe></div>")
+    else:
+        if width is None:
+            if height is None:
+                width = 560, "px"
+            else:
+                width = height[0] * aspect[0] / aspect[1], "px"
+        if height is None:
+            height = width[0] * aspect[1] / aspect[0], "px"
+        style = {
+            "width": "%d%s" % width,
+            "height": "%d%s" % (height[0] + CONTROL_HEIGHT, height[1]),
+            "border": "0",
+        }
+        attrs = {
+                "src": "http://player.youku.com/embed/%s" % node["id"],
+            "style": css(style),
+        }
+        self.body.append(self.starttag(node, "iframe", **attrs))
+        self.body.append("</iframe>")
+
+def depart_youku_node(self, node):
+    pass
+
+class YouKu(Directive):
+    has_content = True
+    required_arguments = 1
+    optional_arguments = 0
+    final_argument_whitespace = False
+    option_spec = {
+        "width": directives.unchanged,
+        "height": directives.unchanged,
+        "aspect": directives.unchanged,
+    }
+
+    def run(self):
+        if "aspect" in self.options:
+            aspect = self.options.get("aspect")
+            m = re.match("(\d+):(\d+)", aspect)
+            if m is None:
+                raise ValueError("invalid aspect ratio %r" % aspect)
+            aspect = tuple(int(x) for x in m.groups())
+        else:
+            aspect = None
+        width = get_size(self.options, "width")
+        height = get_size(self.options, "height")
+        return [youku(id=self.arguments[0], aspect=aspect, width=width, height=height)]
+
+def setup(app):
+    app.add_node(youku, html=(visit_youku_node, depart_youku_node))
+    app.add_directive("youku", YouKu)
+[egg_info]
+tag_build = dev
+tag_date = true
+
+[aliases]
+release = egg_info -RDb ''
+# -*- coding: utf-8 -*-
+
+from setuptools import setup, find_packages
+
+long_desc = '''
+This package contains the youku Sphinx extension.
+
+The extension defines a directive, "youku", for embedding YouKu
+videos.
+'''
+
+requires = ['Sphinx>=0.6']
+
+setup(
+    name='sphinxcontrib-youku',
+    version='1.0',
+    url='http://bitbucket.org/birkenfeld/sphinx-contrib',
+    download_url='http://pypi.python.org/pypi/sphinxcontrib-youku',
+    license='BSD',
+    author='Jason Wu',
+    author_email='jasonwux@gmail.com',
+    description='Sphinx "youku" extension',
+    long_description=long_desc,
+    zip_safe=False,
+    classifiers=[
+        'Development Status :: 4 - Beta',
+        'Environment :: Console',
+        'Environment :: Web Environment',
+        'Intended Audience :: Developers',
+        'License :: OSI Approved :: BSD License',
+        'Operating System :: OS Independent',
+        'Programming Language :: Python',
+        'Topic :: Documentation',
+        'Topic :: Utilities',
+    ],
+    platforms='any',
+    packages=find_packages(),
+    include_package_data=True,
+    install_requires=requires,
+    namespace_packages=['sphinxcontrib'],
+)

File sphinxcontrib/__init__.py

+# -*- coding: utf-8 -*-
+"""
+    sphinxcontrib
+    ~~~~~~~~~~~~~
+
+    This package is a namespace package that contains all extensions
+    distributed in the ``sphinx-contrib`` distribution.
+
+    :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS.
+    :license: BSD, see LICENSE for details.
+"""
+
+__import__('pkg_resources').declare_namespace(__name__)
+

File sphinxcontrib/youku.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from __future__ import division
+
+import re
+from docutils import nodes
+from docutils.parsers.rst import directives
+from sphinx.util.compat import Directive
+
+CONTROL_HEIGHT = 30
+
+def get_size(d, key):
+    if key not in d:
+        return None
+    m = re.match("(\d+)(|%|px)$", d[key])
+    if not m:
+        raise ValueError("invalid size %r" % d[key])
+    return int(m.group(1)), m.group(2) or "px"
+
+def css(d):
+    return "; ".join(sorted("%s: %s" % kv for kv in d.iteritems()))
+
+class youku(nodes.General, nodes.Element): pass
+
+def visit_youku_node(self, node):
+    aspect = node["aspect"]
+    width = node["width"]
+    height = node["height"]
+
+    if aspect is None:
+        aspect = 16, 9
+
+    if (height is None) and (width is not None) and (width[1] == "%"):
+        style = {
+            "padding-top": "%dpx" % CONTROL_HEIGHT,
+            "padding-bottom": "%f%%" % (width[0] * aspect[1] / aspect[0]),
+            "width": "%d%s" % width,
+            "position": "relative",
+        }
+        self.body.append(self.starttag(node, "div", style=css(style)))
+        style = {
+            "position": "absolute",
+            "top": "0",
+            "left": "0",
+            "width": "100%",
+            "height": "100%",
+            "border": "0",
+        }
+        attrs = {
+                "src": "http://player.youku.com/embed/%s" % node["id"],
+            "style": css(style),
+        }
+        self.body.append(self.starttag(node, "iframe", **attrs))
+        self.body.append("</iframe></div>")
+    else:
+        if width is None:
+            if height is None:
+                width = 560, "px"
+            else:
+                width = height[0] * aspect[0] / aspect[1], "px"
+        if height is None:
+            height = width[0] * aspect[1] / aspect[0], "px"
+        style = {
+            "width": "%d%s" % width,
+            "height": "%d%s" % (height[0] + CONTROL_HEIGHT, height[1]),
+            "border": "0",
+        }
+        attrs = {
+                "src": "http://player.youku.com/embed/%s" % node["id"],
+            "style": css(style),
+        }
+        self.body.append(self.starttag(node, "iframe", **attrs))
+        self.body.append("</iframe>")
+
+def depart_youku_node(self, node):
+    pass
+
+class YouKu(Directive):
+    has_content = True
+    required_arguments = 1
+    optional_arguments = 0
+    final_argument_whitespace = False
+    option_spec = {
+        "width": directives.unchanged,
+        "height": directives.unchanged,
+        "aspect": directives.unchanged,
+    }
+
+    def run(self):
+        if "aspect" in self.options:
+            aspect = self.options.get("aspect")
+            m = re.match("(\d+):(\d+)", aspect)
+            if m is None:
+                raise ValueError("invalid aspect ratio %r" % aspect)
+            aspect = tuple(int(x) for x in m.groups())
+        else:
+            aspect = None
+        width = get_size(self.options, "width")
+        height = get_size(self.options, "height")
+        return [youku(id=self.arguments[0], aspect=aspect, width=width, height=height)]
+
+def setup(app):
+    app.add_node(youku, html=(visit_youku_node, depart_youku_node))
+    app.add_directive("youku", YouKu)
+## configuration for tox <http://codespeak.net/tox/>
+
+## tox automates running certain tasks within virtualenvs.  The following
+## tox configuration outlines a basic setup for running unit tests and
+## building sphinx docs in separate virtual environments.  Give it a try!
+
+[tox]
+envlist=python,doc
+
+# test running
+[testenv:python]
+deps=
+    ## if you use nose for test running
+    # nose
+    ## if you use py.test for test running
+    # pytest
+commands=
+    ## run tests with py.test
+    # py.test []
+    ## run tests with nose
+    # nose
+
+[testenv:doc]
+deps=
+    sphinx
+    # add all Sphinx extensions and other dependencies required to build your docs
+commands=
+    ## test links
+    # sphinx-build -W -b linkcheck -d {envtmpdir}/doctrees doc {envtmpdir}/linkcheck
+    ## test html output
+    # sphinx-build -W -b html -d {envtmpdir}/doctrees doc {envtmpdir}/html
+