From 34374dbc2b0cdbcd2d2de815ce9e1feada008fde Mon Sep 17 00:00:00 2001 From: Jon Grace-Cox <30441316+jongracecox@users.noreply.github.com> Date: Sun, 14 Aug 2022 12:33:12 -0400 Subject: [PATCH] Add emoji examples to documentation (#74) Closes #43 --- README.md | 29 +++++++++++++++++++++++++++++ build_examples.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c36acab..e334d3c 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,35 @@ Available named colors are: | yellow | #DFB317 | ![](https://cdn.rawgit.com/jongracecox/anybadge/master/examples/color_yellow.svg) | | yellow_green | #A4A61D | ![](https://cdn.rawgit.com/jongracecox/anybadge/master/examples/color_yellow_green.svg) | +### Emojis + +It is possible to use emoji characters in badge labels and values. Here are some examples: + +![](https://cdn.rawgit.com/jongracecox/anybadge/master/examples/pipeline_frown.svg) +![](https://cdn.rawgit.com/jongracecox/anybadge/master/examples/pipeline_smile.svg) +![](https://cdn.rawgit.com/jongracecox/anybadge/master/examples/documentation_link.svg) +![](https://cdn.rawgit.com/jongracecox/anybadge/master/examples/pypi_link.svg) + +These files were created by using the **actual** emoji character in the label/value text. For example: + +```python +badge = anybadge.Badge(label="Pipeline status", value="😄") +``` + +There are some caveats worth mentioning: +- The "look" of the emoji is determined by the client (Emoji characters are placed as-is into the SVG file, and are + rendered client-side) +- Rendering may fail in some viewers and developer IDEs (for example, PyCharm does not render emojis in the svg viewer) +- Emojis can have different widths, so the layout may be affected. You can use `num_label_padding_chars` and + `num_value_padding_chars` to fix (see below) + +Here are some examples to show how to use padding to fix layout: + +| Badge | Code | +| ----- |----------------------------------------------------------------------| +| ![](https://cdn.rawgit.com/jongracecox/anybadge/master/examples/pipeline_smile.svg) | `anybadge.Badge("Pipeline status", "😄")` | +| ![](https://cdn.rawgit.com/jongracecox/anybadge/master/examples/pipeline_smile_padding.svg) | `anybadge.Badge("Pipeline status", "😄", num_value_padding_chars=1)` | + ### Semantic version support Anybadge supports semantic versions for value and threshold keys. This supports color-coded diff --git a/build_examples.py b/build_examples.py index dcae30f..e39b531 100644 --- a/build_examples.py +++ b/build_examples.py @@ -1,7 +1,10 @@ +from pathlib import Path + import anybadge -def main(): +def color_examples_table(): + """Output the Markdown table containing color examples.""" print( """ | Color Name | Hex | Example | @@ -21,5 +24,30 @@ def main(): ) +def emoji_examples(): + """Generate emoji example badges used in documentation.""" + examples_dir = Path(__file__).parent / Path("examples") + for label, value, file, kwargs in [ + ("Pipeline status", "😄", "pipeline_smile.svg", {}), + ( + "Pipeline status", + "😄", + "pipeline_smile_padding.svg", + {"num_value_padding_chars": 1}, + ), + ("Pipeline status", "😟", "pipeline_frown.svg", {"default_color": "Red"}), + ("🔗", "Documentation", "documentation_link.svg", {}), + ("🔗", "PyPi", "pypi_link.svg", {}), + ]: + anybadge.Badge(label=label, value=value, **kwargs).write_badge( + examples_dir / Path(file), overwrite=True + ) + + +def main(): + color_examples_table() + emoji_examples() + + if __name__ == "__main__": main()