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

info.bliki.wiki.tags.MathTag Maven / Gradle / Ivy

The newest version!
package info.bliki.wiki.tags;

import info.bliki.wiki.filter.ITextConverter;
import info.bliki.wiki.model.IWikiModel;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * Wiki tag rendering TeX math
 *
 */
public class MathTag extends NowikiTag {
    public MathTag() {
        super("math");
    }

    @Override
    public void renderHTML(ITextConverter converter, Appendable writer, IWikiModel model) throws IOException {
        if (model.isMathtranRenderer()) {
            // use the www.mathtran.org rendering service
            String content = getBodyString();
            if (content != null && content.length() > 0) {
                String sizeStr = "D=3";
                Map tagAtttributes = getAttributes();
                String attributeValue = tagAtttributes.get("d");
                if (attributeValue != null) {
                    try {
                        int size = Integer.parseInt(attributeValue);
                        if (size > 0 && size <= 10) {
                            sizeStr = "D=" + size;
                        }
                    } catch (NumberFormatException nfe) {
                        // ignore attribute
                    }
                }
                String texFormula = "http://www.mathtran.org/cgi-bin/mathtran?" + sizeStr + "&tex="
                        + URLEncoder.encode(content, UTF_8.name());
                writer.append("\"");");
            }
        } else {
            // prepare for jsMath client side renderer
            String content = getBodyString();
            if (content != null && content.length() > 0) {
                writer.append("");
                copyMathLTGT(content, writer);
                writer.append("");
            }
        }
    }

    @Override
    public boolean isReduceTokenStack() {
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy