Skip to content

Instantly share code, notes, and snippets.

@terremoth
Created December 10, 2021 20:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terremoth/423c6a48d0fef89133b3646623d4280d to your computer and use it in GitHub Desktop.
Save terremoth/423c6a48d0fef89133b3646623d4280d to your computer and use it in GitHub Desktop.
Add license badges to all projects in felipefialho/awesome-made-by-brazilians repo
<?php
$aFile = file('README.md', FILE_TEXT);
$licensed_file = fopen('NEW_README.md', 'wb+');
// Stub: [![license](https://img.shields.io/github/license/AUTHOR/REPO.svg)](/LICENSE)
// pattern to search = ![Stars](https://img.shields.io/github/stars/LarissaAbreu/contrata-se-dev.svg?style=flat-square)
foreach ($aFile as $line_num => $line) {
$matches = null;
if (preg_match('/\!\[Stars\].*\)/', $line, $matches)) {
$stars_line = $matches[0];
$star_url_parts = explode('/', $stars_line);
$author = $star_url_parts[5];
$repo_name_dirty = $star_url_parts[6];
$repo_name_clean = explode("?", $repo_name_dirty)[0]; // remove ?style=flat-square from the new URL
$repo_name_clean = implode('', explode(".svg", $repo_name_clean)); // remove .svg from repo name
$license_url = "[![license](https://img.shields.io/github/license/$author/$repo_name_clean.svg)](/LICENSE)";
echo $license_url.PHP_EOL;
fwrite($licensed_file, $line.' '.$license_url.PHP_EOL);
} else {
fwrite($licensed_file, $line);
}
}
fclose($licensed_file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment