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

com.redhat.ceylon.common.tools.help.DocBookMarkdownVisitor Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
/*
 * This class is heavily based on org.tautua.markdownpapers.HtmlEmitter
 * which comes with the following header:  
 */

/*
 * Copyright 2011, TAUTUA
 *
 * 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 com.redhat.ceylon.common.tools.help;

import static org.tautua.markdownpapers.util.Utils.EOL;
import static org.tautua.markdownpapers.util.Utils.SPACE;
import static org.tautua.markdownpapers.util.Utils.escape;

import java.io.IOException;

import org.tautua.markdownpapers.ast.CharRef;
import org.tautua.markdownpapers.ast.Code;
import org.tautua.markdownpapers.ast.CodeSpan;
import org.tautua.markdownpapers.ast.CodeText;
import org.tautua.markdownpapers.ast.Comment;
import org.tautua.markdownpapers.ast.Document;
import org.tautua.markdownpapers.ast.Emphasis;
import org.tautua.markdownpapers.ast.Header;
import org.tautua.markdownpapers.ast.Image;
import org.tautua.markdownpapers.ast.InlineUrl;
import org.tautua.markdownpapers.ast.Item;
import org.tautua.markdownpapers.ast.Line;
import org.tautua.markdownpapers.ast.LineBreak;
import org.tautua.markdownpapers.ast.Link;
import org.tautua.markdownpapers.ast.List;
import org.tautua.markdownpapers.ast.Node;
import org.tautua.markdownpapers.ast.Paragraph;
import org.tautua.markdownpapers.ast.Quote;
import org.tautua.markdownpapers.ast.Resource;
import org.tautua.markdownpapers.ast.ResourceDefinition;
import org.tautua.markdownpapers.ast.Ruler;
import org.tautua.markdownpapers.ast.SimpleNode;
import org.tautua.markdownpapers.ast.Tag;
import org.tautua.markdownpapers.ast.TagAttribute;
import org.tautua.markdownpapers.ast.Text;
import org.tautua.markdownpapers.ast.Visitor;

public class DocBookMarkdownVisitor implements Visitor {
    private Appendable buffer;

    public DocBookMarkdownVisitor(Appendable buffer) {
        this.buffer = buffer;
    }

    public void visit(CharRef node) {
        append(node.getValue());
    }

    public void visit(Code node) {
        append("");
        visitChildrenAndAppendSeparator(node, EOL);
        append("");
        append(EOL);
    }

    public void visit(CodeSpan node) {
        append("");
        escapeAndAppend(node.getText());
        append("");
    }

    public void visit(CodeText node) {
        escapeAndAppend(node.getValue());
    }

    public void visit(Comment node) {
        append("");
    }

    public void visit(Document node) {
        visitChildrenAndAppendSeparator(node, EOL);
    }

    public void visit(Emphasis node) {
        switch (node.getType()) {
            case ITALIC:
                append("");
                append(node.getText());
                append("");
                break;
            case BOLD:
                append("");
                append(node.getText());
                append("");
                break;
            case ITALIC_AND_BOLD:
                append("");
                append(node.getText());
                append("");
                break;
        }
    }

    public void visit(Header node) {
        append("");
        node.childrenAccept(this);
        append("");
        append(EOL);
    }

    public void visit(Image node) {
        Resource resource = node.getResource();
        
        append("");
        append("");
        if (node.getText() != null) {
            append("");
            escapeAndAppend(node.getText());
            append("");
        }
        if (resource != null) {
            append("");
        }
        if (resource != null) {
            append("");
            append("");
            escapeAndAppend(resource.getHint());
            append("");
            append("");
        }
        append("");
        escapeAndAppend(node.getText());
        append("");
        append("");
        append("");
    }

    public void visit(InlineUrl node) {
        append("");
        escapeAndAppend(node.getUrl());
        append("");
    }

    public void visit(Item node) {
        append("");// XXX  is only correct when the nod contains text, not if it contains a nested list
        node.childrenAccept(this);
        append("");
        append(EOL);
    }

    public void visit(Line node) {
        node.childrenAccept(this);
    }

    @Override
    public void visit(LineBreak node) {
        Line l = (Line) node.jjtGetParent();
        if(!l.isEnding()) {
            append("
"); } } public void visit(Link node) { Resource resource = node.getResource(); if (resource == null) { if (node.isReferenced()) { append("["); node.childrenAccept(this); append("]"); if (node.getReference() != null) { if (node.hasWhitespaceAtMiddle()) { append(' '); } append("["); append(node.getReference()); append("]"); } } else { append(""); node.childrenAccept(this); append(""); } } else { append(""); node.childrenAccept(this); append(""); } } public void visit(ResourceDefinition node) { // do nothing } public void visit(List node) { if (node.isOrdered()) { append(""); append(EOL); node.childrenAccept(this); append(""); } else { append(""); append(EOL); node.childrenAccept(this); append(""); } append(EOL); } public void visit(Paragraph node) { Node parent = node.jjtGetParent(); if(parent instanceof Item) { if (!((Item)parent).isLoose()) { visitChildrenAndAppendSeparator(node, EOL); return; } } append(""); visitChildrenAndAppendSeparator(node, EOL); append(""); append(EOL); } public void visit(Ruler node) { append("
"); append(EOL); } public void visit(Quote node) { append("
"); append(EOL); node.childrenAccept(this); append("
"); append(EOL); } public void visit(SimpleNode node) { throw new IllegalArgumentException("can not process this element"); } public void visit(Tag node) { append("<"); append(node.getName()); for (TagAttribute attribute : node.getAttributes()) { append(SPACE); append(attribute.getName()); append("=\""); append(attribute.getValue()); append("\""); } if(node.jjtGetNumChildren() == 0) { append("/>"); } else { append(">"); node.childrenAccept(this); append(""); } } public void visit(Text node) { if(node.jjtGetParent() instanceof Tag) { append(node.getValue()); } else { escapeAndAppend(node.getValue()); } } void visitChildrenAndAppendSeparator(Node node, char separator){ int count = node.jjtGetNumChildren(); for(int i = 0; i < count; i++) { node.jjtGetChild(i).accept(this); if(i < count - 1) { append(separator); } } } void escapeAndAppend(String val) { for(char character : val.toCharArray()) { append(escape(character)); } } void append(String val) { try { buffer.append(val); } catch (IOException e) { throw new RuntimeException(e); } } void append(char val) { try { buffer.append(val); } catch (IOException e) { throw new RuntimeException(e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy