Skip to content

Commit

Permalink
Refactor svg badge generation and formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
eli64s committed Jan 3, 2024
1 parent 2622f90 commit d0f98ad
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 52 deletions.
39 changes: 21 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@
<em>Auto-generate detailed and structured README files, powered by AI.</em>
</p>
<p align="center">
<a href="https://github.com/eli64s/readme-ai/actions">
<img src="https://img.shields.io/github/actions/workflow/status/eli64s/readme-ai/.github%2Fworkflows%2Frelease-pipeline.yml?logo=GitHub&label=cicd&color=c125ff" alt="Test">
</a>
<a href="https://app.codecov.io/gh/eli64s/readme-ai"></a>
<img src="https://img.shields.io/codecov/c/github/eli64s/readme-ai?logo=codecov&color=c125ff" alt="Publish">
</a>
<a href="https://pypi.python.org/pypi/readmeai/">
<img src="https://img.shields.io/pypi/v/readmeai?color=c125ff">
<img src="https://img.shields.io/pypi/pyversions/readmeai.svg?color=c125ff" alt="Package version">
</a>
<a href="https://github.com/eli64s/readme-ai/actions" target="_blank">
<img src="https://img.shields.io/github/actions/workflow/status/eli64s/readme-ai/.github%2Fworkflows%2Frelease-pipeline.yml?logo=GitHub&label=cicd&color=c125ff" alt="Test">
</a><a href="https://app.codecov.io/gh/eli64s/readme-ai" target="_blank">
<img src="https://img.shields.io/codecov/c/github/eli64s/readme-ai?logo=codecov&color=c125ff" alt="Publish">
</a><a href="https://pypi.python.org/pypi/readmeai/" target="_blank">
<img src="https://img.shields.io/pypi/v/readmeai?color=c125ff">
<img src="https://img.shields.io/pypi/pyversions/readmeai.svg?color=c125ff" alt="Package version">
</a>
</p>


---

## 🔗 Quick Links
Expand Down Expand Up @@ -81,8 +80,11 @@ Built with flexibility in mind, readme-ai allows users to customize various aspe
Currently, readme-ai uses generative ai to create four distinct sections of the README file.

> i. **Header**: Project slogan that describes the repository in an engaging way.
>
> ii. **Overview**: Provides an intro to the project's core use-case and value proposition.
>
> iii. **Features**: Markdown table containing details about the project's technical components.
>
> iv. **Modules**: Codebase file summaries are generated and formatted into markdown tables.
All other content is extracted from processing and analyzing repository metadata and files.
Expand All @@ -99,16 +101,16 @@ Below are a few examples of README headers generated by the readme-ai tool.

<table>
<tr>
<td align="center"><img src="https://raw.githubusercontent.com/eli64s/readme-ai/main/examples/images/header-default.png" alt="default" width="400"/><br><code>--badges for-the-badge --image blue</code></td>
<td align="center"><img src="https://raw.githubusercontent.com/eli64s/readme-ai/main/examples/images/header-offline.png" alt="offline" width="400"/><br><code>--badges flat-square --image black --offline</code></td>
<td align="center"><img src="https://raw.githubusercontent.com/eli64s/readme-ai/main/examples/images/header-flat.png" alt="flat" width="400"/><br><code>--badges flat --image blue</code></td>
<td align="center"><img src="https://raw.githubusercontent.com/eli64s/readme-ai/main/examples/images/header-offline.png" alt="offline" width="400"/><br><code>--badges flat-square --image grey --offline</code></td>
</tr>
<tr>
<td align="center"><img src="https://github.com/eli64s/readme-ai/blob/main/examples/images/header-skills.png" alt="skills" width="400"/><br><code>--align left --badges skills-light --image black</code></td>
<td align="center"><img src="https://github.com/eli64s/readme-ai/blob/main/examples/images/header-kotlin.png" alt="kotlin" width="400"/><br><code>--badges flat --image blue</code></td>
<td align="center"><img src="https://github.com/eli64s/readme-ai/blob/main/examples/images/header-skills-dark.png" alt="skills" width="400"/><br><code>--align left --badges skills --image yellow</code></td>
<td align="center"><img src="https://github.com/eli64s/readme-ai/blob/main/examples/images/header-plastic.png" alt="plastic" width="400"/><br><code>--badges plastic --image blue</code></td>
</tr>
<tr>
<td align="center"><img src="https://raw.githubusercontent.com/eli64s/readme-ai/main/examples/images/header-skills-light.png" alt="skill-icons" width="400"/><br><code>--badges skills-light --image blue</code></td>
<td align="center"><img src="https://raw.githubusercontent.com/eli64s/readme-ai/main/examples/images/header-java.png" alt="kotlin" width="400"/><br><code>--align left --badges skills-light --image black</code></td>
<td align="center"><img src="https://raw.githubusercontent.com/eli64s/readme-ai/main/examples/images/header-skills-light.png" alt="skills-light" width="400"/><br><code>--badges skills-light --image blue</code></td>
<td align="center"><img src="https://raw.githubusercontent.com/eli64s/readme-ai/main/examples/images/header-purple.png" alt="large-repo" width="400"/><br><code>--align left --badges flat --image purple</code></td>
</tr>
</table>

Expand Down Expand Up @@ -472,7 +474,7 @@ Run the `readmeai` command in your terminal with the following options to tailor
|-------------------|---------|-------------|------|--------|
| `--align`/`-a` | `center` | Set header text alignment (`left`, `center`). | String | Optional |
| `--api-key`/`-k` | `OPENAI_API_KEY` env var | Your GPT model API key. | String | Optional |
| `--badges`/`-b` | `flat` | Badge style options for your README file. | String | Optional |
| `--badges`/`-b` | `minimal` | Badge style options for your README file. | String | Optional |
| `--emojis`/`-e` | `False` | Add emojis to section header tiles. | Boolean | Optional |
| `--image`/`-i` | `blue` | Project logo image displayed in README header. | String | Optional |
| `--max-tokens` | `3899` | Max number of tokens that can be generated. | Integer | Optional |
Expand All @@ -488,10 +490,11 @@ Run the `readmeai` command in your terminal with the following options to tailor

**Badge Icons**

Select your preferred badge icon style to display in your output file using the `--badges` flag.
Select your preferred badge icon style to display in your output file using the `--badges` flag. The `standard` option is the default and displays basic metadata about your repository. If you select another option, the `standard` badges will be automatically included.

| **Options** | **Preview** |
|------------------|----------|
| `standard` | ![license](https://img.shields.io/github/license/eli64s/readme-ai?flat) ![last-commit](https://img.shields.io/github/last-commit/eli64s/readme-ai?flat) ![languages](https://img.shields.io/github/languages/top/eli64s/readme-ai?flat) ![language-count](https://img.shields.io/github/languages/count/eli64s/readme-ai?flat) |
| `flat` | ![flat](https://img.shields.io/badge/Python-3776AB.svg?&style=flat&logo=Python&logoColor=white) |
| `flat-square` | ![flat-square](https://img.shields.io/badge/Python-3776AB.svg?&style=flat-square&logo=Python&logoColor=white) |
| `for-the-badge` | ![for-the-badge](https://img.shields.io/badge/Python-3776AB.svg?&style=for-the-badge&logo=Python&logoColor=white) |
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "readmeai"
version = "0.4.0978"
version = "0.4.0979"
description = "🚀 Generate beautiful README.md files from the terminal using GPT LLM APIs 💫"
authors = ["Eli <[email protected]>"]
license = "MIT"
Expand Down
3 changes: 2 additions & 1 deletion readmeai/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def prompt_for_custom_image(
type=click.Choice(
[opt.value for opt in BadgeOptions], case_sensitive=False
),
default="flat",
default="standard",
help="""\
Badge icon style types to select from when generating README.md badges. The following options are currently available:\n
- flat \n
Expand All @@ -69,6 +69,7 @@ def prompt_for_custom_image(
- skills \n
- skills-light \n
- social \n
- standard \n
""",
)
emojis = click.option(
Expand Down
1 change: 1 addition & 0 deletions readmeai/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class BadgeOptions(str, Enum):
SKILLS = "skills"
SKILLS_LIGHT = "skills-light"
SOCIAL = "social"
STANDARD = "standard"


class ImageOptions(str, Enum):
Expand Down
26 changes: 16 additions & 10 deletions readmeai/markdown/badges.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Functions for building and formatting the badges in the README.md file."""

from readmeai.config.settings import AppConfig, GitService
from readmeai.config.settings import AppConfig, BadgeOptions, GitService
from readmeai.core.factory import FileHandler
from readmeai.utils import utils

Expand All @@ -26,7 +26,7 @@ def format_html_badges(badges: list) -> str:
"""Formats the SVG icons into HTML <img src=""> tags."""
badge_lines = []
total_badges = len(badges)
if total_badges < 8:
if total_badges < 9:
badges_per_line = total_badges
else:
badges_per_line = total_badges // 2 + (total_badges % 2)
Expand All @@ -35,15 +35,15 @@ def format_html_badges(badges: list) -> str:
return ""

for i in range(0, total_badges, badges_per_line):
line = "\n".join(
line = "\n\t".join(
[
f'<img src="{badge}" alt="{badge.split("/badge/")[1].split("-")[0]}">'
for badge in badges[i : i + badges_per_line]
]
)
badge_lines.append(line)

return "\n\n".join(badge_lines)
return "\n\t<br>\n\t".join(badge_lines)


def shields_icons(conf: AppConfig, deps: list, full_name: str):
Expand All @@ -56,25 +56,31 @@ def shields_icons(conf: AppConfig, deps: list, full_name: str):
else:
git_host = GitService.extract_name_from_host(conf.git.source)

metadata_badges = conf.md.badges_shields.format(
git_host=git_host,
full_name=full_name,
badges_style=conf.md.badges_style,
)

if conf.md.badges_style == BadgeOptions.STANDARD.value:
return metadata_badges

badge_set = _read_badge_file(conf.files.shields_icons)
dependency_badges = build_html_badges(
deps,
badge_set,
conf.md.badges_style,
)
metadata_badges = conf.md.badges_shields.format(
git_host=git_host,
full_name=full_name,
badges_style=conf.md.badges_style,
)
return f"{dependency_badges}\n\n{metadata_badges}"

return metadata_badges, dependency_badges


def skill_icons(conf: AppConfig, deps: list) -> str:
"""
Generates badges for the README using skill icons, from the
repository - https://github.com/tandpfun/skill-icons.
"""
deps.extend(["md"])
icons_dict = _read_badge_file(conf.files.skill_icons)
icons_list = [
icon for icon in icons_dict["icons"]["names"] if icon in deps
Expand Down
13 changes: 10 additions & 3 deletions readmeai/markdown/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from pathlib import Path
from typing import List

from readmeai.config.settings import AppConfig, ConfigHelper, GitService
from readmeai.config.settings import (
AppConfig,
BadgeOptions,
ConfigHelper,
GitService,
)
from readmeai.core import factory
from readmeai.markdown import badges, tables
from readmeai.markdown.quickstart import getting_started
Expand Down Expand Up @@ -42,16 +47,18 @@ def format_readme_md(
else:
repo_source = repo_url

if "skills" not in conf.md.badges_style:
md_badges = badges.shields_icons(conf, deps, full_name)
if BadgeOptions.SKILLS.value not in conf.md.badges_style:
md_shields, md_badges = badges.shields_icons(conf, deps, full_name)
else:
md_shields = "<!-- Shields.io badges not used with skill icons. -->"
md_badges = badges.skill_icons(conf, deps)

md_header = conf.md.header.format(
alignment=conf.md.align,
image=conf.md.image,
repo_name=repo_name.upper(),
slogan=conf.md.slogan,
badges_shields=md_shields,
badges=md_badges,
)
formatted_code_summaries = tables.format_code_summaries(
Expand Down
20 changes: 8 additions & 12 deletions readmeai/settings/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tokens_max = 3899
# Markdown Templates
[md]
align = "center"
badges_style = "flat"
badges_style = "standard"
default = "<code>► INSERT-TEXT-HERE</code>"
image = ""
slogan = ""
Expand All @@ -49,25 +49,21 @@ header = """\
<p align="{alignment}">
<em>{slogan}</em>
</p>
<p align="{alignment}">\n\t{badges_shields}\n<p>
<p align="{alignment}">
<em>Developed with the software and tools below</em>
</p>
<p align="{alignment}">
{badges}
</p>
<p align="{alignment}">\n\t{badges}</p>
</div>
<hr>
"""
badges_shields = """\
badges_shields = """
<img src="https://img.shields.io/{git_host}/license/{full_name}?style={badges_style}" alt="license">
<img src="https://img.shields.io/{git_host}/last-commit/{full_name}?style={badges_style}" alt="last-commit">
<img src="https://img.shields.io/{git_host}/languages/top/{full_name}?style={badges_style}" alt="repo-top-language">
<img src="https://img.shields.io/{git_host}/languages/count/{full_name}?style={badges_style}" alt="repo-language-count">
\t<img src="https://img.shields.io/{git_host}/last-commit/{full_name}?style={badges_style}" alt="last-commit">
\t<img src="https://img.shields.io/{git_host}/languages/top/{full_name}?style={badges_style}" alt="repo-top-language">
\t<img src="https://img.shields.io/{git_host}/languages/count/{full_name}?style={badges_style}" alt="repo-language-count">
"""
badges_skills = """\
<a href="https://skillicons.dev">
<img src="{}">
</a>
badges_skills = """<a href="https://skillicons.dev">\n\t\t<img src="{}">\n\t</a>
"""

toc = """
Expand Down
17 changes: 10 additions & 7 deletions tests/test_markdown/test_badges.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ def test_shields_icons_success(config):
"3776AB",
],
}
result = shields_icons(mock_config, deps, "full_name")
assert "Python" in result
assert "3776AB" in result
assert "flat" in result
shields_badges, deps_badges = shields_icons(
mock_config, deps, "full_name"
)
assert "license" in shields_badges
assert "Python" in deps_badges
assert "3776AB" in deps_badges
assert "flat" in deps_badges


def test_skill_icons_success(config):
Expand All @@ -93,17 +96,17 @@ def test_skill_icons_success(config):
"icons": {"names": ["fastapi", "py", "redis", "md", "github", "git"]},
"url": {"base_url": "https://skillicons.dev/icons?i="},
}
deps = ["fastapi", "py", "redis", "md", "github", "git"]
deps = ["fastapi", "py", "redis", "github", "git"]

with patch("readmeai.markdown.badges._read_badge_file") as mock_read:
mock_read.return_value = mock_icons
result = skill_icons(mock_config, deps)
assert result.startswith("<a href=") is True
assert result.endswith("\n</a>\n") is True
assert result.endswith("\n\t</a>\n") is True
assert "&theme=light" in result
assert """<a href="https://skillicons.dev">""" in result
assert (
"""<img src="https://skillicons.dev/icons?i=fastapi,py,redis,md,github,git&theme=light">\n</a>\n"""
"""\n\t\t<img src="https://skillicons.dev/icons?i=fastapi,py,redis,md,github,git&theme=light">\n\t</a>\n"""
in result
)

Expand Down

0 comments on commit d0f98ad

Please sign in to comment.