|
| 1 | +""" |
| 2 | + hig_www.hig_palette |
| 3 | + ~~~~~~~~~~~~~~~~~~~ |
| 4 | +
|
| 5 | + Access properties of GNOME's HIG palette via rST roles. |
| 6 | +""" |
| 7 | + |
| 8 | +from urllib.parse import urljoin |
| 9 | +import base64 |
| 10 | + |
| 11 | +from docutils.utils import unescape |
| 12 | +from docutils import nodes |
| 13 | +from sphinx.errors import ExtensionError |
| 14 | + |
| 15 | +# Name of this extension |
| 16 | +extname = "hig_palette" |
| 17 | +# The variable in conf.py that maps color names to (R, G, B) tuples |
| 18 | +colors_var = "hig_palette_colors_rgb" |
| 19 | +# This can be used within the CSS to apply styles to the resulting image |
| 20 | +svg_class_name = "hig-palette-swatch" |
| 21 | + |
| 22 | +""" |
| 23 | +Creates an SVG containing a rect, filled with the RGB tuple passed in. |
| 24 | +""" |
| 25 | +def create_palette_svg_str(rgb): |
| 26 | + return """ |
| 27 | + <svg width="100%%" height="100%%" |
| 28 | + xmlns="http://www.w3.org/2000/svg" |
| 29 | + xmlns:xlink="http://www.w3.org/1999/xlink"> |
| 30 | +
|
| 31 | + <rect width="100%%" height="100%%" fill="rgb%s" /> |
| 32 | + </svg> |
| 33 | + """ % rgb.__str__() |
| 34 | + |
| 35 | +def create_rgb_node(rgb): |
| 36 | + output = rgb.__str__() |
| 37 | + return nodes.Text(output, output) |
| 38 | + |
| 39 | +def create_hex_node(rgb): |
| 40 | + red, green, blue = rgb |
| 41 | + output = f"#{red:02x}{green:02x}{blue:02x}" |
| 42 | + return nodes.Text(output, output) |
| 43 | + |
| 44 | +def create_svg_node(rgb, name): |
| 45 | + svg_str = create_palette_svg_str(rgb) |
| 46 | + svg_base64_str = base64.b64encode(svg_str.encode("ascii")) |
| 47 | + uri = f"data:image/svg+xml;base64,{svg_base64_str.decode('ascii')}" |
| 48 | + |
| 49 | + node = nodes.image(svg_str) |
| 50 | + node["uri"] = uri |
| 51 | + |
| 52 | + node['classes'].append(svg_class_name) |
| 53 | + node["alt"] = f"Color '{name}' from the GNOME HIG palette" |
| 54 | + |
| 55 | + return node |
| 56 | + |
| 57 | +""" |
| 58 | +Creates a role function that can be registered with Sphinx. |
| 59 | +""" |
| 60 | +def make_palette_role(color_mappings, representation): |
| 61 | + valid_representations = ["rgb", "hex", "svg"] |
| 62 | + |
| 63 | + if not representation in valid_representations: |
| 64 | + raise ExtensionError("Cannot generate representation '%s' for colors" |
| 65 | + % representation, modname=extname) |
| 66 | + |
| 67 | + def role(type, rawtext, text, lineno, inliner, options={}, content={}): |
| 68 | + color_name = unescape(text) |
| 69 | + |
| 70 | + if not color_name in color_mappings: |
| 71 | + raise ExtensionError( |
| 72 | + "Definition for color '%s' not found in conf.py" % color_name, |
| 73 | + modname=extname) |
| 74 | + |
| 75 | + color_rgb = color_mappings[color_name] |
| 76 | + node = None |
| 77 | + |
| 78 | + if representation == "rgb": |
| 79 | + node = create_rgb_node(color_rgb) |
| 80 | + elif representation == "hex": |
| 81 | + node = create_hex_node(color_rgb) |
| 82 | + elif representation == "svg": |
| 83 | + node = create_svg_node(color_rgb, color_name) |
| 84 | + |
| 85 | + return [node], [] |
| 86 | + |
| 87 | + return role |
| 88 | + |
| 89 | +def setup_palette_roles(app): |
| 90 | + color_mappings = app.config[colors_var] |
| 91 | + |
| 92 | + # returns a text node with a tuple (R, G, B) representing a defined color |
| 93 | + app.add_role("palette-rgb", make_palette_role(color_mappings, "rgb")) |
| 94 | + # returns a text node with the hexadecimal representation of a defined color |
| 95 | + app.add_role("palette-hex", make_palette_role(color_mappings, "hex")) |
| 96 | + # returns an image node set to an SVG from create_palette_svg() |
| 97 | + app.add_role("palette-svg", make_palette_role(color_mappings, "svg")) |
| 98 | + |
| 99 | +def setup(app): |
| 100 | + app.add_config_value(colors_var, {}, "env") |
| 101 | + app.connect("builder-inited", setup_palette_roles) |
| 102 | + |
| 103 | + return { |
| 104 | + "version": "0.1", |
| 105 | + "parallel_read_safe": True, |
| 106 | + "parellel_write_safe": True |
| 107 | + } |
0 commit comments