Source

htraf-packaging / doc / extensions / sphinxext_htrafdemo / __init__.py

from docutils import nodes
from docutils.parsers.rst import Directive, directives
from sphinx.util.osutil import copyfile

from urllib import urlopen
import os.path


JQUERY_URL = "http://code.jquery.com/jquery-1.5.2.min.js"


class DemoDirective(Directive):

    has_content = True
    option_spec = {
            'source': directives.flag,
            'hide': directives.flag,
    }

    def run(self):
        wrapper = nodes.compound(classes=['demo-block'])
        data = "\n".join(self.content)
        demo = nodes.raw(data, data,
                         format='html',
                         classes=['demo-area'])
        source = nodes.literal_block(data, data,
                                     language='html',
                                     classes=['demo-source'])
        switch = nodes.literal(classes=['demo-switch'])
        if 'source' not in self.options:
            if 'hide' not in self.options:
                switch += nodes.Text("[- view source]")
            else:
                switch += nodes.Text("[+ view source]")
                source['classes'].append('demo-hide')
            wrapper += demo
            wrapper += switch
            wrapper += source
        else:
            if 'hide' not in self.options:
                switch += nodes.Text("[- view demo]")
            else:
                switch += nodes.Text("[+ view demo]")
                demo['classes'].append('demo-hide')
            wrapper += source
            wrapper += switch
            wrapper += demo
        return [wrapper]


def copy_static(app, exception):
    if app.builder.name != 'html' or exception:
        return
    src_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'static')
    dst_dir = os.path.join(app.builder.outdir, '_static')
    for filename in os.listdir(src_dir):
        src = os.path.join(src_dir, filename)
        if not os.path.isfile(src):
            continue
        dst = os.path.join(dst_dir, filename)
        copyfile(src, dst)
    data = urlopen(JQUERY_URL).read()
    open(os.path.join(dst_dir, 'jquery.js'), 'w').write(data)


def setup(app):
    app.add_directive('demo', DemoDirective)
    app.connect('build-finished', copy_static)
    app.add_stylesheet('htrafdemo.css')
    app.add_javascript('htrafdemo.js')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.