conf.py: Format with black and rename extensions to _extensions

To match changes in master branch and ease cherrypicks.
This commit is contained in:
Rémi Verschelde
2020-05-04 09:26:37 +02:00
parent 7302d9ce8d
commit 7b5a66c678
12 changed files with 392 additions and 201 deletions

343
_extensions/gdscript.py Normal file
View File

@@ -0,0 +1,343 @@
# -*- coding: utf-8 -*-
"""
pygments.lexers.gdscript
~~~~~~~~~~~~~~~~~~~~~~
Lexer for GDScript.
:copyright: Copyright 2xxx by The Godot Engine Community
:license: MIT.
modified by Daniel J. Ramirez <djrmuv@gmail.com> based on the original python.py pygment
"""
import re
from pygments.lexer import (
Lexer,
RegexLexer,
include,
bygroups,
using,
default,
words,
combined,
do_insertions,
)
from pygments.util import get_bool_opt, shebang_matches
from pygments.token import (
Text,
Comment,
Operator,
Keyword,
Name,
String,
Number,
Punctuation,
Generic,
Other,
Error,
)
from pygments import unistring as uni
__all__ = ["GDScriptLexer"]
line_re = re.compile(".*?\n")
class GDScriptLexer(RegexLexer):
"""
For `Godot source code <https://www.godotengine.org>`_ source code.
"""
name = "GDScript"
aliases = ["gdscript", "gd"]
filenames = ["*.gd"]
mimetypes = ["text/x-gdscript", "application/x-gdscript"]
def innerstring_rules(ttype):
return [
# the old style '%s' % (...) string formatting
(
r"%(\(\w+\))?[-#0 +]*([0-9]+|[*])?(\.([0-9]+|[*]))?"
"[hlL]?[E-GXc-giorsux%]",
String.Interpol,
),
# backslashes, quotes and formatting signs must be parsed one at a time
(r'[^\\\'"%\n]+', ttype),
(r'[\'"\\]', ttype),
# unhandled string formatting sign
(r"%", ttype),
# newlines are an error (use "nl" state)
]
tokens = {
"root": [
(r"\n", Text),
(
r'^(\s*)([rRuUbB]{,2})("""(?:.|\n)*?""")',
bygroups(Text, String.Affix, String.Doc),
),
(
r"^(\s*)([rRuUbB]{,2})('''(?:.|\n)*?''')",
bygroups(Text, String.Affix, String.Doc),
),
(r"[^\S\n]+", Text),
(r"#.*$", Comment.Single),
(r"[]{}:(),;[]", Punctuation),
(r"\\\n", Text),
(r"\\", Text),
(r"(in|and|or|not)\b", Operator.Word),
(
r"!=|==|<<|>>|&&|\+=|-=|\*=|/=|%=|&=|\|=|\|\||[-~+/*%=<>&^.!|$]",
Operator,
),
include("keywords"),
(r"(func)((?:\s|\\\s)+)", bygroups(Keyword, Text), "funcname"),
(r"(class)((?:\s|\\\s)+)", bygroups(Keyword, Text), "classname"),
include("builtins"),
(
'([rR]|[uUbB][rR]|[rR][uUbB])(""")',
bygroups(String.Affix, String.Double),
"tdqs",
),
(
"([rR]|[uUbB][rR]|[rR][uUbB])(''')",
bygroups(String.Affix, String.Single),
"tsqs",
),
(
'([rR]|[uUbB][rR]|[rR][uUbB])(")',
bygroups(String.Affix, String.Double),
"dqs",
),
(
"([rR]|[uUbB][rR]|[rR][uUbB])(')",
bygroups(String.Affix, String.Single),
"sqs",
),
(
'([uUbB]?)(""")',
bygroups(String.Affix, String.Double),
combined("stringescape", "tdqs"),
),
(
"([uUbB]?)(''')",
bygroups(String.Affix, String.Single),
combined("stringescape", "tsqs"),
),
(
'([uUbB]?)(")',
bygroups(String.Affix, String.Double),
combined("stringescape", "dqs"),
),
(
"([uUbB]?)(')",
bygroups(String.Affix, String.Single),
combined("stringescape", "sqs"),
),
include("name"),
include("numbers"),
],
"keywords": [
(
words(
(
"do",
"var",
"const",
"extends",
"is",
"export",
"onready",
"tool",
"static",
"setget",
"signal",
"breakpoint",
"switch",
"case",
"assert",
"break",
"continue",
"elif",
"else",
"for",
"if",
"pass",
"return",
"while",
"match",
"master",
"sync",
"slave",
"rpc",
"enum",
),
suffix=r"\b",
),
Keyword,
),
],
"builtins": [
(
words(
(
"Color8",
"ColorN",
"abs",
"acos",
"asin",
"assert",
"atan",
"atan2",
"bytes2var",
"ceil",
"char",
"clamp",
"convert",
"cos",
"cosh",
"db2linear",
"decimals",
"dectime",
"deg2rad",
"dict2inst",
"ease",
"exp",
"floor",
"fmod",
"fposmod",
"funcref",
"hash",
"inst2dict",
"instance_from_id",
"is_inf",
"is_nan",
"lerp",
"linear2db",
"load",
"log",
"max",
"min",
"nearest_po2",
"pow",
"preload",
"print",
"print_stack",
"printerr",
"printraw",
"prints",
"printt",
"rad2deg",
"rand_range",
"rand_seed",
"randf",
"randi",
"randomize",
"range",
"round",
"seed",
"sign",
"sin",
"sinh",
"sqrt",
"stepify",
"str",
"str2var",
"tan",
"tan",
"tanh",
"type_exist",
"typeof",
"var2bytes",
"var2str",
"weakref",
"yield",
),
prefix=r"(?<!\.)",
suffix=r"\b",
),
Name.Builtin,
),
(r"((?<!\.)(self|false|true)|(PI|NAN|INF)" r")\b", Name.Builtin.Pseudo),
(
words(
(
"bool",
"int",
"float",
"String",
"NodePath" "Vector2",
"Rect2",
"Transform2D",
"Vector3",
"Rect3",
"Plane",
"Quat",
"Basis",
"Transform",
"Color",
"RID",
"Object",
"NodePath",
"Dictionary",
"Array",
"PoolByteArray",
"PoolIntArray",
"PoolRealArray",
"PoolStringArray",
"PoolVector2Array",
"PoolVector3Array",
"PoolColorArray",
"null",
),
prefix=r"(?<!\.)",
suffix=r"\b",
),
Name.Builtin.Type,
),
],
"numbers": [
(r"(\d+\.\d*|\d*\.\d+)([eE][+-]?[0-9]+)?j?", Number.Float),
(r"\d+[eE][+-]?[0-9]+j?", Number.Float),
(r"0[xX][a-fA-F0-9]+", Number.Hex),
(r"\d+j?", Number.Integer),
],
"name": [("[a-zA-Z_]\w*", Name),],
"funcname": [("[a-zA-Z_]\w*", Name.Function, "#pop"), default("#pop"),],
"classname": [("[a-zA-Z_]\w*", Name.Class, "#pop")],
"stringescape": [
(
r'\\([\\abfnrtv"\']|\n|N\{.*?\}|u[a-fA-F0-9]{4}|'
r"U[a-fA-F0-9]{8}|x[a-fA-F0-9]{2}|[0-7]{1,3})",
String.Escape,
)
],
"strings-single": innerstring_rules(String.Single),
"strings-double": innerstring_rules(String.Double),
"dqs": [
(r'"', String.Double, "#pop"),
(r'\\\\|\\"|\\\n', String.Escape), # included here for raw strings
include("strings-double"),
],
"sqs": [
(r"'", String.Single, "#pop"),
(r"\\\\|\\'|\\\n", String.Escape), # included here for raw strings
include("strings-single"),
],
"tdqs": [
(r'"""', String.Double, "#pop"),
include("strings-double"),
(r"\n", String.Double),
],
"tsqs": [
(r"'''", String.Single, "#pop"),
include("strings-single"),
(r"\n", String.Single),
],
}
def setup(sphinx):
sphinx.add_lexer("gdscript", GDScriptLexer())

View File

@@ -0,0 +1 @@
__import__('pkg_resources').declare_namespace(__name__)

View File

@@ -0,0 +1,7 @@
# The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,9 @@
/*!
* # Semantic UI 2.2.10 - Tab
* http://github.com/semantic-org/semantic-ui/
*
*
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
*/.ui.tab{display:none}.ui.tab.active,.ui.tab.open{display:block}.ui.tab.loading{position:relative;overflow:hidden;display:block;min-height:250px}.ui.tab.loading *{position:relative!important;left:-10000px!important}.ui.tab.loading.segment:before,.ui.tab.loading:before{position:absolute;content:'';top:100px;left:50%;margin:-1.25em 0 0 -1.25em;width:2.5em;height:2.5em;border-radius:500rem;border:.2em solid rgba(0,0,0,.1)}.ui.tab.loading.segment:after,.ui.tab.loading:after{position:absolute;content:'';top:100px;left:50%;margin:-1.25em 0 0 -1.25em;width:2.5em;height:2.5em;-webkit-animation:button-spin .6s linear;animation:button-spin .6s linear;-webkit-animation-iteration-count:infinite;animation-iteration-count:infinite;border-radius:500rem;border-color:#767676 transparent transparent;border-style:solid;border-width:.2em;box-shadow:0 0 0 1px transparent}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,47 @@
.sphinx-tabs {
margin-bottom: 2em;
}
.sphinx-tabs .sphinx-menu a.item {
color: #2980b9 !important;
}
.sphinx-tabs .sphinx-menu {
border-bottom-color: #a0b3bf !important;
}
.sphinx-tabs .sphinx-menu a.active.item {
border-color: #a0b3bf !important;
}
.sphinx-tab {
border-color: #a0b3bf !important;
box-sizing: border-box;
}
.tab div[class^='highlight']:last-child {
margin-bottom: 0;
}
.tab .wy-plain-list-disc:last-child,
.rst-content .section ul:last-child,
.rst-content .toctree-wrapper ul:last-child,
article ul:last-child {
margin-bottom: 0;
}
/* Code tabs don't need the code-block border */
.code-tab.tab {
padding: 0.4em !important;
}
.code-tab.tab div[class^='highlight'] {
border: none;
}
/* Semantic UI tabs don't shrink, make font smaller when viewing in mobile devices */
@media screen and (max-width: 768px) {
.sphinx-tabs .sphinx-menu a.item {
font-size: 0.9em !important;
}
}

View File

@@ -0,0 +1,72 @@
// From http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport
function elementIsInView (el) {
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
$(function() {
// Change container tags <div> -> <a>
$('.sphinx-menu.menu .item').each(function() {
var this_ = $(this);
var a_this = $('<a>');
a_this.html(this_.html());
$.each(this_.prop('attributes'), function() {
a_this.attr(this.name, this.value);
});
this_.replaceWith(a_this);
});
// We store the data-tab values as sphinx-data-<data-tab value>
// Add data-tab attribute with the extracted value
$('.sphinx-menu.menu .item, .sphinx-tab.tab').each(function() {
var this_ = $(this);
const prefix = 'sphinx-data-';
const classes = this_.attr('class').split(/\s+/);
$.each(classes, function(idx, clazz) {
if (clazz.startsWith(prefix)) {
this_.attr('data-tab',
clazz.substring(prefix.length));
}
});
});
// Mimic the Semantic UI behaviour
$('.sphinx-menu.menu .item').each(function() {
var this1 = $(this);
var data_tab = this1.attr('data-tab');
this1.on('click', function() {
// Find offset in view
const offset = (this1.offset().top - $(window).scrollTop());
$('[data-tab]').each(function() {
var this2 = $(this);
// Remove 'active' class from tabs that aren't the same
if (this2.attr('data-tab') !== data_tab) {
// Keep 'active' if there isn't a tab with the same data-tab value
if (0 < this2.parent().find('[data-tab="' + data_tab + '"]').length) {
this2.removeClass('active');
}
} else {
// Add 'active' if data-tab value is the same
this2.addClass('active');
}
});
// Keep tab with the original view offset
$(window).scrollTop(this1.offset().top - offset);
});
});
});

View File

@@ -0,0 +1,271 @@
""" Tabbed views for Sphinx, with HTML builder """
import base64
import json
import posixpath
import os
from docutils.parsers.rst import Directive
from docutils import nodes
from pygments.lexers import get_all_lexers
from sphinx.util.osutil import copyfile
DIR = os.path.dirname(os.path.abspath(__file__))
FILES = [
'tabs.js',
'tabs.css',
'semantic-ui-2.2.10/segment.min.css',
'semantic-ui-2.2.10/menu.min.css',
'semantic-ui-2.2.10/tab.min.css',
'semantic-ui-2.2.10/tab.min.js',
]
LEXER_MAP = {}
for lexer in get_all_lexers():
for short_name in lexer[1]:
LEXER_MAP[short_name] = lexer[0]
class TabsDirective(Directive):
""" Top-level tabs directive """
has_content = True
def run(self):
""" Parse a tabs directive """
self.assert_has_content()
env = self.state.document.settings.env
node = nodes.container()
node['classes'] = ['sphinx-tabs']
tabs_node = nodes.container()
tabs_node.tagname = 'div'
classes = 'ui top attached tabular menu sphinx-menu'
tabs_node['classes'] = classes.split(' ')
env.temp_data['tab_titles'] = []
env.temp_data['is_first_tab'] = True
self.state.nested_parse(self.content, self.content_offset, node)
tab_titles = env.temp_data['tab_titles']
for idx, [data_tab, tab_name] in enumerate(tab_titles):
tab = nodes.container()
tab.tagname = 'a'
tab['classes'] = ['item'] if idx > 0 else ['active', 'item']
tab['classes'].append(data_tab)
tab += tab_name
tabs_node += tab
node.children.insert(0, tabs_node)
return [node]
class TabDirective(Directive):
""" Tab directive, for adding a tab to a collection of tabs """
has_content = True
def run(self):
""" Parse a tab directive """
self.assert_has_content()
env = self.state.document.settings.env
args = self.content[0].strip()
try:
args = json.loads(args)
self.content.trim_start(1)
except ValueError:
args = {}
tab_name = nodes.container()
self.state.nested_parse(
self.content[:1], self.content_offset, tab_name)
args['tab_name'] = tab_name
if 'tab_id' not in args:
args['tab_id'] = env.new_serialno('tab_id')
data_tab = "sphinx-data-tab-{}".format(args['tab_id'])
env.temp_data['tab_titles'].append((data_tab, args['tab_name']))
text = '\n'.join(self.content)
node = nodes.container(text)
classes = 'ui bottom attached sphinx-tab tab segment'
node['classes'] = classes.split(' ')
node['classes'].extend(args.get('classes', []))
node['classes'].append(data_tab)
if env.temp_data['is_first_tab']:
node['classes'].append('active')
env.temp_data['is_first_tab'] = False
self.state.nested_parse(self.content[2:], self.content_offset, node)
return [node]
class GroupTabDirective(Directive):
""" Tab directive that toggles with same tab names across page"""
has_content = True
def run(self):
""" Parse a tab directive """
self.assert_has_content()
group_name = self.content[0]
self.content.trim_start(2)
for idx, line in enumerate(self.content.data):
self.content.data[idx] = ' ' + line
tab_args = {
'tab_id': base64.b64encode(
group_name.encode('utf-8')).decode('utf-8')
}
new_content = [
'.. tab:: {}'.format(json.dumps(tab_args)),
' {}'.format(group_name),
'',
]
for idx, line in enumerate(new_content):
self.content.data.insert(idx, line)
self.content.items.insert(idx, (None, idx))
node = nodes.container()
self.state.nested_parse(self.content, self.content_offset, node)
return node.children
class CodeTabDirective(Directive):
""" Tab directive with a codeblock as its content"""
has_content = True
def run(self):
""" Parse a tab directive """
self.assert_has_content()
args = self.content[0].strip().split()
self.content.trim_start(2)
lang = args[0]
tab_name = ' '.join(args[1:]) if len(args) > 1 else LEXER_MAP[lang]
for idx, line in enumerate(self.content.data):
self.content.data[idx] = ' ' + line
tab_args = {
'tab_id': '-'.join(tab_name.lower().split()),
'classes': ['code-tab'],
}
new_content = [
'.. tab:: {}'.format(json.dumps(tab_args)),
' {}'.format(tab_name),
'',
' .. code-block:: {}'.format(lang),
'',
]
for idx, line in enumerate(new_content):
self.content.data.insert(idx, line)
self.content.items.insert(idx, (None, idx))
node = nodes.container()
self.state.nested_parse(self.content, self.content_offset, node)
return node.children
class _FindTabsDirectiveVisitor(nodes.NodeVisitor):
""" Visitor pattern than looks for a sphinx tabs
directive in a document """
def __init__(self, document):
nodes.NodeVisitor.__init__(self, document)
self._found = False
def unknown_visit(self, node):
if not self._found and isinstance(node, nodes.container) and \
'classes' in node and isinstance(node['classes'], list):
self._found = 'sphinx-tabs' in node['classes']
@property
def found_tabs_directive(self):
""" Return whether a sphinx tabs directive was found """
return self._found
# pylint: disable=unused-argument
def add_assets(app, pagename, templatename, context, doctree):
""" Add CSS and JS asset files """
if doctree is None:
return
visitor = _FindTabsDirectiveVisitor(doctree)
doctree.walk(visitor)
assets = ['sphinx_tabs/' + f for f in FILES]
css_files = [posixpath.join('_static', path)
for path in assets if path.endswith('css')]
script_files = [posixpath.join('_static', path)
for path in assets if path.endswith('js')]
if visitor.found_tabs_directive:
if 'css_files' not in context:
context['css_files'] = css_files
else:
context['css_files'].extend(css_files)
if 'script_files' not in context:
context['script_files'] = script_files
else:
context['script_files'].extend(script_files)
else:
for path in css_files:
if 'css_files' in context and path in context['css_files']:
context['css_files'].remove(path)
for path in script_files:
if 'script_files' in context and path in context['script_files']:
context['script_files'].remove(path)
# pylint: enable=unused-argument
def copy_assets(app, exception):
""" Copy asset files to the output """
builders = ('html', 'readthedocs', 'readthedocssinglehtmllocalmedia',
'singlehtml')
if app.builder.name not in builders:
app.warn('Not copying tabs assets! Not compatible with %s builder' %
app.builder.name)
return
if exception:
app.warn('Not copying tabs assets! Error occurred previously')
return
app.info('Copying tabs assets... ', nonl=True)
installdir = os.path.join(app.builder.outdir, '_static', 'sphinx_tabs')
for path in FILES:
source = os.path.join(DIR, path)
dest = os.path.join(installdir, path)
destdir = os.path.dirname(dest)
if not os.path.exists(destdir):
os.makedirs(destdir)
copyfile(source, dest)
app.info('done')
def setup(app):
""" Set up the plugin """
app.add_directive('tabs', TabsDirective)
app.add_directive('tab', TabDirective)
app.add_directive('group-tab', GroupTabDirective)
app.add_directive('code-tab', CodeTabDirective)
app.connect('html-page-context', add_assets)
app.connect('build-finished', copy_assets)