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

org.sweble.wikitext.engine.utils.HtmlPrinter Maven / Gradle / Ivy

Go to download

A minimal engine using the Sweble Wikitext Parser to process articles in the context of a MediaWiki-like configuration.

There is a newer version: 3.1.9
Show newest version
/* 
 * This file is auto-generated.
 * DO NOT MODIFY MANUALLY!
 * 
 * Generated by AstNodeGenerator.
 * Last generated: 2014-09-12 00:38:33.
 */

package org.sweble.wikitext.engine.utils;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import de.fau.cs.osr.ptk.common.ast.*;



import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.sweble.wikitext.engine.Page;
import org.sweble.wikitext.lazy.encval.*;
import org.sweble.wikitext.lazy.parser.*;
import org.sweble.wikitext.lazy.preprocessor.*;
import org.sweble.wikitext.lazy.utils.*;



public class HtmlPrinter
        extends
        	de.fau.cs.osr.ptk.common.PrinterBase
        
{
	public static String print(AstNode node, String articleTitle)
	{
		StringWriter writer = new StringWriter();
		new HtmlPrinter(writer, articleTitle).go(node);
		return writer.toString();
	}
	
	public static Writer print(Writer writer, AstNode node, String articleTitle)
	{
		new HtmlPrinter(writer, articleTitle).go(node);
		return writer;
	}


	
	// =========================================================================
	
	public void visit(AstNode astNode) throws IOException
	{
		print("");
		print(astNode.getClass().getSimpleName());
		print("");

	}
	public void visit(NodeList l) throws IOException
	{
		iterate(l);

	}
	public void visit(Page page) throws IOException
	{
		printNewline(false);
		if (standaloneHtml) {
			print("");
			printNewline(true);
			print("");
			printNewline(true);
			print("");
			printNewline(true);
			print("\t");
			printNewline(true);
			print("\t\t");
			printNewline(true);
			print("\t\t");
			print(escHtml(articleTitle));
			print("");
			printNewline(false);
			if (cssResource != null) {
				print("\t");
			} else if (cssFile != null) {
				print("\t");
			} else if (cssLink != null) {
				print("\t");
			}
			printNewline(false);
			print("\t");
			printNewline(true);
			print("\t");
			printNewline(true);
		}
		print("
"); printNewline(true); print("\t

"); print(escHtml(articleTitle)); print("

"); printNewline(true); print("\t
"); printNewline(false); incIndent("\t\t"); iterate(page.getContent()); decIndent(); printNewline(false); print("\t
"); printNewline(true); print("
"); printNewline(true); print("
"); printNewline(false); if (standaloneHtml) { print("\t"); printNewline(true); print(""); } printNewline(false); printNewline(false); } public void visit(Text text) throws IOException { print(escHtml(text.getContent())); } public void visit(Italics n) throws IOException { print(""); iterate(n.getContent()); print(""); } public void visit(Bold n) throws IOException { print(""); iterate(n.getContent()); print(""); } public void visit(Whitespace n) throws IOException { iterate(n.getContent()); } public void visit(Paragraph p) throws IOException { printNewline(false); renderBlockLevelElementsFirst(p); printNewline(false); if (!isParagraphEmpty(p)) { print("

"); printNewline(false); incIndent("\t"); iterate(p.getContent()); decIndent(); printNewline(false); print("

"); } printNewline(false); printNewline(false); } public void visit(SemiPre sp) throws IOException { printNewline(false); print("
");
		iterate(sp.getContent());
		print("
"); printNewline(false); } public void visit(SemiPreLine line) throws IOException { iterate(line.getContent()); print("\n"); } public void visit(Section s) throws IOException { printNewline(false); print("
"); printNewline(true); print("\t"); iterate(s.getTitle()); print(""); printNewline(true); print("\t
"); printNewline(false); incIndent("\t\t"); iterate(s.getBody()); decIndent(); printNewline(false); print("\t
"); printNewline(true); print("
"); printNewline(false); } public void visit(XmlComment e) throws IOException { } public void visit(XmlElement e) throws IOException { print("<"); print(e.getName()); iterate(e.getXmlAttributes()); if (e.getEmpty()) { print(" />"); } else { print(">"); iterate(e.getBody()); print(""); } } public void visit(XmlAttribute a) throws IOException { print(" "); print(a.getName()); print("=\""); iterate(a.getValue()); print("\""); } public void visit(XmlAttributeGarbage g) throws IOException { } public void visit(XmlCharRef ref) throws IOException { print("&#"); print(ref.getCodePoint()); print(";"); } public void visit(XmlEntityRef ref) throws IOException { print("&"); print(ref.getName()); print(";"); } public void visit(DefinitionList n) throws IOException { printNewline(false); print("
"); printNewline(false); incIndent("\t"); iterate(n.getContent()); decIndent(); printNewline(false); print("
"); printNewline(false); } public void visit(DefinitionTerm n) throws IOException { printNewline(false); print("
"); iterate(n.getContent()); print("
"); printNewline(false); } public void visit(DefinitionDefinition n) throws IOException { printNewline(false); print("
"); iterate(n.getContent()); print("
"); printNewline(false); } public void visit(Enumeration n) throws IOException { printNewline(false); print("
    "); printNewline(false); incIndent("\t"); iterate(n.getContent()); decIndent(); printNewline(false); print("
"); printNewline(false); } public void visit(EnumerationItem n) throws IOException { printNewline(false); print("
  • "); iterate(n.getContent()); print("
  • "); printNewline(false); } public void visit(Itemization n) throws IOException { printNewline(false); print("
      "); printNewline(false); incIndent("\t"); iterate(n.getContent()); decIndent(); printNewline(false); print("
    "); printNewline(false); } public void visit(ItemizationItem n) throws IOException { printNewline(false); print("
  • "); iterate(n.getContent()); print("
  • "); printNewline(false); } public void visit(ExternalLink link) throws IOException { print(""); if (!link.getTitle().isEmpty()) { iterate(link.getTitle()); } else { printExternalLinkNumber(link); } print(""); } public void visit(Url url) throws IOException { print(""); print(url.getProtocol()); print(":"); print(url.getPath()); print(""); } public void visit(InternalLink n) throws IOException { print(""); print(n.getPrefix()); if (n.getTitle().getContent().isEmpty()) { print(makeLinkTitle(n)); } else { iterate(n.getTitle().getContent()); } print(n.getPostfix()); print(""); } public void visit(Table table) throws IOException { printNewline(false); print(""); printNewline(false); incIndent("\t"); iterate(table.getBody()); decIndent(); printNewline(false); printNewline(true); print(""); printNewline(false); } public void visit(TableCaption caption) throws IOException { printNewline(false); print(""); printNewline(false); incIndent("\t"); iterate(caption.getBody()); decIndent(); printNewline(false); print(""); printNewline(false); } public void visit(TableRow row) throws IOException { printNewline(false); print(""); printNewline(false); incIndent("\t"); iterate(row.getBody()); decIndent(); printNewline(false); print(""); printNewline(false); } public void visit(TableHeader header) throws IOException { printNewline(false); print(""); printNewline(false); incIndent("\t"); iterate(header.getBody()); decIndent(); printNewline(false); print(""); printNewline(false); } public void visit(TableCell cell) throws IOException { printNewline(false); print(""); printNewline(false); incIndent("\t"); iterate(cell.getBody()); decIndent(); printNewline(false); print(""); printNewline(false); } public void visit(HorizontalRule rule) throws IOException { printNewline(false); print("
    "); printNewline(false); } public void visit(Signature sig) throws IOException { print(""); print(makeSignature(sig)); print(""); } public void visit(Redirect n) throws IOException { print("↳ "); print(n.getTarget()); print(""); } public void visit(IllegalCodePoint n) throws IOException { print(""); print(asXmlCharRefs(n.getCodePoint())); print(""); } public void visit(MagicWord n) throws IOException { print("__"); print(n.getWord()); print("__"); } public void visit(TagExtension n) throws IOException { print(""); if (renderTagExtensions) { if (n.getBody().isEmpty()) { print("<"); print(n.getName()); iterate(n.getXmlAttributes()); print(" />"); } else { print("<"); print(n.getName()); iterate(n.getXmlAttributes()); print(">"); print(escHtml(n.getBody())); print("</"); print(n.getName()); print(">"); } } else { if (n.getXmlAttributes().isEmpty()) { print("<"); print(n.getName()); } else { print("<"); print(n.getName()); print(" ..."); } if (n.getBody().isEmpty()) { print("/>"); } else { print(">...</"); print(n.getName()); print(">"); } } print(""); } public void visit(XmlElementEmpty e) throws IOException { print(""); print("<"); print(e.getName()); iterate(e.getXmlAttributes()); print(" />"); print(""); } public void visit(XmlElementOpen e) throws IOException { print(""); print("<"); print(e.getName()); iterate(e.getXmlAttributes()); print(">"); print(""); } public void visit(XmlElementClose e) throws IOException { print(""); print("</"); print(e.getName()); print(">"); print(""); } public void visit(Template tmpl) throws IOException { print(""); if (renderTemplates) { print("{"); print("{"); iterate(tmpl.getName()); iterate(tmpl.getArgs()); print("}}"); } else { if (tmpl.getArgs().isEmpty()) { print("{"); print("{"); iterate(tmpl.getName()); print("}}"); } else { print("{"); print("{"); iterate(tmpl.getName()); print("|...}}"); } } print(""); } public void visit(TemplateParameter param) throws IOException { print(""); if (renderTemplates) { print("{"); print("{"); print("{"); iterate(param.getName()); dispatch(param.getDefaultValue()); iterate(param.getGarbage()); print("}}}"); } else { if (param.getDefaultValue() == null) { print("{"); print("{"); print("{"); iterate(param.getName()); print("}}}"); } else { print("{"); print("{"); print("{"); iterate(param.getName()); print("|...}}}"); } } print(""); } public void visit(TemplateArgument arg) throws IOException { print("|"); if (arg.getHasName()) { iterate(arg.getValue()); } else { iterate(arg.getName()); print("="); iterate(arg.getValue()); } } // ========================================================================= private String classPrefix; private String articleTitle = ""; private boolean renderTemplates = false; private boolean renderTagExtensions = false; private List numberedLinks = new ArrayList(); private boolean standaloneHtml = true; private String cssLink; private File cssFile; private String cssResource; // =========================================================================== public HtmlPrinter(Writer writer, String articleTitle) { super(writer); this.articleTitle = articleTitle; setCssResource("HtmlPrinter.css", ""); } // =========================================================================== private void setClassPrefix(String classPrefix) { if (classPrefix != null) { this.classPrefix = classPrefix; if (!classPrefix.isEmpty()) this.classPrefix += '-'; } } public boolean isStandaloneHtml() { return standaloneHtml; } public void setStandaloneHtml(boolean standaloneHtml, String classPrefix) { this.standaloneHtml = standaloneHtml; setClassPrefix(classPrefix); } public String getCssLink() { return cssLink; } public void setCssLink(String cssLink, String classPrefix) { this.cssFile = null; this.cssResource = null; this.cssLink = cssLink; setClassPrefix(classPrefix); } public File getCssFile() { return cssFile; } public void setCssFile(File cssFile, String classPrefix) { this.cssResource = null; this.cssLink = null; this.cssFile = cssFile; setClassPrefix(classPrefix); } public String getCssResource() { return cssResource; } public void setCssResource(String cssResource, String classPrefix) { this.cssFile = null; this.cssLink = null; this.cssResource = cssResource; setClassPrefix(classPrefix); } public void setRenderTemplates(boolean renderTemplates) { this.renderTemplates = renderTemplates; } public void setRenderTagExtensions(boolean renderTagExtensions) { this.renderTagExtensions = renderTagExtensions; } // =========================================================================== /* private void iterate(List list) { for (AstNode n : list) dispatch(n); } */ private String asXmlCharRefs(String codePoint) { StringBuilder b = new StringBuilder(); for (int i = 0; i < codePoint.length(); ++i) { b.append("&#"); b.append((int) codePoint.charAt(i)); b.append(";"); } return b.toString(); } @SuppressWarnings("unchecked") private void renderBlockLevelElementsFirst(Paragraph p) { List l = (List) p.getAttribute("blockLevelElements"); if (l == null) return; for(AstNode n : l) dispatch(n); } @SuppressWarnings("unchecked") private boolean isParagraphEmpty(Paragraph p) { if (!p.isEmpty()) { List l = (List) p.getAttribute("blockLevelElements"); if (l == null || p.size() - l.size() > 0) return false; } return true; } private void printExternalLinkNumber(ExternalLink link) { numberedLinks.add(link); print(numberedLinks.size()); } private String makeLinkTitle(InternalLink n) { return n.getTarget(); } private String makeLinkTarget(InternalLink n) { return n.getTarget(); } private String makeSignature(Signature sig) { return "[SIG]"; } }




    © 2015 - 2025 Weber Informatics LLC | Privacy Policy