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

de.undercouch.citeproc.csl.internal.format.HtmlFormat Maven / Gradle / Ivy

package de.undercouch.citeproc.csl.internal.format;

import de.undercouch.citeproc.csl.internal.RenderContext;
import de.undercouch.citeproc.csl.internal.SBibliography;
import de.undercouch.citeproc.csl.internal.TokenBuffer;
import de.undercouch.citeproc.csl.internal.token.DisplayGroupToken;
import de.undercouch.citeproc.csl.internal.token.Token;
import de.undercouch.citeproc.output.Bibliography;
import de.undercouch.citeproc.output.SecondFieldAlign;
import org.apache.commons.text.StringEscapeUtils;

import java.util.List;

import static de.undercouch.citeproc.csl.internal.behavior.FormattingAttributes.FS_ITALIC;
import static de.undercouch.citeproc.csl.internal.behavior.FormattingAttributes.FW_BOLD;
import static de.undercouch.citeproc.csl.internal.behavior.FormattingAttributes.VA_SUP;

/**
 * The HTML output format
 * @author Michel Kraemer
 */
public class HtmlFormat extends BaseFormat {
    @Override
    public String getName() {
        return "html";
    }

    @Override
    protected String doFormatCitation(TokenBuffer buffer, RenderContext ctx) {
        return format(buffer);
    }

    @Override
    protected String doFormatBibliographyEntry(TokenBuffer buffer,
            RenderContext ctx, int index) {
        String result;

        SecondFieldAlign sfa = ctx.getStyle().getBibliography().getSecondFieldAlign();
        if (sfa != SecondFieldAlign.FALSE && !buffer.getTokens().isEmpty()) {
            // find tokens that are part of the first field
            int i = 0;
            List tokens = buffer.getTokens();
            while (i < tokens.size() && tokens.get(i).isFirstField()) {
                ++i;
            }
            TokenBuffer firstBuffer = buffer.copy(0, i);
            TokenBuffer restBuffer = buffer.copy(i, tokens.size());

            // render first field and rest independently
            result = "\n    
" + format(firstBuffer) + "
" + format(restBuffer) + "
\n "; } else { result = format(buffer); } return "
" + result + "
\n"; } @Override protected String doFormatLink(String text, String uri) { return "" + text + ""; } @Override public Bibliography makeBibliography(String[] entries, SBibliography bibliographyElement) { SecondFieldAlign sfa = bibliographyElement.getSecondFieldAlign(); return new Bibliography(entries, "
\n", "
", null, null, null, null, null, null, sfa); } @Override protected String escape(String str) { return StringEscapeUtils.escapeHtml4(str); } @Override protected String openFontStyle(int fontStyle) { if (fontStyle == FS_ITALIC) { return ""; } else { return ""; } } @Override protected String closeFontStyle(int fontStyle) { return ""; } @Override protected String openFontVariant(int fontVariant) { return ""; } @Override protected String closeFontVariant(int fontVariant) { return ""; } @Override protected String openFontWeight(int fontWeight) { if (fontWeight == FW_BOLD) { return ""; } else { return ""; } } @Override protected String closeFontWeight(int fontWeight) { return ""; } @Override protected String openTextDecoration(int textDecoration) { return ""; } @Override protected String closeTextDecoration(int textDecoration) { return ""; } @Override protected String openVerticalAlign(int verticalAlign) { if (verticalAlign == VA_SUP) { return ""; } else { return ""; } } @Override protected String closeVerticalAlign(int verticalAlign) { if (verticalAlign == VA_SUP) { return ""; } else { return ""; } } @Override protected String openDisplayGroup(DisplayGroupToken.Type type) { switch (type) { case BLOCK: return "\n\n
"; case LEFT_MARGIN: return "\n
"; case RIGHT_INLINE: return "
"; case INDENT: return "
"; } return null; } @Override protected String closeDisplayGroup(DisplayGroupToken.Type type) { switch (type) { case BLOCK: return "
\n"; case LEFT_MARGIN: return "
"; case RIGHT_INLINE: case INDENT: return "
\n "; } return null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy