Пример #1
0
def svgiconref(ref):
    """
    Renders markup for presenting an SVG icon with the given name referencing
    the SVG icon set from an external source.
    """
    if not ref:
        raise AttributeError(
            'Expected valid \'ref\' argument for template tag \'svgiconref\'.')

    # split reference into target and icon name
    try:
        target, name = ref.split('/', 2)
    except ValueError:
        raise AttributeError(
            'Expected valid \'ref\' argument for template tag \'svgiconref\' '
            + 'in the format \'<target>/<icon-name>\'.')

    # determine target icon set filename
    if settings.DEBUG:
        # in debug mode, we use the media api to refer to an individual
        # SVG file. We need to use the media api, since there
        # is some processing involved here in order to guarantee that the
        # SVG icon behaves in the same way as in production mode.
        url = '/%ssvgicons/%s/%s.svg' % (settings.MEDIA_API_URL, target, name)
    else:
        # the deployment process should have generated all required SVG icon
        # files, so we simply refer to it...
        identifier = load_resource_version_identifier()
        filename = get_svgicons_filename(target, identifier)
        url = '%s%s' % (settings.STATIC_URL, filename)

    return format_html(
        '<i class="svgicon svgicon-{}"><svg>' +
        '<use xlink:href="{}#svgicon-{}"/></svg>' + '</i>', name, url, name)
Пример #2
0
    def handle(self, *args, **options):
        """
        Run command.
        """
        self.verbose('Processing svgicons...Please Wait...')

        identifier = options.get('identifier')
        if not identifier:
            identifier = load_resource_version_identifier()

        for target in get_resource_targets():
            # build filename
            filename = get_svgicons_filename(target, identifier)

            # build icon sheet
            resources = get_resources(target, 'svg')
            resources_with_style = get_resources(target, 'svg', 'with-style')
            if len(resources) > 0 or len(resources_with_style) > 0:
                self.verbose('> %s' % filename)

                svg = get_combined_svg(resources, resources_with_style)

                # folder exists?
                if not os.path.isdir(settings.STATIC_ROOT):
                    os.makedirs(settings.STATIC_ROOT)

                # write file
                path = os.path.join(settings.STATIC_ROOT, filename)
                with codecs.open(path, 'w', encoding='utf-8') as f:
                    f.write(svg)

        self.verbose('Complete.')
Пример #3
0
def inline_svgicons(target):
    """
    Renders inline markup for defining an SVG icon sheet for all SVG icon
    assets defined for the given bucket name (target). SVG assets are using the
    standard resource system that is also used for CSS and Javascript assets in
    combination with resources and inline_resources template tags.
    """
    if target not in get_resource_target_definition():
        raise AttributeError(
            'Expected valid \'target\' argument for template ' +
            'tag \'inline_svgicons\'.')

    if settings.DEBUG:
        return mark_safe(
            get_combined_svg(get_resources(target, 'svg'),
                             get_resources(target, 'svg', 'with-style')))
    else:
        identifier = load_resource_version_identifier()
        filename = get_svgicons_filename(target, identifier)
        path = os.path.join(settings.STATIC_ROOT, filename)
        return mark_safe(file_get_contents(path))
Пример #4
0
 def _get_path(self):
     identifier = load_resource_version_identifier()
     filename = get_svgicons_filename('frontend', identifier)
     return os.path.join(settings.STATIC_ROOT, filename)
Пример #5
0
 def test_should_contain_revision_number_if_configured(self):
     identifier = generate_resource_version_identifier()
     self.assertEqual('cubane.svgicons.frontend.%s.svg' % identifier, get_svgicons_filename('frontend', identifier))
Пример #6
0
 def test_should_ignore_identifier_if_revisions_are_not_tracked(self):
     identifier = generate_resource_version_identifier()
     self.assertEqual('cubane.svgicons.frontend.svg', get_svgicons_filename('frontend', identifier='foo'))
Пример #7
0
 def test_should_contain_target_name(self):
     self.assertEqual('cubane.svgicons.frontend.svg', get_svgicons_filename('frontend'))