summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-07-07 18:05:44 -0400
committerDrew DeVault <sir@cmpwn.com>2019-07-07 18:05:44 -0400
commitafe317aca93d46a6e228c25b23fada0d90362816 (patch)
tree5f9dd85a526194fe20061d52d517574198e7da4b
parent8e60f1670a276cac280694ddec289e724f2afe8a (diff)
Add color option to annotations
-rw-r--r--gitsrht/annotations.py24
1 files changed, 20 insertions, 4 deletions
diff --git a/gitsrht/annotations.py b/gitsrht/annotations.py
index aa31ff9..d8a6f12 100644
--- a/gitsrht/annotations.py
+++ b/gitsrht/annotations.py
@@ -189,6 +189,14 @@ def validate_annotation(valid, anno):
valid.expect("to" in anno, "'to' is required")
valid.expect("title" not in anno or isinstance(anno["title"], str),
"'title' must be a string")
+ valid.expect("color" not in anno or isinstance(anno["color"], str),
+ "'color' must be a string")
+ if "color" in anno and anno["color"] != "transparent":
+ valid.expect("color" not in anno or len(anno["color"]) == 7,
+ "'color' must be a 7 digit string or 'transparent'")
+ valid.expect("color" not in anno or not any(
+ c for c in anno["color"].lower() if c not in "#0123456789abcdef"),
+ "'color' must be in hexadecimal or 'transparent'")
elif anno["type"] == "markdown":
for field in ["lineno"]:
valid.expect(field in anno, "f'{field}' is required")
@@ -226,14 +234,22 @@ class AnnotatedFormatter(_BaseFormatter):
end = anno["colno"] + anno["len"] - 1
target = anno["to"]
title = anno.get("title", "")
+ color = anno.get("color", None)
url = urlparse(target)
if url.scheme == "":
target = self.link_prefix + "/" + target
if start <= colno < end:
- return (f"<a class='annotation' title='{title}' " +
- f"href='{escape_html(target)}' " +
- f"rel='nofollow noopener' " +
- f">{escape_html(token)}</a>""")
+ if color is not None:
+ return (f"<a class='annotation' title='{title}' " +
+ f"href='{escape_html(target)}' " +
+ f"rel='nofollow noopener' " +
+ f"style='background-color: {color}' " +
+ f">{escape_html(token)}</a>""")
+ else:
+ return (f"<a class='annotation' title='{title}' " +
+ f"href='{escape_html(target)}' " +
+ f"rel='nofollow noopener' " +
+ f">{escape_html(token)}</a>""")
elif anno["type"] == "markdown":
if "\n" not in token:
continue