In ~Palm and earlier, all built-in XBlock Sass was included into LMS and CMS
styles before being compiled. The generated CSS was coupled together with
broader LMS/CMS CSS. This means that comprehensive themes have been able to
modify built-in XBlock appearance by setting certain Sass variables. We say that
built-in XBlock Sass was, and is expected to be, "theme-aware".
Shortly after Palm, we decoupled XBlock Sass from LMS and CMS Sass [1]. Each
built-in block's Sass is now compiled into two separate CSS targets, one for
block editing and one for block display. The CSS, now located at
`common/static/css/xmodule`, is injected into the running Webpack context with
the new `XModuleWebpackLoader`. Built-in XBlocks already used
`add_webpack_to_fragment` in order to add JS Webpack bundles to their view
fragments, so when CSS was added to Webpack, it Just Worked.
This unlocked a slieu of simplifications for static asset processing [2];
however, it accidentally made XBlock Sass theme-*unaware*, or perhaps
theme-confused, since the CSS was targeted at `common/static/css/xmodule`
regardless of the theme. The result of this is that **built-in XBlock views will
use CSS based on the Sass variables _last theme to be compiled._** Sass
variables are only used in a handful of places in XBlocks, so the bug is subtle,
but it is there for those running off of master. For example, using edX.org's
theme on master, we can see that there is a default blue underline in the Studio
sequence nav [3]. With this bugfix, it becomes the standard edX.org
greenish-black [4].
This commit makes several changes, firstly to fix the bug, and secondly to leave
ourselves with a more comprehensible asset setup in the `xmodule/` directory.
* We remove the `XModuleWebpackLoader`, thus taking built-in XBlock Sass back
out of Webpack.
* We compile XBlock Sass not to `common/static/css/xmodule`, but to:
* `[lms|cms]/static/css` for the default theme, and
* `<THEME_ROOT>/[lms|cms]/static/css`, for any custom theme.
This is where the comprehensive theming system expects to find themable
assets. Unfortunately, this does mean that the Sass is compiled twice, both
for LMS and CMS. We would have liked to compile it once to somewhere in the
`common/`, but comprehensive theming does not consider `common/` assets to be
themable.
* We split `add_webpack_to_fragment` into two more specialized functions:
* `add_webpack_js_to_fragment` , for adding *just* JS from a Webpack bundle,
and
* `add_sass_to_fragment`, for adding static links to CSS compiled themable
Sass (not Webpack). Both these functions are moved to a new module
`xmodule/util/builtin_assets.py`, since the original module
(`xmodule/util/xmodule_django.py`) didn't make a ton of sense.
* In an orthogonal bugfix, we merge Sass `CourseInfoBlock`, `StaticTabBlock`,
`AboutBlock` into the `HtmlBlock` Sass files. The first three were never used,
as their styling was handled by `HtmlBlock` (their shared parent class).
* As a refactoring, we change Webpack bundle names and Sass module names to be
less misleading:
* student_view, public_view, and author_view: was `<Name>BlockPreview`, is now
`<Name>BlockDisplay`.
* studio_view: was `<Name>BlockStudio`, is now `<Name>BlockEditor`.
* As a refactoring, we move the contents of `xmodule/static` into the existing
`xmodule/assets` directory, and adopt its simper structure. We now have:
* `xmodule/assets/*.scss`: Top-level compiled Sass modules. These could be
collapsed away in a future refactoring.
* `xmodule/assets/<blocktype>/*`: Resources for each block, including both JS
modules and Sass includes (underscore-prefixed so that they aren't
compiled). This structure maps closely with what externally-defined XBlocks
do.
* `xmodule/js` still exists, but it will soon be folded into the
`xmodule/assets`.
* We add a new README [4] to explain the new structure, and also update a
docstring in `openedx/lib/xblock/utils` which had fallen out of date with
reality.
* Side note: We avoid the term "XModule" in all of this, because that's
(thankfully) become a much less useful/accurate way to describe these blocks.
Instead, we say "built-in XBlocks".
Refs:
1. https://github.com/openedx/edx-platform/pull/32018
2. https://github.com/openedx/edx-platform/issues/32292
3. https://github.com/openedx/edx-platform/assets/3628148/8b44545d-0f71-4357-9385-69d6e1cca86f
4. https://github.com/openedx/edx-platform/assets/3628148/d0b7b309-b8a4-4697-920a-8a520e903e06
5. https://github.com/openedx/edx-platform/tree/master/xmodule/assets#readme
Part of: https://github.com/openedx/edx-platform/issues/32292
160 lines
4.9 KiB
Python
160 lines
4.9 KiB
Python
"""
|
|
Template block
|
|
"""
|
|
|
|
from string import Template
|
|
from xblock.core import XBlock
|
|
|
|
from lxml import etree
|
|
from pkg_resources import resource_filename
|
|
from web_fragments.fragment import Fragment
|
|
from xmodule.editing_block import EditingMixin
|
|
from xmodule.raw_block import RawMixin
|
|
from xmodule.util.builtin_assets import add_webpack_js_to_fragment, add_sass_to_fragment
|
|
from xmodule.x_module import (
|
|
HTMLSnippet,
|
|
ResourceTemplates,
|
|
shim_xmodule_js,
|
|
XModuleMixin,
|
|
XModuleToXBlockMixin,
|
|
)
|
|
from xmodule.xml_block import XmlMixin
|
|
|
|
from openedx.core.djangolib.markup import Text
|
|
|
|
|
|
class CustomTagTemplateBlock( # pylint: disable=abstract-method
|
|
RawMixin,
|
|
XmlMixin,
|
|
EditingMixin,
|
|
XModuleToXBlockMixin,
|
|
HTMLSnippet,
|
|
ResourceTemplates,
|
|
XModuleMixin,
|
|
):
|
|
"""
|
|
A block which provides templates for CustomTagBlock. The template name
|
|
is set on the `impl` attribute of CustomTagBlock. See below for more details
|
|
on how to use it.
|
|
"""
|
|
|
|
|
|
@XBlock.needs('mako')
|
|
class CustomTagBlock(CustomTagTemplateBlock): # pylint: disable=abstract-method
|
|
"""
|
|
This block supports tags of the form
|
|
<customtag option="val" option2="val2" impl="tagname"/>
|
|
|
|
In this case, $tagname should refer to a file in data/custom_tags, which
|
|
contains a Python string.Template formatted template that uses ${option} and
|
|
${option2} for the content.
|
|
|
|
For instance:
|
|
|
|
data/mycourse/custom_tags/book::
|
|
More information given in <a href="/book/${page}">the text</a>
|
|
|
|
course.xml::
|
|
...
|
|
<customtag page="234" impl="book"/>
|
|
...
|
|
|
|
Renders to::
|
|
More information given in <a href="/book/234">the text</a>
|
|
"""
|
|
resources_dir = None
|
|
template_dir_name = 'customtag'
|
|
|
|
preview_view_js = {
|
|
'js': [],
|
|
'xmodule_js': resource_filename(__name__, 'js/src/xmodule.js'),
|
|
}
|
|
studio_view_js = {
|
|
'js': [resource_filename(__name__, 'js/src/raw/edit/xml.js')],
|
|
'xmodule_js': resource_filename(__name__, 'js/src/xmodule.js'),
|
|
}
|
|
|
|
def studio_view(self, _context):
|
|
"""
|
|
Return the studio view.
|
|
"""
|
|
fragment = Fragment(
|
|
self.runtime.service(self, 'mako').render_template(self.mako_template, self.get_context())
|
|
)
|
|
add_sass_to_fragment(fragment, 'CustomTagBlockEditor.scss')
|
|
add_webpack_js_to_fragment(fragment, 'CustomTagBlockEditor')
|
|
shim_xmodule_js(fragment, 'XMLEditingDescriptor')
|
|
return fragment
|
|
|
|
def render_template(self, system, xml_data):
|
|
'''Render the template, given the definition xml_data'''
|
|
xmltree = etree.fromstring(xml_data)
|
|
if 'impl' in xmltree.attrib:
|
|
template_name = xmltree.attrib['impl']
|
|
else:
|
|
# VS[compat] backwards compatibility with old nested customtag structure
|
|
child_impl = xmltree.find('impl')
|
|
if child_impl is not None:
|
|
template_name = child_impl.text
|
|
else:
|
|
# TODO (vshnayder): better exception type
|
|
raise Exception("Could not find impl attribute in customtag {}"
|
|
.format(self.location))
|
|
|
|
params = dict(list(xmltree.items()))
|
|
|
|
# cdodge: look up the template as a module
|
|
template_loc = self.location.replace(category='custom_tag_template', name=template_name)
|
|
|
|
template_block = system.get_block(template_loc)
|
|
template_block_data = template_block.data
|
|
template = Template(template_block_data)
|
|
return template.safe_substitute(params)
|
|
|
|
@property
|
|
def rendered_html(self):
|
|
return self.render_template(self.runtime, self.data)
|
|
|
|
def student_view(self, _context):
|
|
"""
|
|
Renders the student view.
|
|
"""
|
|
fragment = Fragment()
|
|
fragment.add_content(self.rendered_html)
|
|
return fragment
|
|
|
|
def export_to_file(self):
|
|
"""
|
|
Custom tags are special: since they're already pointers, we don't want
|
|
to export them in a file with yet another layer of indirection.
|
|
"""
|
|
return False
|
|
|
|
|
|
class TranslateCustomTagBlock( # pylint: disable=abstract-method
|
|
XModuleToXBlockMixin,
|
|
XModuleMixin,
|
|
):
|
|
"""
|
|
Converts olx of the form `<$custom_tag attr="" attr=""/>` to CustomTagBlock
|
|
of the form `<customtag attr="" attr="" impl="$custom_tag"/>`.
|
|
"""
|
|
resources_dir = None
|
|
|
|
@classmethod
|
|
def parse_xml(cls, node, runtime, _keys, _id_generator):
|
|
"""
|
|
Transforms the xml_data from <$custom_tag attr="" attr=""/> to
|
|
<customtag attr="" attr="" impl="$custom_tag"/>
|
|
"""
|
|
|
|
runtime.error_tracker(Text('WARNING: the <{tag}> tag is deprecated. '
|
|
'Instead, use <customtag impl="{tag}" attr1="..." attr2="..."/>. ')
|
|
.format(tag=node.tag))
|
|
|
|
tag = node.tag
|
|
node.tag = 'customtag'
|
|
node.attrib['impl'] = tag
|
|
|
|
return runtime.process_xml(etree.tostring(node))
|