@Test
  public void addAndGetMacroInvocations() {
    MacroInvocation invocation =
        htmlSerializerContext.addMacroInvocation("foo", "params"); // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("foo", invocation.getMacroName()); // $NON-NLS-1$
    assertEquals("params", invocation.getParameters()); // $NON-NLS-1$

    List<MacroInvocation> invocations = htmlSerializerContext.getMacroInvocations();
    assertEquals(1, invocations.size());
    invocation = invocations.get(0);
    assertEquals("foo", invocation.getMacroName()); // $NON-NLS-1$
    assertEquals("params", invocation.getParameters()); // $NON-NLS-1$
  }
  @Test
  public void addAndGetHeaders() {
    Header[] headers = {
      new Header("foo", 1), // $NON-NLS-1$
      new Header("bar", 2), // $NON-NLS-1$
      new Header("baz", 3) // $NON-NLS-1$
    };
    for (Header header : headers) {
      htmlSerializerContext.addHeader(header.getText(), header.getLevel());
    }

    List<Header> result = htmlSerializerContext.getHeaders();
    for (int i = 0; i < result.size(); i++) {
      assertTrue(EqualsBuilder.reflectionEquals(result.get(i), headers[i], true));
    }
  }
 @Test
 public void markdownToHtml() {
   when(markdownProcessor.markdownToHtml(
           "md", PROJECT, BRANCH, PAGE, authentication, Locale.US, CONTEXT))
       .thenReturn("html"); // $NON-NLS-1$ //$NON-NLS-2$
   assertEquals("html", htmlSerializerContext.markdownToHtml("md")); // $NON-NLS-1$ //$NON-NLS-2$
 }
 @Test
 public void getAttachmentUri() {
   String uri = htmlSerializerContext.getAttachmentUri("test.png"); // $NON-NLS-1$
   assertEquals(
       CONTEXT
           + "/attachment/"
           + PROJECT
           + "/"
           + BRANCH
           + "/"
           + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
           Util.toUrlPagePath(PAGE)
           + "/test.png", //$NON-NLS-1$
       uri);
 }
 @Test
 public void getPageUri() {
   String page = "foo/bar/baz"; // $NON-NLS-1$
   String uri = htmlSerializerContext.getPageUri(page);
   assertEquals(
       CONTEXT
           + "/page/"
           + PROJECT
           + "/"
           + BRANCH
           + "/"
           + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
           Util.toUrlPagePath(page),
       uri);
 }
 @Test
 public void getUrl() {
   String result = htmlSerializerContext.getUrl(CONTEXT + "/foo/bar"); // $NON-NLS-1$
   assertEquals(DOCUMENTR_HOST + "/foo/bar", result); // $NON-NLS-1$
 }
Пример #7
0
  private String markdownToHtml(
      RootNode rootNode,
      String projectName,
      String branchName,
      String path,
      Authentication authentication,
      Locale locale,
      boolean nonCacheableMacros,
      String contextPath) {

    HtmlSerializerContext context =
        new HtmlSerializerContext(
            projectName,
            branchName,
            path,
            this,
            authentication,
            locale,
            pageStore,
            systemSettingsStore,
            contextPath);
    HtmlSerializer serializer = new HtmlSerializer(context);
    String html = serializer.toHtml(rootNode);

    List<MacroInvocation> macroInvocations = Lists.newArrayList(context.getMacroInvocations());
    // reverse order so that inner invocations will be processed before outer
    Collections.reverse(macroInvocations);
    int nonCacheableMacroIdx = 1;
    for (MacroInvocation invocation : macroInvocations) {
      IMacro macro = macroFactory.get(invocation.getMacroName());
      if (macro == null) {
        macro = new UnknownMacroMacro();
      }
      IMacroDescriptor macroDescriptor = macro.getDescriptor();
      String startMarker = invocation.getStartMarker();
      String endMarker = invocation.getEndMarker();
      String body = StringUtils.substringBetween(html, startMarker, endMarker);
      if (macroDescriptor.isCacheable()) {
        MacroContext macroContext =
            MacroContext.create(
                invocation.getMacroName(),
                invocation.getParameters(),
                body,
                context,
                locale,
                beanFactory);
        IMacroRunnable macroRunnable = macro.createRunnable();
        String macroHtml = StringUtils.defaultString(macroRunnable.getHtml(macroContext));
        html = StringUtils.replace(html, startMarker + body + endMarker, macroHtml);
      } else if (nonCacheableMacros) {
        String macroName = invocation.getMacroName();
        String params = invocation.getParameters();
        String idx = String.valueOf(nonCacheableMacroIdx++);
        html =
            StringUtils.replace(
                html,
                startMarker + body + endMarker,
                "__"
                    + NON_CACHEABLE_MACRO_MARKER
                    + "_"
                    + idx
                    + "__"
                    + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    macroName
                    + " "
                    + StringUtils.defaultString(params)
                    + //$NON-NLS-1$
                    "__"
                    + NON_CACHEABLE_MACRO_BODY_MARKER
                    + "__"
                    + //$NON-NLS-1$ //$NON-NLS-2$
                    body
                    + "__/"
                    + NON_CACHEABLE_MACRO_MARKER
                    + "_"
                    + idx
                    + "__"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
      } else {
        html = StringUtils.replace(html, startMarker + body + endMarker, StringUtils.EMPTY);
      }
    }
    html = cleanupHtml(html, macroInvocations, true);
    return html;
  }