Exemplo n.º 1
0
  /** {@inheritDoc} */
  public List<Block> execute(
      SectionMacroParameters parameters, String content, MacroTransformationContext context)
      throws MacroExecutionException {
    XDOM parsedDom;

    // get a parser for the desired syntax identifier
    Parser parser = getSyntaxParser(context.getSyntax().toIdString());

    try {
      // parse the content of the wiki macro that has been injected by the
      // component manager
      // the content of the macro call itself is ignored.
      parsedDom = parser.parse(new StringReader(content));
    } catch (ParseException e) {
      throw new MacroExecutionException(
          "Failed to parse content ["
              + content
              + "] with Syntax parser ["
              + parser.getSyntax()
              + "]",
          e);
    }

    List<MacroBlock> potentialColumns = parsedDom.getChildrenByType(MacroBlock.class, false);

    int count = this.countColumns(potentialColumns);

    if (count == 0) {
      throw new MacroExecutionException(
          "Section macro expect at least one column macro as first-level children");
    }

    double computedColumnWidth = ((TOTAL_WIDTH - COLUMN_RIGHT_PADDING_RATE * (count - 1)) / count);

    // Make the actual columns injecting divs around column macros
    this.makeColumns(potentialColumns, computedColumnWidth);

    // finally, clear the floats introduced by the columns
    Map<String, String> clearFloatsParams = new HashMap<String, String>();
    clearFloatsParams.put(PARAMETER_STYLE, STYLE_CLEAR_BOTH);

    parsedDom.addChild(new GroupBlock(clearFloatsParams));

    Map<String, String> sectionParameters = new HashMap<String, String>();
    if (parameters.isJustify()) {
      sectionParameters.put(PARAMETER_STYLE, STYLE_TEXT_ALIGN_JUSTIFY);
    }

    Block sectionRoot = new GroupBlock(sectionParameters);
    sectionRoot.addChildren(parsedDom.getChildren());

    return Collections.singletonList(sectionRoot);
  }
Exemplo n.º 2
0
  @Override
  public XDOM parse(Reader source) throws ParseException {
    try {
      RootNode rootNode = PEGDOWN_PROCESSOR.parseMarkdown(IOUtils.toString(source).toCharArray());
      String markdownAsHtml = new ToHtmlSerializer().toHtml(rootNode);
      XDOM xdom =
          this.xhtmlParser.parse(
              new StringReader("<html><body>" + markdownAsHtml + "</body></html>"));

      // Replace the Syntax MetaData which is set with XHTML with Markdown.
      return new XDOM(
          xdom.getChildren(),
          new MetaData(
              Collections.<String, Object>singletonMap(MetaData.SYNTAX, Syntax.MARKDOWN_1_0)));

    } catch (IOException e) {
      throw new ParseException("Failed to convert Markdown to HTML", e);
    }
  }
Exemplo n.º 3
0
  @Override
  public List<Block> execute(
      IncludeMacroParameters parameters, String content, MacroTransformationContext context)
      throws MacroExecutionException {
    // Step 1: Perform checks.
    if (parameters.getReference() == null) {
      throw new MacroExecutionException(
          "You must specify a 'reference' parameter pointing to the entity to include.");
    }

    DocumentReference includedReference = resolve(context.getCurrentMacroBlock(), parameters);

    checkRecursiveInclusion(context.getCurrentMacroBlock(), includedReference);

    if (!this.documentAccessBridge.isDocumentViewable(includedReference)) {
      throw new MacroExecutionException(
          String.format(
              "Current user [%s] doesn't have view rights on document [%s]",
              this.documentAccessBridge.getCurrentUserReference(),
              this.defaultEntityReferenceSerializer.serialize(includedReference)));
    }

    Context parametersContext = parameters.getContext();

    // Step 2: Retrieve the included document.
    DocumentModelBridge documentBridge;
    try {
      documentBridge = this.documentAccessBridge.getDocument(includedReference);
    } catch (Exception e) {
      throw new MacroExecutionException(
          "Failed to load Document ["
              + this.defaultEntityReferenceSerializer.serialize(includedReference)
              + "]",
          e);
    }

    // Step 3: Display the content of the included document.

    // Check the value of the "context" parameter.
    //
    // If CONTEXT_NEW then display the content in an isolated execution and transformation context.
    //
    // if CONTEXT_CURRENT then display the content without performing any transformations (we don't
    // want any Macro
    // to be executed at this stage since they should be executed by the currently running Macro
    // Transformation.
    DocumentDisplayerParameters displayParameters = new DocumentDisplayerParameters();
    displayParameters.setContentTransformed(parametersContext == Context.NEW);
    displayParameters.setExecutionContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setSectionId(parameters.getSection());
    displayParameters.setTransformationContextIsolated(displayParameters.isContentTransformed());
    displayParameters.setTransformationContextRestricted(
        context.getTransformationContext().isRestricted());
    displayParameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax());

    Stack<Object> references = this.inclusionsBeingExecuted.get();
    if (parametersContext == Context.NEW) {
      if (references == null) {
        references = new Stack<Object>();
        this.inclusionsBeingExecuted.set(references);
      }
      references.push(includedReference);
    }

    XDOM result;
    try {
      result = this.documentDisplayer.display(documentBridge, displayParameters);
    } catch (Exception e) {
      throw new MacroExecutionException(e.getMessage(), e);
    } finally {
      if (parametersContext == Context.NEW) {
        references.pop();
      }
    }

    // Step 4: Wrap Blocks in a MetaDataBlock with the "source" meta data specified so that we know
    // from where the
    // content comes and "base" meta data so that reference are properly resolved
    MetaDataBlock metadata = new MetaDataBlock(result.getChildren(), result.getMetaData());
    String source = this.defaultEntityReferenceSerializer.serialize(includedReference);
    metadata.getMetaData().addMetaData(MetaData.SOURCE, source);
    if (parametersContext == Context.NEW) {
      metadata.getMetaData().addMetaData(MetaData.BASE, source);
    }

    return Arrays.<Block>asList(metadata);
  }