Added all necessary parameters to __main__; work on badge template in progress

This commit is contained in:
nshan651
2022-10-04 11:03:47 -05:00
parent 05f8734b9a
commit ba57b76687
3 changed files with 108 additions and 32 deletions

View File

@@ -117,15 +117,22 @@ def badge(
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: str = '#555',
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.
@@ -182,14 +189,25 @@ 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')
'whole_link may not bet set with left_link, right_link, or center_link'
)
if right_image and center_image:
raise ValueError('cannot have both right_image and center_image')
template = _JINJA2_ENVIRONMENT.get_template('badge-template-full.svg')
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)
right_text_width = None
if right_text:
right_text_width = measurer.text_width(right_text) / 10.0
@@ -202,12 +220,17 @@ def badge(
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=_NAME_TO_COLOR.get(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)

View File

@@ -39,9 +39,6 @@ def main():
'--right-text',
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,
@@ -52,6 +49,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',
@@ -61,19 +70,9 @@ def main():
default='#007ec6',
help='the background color of the right-hand-side of the badge')
parser.add_argument(
'--logo',
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')
'--center-color',
default='#555',
help='the background color of the right-hand-side of the badge')
parser.add_argument('--browser',
action='store_true',
default=False,
@@ -91,11 +90,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,
@@ -106,6 +100,54 @@ 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',
@@ -113,9 +155,9 @@ def main():
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)
@@ -133,15 +175,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')

View File

@@ -2,6 +2,7 @@
{% set logo_padding = 3 if (logo and left_text) else 0 %}
{% set left_width = left_text_width + 10 + logo_width + logo_padding %}
{% set right_width = right_text_width + 10 if right_text_width else 0 %}
{% set image_width = 107 if right_image else 0 %}
{% set id_smooth = 'smooth' + id_suffix %}
{% set id_round = 'round' + id_suffix %}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{{ left_width + right_width }}" height="20">
@@ -37,6 +38,9 @@
{% endif %}
<text x="{{ (((left_width+logo_width+logo_padding)/2)+1)*10 }}" y="150" fill="#010101" fill-opacity=".3" transform="scale(0.1)" textLength="{{ (left_width-(10+logo_width+logo_padding))*10 }}" lengthAdjust="spacing">{{ left_text }}</text>
<text x="{{ (((left_width+logo_width+logo_padding)/2)+1)*10 }}" y="140" transform="scale(0.1)" textLength="{{ (left_width-(10+logo_width+logo_padding))*10 }}" lengthAdjust="spacing">{{ left_text }}</text>
{% if right_image %}
<image x="{{ left_width-image_width }}" y="3" width="{{ image_width }}" height="14" xlink:href="{{ right_image }}"/>
{% endif %}
{% if right_text %}
<text x="{{ (left_width+right_width/2-1)*10 }}" y="150" fill="#010101" fill-opacity=".3" transform="scale(0.1)" textLength="{{ (right_width-10)*10 }}" lengthAdjust="spacing">{{ right_text }}</text>
<text x="{{ (left_width+right_width/2-1)*10 }}" y="140" transform="scale(0.1)" textLength="{{ (right_width-10)*10 }}" lengthAdjust="spacing">{{ right_text }}</text>