
org.sweble.wikitext.engine.utils.HtmlPrinter.apt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swc-engine Show documentation
Show all versions of swc-engine Show documentation
A minimal engine using the Sweble Wikitext Parser to
process articles in the context of a MediaWiki-like configuration.
%*
* 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 extends AstNode> 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())}
{!iterate(p.getContent())}
{/if} %visit(SemiPre sp){!iterate(sp.getContent())}%visit(SemiPreLine line)% {!iterate(line.getContent())}{="\n"} %* ====[ Section ]========================================================= *% %visit(Section s)
{!iterate(s.getBody())}
-
{!iterate(n.getContent())}
-
{!iterate(n.getContent())}
-
{!iterate(n.getContent())}
%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} %* ========================================================================== *%