Skip to content

Commit

Permalink
Merge pull request #37 from nshan651/center_
Browse files Browse the repository at this point in the history
Add center_* support
  • Loading branch information
brianquinlan committed Oct 27, 2022
2 parents 03b8248 + 4c22f99 commit c855208
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 45 deletions.
50 changes: 42 additions & 8 deletions pybadges/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,25 @@ def _embed_image(url: str) -> str:

def badge(
left_text: str,
right_text: str,
right_text: Optional[str] = None,
left_link: Optional[str] = None,
right_link: Optional[str] = None,
center_link: Optional[str] = None,
whole_link: Optional[str] = None,
logo: Optional[str] = None,
left_color: str = '#555',
right_color: str = '#007ec6',
center_color: Optional[str] = None,
measurer: Optional[text_measurer.TextMeasurer] = None,
embed_logo: bool = False,
whole_title: Optional[str] = None,
left_title: Optional[str] = None,
right_title: Optional[str] = None,
center_title: Optional[str] = None,
whole_title: Optional[str] = None,
right_image: Optional[str] = None,
center_image: Optional[str] = None,
embed_logo: bool = False,
embed_right_image: bool = False,
embed_center_image: bool = False,
id_suffix: str = '',
) -> str:
"""Creates a github-style badge as an SVG image.
Expand Down Expand Up @@ -182,28 +189,55 @@ def badge(
measurer = (
precalculated_text_measurer.PrecalculatedTextMeasurer.default())

if (left_link or right_link) and whole_link:
if (left_link or right_link or center_link) and whole_link:
raise ValueError(
'whole_link may not bet set with left_link or right_link')
template = _JINJA2_ENVIRONMENT.get_template('badge-template-full.svg')
'whole_link may not bet set with left_link, right_link, or center_link'
)

if center_image and not (right_image or right_text):
raise ValueError('cannot have a center_image without a right element')

if (center_image and not center_color) or (not center_image and
center_color):
raise ValueError('must have both a center_image and a center_color')

if logo and embed_logo:
logo = _embed_image(logo)

if right_image and embed_right_image:
right_image = _embed_image(right_image)

if center_image and embed_center_image:
center_image = _embed_image(center_image)

if center_color:
center_color = _NAME_TO_COLOR.get(center_color, center_color)

right_text_width = None
if right_text:
right_text_width = measurer.text_width(right_text) / 10.0

template = _JINJA2_ENVIRONMENT.get_template('badge-template-full.svg')

svg = template.render(
left_text=left_text,
right_text=right_text,
left_text_width=measurer.text_width(left_text) / 10.0,
right_text_width=measurer.text_width(right_text) / 10.0,
right_text_width=right_text_width,
left_link=left_link,
right_link=right_link,
whole_link=whole_link,
center_link=center_link,
logo=logo,
left_color=_NAME_TO_COLOR.get(left_color, left_color),
right_color=_NAME_TO_COLOR.get(right_color, right_color),
whole_title=whole_title,
center_color=center_color,
left_title=left_title,
right_title=right_title,
center_title=center_title,
whole_title=whole_title,
right_image=right_image,
center_image=center_image,
id_suffix=id_suffix,
)
xml = minidom.parseString(svg)
Expand Down
107 changes: 80 additions & 27 deletions pybadges/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import tempfile
import webbrowser

sys.path.append('/home/nick/git/pybadges/')
import pybadges
from pybadges.version import __version__

Expand All @@ -37,11 +38,8 @@ def main():
help='the text to show on the left-hand-side of the badge')
parser.add_argument(
'--right-text',
default='APACHE',
default=None,
help='the text to show on the right-hand-side of the badge')
parser.add_argument('--whole-link',
default=None,
help='the url to redirect to when the badge is clicked')
parser.add_argument(
'--left-link',
default=None,
Expand All @@ -52,6 +50,18 @@ def main():
default=None,
help='the url to redirect to when the right-hand of the badge is ' +
'clicked')
parser.add_argument(
'--center-link',
default=None,
help='the url to redirect to when the center of the badge is ' +
'clicked')
parser.add_argument('--whole-link',
default=None,
help='the url to redirect to when the badge is clicked')
parser.add_argument(
'--logo',
default=None,
help='a URI reference to a logo to display in the badge')
parser.add_argument(
'--left-color',
default='#555',
Expand All @@ -61,19 +71,9 @@ def main():
default='#007ec6',
help='the background color of the right-hand-side of the badge')
parser.add_argument(
'--logo',
'--center-color',
default=None,
help='a URI reference to a logo to display in the badge')
parser.add_argument(
'--embed-logo',
nargs='?',
type=lambda x: x.lower() in ['y', 'yes', 't', 'true', '1', ''],
const='yes',
default='no',
help='if the logo is specified then include the image data directly in '
'the badge (this will prevent a URL fetch and may work around the '
'fact that some browsers do not fetch external image references); '
'only works if --logo is a HTTP/HTTPS URI or a file path')
help='the background color of the right-hand-side of the badge')
parser.add_argument('--browser',
action='store_true',
default=False,
Expand All @@ -91,11 +91,6 @@ def main():
help='the path to the ttf font file containing DejaVu Sans. If not ' +
'present on your system, you can download it from ' +
'https://www.fontsquirrel.com/fonts/dejavu-sans')
parser.add_argument(
'--whole-title',
default=None,
help='the title to associate with the entire badge. See '
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
parser.add_argument(
'--left-title',
default=None,
Expand All @@ -106,16 +101,67 @@ def main():
default=None,
help='the title to associate with the right part of the badge. See '
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
parser.add_argument(
'--center-title',
default=None,
help='the title to associate with the center part of the badge. See '
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
parser.add_argument(
'--whole-title',
default=None,
help='the title to associate with the entire badge. See '
'https://developer.mozilla.org/en-US/docs/Web/SVG/Element/title')
parser.add_argument(
'--right-image',
default=None,
help='the image to associate with the right-hand side of the badge')
parser.add_argument(
'--center-image',
default=None,
help='the image to associate with the center of the badge')
parser.add_argument(
'--embed-logo',
nargs='?',
type=lambda x: x.lower() in ['y', 'yes', 't', 'true', '1', ''],
const='yes',
default='no',
help='if the logo is specified then include the image data directly in '
'the badge (this will prevent a URL fetch and may work around the '
'fact that some browsers do not fetch external image references); '
'only works if --logo is a HTTP/HTTPS URI or a file path')
parser.add_argument(
'--embed-right-image',
nargs='?',
type=lambda x: x.lower() in ['y', 'yes', 't', 'true', '1', ''],
const='yes',
default='no',
help=
'if the right image is specified then include the image data directly in '
'the badge (this will prevent a URL fetch and may work around the '
'fact that some browsers do not fetch external image references); '
'only works if --logo is a HTTP/HTTPS URI or a file path')
parser.add_argument(
'--embed-center-image',
nargs='?',
type=lambda x: x.lower() in ['y', 'yes', 't', 'true', '1', ''],
const='yes',
default='no',
help=
'if the center image is specified then include the image data directly in '
'the badge (this will prevent a URL fetch and may work around the '
'fact that some browsers do not fetch external image references); '
'only works if --logo is a HTTP/HTTPS URI or a file path')
parser.add_argument(
'-v',
'--version',
action='version',
version='%(prog)s {version}'.format(version=__version__))
args = parser.parse_args()

if (args.left_link or args.right_link) and args.whole_link:
if (args.left_link or args.right_link or
args.center_link) and args.whole_link:
print('argument --whole-link: cannot be set with ' +
'--left-link or --right-link',
'--left-link, --right-link, or --center_link',
file=sys.stderr)
sys.exit(1)

Expand All @@ -133,15 +179,22 @@ def main():
right_text=args.right_text,
left_link=args.left_link,
right_link=args.right_link,
center_link=args.center_link,
whole_link=args.whole_link,
logo=args.logo,
left_color=args.left_color,
right_color=args.right_color,
logo=args.logo,
center_color=args.center_color,
measurer=measurer,
embed_logo=args.embed_logo,
whole_title=args.whole_title,
left_title=args.left_title,
right_title=args.right_title)
right_title=args.right_title,
center_title=args.center_title,
whole_title=args.whole_title,
right_image=args.right_image,
center_image=args.center_image,
embed_logo=args.embed_logo,
embed_right_image=args.embed_right_image,
embed_center_image=args.embed_center_image)

if args.browser:
_, badge_path = tempfile.mkstemp(suffix='.svg')
Expand Down
40 changes: 33 additions & 7 deletions pybadges/badge-template-full.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/golden-images/accuracy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c855208

Please sign in to comment.