Search in sources :

Example 6 with BlockRenderer

use of org.xwiki.rendering.renderer.BlockRenderer in project xwiki-platform by xwiki.

the class ExpandedMacroBlockTest method getContentUsesAnotherBlockRenderer.

@Test
public void getContentUsesAnotherBlockRenderer() throws Exception {
    ExpandedMacroBlock expandedMacroBlock = new ExpandedMacroBlock("gallery", Collections.<String, String>emptyMap(), this.contentRenderer, false, this.componentManager);
    XDOM parent = new XDOM(Collections.singletonList(expandedMacroBlock));
    parent.getMetaData().addMetaData(MetaData.SYNTAX, Syntax.MARKDOWN_1_1);
    BlockRenderer markdownRenderer = mock(BlockRenderer.class);
    when(this.componentManager.hasComponent(BlockRenderer.class, Syntax.MARKDOWN_1_1.toIdString())).thenReturn(true);
    when(this.componentManager.getInstance(BlockRenderer.class, Syntax.MARKDOWN_1_1.toIdString())).thenReturn(markdownRenderer);
    expandedMacroBlock.getContent();
    verify(markdownRenderer).render(any(XDOM.class), any(WikiPrinter.class));
    verify(this.contentRenderer, never()).render(any(XDOM.class), any(WikiPrinter.class));
}
Also used : WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

Example 7 with BlockRenderer

use of org.xwiki.rendering.renderer.BlockRenderer in project xwiki-platform by xwiki.

the class LocalizationScriptService method render.

/**
 * @param keys the translations keys to try one by one
 * @param syntax the syntax in which to render the translation message
 * @param parameters the translation parameters
 * @param locale the {@link Locale} for which this translation is searched. The result might me associated to a
 *            different {@link Locale} (for example getting the {@code fr} translation when asking for the
 *            {@code fr_FR} one).
 * @return the rendered translation message, the key if no translation can be found and null if the rendering failed
 * @since 10.2
 */
public String render(Collection<String> keys, Syntax syntax, Collection<?> parameters, Locale locale) {
    if (CollectionUtils.isEmpty(keys)) {
        return null;
    }
    Translation translation = null;
    for (String key : keys) {
        if (key != null) {
            translation = this.localization.getTranslation(key, locale);
            if (translation != null) {
                break;
            }
        }
    }
    String result;
    if (translation != null) {
        Block block = parameters != null ? translation.render(locale, parameters.toArray()) : translation.render(locale);
        try {
            BlockRenderer renderer = this.componentManager.get().getInstance(BlockRenderer.class, syntax.toIdString());
            DefaultWikiPrinter wikiPrinter = new DefaultWikiPrinter();
            renderer.render(block, wikiPrinter);
            result = wikiPrinter.toString();
        } catch (ComponentLookupException e) {
            // TODO set current error
            result = null;
        }
    } else {
        result = null;
        for (String key : keys) {
            if (key != null) {
                result = key;
            }
        }
    }
    return result;
}
Also used : Translation(org.xwiki.localization.Translation) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) Block(org.xwiki.rendering.block.Block) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 8 with BlockRenderer

use of org.xwiki.rendering.renderer.BlockRenderer in project xwiki-platform by xwiki.

the class DocumentSolrMetadataExtractorTest method getSimpleDocument.

@Test
public void getSimpleDocument() throws Exception {
    // 
    // Mock
    // 
    // ID
    String id = "wiki:Space.Name_" + Locale.ROOT.toString();
    SolrReferenceResolver documentSolrReferenceResolver = this.mocker.getInstance(SolrReferenceResolver.class, "document");
    when(documentSolrReferenceResolver.getId(documentReference)).thenReturn(id);
    // Full Name
    String fullName = "Space.Name";
    EntityReferenceSerializer<String> localEntityReferenceSerializer = this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "local");
    when(localEntityReferenceSerializer.serialize(this.documentReference)).thenReturn(fullName);
    // Hierarchy
    when(localEntityReferenceSerializer.serialize(this.documentReference.getParent())).thenReturn("Path.To.Page");
    when(localEntityReferenceSerializer.serialize(this.documentReference.getParent().getParent())).thenReturn("Path.To");
    when(localEntityReferenceSerializer.serialize(this.documentReference.getParent().getParent().getParent())).thenReturn("Path");
    // Creator.
    DocumentReference creatorReference = new DocumentReference("wiki", "Space", "Creator");
    when(this.document.getCreatorReference()).thenReturn(creatorReference);
    String creatorStringReference = "wiki:Space.Creator";
    EntityReferenceSerializer<String> entityReferenceSerializer = this.mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "default");
    when(entityReferenceSerializer.serialize(creatorReference)).thenReturn(creatorStringReference);
    String creatorDisplayName = "Crea Tor";
    when(this.xcontext.getWiki().getPlainUserName(creatorReference, this.xcontext)).thenReturn(creatorDisplayName);
    // Author.
    DocumentReference authorReference = new DocumentReference("wiki", "Space", "Author");
    when(this.document.getAuthorReference()).thenReturn(authorReference);
    String authorStringReference = "wiki:Space.Author";
    when(entityReferenceSerializer.serialize(authorReference)).thenReturn(authorStringReference);
    String authorDisplayName = "Au Thor";
    when(this.xcontext.getWiki().getPlainUserName(authorReference, this.xcontext)).thenReturn(authorDisplayName);
    // Creation Date
    Date creationDate = new Date();
    when(this.document.getCreationDate()).thenReturn(creationDate);
    // Date
    Date date = new Date();
    when(this.document.getContentUpdateDate()).thenReturn(date);
    // Version
    String version = "1.1";
    when(this.document.getVersion()).thenReturn(version);
    // Version summary
    String comment = "1.1 comment";
    when(this.document.getComment()).thenReturn(comment);
    // XObjects.
    when(this.document.getXObjects()).thenReturn(Collections.<DocumentReference, List<BaseObject>>emptyMap());
    // Title
    String title = "title";
    when(this.document.getRenderedTitle(any(), same(this.xcontext))).thenReturn(title);
    // Rendered Content
    final String renderedContent = "rendered content";
    BlockRenderer plainRenderer = this.mocker.registerMockComponent(BlockRenderer.class, "plain/1.0");
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            WikiPrinter printer = (WikiPrinter) args[1];
            printer.print(renderedContent);
            return null;
        }
    }).when(plainRenderer).render((Block) any(), any());
    // Raw Content
    String rawContent = "raw content";
    when(this.document.getContent()).thenReturn(rawContent);
    // 
    // Call
    // 
    SolrInputDocument solrDocument = this.mocker.getComponentUnderTest().getSolrDocument(this.documentReference);
    // 
    // Assert and verify
    // 
    assertEquals(id, solrDocument.getFieldValue(FieldUtils.ID));
    assertEquals(this.documentReference.getWikiReference().getName(), solrDocument.getFieldValue(FieldUtils.WIKI));
    assertEquals(Arrays.asList("Path", "To", "Page"), solrDocument.getFieldValues(FieldUtils.SPACES));
    assertEquals(this.documentReference.getName(), solrDocument.getFieldValue(FieldUtils.NAME));
    assertEquals(Arrays.asList("0/Path.", "1/Path.To.", "2/Path.To.Page."), solrDocument.getFieldValues(FieldUtils.SPACE_FACET));
    assertEquals(Arrays.asList("Path", "Path.To", "Path.To.Page"), solrDocument.getFieldValues(FieldUtils.SPACE_PREFIX));
    assertEquals(Locale.US.toString(), solrDocument.getFieldValue(FieldUtils.LOCALE));
    assertEquals(Locale.US.getLanguage(), solrDocument.getFieldValue(FieldUtils.LANGUAGE));
    Collection<?> actualLocales = solrDocument.getFieldValues(FieldUtils.LOCALES);
    // The order of the locales in the returned collection is nondeterministic.
    assertTrue(actualLocales.size() == 2 && actualLocales.contains("") && actualLocales.contains(Locale.US.toString()));
    assertEquals(this.document.isHidden(), solrDocument.getFieldValue(FieldUtils.HIDDEN));
    assertEquals(EntityType.DOCUMENT.name(), solrDocument.getFieldValue(FieldUtils.TYPE));
    assertEquals(fullName, solrDocument.getFieldValue(FieldUtils.FULLNAME));
    assertEquals(title, solrDocument.getFieldValue(FieldUtils.getFieldName(FieldUtils.TITLE, Locale.US)));
    assertEquals(rawContent, solrDocument.getFieldValue(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RAW_CONTENT, Locale.US)));
    assertEquals(renderedContent, solrDocument.getFieldValue(FieldUtils.getFieldName(FieldUtils.DOCUMENT_RENDERED_CONTENT, Locale.US)));
    assertEquals(version, solrDocument.getFieldValue(FieldUtils.VERSION));
    assertEquals(comment, solrDocument.getFieldValue(FieldUtils.COMMENT));
    assertEquals(authorStringReference, solrDocument.getFieldValue(FieldUtils.AUTHOR));
    assertEquals(authorDisplayName, solrDocument.getFieldValue(FieldUtils.AUTHOR_DISPLAY));
    assertEquals(creatorStringReference, solrDocument.getFieldValue(FieldUtils.CREATOR));
    assertEquals(creatorDisplayName, solrDocument.getFieldValue(FieldUtils.CREATOR_DISPLAY));
    assertEquals(creationDate, solrDocument.getFieldValue(FieldUtils.CREATIONDATE));
    assertEquals(date, solrDocument.get(FieldUtils.DATE).getValue());
}
Also used : Date(java.util.Date) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) BaseObject(com.xpn.xwiki.objects.BaseObject) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrReferenceResolver(org.xwiki.search.solr.internal.reference.SolrReferenceResolver) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 9 with BlockRenderer

use of org.xwiki.rendering.renderer.BlockRenderer in project xwiki-platform by xwiki.

the class EditableGadgetRenderer method getGadgetEditMetadata.

/**
 * @param gadget the gadget to decorate
 * @return the block containing the metadata that will allow clients to edit this gadget
 */
protected Block getGadgetEditMetadata(Gadget gadget) {
    GroupBlock metadataBlock = new GroupBlock();
    metadataBlock.setParameter(CLASS, METADATA);
    // look at the content of the gadget and store whether it's a macro or not
    boolean isMacro = gadget.getContent().size() == 1 && gadget.getContent().get(0) instanceof MacroMarkerBlock;
    GroupBlock isMacroBlock = new GroupBlock();
    isMacroBlock.setParameter(CLASS, "isMacro");
    isMacroBlock.addChild(new WordBlock(Boolean.toString(isMacro)));
    metadataBlock.addChild(isMacroBlock);
    if (isMacro) {
        // render the annotated macro call in the page, to be able to edit it. Only the macro call comments will be
        // rendered, since transformations are not ran, so there is no content in the macro. But annotation is
        // enough.
        GroupBlock renderedContentBlock = new GroupBlock();
        renderedContentBlock.setParameter(CLASS, "content");
        WikiPrinter printer = new DefaultWikiPrinter();
        BlockRenderer gadgetContentRenderer = getGadgetContentRenderer();
        gadgetContentRenderer.render(gadget.getContent(), printer);
        RawBlock rawBlock = new RawBlock(printer.toString(), getRawBlockSyntax(gadgetContentRenderer));
        renderedContentBlock.addChild(rawBlock);
        // render the title in the page as well, to be edited as source
        GroupBlock gadgetTitleBlock = new GroupBlock();
        gadgetTitleBlock.setParameter(CLASS, "title");
        // even if it's not a word, it's fine since it will be rendered in one piece
        gadgetTitleBlock.addChild(new WordBlock(gadget.getTitleSource()));
        metadataBlock.addChild(renderedContentBlock);
        metadataBlock.addChild(gadgetTitleBlock);
    }
    return metadataBlock;
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) WordBlock(org.xwiki.rendering.block.WordBlock) RawBlock(org.xwiki.rendering.block.RawBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Example 10 with BlockRenderer

use of org.xwiki.rendering.renderer.BlockRenderer in project xwiki-platform by xwiki.

the class InternalTemplateManager method render.

private void render(XDOM xdom, Writer writer) {
    WikiPrinter printer = new WriterWikiPrinter(writer);
    BlockRenderer blockRenderer;
    try {
        blockRenderer = this.componentManagerProvider.get().getInstance(BlockRenderer.class, getTargetSyntax().toIdString());
    } catch (ComponentLookupException e) {
        blockRenderer = this.plainRenderer;
    }
    blockRenderer.render(xdom, printer);
}
Also used : WriterWikiPrinter(org.xwiki.rendering.renderer.printer.WriterWikiPrinter) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WriterWikiPrinter(org.xwiki.rendering.renderer.printer.WriterWikiPrinter) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer)

Aggregations

BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)17 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)14 DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)8 Test (org.junit.Test)7 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)6 XDOM (org.xwiki.rendering.block.XDOM)6 StringReader (java.io.StringReader)4 Block (org.xwiki.rendering.block.Block)4 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)4 Parser (org.xwiki.rendering.parser.Parser)3 XWikiException (com.xpn.xwiki.XWikiException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 MutableRenderingContext (org.xwiki.rendering.internal.transformation.MutableRenderingContext)2 ParseException (org.xwiki.rendering.parser.ParseException)2 StreamParser (org.xwiki.rendering.parser.StreamParser)2 RenderingContext (org.xwiki.rendering.transformation.RenderingContext)2 Transformation (org.xwiki.rendering.transformation.Transformation)2 BaseObject (com.xpn.xwiki.objects.BaseObject)1 IOException (java.io.IOException)1