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

fitnesse.wikitext.parser.Include Maven / Gradle / Ivy

There is a newer version: 20240707
Show newest version
package fitnesse.wikitext.parser;

import fitnesse.wiki.PageData;
import util.Maybe;

public class Include extends SymbolType implements Rule, Translation {
    private static final String[] setUpSymbols = new String[] {"COLLAPSE_SETUP"};
    private static final String includeHelpOption = "-h";

    public Include() {
        super("Include");
        wikiMatcher(new Matcher().startLineOrCell().string("!include"));
        wikiRule(this);
        htmlTranslation(this);
    }
    
    public Maybe parse(Symbol current, Parser parser) {
        Symbol next = parser.moveNext(1);
        if (!next.isType(SymbolType.Whitespace)) return Symbol.nothing;
        
        next = parser.moveNext(1);
        String option = "";
        if (next.isType(SymbolType.Text) && next.getContent().startsWith("-")) {
            option = next.getContent();
            next = parser.moveNext(1);
            if (!next.isType(SymbolType.Whitespace)) return Symbol.nothing;
            next = parser.moveNext(1);
        }
        if (!next.isType(SymbolType.Text) && !next.isType(WikiWord.symbolType)) return Symbol.nothing;

        current.add(option).add(next);

        Maybe includedPage = parser.getPage().getNamedPage().findIncludedPage(next.getContent());
        if (includedPage.isNothing()) {
            current.add(new Symbol(SymbolType.Meta).add(includedPage.because()));
        }
        else if (includeHelpOption.equals(option)) {
        	String helpText = includedPage.getValue().getProperty(PageData.PropertyHELP);	
        	current.add("").add(Parser.make(
        			parser.getPage(),helpText).parse());
        } else {
            current.childAt(1).putProperty(WikiWord.WITH_EDIT, "true");
            ParsingPage included = option.equals("-setup") || option.equals("-teardown")
                    ? parser.getPage()
                    : parser.getPage().copyForNamedPage(includedPage.getValue());
            current.add("").add(Parser.make(
                            included,
                            includedPage.getValue().getContent())
                            .parse());
            if (option.equals("-setup")) current.evaluateVariables(setUpSymbols, parser.getVariableSource());
        }

      // Remove trailing newline so we do not introduce excessive whitespace in the page.
      if (parser.peek().isType(SymbolType.Newline)) {
        parser.moveNext(1);
      }

      return new Maybe(current);
    }

    public String toTarget(Translator translator, Symbol symbol) {
        if (symbol.getChildren().size() < 4) {
            return translator.translate(symbol.childAt(2));
        }
        String option = symbol.childAt(0).getContent();
        if (option.equals("-seamless")) {
            return translator.translate(symbol.childAt(3));
        } else if (includeHelpOption.equals(option)) {
        	return translator.translate(symbol.childAt(3));
        } else {
            String collapseState = stateForOption(option, symbol);
            String title = "Included page: "
                    + translator.translate(symbol.childAt(1));
            return Collapsible.generateHtml(collapseState, title, translator.translate(symbol.childAt(3)));
        }
    }

    private String stateForOption(String option, Symbol symbol) {
        return ((option.equals("-setup") || option.equals("-teardown")) && symbol.getVariable("COLLAPSE_SETUP", "true").equals("true"))
                || option.equals("-c")
                ? Collapsible.CLOSED
                : "";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy