Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ Or, you can scale the icon with respect to its default dimensions like this:
```


If you want your own icon, you can register it globally and then use it.
Register the icon like this:
```
from octicons.templatetags.octicons import Octicon
Octicon.register(<icon_name>, <path_to_icon>)
```
The icon's height and widht must be 24 pixels.


All attributes passed will be added as HTML attributes to the SVG element
of the icon.

Expand Down
21 changes: 21 additions & 0 deletions octicons/templatetags/octicons.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django import template
from django.utils.html import format_html
from xml.dom import minidom

from . import OCTICON_DATA

Expand Down Expand Up @@ -102,6 +103,26 @@ def _calculate_height(self, width):
return int((int(width) * self.height) / self.width)


# Register method for a new icon.
@classmethod
def register(cls, symbol, svg_file):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a docstring for this method, which should explain what this method does and how it should be used. This is important since it's a public method.

doc = minidom.parse(svg_file)
path_strings = [path.getAttribute('d') for path
in doc.getElementsByTagName('path')]
path = '<path fill-rule="evenodd" d="'+path_strings[0]+'"'+"/>"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments for all these magic literals.


values = {
'name': symbol,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent the key-value pairs one level.

'keywords': [symbol],
'width': 24,
'height': 24,
'path': path
}

OCTICON_DATA.update({symbol:values})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should return True if the registration is successful.

doc.unlink()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? Please add a comment.



@register.simple_tag
def octicon(symbol, **options):
icon = Octicon(symbol, **options)
Expand Down
1 change: 1 addition & 0 deletions tests/icon/pen.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions tests/test_octicon.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest
from octicons.templatetags import OCTICON_DATA
from octicons.templatetags.octicons import Octicon
import os.path


def test_failure_when_octicon_not_exist():
Expand Down Expand Up @@ -96,3 +98,11 @@ def test_a11y_include_attributes():
def test_a11y_has_aria_hidden_when_no_label_passed():
icon = Octicon('x')
assert 'aria-hidden="true"' in icon.to_svg


# Test for the register method
def test_register_octicon():
my_path = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(my_path, "icon/pen.svg")
Octicon.register('pen', path)
assert OCTICON_DATA.get('pen')