Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
split_docname = self.srcpath.split("/")
if len(split_docname) < 2:
# TODO: Warn about this? Something like ``self.state.document.settings.env``?
split_docname.append("")
self.subchapter = split_docname[-1]
self.chapter = split_docname[-2]
self.basecourse = self.state.document.settings.env.config.html_context.get(
"basecourse", "unknown"
)
self.options["basecourse"] = self.basecourse
self.options["chapter"] = self.chapter
self.options["subchapter"] = self.subchapter
# This is a base class for all Runestone directives which require a divid as their first parameter.
class RunestoneIdDirective(RunestoneDirective):
def getNumber(self):
env = self.state.document.settings.env
if (
self.name in UNNUMBERED_DIRECTIVES
or env.config.generate_component_labels is False
):
return ""
env.assesscounter += 1
res = "Q-%d"
if hasattr(env, "assessprefix"):
res = env.assessprefix + "%d"
def __init__(self, *args, **kwargs):
super(RunestoneDirective, self).__init__(*args, **kwargs)
env = self.state.document.settings.env
self.srcpath = env.docname
# Rather tha use ``os.sep`` to split ``self.srcpath``, use ``'/'``, because Sphinx internally stores filesnames using this separator, even on Windows.
split_docname = self.srcpath.split("/")
if len(split_docname) < 2:
# TODO: Warn about this? Something like ``self.state.document.settings.env``?
split_docname.append("")
self.subchapter = split_docname[-1]
self.chapter = split_docname[-2]
self.basecourse = self.state.document.settings.env.config.html_context.get(
"basecourse", "unknown"
)
self.options["basecourse"] = self.basecourse
self.options["chapter"] = self.chapter
self.options["subchapter"] = self.subchapter
app.add_autoversioned_javascript("matrixeq.js")
app.add_node(MatrixEqNode, html=(visit_matrixeq_node, depart_matrixeq_node))
app.add_node(
InlineMatrixEqNode,
html=(visit_inline_matrixeq_node, depart_inline_matrixeq_node),
)
app.connect("doctree-resolved", process_matrixeq_nodes)
app.connect("env-purge-doc", purge_matrixeq)
# ==========================================================================
# A python class derived from the sphinx class Directive
# This initializes the new directive
class MatrixEq(RunestoneDirective):
"""
.. matrixeq:: uniqueid
:notexecutable: -- the matrix equation can't be executed by the user
:comment: -- A comment to include to the right of the equation
:nolabel: -- don't label the equation using the uniqueid provided
:backgroundcolor: -- the color of the background; either #RRBBGG or a color name
:foregroundcolor: -- the color used for the matrix elements; either #RRBBGG or a color name
:highlightcolor: -- the color used for "bolded" elements; ; either #RRBBGG or a color name
A single matrix is defined using javascript array notation, e.g., [a,b,c;d,e,f]
A "name" can be assigned to a matrix by including a string and a colon after the
beginning [, but before the first value, e.g., [M1: a,b,c;d,e,f]. If no name is
specified, a default name is assigned.
A background color can be assigned to a matrix by including a color specifier
_js_escapes = (_base_js_escapes +
tuple([('%c' % z, '\\u%04X' % z) for z in range(32)]))
# escapejs from Django: https://www.djangoproject.com/
def escapejs(value):
"""Hex encodes characters for use in JavaScript strings."""
if not isinstance(value, str):
value = str(value)
for bad, good in _js_escapes:
value = value.replace(bad, good)
return value
class Assessment(RunestoneDirective):
"""Base Class for assessments"""
def getNumber(self):
env = self.state.document.settings.env
if not hasattr(env,'assesscounter'):
env.assesscounter = 0
env.assesscounter += 1
res = "Q-%d"
if hasattr(env,'assessprefix'):
res = env.assessprefix + "%d"
res = res % env.assesscounter
if hasattr(env, 'assesssuffix'):
class Assignment(RunestoneDirective):
"""
.. assignment:
:name: Problem Set 1
:assignment_type: formative
:questions: (divid_1 50), (divid_2 100), ...
:deadline: 23-09-2016 15:30
:points: integer
"""
required_arguments = 0 # not a sphinx_id for these; just a name parameter
optional_arguments = 0
has_content = False
option_spec = RunestoneDirective.option_spec.copy()
option_spec.update(
{
"name": directives.unchanged,
"assignment_type": directives.unchanged,
"questions": directives.unchanged,
"deadline": directives.unchanged,
"points": directives.positive_int,
"threshold": directives.positive_int,
"autograde": directives.unchanged,
"generate_html": directives.flag,
}
)
def run(self):
"""
.. assignment:
""" This is called at the start of processing a ua node. If ua had recursive nodes
etc and did not want to do all of the processing in visit_ua_node any finishing touches could be
added here.
"""
pass
def process_nodes(app, env, docname):
pass
def purge(app, env, docname):
pass
class usageAssignment(RunestoneDirective):
"""
.. usageassignment:: prep_1
:chapters: chap_name1[, chapname2]*
:subchapters: subchapter_name[, subchaptername2]*
:assignment_name:
:assignment_type:
:deadline:
:sections:
:pct_required: :points:
"""
required_arguments = 0 # use assignment_name parameter
optional_arguments = 0
has_content = False
option_spec = RunestoneDirective.option_spec.copy()
Content
...
"""
# Raise an error if the directive does not have contents.
self.assert_has_content()
# Create the node, to be populated by "nested_parse".
self.options["tabname"] = self.arguments[0]
tab_node = TabNode(self.options, rawsource=self.block_text)
# Parse the child nodes (content of the tab)
self.state.nested_parse(self.content, self.content_offset, tab_node)
return [tab_node]
class TabbedStuffDirective(RunestoneDirective):
"""
.. tabbed:: identifier
:inactive: Optional flag that calls for no tabs to be open on page load
Content (put tabs here)
...
config values (conf.py):
- tabbed_div_class - custom CSS class of the component's outermost div
"""
required_arguments = 1 # the div to put the tabbed exhibit in
optional_arguments = 0
self.body.append(res)
def depart_tabbedstuff_node(self, node):
divid = node.divid
res = ""
# close the tab plugin div and init the Bootstrap tabs
res += END
res = res % {"divid": divid}
self.body.append(res)
class TabDirective(RunestoneDirective):
"""
.. tab:: identifier
:active: Optional flag that specifies this tab to be opened when page is loaded (default is first tab)--overridden by :inactive: flag on tabbedStuff
Content
...
config values (conf.py):
- tabbed_div_class - custom CSS class of the component's outermost div
"""
required_arguments = 1 # the name of the tab
optional_arguments = 0
final_argument_whitespace = True
"""
res = ""
res = TEMPLATE_START % self.options
res += TEMPLATE_END % self.options
rawnode = nodes.raw(self.block_text, res, format="html")
rawnode.source, rawnode.line = self.state_machine.get_source_and_line(
self.lineno
)
return [rawnode]
class QuestionNumber(RunestoneDirective):
"""Set Parameters for Question Numbering
.. qnum::
'prefix': character prefix before the number
'suffix': character prefix after the number
'start': start numbering with this value
.. qnum::
:prefix: turtle-
:start: 10
"""
required_arguments = 0
optional_arguments = 3
has_content = False
option_spec = {
"prefix": directives.unchanged,
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
__author__ = "bmiller"
from docutils import nodes
from docutils.parsers.rst import directives
from runestone.common.runestonedirective import RunestoneDirective
def setup(app):
app.add_directive("shortname", Meta)
app.add_directive("description", Meta)
class Meta(RunestoneDirective):
required_arguments = 1
optional_arguments = 50
def run(self):
"""
process the video directive and generate html for output.
:param self:
:return:
"""
raw_node = nodes.raw(self.block_text, "", format="html")
raw_node.source, raw_node.line = self.state_machine.get_source_and_line(
self.lineno
)
return [raw_node]