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

org.sweble.wikitext.engine.utils.HtmlPrinter.apt 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
%*
 * Copyright 2011 The Open Source Research Group,
 *                University of Erlangen-Nürnberg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *%

%Name HtmlPrinter;

%Package org.sweble.wikitext.engine.utils;

%CtorParams String articleTitle;


%Header {

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.*;

}


%Body {

	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]";
	}
}


%* ====[  Meta stuff  ]====================================================== *%


%visit(AstNode)%
%
{=astNode.getClass().getSimpleName()}%



%visit(NodeList l)%
{!iterate(l)}


%visit(Page page)
{if(standaloneHtml)}%



	
		
		{=escHtml(articleTitle)}
		{if(cssResource != null)}
			
		{elif(cssFile != null)}
			
		{elif(cssLink != null)}
			
		{/if}
	
	
{/if}%

{=escHtml(articleTitle)}

{!iterate(page.getContent())}
{if(standaloneHtml)} {/if} %* ====[ Formatting ]====================================================== *% %visit(Text)% {=escHtml(text.getContent())} %visit(Italics n)% {!iterate(n.getContent())} %visit(Bold n)% {!iterate(n.getContent())} %visit(Whitespace n)% {!iterate(n.getContent())} %visit(Paragraph p) {!renderBlockLevelElementsFirst(p)} {if (!isParagraphEmpty(p))}

{!iterate(p.getContent())}

{/if} %visit(SemiPre sp)
{!iterate(sp.getContent())}
%visit(SemiPreLine line)% {!iterate(line.getContent())}{="\n"} %* ====[ Section ]========================================================= *% %visit(Section s)
{!iterate(s.getTitle())}
{!iterate(s.getBody())}
%* ====[ XML stuff ]======================================================= *% %visit(XmlComment e)% %visit(XmlElement e)% <{=e.getName()}{!iterate(e.getXmlAttributes())}% {if(e.getEmpty())} />{else}>{!iterate(e.getBody())}{/if} %visit(XmlAttribute a)% {=a.getName()}="{!iterate(a.getValue())}" %visit(XmlAttributeGarbage g)% %visit(XmlCharRef ref)% &#{=ref.getCodePoint()}; %visit(XmlEntityRef ref)% &{=ref.getName()}; %* ====[ Lists ]=========================================================== *% %visit(DefinitionList n)
{!iterate(n.getContent())}
%visit(DefinitionTerm n)
{!iterate(n.getContent())}
%visit(DefinitionDefinition n)
{!iterate(n.getContent())}
%visit(Enumeration n)
    {!iterate(n.getContent())}
%visit(EnumerationItem n)
  • {!iterate(n.getContent())}
  • %visit(Itemization n) %visit(ItemizationItem n)
  • {!iterate(n.getContent())}
  • %* ====[ Links ( External, Internal, Image, Category ) ]=================== *% %visit(ExternalLink link)% % {if(!link.getTitle().isEmpty())}{!iterate(link.getTitle())}% {else}% {!printExternalLinkNumber(link)}% {/if}% %visit(Url url)% % {=url.getProtocol()}:{=url.getPath()}% %visit(InternalLink n)% % {=n.getPrefix()}% {if(n.getTitle().getContent().isEmpty())}% {=makeLinkTitle(n)}% {else}% {!iterate(n.getTitle().getContent())}% {/if}% {=n.getPostfix()}% % %* %visit(ImageLink n)% {if(n.getFormat().isFramed())}% {if(!n.hasAttribute("rendered"))}% {!n.setAttribute("rendered", true)}
    {if(!n.getTitle().isEmpty())}
    {!iterate(n.getTitle())}
    {/if}
    % {/if}% {else}% % {=escHtml(n.getAlt())}% {/if}% *% %* ====[ Tables ]========================================================== *% %visit(Table table) {!iterate(table.getBody())} %* {if(!table.getCaption().getXmlAttributes().isEmpty() || !table.getCaption().getBody().isEmpty())} {!dispatch(table.getCaption())} {/if} {if(!table.getHeader().getXmlAttributes().isEmpty() || !table.getHeader().getBody().isEmpty())} {!dispatch(table.getHeader())} {/if} {if(!table.getBody().isEmpty())} {!iterate(table.getBody())} {/if} *% %visit(TableCaption caption) {!iterate(caption.getBody())} %visit(TableRow row) {!iterate(row.getBody())} %visit(TableHeader header) {!iterate(header.getBody())} %visit(TableCell cell) {!iterate(cell.getBody())} %* ====[ Miscellaneous ]=================================================== *% %visit(HorizontalRule rule)
    %visit(Signature sig)% {=makeSignature(sig)}% %visit(Redirect n)% ↳ {=n.getTarget()} %visit(IllegalCodePoint n)% {=asXmlCharRefs(n.getCodePoint())} %visit(MagicWord n)% __{=n.getWord()}__ %* ====[ Elements that should have been resolved ]========================= *% %visit(TagExtension n)% % {if(renderTagExtensions)}% {if(n.getBody().isEmpty())}% <{=n.getName()}{!iterate(n.getXmlAttributes())} />% {else}% <{=n.getName()}{!iterate(n.getXmlAttributes())}>{=escHtml(n.getBody())}</{=n.getName()}>% {/if}% {else}% {if(n.getXmlAttributes().isEmpty())}% <{=n.getName()}% {else}% <{=n.getName()} ...% {/if}% {if(n.getBody().isEmpty())}% />% {else}% >...</{=n.getName()}>% {/if}% {/if}% %visit(XmlElementEmpty e)% % <{=e.getName()}{!iterate(e.getXmlAttributes())} />% %visit(XmlElementOpen e)% % <{=e.getName()}{!iterate(e.getXmlAttributes())}>% %visit(XmlElementClose e)% % </{=e.getName()}>% %visit(Template tmpl)% % {if(renderTemplates)}% %{%{{!iterate(tmpl.getName())}{!iterate(tmpl.getArgs())}}}% {else}% {if(tmpl.getArgs().isEmpty())}% %{%{{!iterate(tmpl.getName())}}}% {else}% %{%{{!iterate(tmpl.getName())}|...}}% {/if}% {/if}% %visit(TemplateParameter param)% % {if(renderTemplates)}% %{%{%{{!iterate(param.getName())}{!dispatch(param.getDefaultValue())}{!iterate(param.getGarbage())}}}}% {else}% {if(param.getDefaultValue() == null)}% %{%{%{{!iterate(param.getName())}}}}% {else}% %{%{%{{!iterate(param.getName())}|...}}}% {/if}% {/if}% %visit(TemplateArgument arg)% |{if(arg.getHasName())}% {!iterate(arg.getValue())}% {else}% {!iterate(arg.getName())}={!iterate(arg.getValue())}% {/if} %* ========================================================================== *%




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy