001/*
002 * JDrupes MDoclet
003 * Copyright 2013 Raffael Herzog
004 * Copyright (C) 2017 Michael N. Lipp
005 * 
006 * This program is free software; you can redistribute it and/or modify it 
007 * under the terms of the GNU General Public License as published by 
008 * the Free Software Foundation; either version 3 of the License, or 
009 * (at your option) any later version.
010 * 
011 * This program is distributed in the hope that it will be useful, but 
012 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
013 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
014 * for more details.
015 * 
016 * You should have received a copy of the GNU General Public License along 
017 * with this program; if not, see <http://www.gnu.org/licenses/>.
018 */
019package org.jdrupes.mdoclet.renderers;
020
021/**
022 * Utilities for tag rendering.
023 */
024public final class TagRendering {
025
026    private TagRendering() {
027    }
028
029    /**
030     * Removes the `<p>` tag if the given HTML contains only one paragraph.
031     *
032     * **Note:** This implementation may be a bit simplistic: If the HTML starts with a
033     * `<p>` tag, it will be removed. If it ends with `</p>`, this one will be removed,
034     * too. In 99% of the cases, this *exactly* what we wanted. It some special cases,
035     * the result may be invalid HTML -- it should never break things, however, because
036     * HTML is designed to handle "forgotten" tags gracefully.
037     *
038     * @return The HTML without leading `<p>` or trailing `</p>`.
039     */
040    public static String simplifySingleParagraph(String html) {
041        html = html.trim();
042        String upper = html.toUpperCase();
043        if ( upper.startsWith("<P>") ) {
044            html = html.substring(3);
045        }
046        if ( upper.endsWith("</P>") ) {
047            html = html.substring(0, html.length() - 4);
048        }
049        return html;
050    }
051
052}