chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import os
|
||||
|
||||
from docutils import nodes
|
||||
from docutils.parsers.rst import Directive
|
||||
from sphinx.util import logging
|
||||
from sphinx.util.osutil import copyfile
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
JS_FILE = "languagesections.js"
|
||||
|
||||
|
||||
class CodeSectionDirective(Directive):
|
||||
has_content = True
|
||||
|
||||
def run(self):
|
||||
self.assert_has_content()
|
||||
text = "\n".join(self.content)
|
||||
node = nodes.container(text)
|
||||
node["classes"].append("code-section")
|
||||
self.add_name(node)
|
||||
self.state.nested_parse(self.content, self.content_offset, node)
|
||||
return [node]
|
||||
|
||||
|
||||
class PlainSectionDirective(Directive):
|
||||
has_content = True
|
||||
|
||||
def run(self):
|
||||
self.assert_has_content()
|
||||
text = "\n".join(self.content)
|
||||
node = nodes.container(text)
|
||||
node["classes"].append("plain-section")
|
||||
self.add_name(node)
|
||||
self.state.nested_parse(self.content, self.content_offset, node)
|
||||
return [node]
|
||||
|
||||
|
||||
def add_assets(app):
|
||||
app.add_js_file(JS_FILE)
|
||||
|
||||
|
||||
def copy_assets(app, exception):
|
||||
if app.builder.name != "html" or exception:
|
||||
return
|
||||
logger.info("Copying examplecode stylesheet/javascript... ", nonl=True)
|
||||
dest = os.path.join(app.builder.outdir, "_static", JS_FILE)
|
||||
source = os.path.join(os.path.abspath(os.path.dirname(__file__)), JS_FILE)
|
||||
copyfile(source, dest)
|
||||
logger.info("done")
|
||||
|
||||
|
||||
def setup(app):
|
||||
app.add_directive("code-section", CodeSectionDirective)
|
||||
app.add_directive("plain-section", PlainSectionDirective)
|
||||
app.connect("builder-inited", add_assets)
|
||||
app.connect("build-finished", copy_assets)
|
||||
@@ -0,0 +1,80 @@
|
||||
|
||||
$(function() {
|
||||
|
||||
$('div.code-section').each(function() {
|
||||
var example_sel = $('<ul />', { class: 'section-selector' });
|
||||
var i = 0;
|
||||
$('div[class^="highlight-"]', this).each(function() {
|
||||
language_name = $(this).attr('class').substring(10).replace('notranslate', '');
|
||||
language_name = language_name.charAt(0).toUpperCase() + language_name.substr(1);
|
||||
|
||||
var sel_item = $('<li />', {
|
||||
class: $(this).attr('class'),
|
||||
text: language_name
|
||||
});
|
||||
if (i++) {
|
||||
$(this).hide();
|
||||
} else {
|
||||
sel_item.addClass('selected');
|
||||
}
|
||||
example_sel.append(sel_item);
|
||||
$(this).addClass('example');
|
||||
});
|
||||
$(this).prepend(example_sel);
|
||||
example_sel = null;
|
||||
i = null;
|
||||
});
|
||||
|
||||
$('div.plain-section').each(function() {
|
||||
var example_sel = $('<ul />', { class: 'section-selector' });
|
||||
var i = 0;
|
||||
$('div.container', this).each(function() {
|
||||
var language_name = $(this).attr('class').replace(' docutils container', '').trim();
|
||||
language_name = language_name.charAt(0).toUpperCase() + language_name.substr(1);
|
||||
|
||||
var sel_item = $('<li />', {
|
||||
class: $(this).attr('class'),
|
||||
text: language_name
|
||||
});
|
||||
if (i++) {
|
||||
$(this).hide();
|
||||
} else {
|
||||
sel_item.addClass('selected');
|
||||
}
|
||||
example_sel.append(sel_item);
|
||||
$(this).addClass('example');
|
||||
});
|
||||
$(this).prepend(example_sel);
|
||||
example_sel = null;
|
||||
i = null;
|
||||
});
|
||||
|
||||
$('div.code-section ul.section-selector li,div.plain-section ul.section-selector li').click(function(evt) {
|
||||
evt.preventDefault();
|
||||
|
||||
var sel_class = $(this).attr('class')
|
||||
.replace(' docutils container', '')
|
||||
.replace('notranslate', '')
|
||||
.replace(' selected', '');
|
||||
|
||||
$('ul.section-selector li').each(function() {
|
||||
var parent = $(this).parent().parent();
|
||||
var my_sel_class = sel_class;
|
||||
// When the target language is not available, default to bash or python.
|
||||
if (!$('div.' + sel_class, parent).length) {
|
||||
if ($('div.highlight-bash', parent).length)
|
||||
my_sel_class = 'highlight-bash';
|
||||
else
|
||||
my_sel_class = 'highlight-python';
|
||||
}
|
||||
|
||||
$('div.example', parent).hide();
|
||||
$('div.' + my_sel_class, parent).show();
|
||||
|
||||
$('ul.section-selector li', parent).removeClass('selected');
|
||||
$('ul.section-selector li.' + my_sel_class, parent).addClass('selected');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user