Search in sources :

Example 1 with TemplateContent

use of org.xwiki.template.TemplateContent in project xwiki-platform by xwiki.

the class TemplateLESSSource method getContent.

@Override
public String getContent() throws FileNotFound, CannotReadFile {
    try {
        // We execute velocity on the main skin file only (which is included by SSX objects using LESS).
        // 
        // This a limitation we introduce because when we do an HTML export, we must execute velocity to know which
        // resources have to be included in the ZIP file, but we avoid executing LESS and we use the cache instead
        // (otherwise the export would be too slow).
        // 
        // When we do an HTML export, we execute Velocity on the main skin file, but not on any .less.vm that the
        // skin might have. Actually we have no way to know which .less.vm are included, without running LESS.
        // 
        // That is why we do not execute Velocity on any ".less.vm" file but only on the main skin template.
        String mainSkinTemplate = "less/" + CachedLESSCompiler.MAIN_SKIN_STYLE_FILENAME;
        if (mainSkinTemplate.equals(templateName)) {
            return templateManager.renderFromSkin(templateName, skin);
        }
        // Otherwise, return the raw content
        Template template = templateManager.getTemplate(templateName, skin);
        TemplateContent templateContent = template.getContent();
        return templateContent.getContent();
    } catch (Exception e) {
        throw new CannotReadFile();
    }
}
Also used : TemplateContent(org.xwiki.template.TemplateContent) Template(org.xwiki.template.Template)

Example 2 with TemplateContent

use of org.xwiki.template.TemplateContent in project xwiki-platform by xwiki.

the class Less4jCompilerTest method compile.

@Test
public void compile() throws Exception {
    // Mocks
    when(skinManager.getSkin("skin")).thenReturn(skin);
    // Is is actually more an integration test than a unit test
    // Import 1
    when(skin.getResource("less/style.less.vm")).thenReturn(mock(Resource.class));
    StringWriter import1source = new StringWriter();
    IOUtils.copy(new FileInputStream(getClass().getResource("/style.less.vm").getFile()), import1source);
    when(templateManager.renderFromSkin("less/style.less.vm", skin)).thenReturn(import1source.toString());
    // Import 2
    when(skin.getResource("less/subdir/import2.less")).thenReturn(mock(Resource.class));
    Template import2 = mock(Template.class);
    when(templateManager.getTemplate("less/subdir/import2.less", skin)).thenReturn(import2);
    TemplateContent importContent2 = mock(TemplateContent.class);
    when(import2.getContent()).thenReturn(importContent2);
    StringWriter import2source = new StringWriter();
    IOUtils.copy(new FileInputStream(getClass().getResource("/import2.less").getFile()), import2source);
    when(importContent2.getContent()).thenReturn(import2source.toString());
    // Import 3
    when(skin.getResource("less/subdir/import3.less")).thenReturn(mock(Resource.class));
    Template import3 = mock(Template.class);
    when(templateManager.getTemplate("less/subdir/import3.less", skin)).thenReturn(import3);
    TemplateContent importContent3 = mock(TemplateContent.class);
    when(import3.getContent()).thenReturn(importContent3);
    StringWriter import3source = new StringWriter();
    IOUtils.copy(new FileInputStream(getClass().getResource("/import3.less").getFile()), import3source);
    when(importContent3.getContent()).thenReturn(import3source.toString());
    // Test
    StringWriter source = new StringWriter();
    IOUtils.copy(new FileInputStream(getClass().getResource("/style3.less").getFile()), source);
    String result = mocker.getComponentUnderTest().compile(source.toString(), "skin", false);
    // Now with sourcemaps.
    String result2 = mocker.getComponentUnderTest().compile(source.toString(), "skin", true);
    // Verify
    StringWriter expected = new StringWriter();
    IOUtils.copy(new FileInputStream(getClass().getResource("/style3.css").getFile()), expected);
    assertEquals(expected.toString(), result);
    assertTrue(result2.contains("/*# sourceMappingURL=data:application/json;base64,"));
}
Also used : StringWriter(java.io.StringWriter) TemplateContent(org.xwiki.template.TemplateContent) Resource(org.xwiki.skin.Resource) FileInputStream(java.io.FileInputStream) Template(org.xwiki.template.Template) Test(org.junit.Test)

Example 3 with TemplateContent

use of org.xwiki.template.TemplateContent in project xwiki-platform by xwiki.

the class LESSSkinFileResourceReferenceTest method getContent.

@Test
public void getContent() throws Exception {
    LESSSkinFileResourceReference lessSkinFileResourceReference = new LESSSkinFileResourceReference("style.less", templateManager, skinManager);
    // Mocks
    Template template = mock(Template.class);
    when(templateManager.getTemplate("less/style.less", skin)).thenReturn(template);
    TemplateContent templateContent = mock(TemplateContent.class);
    when(template.getContent()).thenReturn(templateContent);
    when(templateContent.getContent()).thenReturn("// My LESS file");
    // Test
    assertEquals("// My LESS file", lessSkinFileResourceReference.getContent("skin"));
}
Also used : TemplateContent(org.xwiki.template.TemplateContent) Template(org.xwiki.template.Template) Test(org.junit.Test)

Aggregations

Template (org.xwiki.template.Template)3 TemplateContent (org.xwiki.template.TemplateContent)3 Test (org.junit.Test)2 FileInputStream (java.io.FileInputStream)1 StringWriter (java.io.StringWriter)1 Resource (org.xwiki.skin.Resource)1