All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.somath.publisher.formatter.HtmlFormatter Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package se.somath.publisher.formatter;

import java.util.LinkedList;
import java.util.List;

public class HtmlFormatter {
    private StringBuilder accumulator = null;
    private List formatted = new LinkedList();

    public List format(List unFormattedContent) {
        boolean inComment = false;
        boolean inParagraph = false;
        boolean inListItem = false;

        for (String line : unFormattedContent) {
            inComment = foundCommentOpening(inComment, line);
            addParagraphBeforeComment(inComment, inParagraph);

            inParagraph = foundParagraphOpening(inComment, inParagraph, line);
            inListItem = foundListItemOpening(inComment, inListItem, line);

            if (inParagraph && !inComment) {
                inParagraph = addParagraphLine(line);
                continue;
            }

            if (inListItem && !inComment) {
                inListItem = addListItemLine(line);
                continue;
            }

            inComment = foundCommentClosing(inComment, line);

            formatted.add(line);
        }

        return formatted;
    }

    private boolean foundCommentOpening(boolean inComment, String line) {
        if (line.contains("")) {
            inComment = false;
        }
        return inComment;
    }

    private boolean addAccumulatedLinesIfEndFound(String line, String endTag) {
        String trimmedLine = addTrimmedLine(line);
        if (trimmedLine.contains(endTag)) {
            formatted.add(accumulator.toString());
            accumulator = null;
            return false;
        }
        return true;
    }

    private String addTrimmedLine(String line) {
        String trimmedLine = line.trim();
        accumulator.append(trimmedLine);
        accumulator.append(" ");
        return trimmedLine;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy