Skip to content
This repository was archived by the owner on Aug 11, 2025. It is now read-only.

Commit c855208

Browse files
authored
Merge pull request #37 from nshan651/center_
Add center_* support
2 parents 03b8248 + 4c22f99 commit c855208

File tree

8 files changed

+166
-45
lines changed

8 files changed

+166
-45
lines changed

pybadges/__init__.py

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,25 @@ def _embed_image(url: str) -> str:
114114

115115
def badge(
116116
left_text: str,
117-
right_text: str,
117+
right_text: Optional[str] = None,
118118
left_link: Optional[str] = None,
119119
right_link: Optional[str] = None,
120+
center_link: Optional[str] = None,
120121
whole_link: Optional[str] = None,
121122
logo: Optional[str] = None,
122123
left_color: str = '#555',
123124
right_color: str = '#007ec6',
125+
center_color: Optional[str] = None,
124126
measurer: Optional[text_measurer.TextMeasurer] = None,
125-
embed_logo: bool = False,
126-
whole_title: Optional[str] = None,
127127
left_title: Optional[str] = None,
128128
right_title: Optional[str] = None,
129+
center_title: Optional[str] = None,
130+
whole_title: Optional[str] = None,
131+
right_image: Optional[str] = None,
132+
center_image: Optional[str] = None,
133+
embed_logo: bool = False,
134+
embed_right_image: bool = False,
135+
embed_center_image: bool = False,
129136
id_suffix: str = '',
130137
) -> str:
131138
"""Creates a github-style badge as an SVG image.
@@ -182,28 +189,55 @@ def badge(
182189
measurer = (
183190
precalculated_text_measurer.PrecalculatedTextMeasurer.default())
184191

185-
if (left_link or right_link) and whole_link:
192+
if (left_link or right_link or center_link) and whole_link:
186193
raise ValueError(
187-
'whole_link may not bet set with left_link or right_link')
188-
template = _JINJA2_ENVIRONMENT.get_template('badge-template-full.svg')
194+
'whole_link may not bet set with left_link, right_link, or center_link'
195+
)
196+
197+
if center_image and not (right_image or right_text):
198+
raise ValueError('cannot have a center_image without a right element')
199+
200+
if (center_image and not center_color) or (not center_image and
201+
center_color):
202+
raise ValueError('must have both a center_image and a center_color')
189203

190204
if logo and embed_logo:
191205
logo = _embed_image(logo)
192206

207+
if right_image and embed_right_image:
208+
right_image = _embed_image(right_image)
209+
210+
if center_image and embed_center_image:
211+
center_image = _embed_image(center_image)
212+
213+
if center_color:
214+
center_color = _NAME_TO_COLOR.get(center_color, center_color)
215+
216+
right_text_width = None
217+
if right_text:
218+
right_text_width = measurer.text_width(right_text) / 10.0
219+
220+
template = _JINJA2_ENVIRONMENT.get_template('badge-template-full.svg')
221+
193222
svg = template.render(
194223
left_text=left_text,
195224
right_text=right_text,
196225
left_text_width=measurer.text_width(left_text) / 10.0,
197-
right_text_width=measurer.text_width(right_text) / 10.0,
226+
right_text_width=right_text_width,
198227
left_link=left_link,
199228
right_link=right_link,
200229
whole_link=whole_link,
230+
center_link=center_link,
201231
logo=logo,
202232
left_color=_NAME_TO_COLOR.get(left_color, left_color),
203233
right_color=_NAME_TO_COLOR.get(right_color, right_color),
204-
whole_title=whole_title,
234+
center_color=center_color,
205235
left_title=left_title,
206236
right_title=right_title,
237+
center_title=center_title,
238+
whole_title=whole_title,
239+
right_image=right_image,
240+
center_image=center_image,
207241
id_suffix=id_suffix,
208242
)
209243
xml = minidom.parseString(svg)

pybadges/__main__.py

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import tempfile
2323
import webbrowser
2424

25+
sys.path.append('/home/nick/git/pybadges/')
2526
import pybadges
2627
from pybadges.version import __version__
2728

@@ -37,11 +38,8 @@ def main():
3738
help='the text to show on the left-hand-side of the badge')
3839
parser.add_argument(
3940
'--right-text',
40-
default='APACHE',
41+
default=None,
4142
help='the text to show on the right-hand-side of the badge')
42-
parser.add_argument('--whole-link',
43-
default=None,
44-
help='the url to redirect to when the badge is clicked')
4543
parser.add_argument(
4644
'--left-link',
4745
default=None,
@@ -52,6 +50,18 @@ def main():
5250
default=None,
5351
help='the url to redirect to when the right-hand of the badge is ' +
5452
'clicked')
53+
parser.add_argument(
54+
'--center-link',
55+
default=None,
56+
help='the url to redirect to when the center of the badge is ' +
57+
'clicked')
58+
parser.add_argument('--whole-link',
59+
default=None,
60+
help='the url to redirect to when the badge is clicked')
61+
parser.add_argument(
62+
'--logo',
63+
default=None,
64+
help='a URI reference to a logo to display in the badge')
5565
parser.add_argument(
5666
'--left-color',
5767
default='#555',
@@ -61,19 +71,9 @@ def main():
6171
default='#007ec6',
6272
help='the background color of the right-hand-side of the badge')
6373
parser.add_argument(
64-
'--logo',
74+
'--center-color',
6575
default=None,
66-
help='a URI reference to a logo to display in the badge')
67-
parser.add_argument(
68-
'--embed-logo',
69-
nargs='?',
70-
type=lambda x: x.lower() in ['y', 'yes', 't', 'true', '1', ''],
71-
const='yes',
72-
default='no',
73-
help='if the logo is specified then include the image data directly in '
74-
'the badge (this will prevent a URL fetch and may work around the '
75-
'fact that some browsers do not fetch external image references); '
76-
'only works if --logo is a HTTP/HTTPS URI or a file path')
76+
help='the background color of the right-hand-side of the badge')
7777
parser.add_argument('--browser',
7878
action='store_true',
7979
default=False,
@@ -91,11 +91,6 @@ def main():
9191
help='the path to the ttf font file containing DejaVu Sans. If not ' +
9292
'present on your system, you can download it from ' +
9393
'https://www.fontsquirrel.com/fonts/dejavu-sans')
94-
parser.add_argument(
95-
'--whole-title',
96-
default=None,
97-
help='the title to associate with the entire badge. See '
98-
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
9994
parser.add_argument(
10095
'--left-title',
10196
default=None,
@@ -106,16 +101,67 @@ def main():
106101
default=None,
107102
help='the title to associate with the right part of the badge. See '
108103
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
104+
parser.add_argument(
105+
'--center-title',
106+
default=None,
107+
help='the title to associate with the center part of the badge. See '
108+
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
109+
parser.add_argument(
110+
'--whole-title',
111+
default=None,
112+
help='the title to associate with the entire badge. See '
113+
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
114+
parser.add_argument(
115+
'--right-image',
116+
default=None,
117+
help='the image to associate with the right-hand side of the badge')
118+
parser.add_argument(
119+
'--center-image',
120+
default=None,
121+
help='the image to associate with the center of the badge')
122+
parser.add_argument(
123+
'--embed-logo',
124+
nargs='?',
125+
type=lambda x: x.lower() in ['y', 'yes', 't', 'true', '1', ''],
126+
const='yes',
127+
default='no',
128+
help='if the logo is specified then include the image data directly in '
129+
'the badge (this will prevent a URL fetch and may work around the '
130+
'fact that some browsers do not fetch external image references); '
131+
'only works if --logo is a HTTP/HTTPS URI or a file path')
132+
parser.add_argument(
133+
'--embed-right-image',
134+
nargs='?',
135+
type=lambda x: x.lower() in ['y', 'yes', 't', 'true', '1', ''],
136+
const='yes',
137+
default='no',
138+
help=
139+
'if the right image is specified then include the image data directly in '
140+
'the badge (this will prevent a URL fetch and may work around the '
141+
'fact that some browsers do not fetch external image references); '
142+
'only works if --logo is a HTTP/HTTPS URI or a file path')
143+
parser.add_argument(
144+
'--embed-center-image',
145+
nargs='?',
146+
type=lambda x: x.lower() in ['y', 'yes', 't', 'true', '1', ''],
147+
const='yes',
148+
default='no',
149+
help=
150+
'if the center image is specified then include the image data directly in '
151+
'the badge (this will prevent a URL fetch and may work around the '
152+
'fact that some browsers do not fetch external image references); '
153+
'only works if --logo is a HTTP/HTTPS URI or a file path')
109154
parser.add_argument(
110155
'-v',
111156
'--version',
112157
action='version',
113158
version='%(prog)s {version}'.format(version=__version__))
114159
args = parser.parse_args()
115160

116-
if (args.left_link or args.right_link) and args.whole_link:
161+
if (args.left_link or args.right_link or
162+
args.center_link) and args.whole_link:
117163
print('argument --whole-link: cannot be set with ' +
118-
'--left-link or --right-link',
164+
'--left-link, --right-link, or --center_link',
119165
file=sys.stderr)
120166
sys.exit(1)
121167

@@ -133,15 +179,22 @@ def main():
133179
right_text=args.right_text,
134180
left_link=args.left_link,
135181
right_link=args.right_link,
182+
center_link=args.center_link,
136183
whole_link=args.whole_link,
184+
logo=args.logo,
137185
left_color=args.left_color,
138186
right_color=args.right_color,
139-
logo=args.logo,
187+
center_color=args.center_color,
140188
measurer=measurer,
141-
embed_logo=args.embed_logo,
142-
whole_title=args.whole_title,
143189
left_title=args.left_title,
144-
right_title=args.right_title)
190+
right_title=args.right_title,
191+
center_title=args.center_title,
192+
whole_title=args.whole_title,
193+
right_image=args.right_image,
194+
center_image=args.center_image,
195+
embed_logo=args.embed_logo,
196+
embed_right_image=args.embed_right_image,
197+
embed_center_image=args.embed_center_image)
145198

146199
if args.browser:
147200
_, badge_path = tempfile.mkstemp(suffix='.svg')

pybadges/badge-template-full.svg

Lines changed: 33 additions & 7 deletions
Loading

tests/golden-images/accuracy.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)