Load.java
/***************************************************************************
Copyright 2014 Emily Estes
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.
***************************************************************************/
package net.metanotion.contentstore.markup;
import java.io.Reader;
import java.util.ArrayList;
import net.metanotion.contentstore.markup.parser.ParseException;
import net.metanotion.contentstore.markup.parser.MarkupParser;
import net.metanotion.contentstore.markup.parser.syntaxtree.*;
import net.metanotion.web.html.Html;
import net.metanotion.web.html.RawString;
import net.metanotion.web.html.SafeString;
final class Load {
private static final Html BR = new net.metanotion.web.html.Tag("br");
private static final class DivTag {
public final RawString html;
public DivTag(final RawString html) { this.html = html; }
}
private static final class ATag {
public final RawString html;
public ATag(final RawString html) { this.html = html; }
}
private static final class ImgTag {
public final RawString html;
public ImgTag(final RawString html) { this.html = html; }
}
private ArrayList<Object> newPara(final int brCt, ArrayList<Object> currentPara, final ArrayList<Html> output) {
if((currentPara.size() > 0) && (brCt > 0)) {
if(brCt == 1) {
currentPara.add(BR);
} else {
output.add(new net.metanotion.web.html.Tag("p").addAll(currentPara));
currentPara = new ArrayList<>();
}
}
return currentPara;
}
public Iterable<Html> load(final Reader in) throws ParseException {
final MarkupParser parse = new MarkupParser(in);
final Content c = parse.Content();
final ArrayList<Html> output = new ArrayList<>();
ArrayList<Object> currentPara = new ArrayList<>();
int brCt = 0;
for(final Node nc: c.f0.nodes) {
final Node n = ((NodeChoice) nc).choice;
if(n instanceof Raw) {
final Object o = parseRaw((Raw) n);
if(o == BR) {
brCt++;
} else {
currentPara = newPara(brCt, currentPara, output);
brCt = 0;
currentPara.add(o);
}
} else if(n instanceof Tag) {
final Object o = parseTag((Tag) n);
if(o instanceof DivTag) {
currentPara = newPara(2, currentPara, output);
output.add(((DivTag) o).html);
} else {
currentPara = newPara(brCt, currentPara, output);
if(o instanceof ATag) {
currentPara.add(((ATag) o).html);
} else {
currentPara.add(((ImgTag) o).html);
}
}
brCt = 0;
}
}
if(currentPara.size() > 0) { output.add(new net.metanotion.web.html.Tag("p").addAll(currentPara)); }
return output;
}
public Object parseRaw(final Raw r) {
if(r.f0.which == 0) {
return BR;
} else {
return new SafeString(r.f0.choice.toString());
}
}
public Object parseTag(final Tag t) {
if(t.f0.which == 0) {
final Anchor a = (Anchor) t.f0.choice;
final StringBuilder sb = new StringBuilder(a.f0.toString());
for(final Node n: a.f1.nodes) { sb.append(n.toString()); }
sb.append(a.f2.toString());
return new ATag(new RawString(sb.toString()));
} else if(t.f0.which == 1) {
final Div d = (Div) t.f0.choice;
final StringBuilder sb = new StringBuilder(d.f0.toString());
for(final Node n: d.f1.nodes) { sb.append(n.toString()); }
sb.append(d.f2.toString());
return new DivTag(new RawString(sb.toString()));
} else {
final Img i = (Img) t.f0.choice;
final StringBuilder sb = new StringBuilder(i.f0.toString());
for(final Node n: i.f1.nodes) { sb.append(n.toString()); }
sb.append(i.f2.toString());
return new ImgTag(new RawString(sb.toString()));
}
}
}