NameBeautifier.java

/*
 * Copyright © 2026 The CTAN Team and individual authors
 *
 * This file is distributed under the 3-clause BSD license.
 * See file LICENSE for details.
 */

package org.ctan.site.services;

/**
 * The class <code>NameBeautifier</code> contains a translation method to adapt
 * the name of a package. The following rules are applied:
 *
 * <ul>
 * <li>If the name is <code>null</code> then compute a new value.</li>
 * <li>If the name is blank then compute a new value.</li>
 * <li>If the name is equal to the id/key then compute a new value.</li>
 * <li>If the name starts with an exclamation mark (!) then the remainder is
 * taken as name.</li>
 * <li>If the key contains the string <i>$key</i> then the match is replaced
 * with <i>$logo</i> and the first character of the name is translated to upper
 * case
 * <table style="border-bottom: 1pt solid black; border-top: 1pt solid black;
 * margin:1rem">
 * <tr style="border-bottom: .5pt solid black;">
 * <th>$key</th>
 * <th>$logo</th>
 * </tr>
 * <tr>
 * <td>ascii</td>
 * <td>ASCII</td>
 * </tr>
 * <tr>
 * <td>bakoma</td>
 * <td>BaKoMa</td>
 * </tr>
 * <tr>
 * <td>beamertheme</td>
 * <td>BeamerTheme</td>
 * </tr>
 * <tr>
 * <td>context</td>
 * <td>ConTeXt</td>
 * </tr>
 * <tr>
 * <td>dante</td>
 * <td>DANTE</td>
 * </tr>
 * <tr>
 * <td>latex</td>
 * <td>LaTeX</td>
 * </tr>
 * <tr>
 * <td>luatex</td>
 * <td>LuaTeX</td>
 * </tr>
 * <tr>
 * <td>lualatex</td>
 * <td>LuaLaTeX</td>
 * </tr>
 * <tr>
 * <td>pgf</td>
 * <td>PGF</td>
 * </tr>
 * <tr>
 * <td>pdf</td>
 * <td>PDF</td>
 * </tr>
 * <tr>
 * <td>pstricks</td>
 * <td>PSTricks</td>
 * </tr>
 * <tr>
 * <td>rcs</td>
 * <td>RCS</td>
 * </tr>
 * <tr>
 * <td>tikz</td>
 * <td>TikZ</td>
 * </tr>
 * <tr>
 * <td>xelatex</td>
 * <td>XeLaTeX</td>
 * </tr>
 * <tr>
 * <td>xetex</td>
 * <td>XeTeX</td>
 * </tr>
 * </table>
 * </li>
 *
 * <li>If the key starts with the string <i>$key</i> then the match is replaced
 * with <i>$logo</i> and the first character of the name is translated to upper
 * case
 * <table style="border-bottom: 1pt solid black; border-top: 1pt solid black;
 * margin:1rem">
 * <tr style="border-bottom: .5pt solid black;">
 * <th>$key</th>
 * <th>$logo</th>
 * </tr>
 * <tr>
 * <td>pst</td>
 * <td>PST</td>
 * </tr>
 * <tr>
 * <td>urw</td>
 * <td>URW</td>
 * </tr>
 * </table>
 * </li>
 *
 * <li>If the key starts with the string "babel-" then the match is replaced
 * with "Babel-" and the next character of the name is translated to upper case.
 *
 * <li>If the key starts with the string "ptex" then the match is replaced with
 * "pTeX" and returned.
 *
 * <li>Else only the first character of the name is translated to upper case.
 * </ul>
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
public class NameBeautifier {

    /**
     * The method <code>beautify</code> provides means to compute the beautified
     * name.
     *
     * @param id the package key
     * @param name the package name
     * @return the beautified name
     */
    public static String beautify(String id, String name) {

        if (name != null && name.startsWith("!")) {
            return name.substring(1);
        } else if (name != null && !name.isBlank() && !name.equals(id)) {
            return name;
        }

        name = id;
        if (name == null) {
            return "";
        } else if (name.startsWith("babel-")) {
            return "Babel-" + Character.toUpperCase(name.charAt(6))
                + name.substring(7);
        } else if (name.startsWith("ptex-")) {
            return "pTeX-" + name.substring(5);
        }
        name = name.replaceAll("^pst", "PST")
            .replaceAll("^urw", "URW")
            .replaceAll("ascii", "ASCII")
            .replaceAll("bakoma", "BaKoMa")
            .replaceAll("beamertheme", "BeamerTheme")
            .replaceAll("context", "ConTeXt")
            .replaceAll("dante", "DANTE")
            .replaceAll("latex", "LaTeX")
            .replaceAll("luatex", "LuaTeX")
            .replaceAll("lualatex", "LuaLaTeX")
            .replaceAll("pdf", "PDF")
            .replaceAll("pgf", "PGF")
            .replaceAll("pstricks", "PSTricks")
            .replaceAll("rcs", "RCS")
            .replaceAll("tikz", "TikZ")
            .replaceAll("xetex", "XeTeX")
            .replaceAll("xelatex", "XeLaTeX")
            .replaceAll("tex($|[^ti])", "TeX$1");

        return Character.toUpperCase(name.charAt(0)) + name.substring(1);
    }
}