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

org.sweble.wikitext.lazy.utils.WikitextPrinter Maven / Gradle / Ivy

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:37:37.
 */

package org.sweble.wikitext.lazy.utils;

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

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



import java.util.List;

import org.sweble.wikitext.lazy.encval.*;
import org.sweble.wikitext.lazy.parser.*;
import org.sweble.wikitext.lazy.preprocessor.*;



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

	public WikitextPrinter(Writer writer)
	{
		super(writer);
	}

	
	// =========================================================================
	
	public void visit(LazyPreprocessedPage page) throws IOException
	{
		iterate(page.getContent());

	}
	public void visit(LazyParsedPage page) throws IOException
	{
		iterate(page.getContent());

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

	}
	public void visit(Text text) throws IOException
	{
		print(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 ws) throws IOException
	{
		printNewline(false);
		printNewline(false);

	}
	public void visit(Paragraph p) throws IOException
	{
		printNewline(true);
		iterate(p.getContent());

	}
	public void visit(Section s) throws IOException
	{
		printNewline(true);
		print(strrep('=', s.getLevel()));
		print(" ");
		iterate(trim(s.getTitle()));
		print(" ");
		print(strrep('=', s.getLevel()));
		printNewline(false);
		incIndent("");
		iterate(s.getBody());
		decIndent();
		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
	{
		if (a.getHasValue()) {
			print(" ");
			print(a.getName());
			print("=\"");
			iterate(a.getValue());
			print("\"");
		} else {
			print(" ");
			print(a.getName());
		}

	}
	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(SemiPre sp) throws IOException
	{
		printNewline(true);
		iterate(sp.getContent());

	}
	public void visit(SemiPreLine l) throws IOException
	{
		printNewline(false);
		print(" ");
		iterate(l.getContent());
		printNewline(false);

	}
	public void visit(DefinitionList n) throws IOException
	{
		printNewline(true);
		iterate(n.getContent());

	}
	public void visit(DefinitionTerm n) throws IOException
	{
		printNewline(false);
		print("; ");
		iterate(trim(n.getContent()));
		printNewline(false);

	}
	public void visit(DefinitionDefinition n) throws IOException
	{
		printNewline(false);
		print(": ");
		iterate(trim(n.getContent()));
		printNewline(false);

	}
	public void visit(Enumeration n) throws IOException
	{
		printNewline(true);
		iterate(n.getContent());

	}
	public void visit(EnumerationItem n) throws IOException
	{
		printNewline(false);
		print("# ");
		iterate(trim(n.getContent()));
		printNewline(false);

	}
	public void visit(Itemization n) throws IOException
	{
		printNewline(true);
		iterate(n.getContent());

	}
	public void visit(ItemizationItem n) throws IOException
	{
		printNewline(false);
		print("* ");
		iterate(trim(n.getContent()));
		printNewline(false);

	}
	public void visit(ExternalLink link) throws IOException
	{
		print("[");
		print(link.getTarget().getProtocol());
		print(":");
		print(link.getTarget().getPath());
		if (!link.getTitle().isEmpty()) {
			print(" ");
			iterate(trim(link.getTitle()));
		}
		print("]");

	}
	public void visit(Url url) throws IOException
	{
		print(url.getProtocol());
		print(":");
		print(url.getPath());

	}
	public void visit(InternalLink n) throws IOException
	{
		print(n.getPrefix());
		print("[[");
		print(n.getTarget());
		if (!n.getTitle().getContent().isEmpty()) {
			print("|");
			iterate(n.getTitle().getContent());
		}
		print("]]");
		print(n.getPostfix());

	}
	public void visit(ImageLink n) throws IOException
	{
		print("[[");
		print(n.getTarget());
		print(imageLinkOptions(n));
		if (!n.getAlt().isEmpty()) {
			print("|alt=");
			iterate(n.getAlt());
		}
		if (!n.getTitle().getContent().isEmpty()) {
			print("|");
			iterate(n.getTitle().getContent());
		}
		print("]]");

	}
	public void visit(Table table) throws IOException
	{
		printNewline(true);
		print("{");
		print("|");
		iterate(table.getXmlAttributes());
		printNewline(false);
		incIndent("");
		iterate(table.getBody());
		decIndent();
		printNewline(false);
		printNewline(true);
		print("|}");

	}
	public void visit(TableCaption caption) throws IOException
	{
		printNewline(false);
		print("|+");
		if (!caption.getXmlAttributes().isEmpty()) {
			iterate(caption.getXmlAttributes());
			print(" |");
		}
		print(" ");
		iterate(trim(caption.getBody()));
		printNewline(false);

	}
	public void visit(TableRow row) throws IOException
	{
		printNewline(false);
		print("|-");
		if (!row.getXmlAttributes().isEmpty()) {
			iterate(row.getXmlAttributes());
		}
		printNewline(false);
		incIndent("");
		iterate(row.getBody());
		decIndent();
		printNewline(false);
		printNewline(false);

	}
	public void visit(TableHeader header) throws IOException
	{
		printNewline(false);
		print("!");
		if (!header.getXmlAttributes().isEmpty()) {
			iterate(header.getXmlAttributes());
			print(" |");
		}
		print(" ");
		iterate(trim(header.getBody()));
		printNewline(false);

	}
	public void visit(TableCell cell) throws IOException
	{
		printNewline(false);
		print("|");
		if (!cell.getXmlAttributes().isEmpty()) {
			iterate(cell.getXmlAttributes());
			print(" |");
		}
		print(" ");
		iterate(trim(cell.getBody()));
		printNewline(false);

	}
	public void visit(HorizontalRule rule) throws IOException
	{
		printNewline(true);
		printNewline(true);
		print("----");

	}
	public void visit(Signature sig) throws IOException
	{
		print(strrep('~', sig.getTildeCount()));

	}
	public void visit(TagExtension n) throws IOException
	{
		if (n.getBody().isEmpty()) {
			print("<");
			print(n.getName());
			iterate(n.getXmlAttributes());
			print(" />");
		} else {
			print("<");
			print(n.getName());
			iterate(n.getXmlAttributes());
			print(">");
			print(n.getBody());
			print("");
		}

	}
	public void visit(XmlElementEmpty e) throws IOException
	{
		print("<");
		print(e.getName());
		iterate(e.getXmlAttributes());
		print(" />");

	}
	public void visit(XmlElementOpen e) throws IOException
	{
		print("<");
		print(e.getName());
		iterate(e.getXmlAttributes());
		print(">");

	}
	public void visit(XmlElementClose e) throws IOException
	{
		print("");

	}
	public void visit(Template tmpl) throws IOException
	{
		print("{");
		print("{");
		iterate(tmpl.getName());
		iterate(tmpl.getArgs());
		print("}}");

	}
	public void visit(TemplateParameter param) throws IOException
	{
		print("{");
		print("{");
		print("{");
		iterate(param.getName());
		dispatch(param.getDefaultValue());
		iterate(param.getGarbage());
		print("}}}");

	}
	public void visit(TemplateArgument arg) throws IOException
	{
		print("|");
		if (!arg.getHasName()) {
			iterate(arg.getValue());
		} else {
			iterate(arg.getName());
			print("=");
			iterate(arg.getValue());
		}

	}
	public void visit(Ticks ticks) throws IOException
	{
		print(strrep('\'', ticks.getTickCount()));

	}
	public void visit(MagicWord word) throws IOException
	{
		print("__");
		print(word.getWord());
		print("__");

	}
	public void visit(Redirect r) throws IOException
	{
		print("#REDIRECT[[");
		print(r.getTarget());
		print("]]");

	}
	public void visit(IllegalCodePoint icp) throws IOException
	{
		print(icp.getCodePoint());

	}
	public void visit(Ignored i) throws IOException
	{

	}
	public void visit(OnlyInclude oi) throws IOException
	{
		iterate(oi.getContent());

	}

	
	// =========================================================================
	


	private static NodeList trim(NodeList l)
	{
		return TextUtils.trim(l);
	}
	
	/*
	private void iterate(List list)
	{
		for (AstNode n : list)
			dispatch(n);
	}
	*/

	private static String imageLinkOptions(ImageLink n)
	{
		StringBuilder s = new StringBuilder();
		
		if (n.getWidth() >= 0 || n.getHeight() >= 0)
		{
			s.append('|');
			if (n.getWidth() >= 0)
				s.append(n.getWidth());
			s.append('x');
			if (n.getHeight() >= 0)
				s.append(n.getHeight());
			s.append("px");
		}
		
		if (n.getUpright())
			s.append("|upright");
		
		if (n.getHAlign() != ImageHorizAlign.NONE)
		{
			s.append('|');
			s.append(n.getHAlign().asKeyword());
		}
		
		if (n.getVAlign() != ImageVertAlign.MIDDLE)
		{
			s.append('|');
			s.append(n.getVAlign().asKeyword());
		}
		
		if (n.getFormat() != ImageViewFormat.UNRESTRAINED)
		{
			s.append('|');
			s.append(n.getFormat().asKeyword());
		}
		
		if (n.getBorder() != false)
			s.append("|border");
		
		if (n.getLinkPage() != null)
		{
			s.append("|link=");
			s.append(n.getLinkPage());
		}
		
		if (n.getLinkUrl() != null)
		{
			s.append("|link=");
			s.append(n.getLinkUrl().getProtocol());
			s.append(':');
			s.append(n.getLinkUrl().getPath());
		}
		
		return s.toString();
	}


}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy