protected <T extends Site.Page> Page generateChild(
      Confluence confluence, T child, String spaceKey, String parentPageTitle, String titlePrefix) {

    java.net.URI source = child.getUri(getProject(), getFileExt());

    getLog()
        .info(
            String.format(
                "generateChild spacekey=[%s] parentPageTtile=[%s]\n%s",
                spaceKey, parentPageTitle, child.toString()));

    try {

      if (!isSnapshot() && isRemoveSnapshots()) {
        final String snapshot = titlePrefix.concat("-SNAPSHOT");
        boolean deleted =
            ConfluenceUtils.removePage(confluence, spaceKey, parentPageTitle, snapshot);

        if (deleted) {
          getLog().info(String.format("Page [%s] has been removed!", snapshot));
        }
      }

      final String pageName = String.format("%s - %s", titlePrefix, child.getName());

      Page p = ConfluenceUtils.getOrCreatePage(confluence, spaceKey, parentPageTitle, pageName);

      if (source != null /*&& source.isFile() && source.exists() */) {

        final MiniTemplator t =
            new MiniTemplator.Builder()
                .setSkipUndefinedVars(true)
                .build(Site.processUri(source), getCharset());

        addProperties(t);

        p.setContent(t.generateOutput());
      }

      p = confluence.storePage(p);

      for (String label : child.getComputedLabels()) {

        confluence.addLabelByName(label, Long.parseLong(p.getId()));
      }

      child.setName(pageName);

      return p;

    } catch (Exception e) {
      final String msg = "error loading template";
      getLog().error(msg, e);
      // throw new MavenReportException(msg, e);

      return null;
    }
  }
  public void addProperties(MiniTemplator t) {
    java.util.Map<String, String> props = getProperties();

    if (props == null || props.isEmpty()) {
      getLog().info("no properties set!");
    } else {
      for (java.util.Map.Entry<String, String> e : props.entrySet()) {
        // getLog().debug(String.format("property %s = %s", e.getKey(), e.getValue()));

        try {
          t.setVariable(e.getKey(), e.getValue(), true /* isOptional */);
        } catch (VariableNotDefinedException e1) {
          getLog().warn(String.format("variable %s not defined in template", e.getKey()));
        }
      }
    }
  }
Пример #3
0
  public ThemePage parsePage(String url, String response, boolean generateHtml) {
    ThemePage page = new ThemePage();
    String redirectUrl = Client.getInstance().getRedirect(url);
    if (redirectUrl == null) redirectUrl = url;
    page.setUrl(redirectUrl);

    Log.d("kek", "page getted");
    long time = System.currentTimeMillis();
    Matcher matcher = elemToScrollPattern.matcher(redirectUrl);
    while (matcher.find()) {
      page.setElementToScroll(matcher.group(1));
    }
    matcher = themeIdPattern.matcher(response);
    if (matcher.find()) {
      Log.d("suka", "IDS PARSING " + matcher.group(1) + " : " + matcher.group(2));
      page.setForumId(Integer.parseInt(matcher.group(1)));
      page.setId(Integer.parseInt(matcher.group(2)));
    }
    matcher = countsPattern.matcher(response);
    if (matcher.find()) {
      page.setAllPagesCount(Integer.parseInt(matcher.group(1)) + 1);
      page.setPostsOnPageCount(Integer.parseInt(matcher.group(2)));
    }
    matcher = paginationPattern.matcher(response);
    if (matcher.find()) {
      page.setCurrentPage(Integer.parseInt(matcher.group(2)));
    }
    matcher = titlePattern.matcher(response);
    if (matcher.find()) {
      page.setTitle(matcher.group(1));
      page.setDesc(matcher.group(2));
    }
    matcher = alreadyInFavPattern.matcher(response);
    page.setInFavorite(matcher.find());
    matcher = postsPattern.matcher(response);
    Log.d("kek", "posts matcher " + (System.currentTimeMillis() - time));
    int memberId = Api.Auth().getUserIdInt();
    while (matcher.find()) {
      ThemePost post = new ThemePost();
      post.setId(Integer.parseInt(matcher.group(1)));
      post.setDate(matcher.group(2));
      post.setNumber(Integer.parseInt(matcher.group(3)));
      post.setOnline(matcher.group(4).contains("green"));
      post.setAvatar(matcher.group(5));
      post.setNick(Html.fromHtml(matcher.group(6)).toString());
      post.setUserId(Integer.parseInt(matcher.group(7)));
      post.setCurator(matcher.group(8) != null);
      post.setGroupColor(matcher.group(9));
      post.setGroup(matcher.group(10));
      post.setCanMinus(!matcher.group(11).isEmpty());
      post.setReputation(matcher.group(12));
      post.setCanPlus(!matcher.group(13).isEmpty());
      post.setCanReport(!matcher.group(14).isEmpty());
      post.setCanEdit(!matcher.group(15).isEmpty());
      post.setCanDelete(!matcher.group(16).isEmpty());
      page.setCanQuote(!matcher.group(17).isEmpty());
      post.setBody(matcher.group(18));
      if (post.isCurator() && post.getUserId() == memberId) page.setCurator(true);
      page.addPost(post);
    }
    Log.d("kek", "poll matcher " + (System.currentTimeMillis() - time));
    matcher = pollMainPattern.matcher(response);
    if (matcher.find()) {
      Poll poll = new Poll();
      final boolean isResult = matcher.group().contains("img");
      poll.setIsResult(isResult);
      poll.setTitle(matcher.group(1));
      Matcher matcher1 = pollQuestions.matcher(matcher.group(2));
      while (matcher1.find()) {
        PollQuestion pollQuestion = new PollQuestion();
        pollQuestion.setTitle(matcher1.group(1));
        Matcher itemsMatcher = pollQuestionItems.matcher(matcher1.group(2));
        while (itemsMatcher.find()) {
          PollQuestionItem questionItem = new PollQuestionItem();
          if (!isResult) {
            questionItem.setType(itemsMatcher.group(1));
            questionItem.setName(itemsMatcher.group(2));
            questionItem.setValue(Integer.parseInt(itemsMatcher.group(3)));
            questionItem.setTitle(itemsMatcher.group(4));
          } else {
            questionItem.setTitle(itemsMatcher.group(5));
            questionItem.setVotes(Integer.parseInt(itemsMatcher.group(6)));
            questionItem.setPercent(Float.parseFloat(itemsMatcher.group(7).replace(",", ".")));
          }
          pollQuestion.addItem(questionItem);
        }
        poll.addQuestion(pollQuestion);
      }
      matcher1 = pollButtons.matcher(matcher.group(4));
      while (matcher1.find()) {
        String value = matcher1.group(1);
        if (value.contains("Голосовать")) {
          poll.setVoteButton();
        } else if (value.contains("результаты")) {
          poll.setShowResultButton();
        } else if (value.contains("пункты опроса")) {
          poll.setShowPollButton();
        }
      }
      poll.setVotesCount(Integer.parseInt(matcher.group(3)));
      page.setPoll(poll);
    }
    Log.d("kek", "end created page obj " + (System.currentTimeMillis() - time));
    if (generateHtml) {
      long time2 = System.currentTimeMillis();
      MiniTemplator t = App.getInstance().getTemplator();
      boolean authorized = Api.Auth().getState();
      boolean prevDisabled = page.getCurrentPage() <= 1;
      boolean nextDisabled = page.getCurrentPage() == page.getAllPagesCount();

      t.setVariableOpt("topic_title", page.getTitle());
      t.setVariableOpt("topic_url", redirectUrl);
      t.setVariableOpt("topic_description", page.getDesc());
      t.setVariableOpt("in_favorite", Boolean.toString(page.isInFavorite()));
      t.setVariableOpt("all_pages", page.getAllPagesCount());
      t.setVariableOpt("posts_on_page", page.getPostsOnPageCount());
      t.setVariableOpt("current_page", page.getCurrentPage());
      t.setVariableOpt("authorized", Boolean.toString(authorized));
      t.setVariableOpt("is_curator", Boolean.toString(page.isCurator()));
      t.setVariableOpt("member_id", Api.Auth().getUserIdInt());
      t.setVariableOpt("elem_to_scroll", page.getElementToScroll());
      t.setVariableOpt("body_type", "topic");
      t.setVariableOpt(
          "navigation_disable", prevDisabled && nextDisabled ? "navigation_disable" : "");
      t.setVariableOpt("first_disable", getDisableStr(prevDisabled));
      t.setVariableOpt("prev_disable", getDisableStr(prevDisabled));
      t.setVariableOpt("next_disable", getDisableStr(nextDisabled));
      t.setVariableOpt("last_disable", getDisableStr(nextDisabled));
      t.setVariableOpt("disable_avatar_js", Boolean.toString(true));
      t.setVariableOpt(
          "disable_avatar",
          App.getInstance().getPreferences().getBoolean("theme.show_avatars", true)
              ? ""
              : "disable_avatar");
      t.setVariableOpt(
          "avatar_type",
          App.getInstance().getPreferences().getBoolean("theme.circle_avatars", true)
              ? "avatar_circle"
              : "");

      Log.d("kek", "template check 1 " + (System.currentTimeMillis() - time2));

      int hatPostId = page.getPosts().get(0).getId();
      Log.d("kek", "template check 2 " + (System.currentTimeMillis() - time2));
      String letter;
      Matcher letterMatcher = null;
      for (ThemePost post : page.getPosts()) {
        t.setVariableOpt("user_online", post.isOnline() ? "online" : "");
        t.setVariableOpt("post_id", post.getId());
        t.setVariableOpt("user_id", post.getUserId());

        // Post header
        // t.setVariableOpt("avatar", post.getAvatar().isEmpty() ? "file:///android_asset/av.png" :
        // "http://s.4pda.to/forum/uploads/".concat(post.getAvatar()));
        t.setVariableOpt(
            "avatar",
            post.getAvatar().isEmpty()
                ? ""
                : "http://s.4pda.to/forum/uploads/".concat(post.getAvatar()));
        t.setVariableOpt("none_avatar", post.getAvatar().isEmpty() ? "none_avatar" : "");

        letter = post.getNick().substring(0, 1);
        if (letterMatcher != null) {
          letterMatcher = letterMatcher.reset(letter);
        } else {
          letterMatcher = firstLetter.matcher(letter);
        }
        if (!letterMatcher.find()) {
          letterMatcher = letterMatcher.reset(post.getNick());
          if (letterMatcher.find()) {
            letter = letterMatcher.group(1);
          }
        }
        t.setVariableOpt("nick_letter", letter);
        t.setVariableOpt("nick", post.getNick());
        t.setVariableOpt("curator", post.isCurator() ? "curator" : "");
        t.setVariableOpt("group_color", post.getGroupColor());
        t.setVariableOpt("group", post.getGroup());
        t.setVariableOpt("reputation", post.getReputation());
        t.setVariableOpt("date", post.getDate());
        t.setVariableOpt("number", post.getNumber());

        // Post body
        if (page.getPosts().size() > 1 && hatPostId == post.getId()) {
          t.setVariableOpt("hat_state_class", "close");
          t.addBlockOpt("hat_button");
          t.addBlockOpt("hat_content_start");
          t.addBlockOpt("hat_content_end");
        } else {
          t.setVariableOpt("hat_state_class", "");
        }
        t.setVariableOpt("body", post.getBody());

        // Post footer

        if (post.canReport() && authorized) t.addBlockOpt("report_block");
        if (page.canQuote() && authorized && post.getUserId() != memberId)
          t.addBlockOpt("reply_block");
        if (authorized && post.getUserId() != memberId) t.addBlockOpt("vote_block");
        if (post.canDelete() && authorized) t.addBlockOpt("delete_block");
        if (post.canEdit() && authorized) t.addBlockOpt("edit_block");

        t.addBlockOpt("post");
      }

      // Poll block

      if (page.getPoll() != null) {
        Poll poll = page.getPoll();
        boolean isResult = poll.isResult();
        t.setVariableOpt("poll_type", isResult ? "result" : "default");
        t.setVariableOpt(
            "poll_title",
            poll.getTitle().isEmpty() || poll.getTitle().equals("-") ? "Опрос" : poll.getTitle());

        for (PollQuestion question : poll.getQuestions()) {
          t.setVariableOpt("question_title", question.getTitle());

          for (PollQuestionItem questionItem : question.getQuestionItems()) {
            t.setVariableOpt("question_item_title", questionItem.getTitle());

            if (isResult) {
              t.setVariableOpt("question_item_votes", questionItem.getVotes());
              t.setVariableOpt("question_item_percent", Float.toString(questionItem.getPercent()));
              t.addBlockOpt("poll_result_item");
            } else {
              t.setVariableOpt("question_item_type", questionItem.getType());
              t.setVariableOpt("question_item_name", questionItem.getName());
              t.setVariableOpt("question_item_value", questionItem.getValue());
              t.addBlockOpt("poll_default_item");
            }
          }
          t.addBlockOpt("poll_question_block");
        }
        t.setVariableOpt("poll_votes_count", poll.getVotesCount());
        if (poll.haveButtons()) {
          if (poll.haveVoteButton()) t.addBlockOpt("poll_vote_button");
          if (poll.haveShowResultsButton()) t.addBlockOpt("poll_show_results_button");
          if (poll.haveShowPollButton()) t.addBlockOpt("poll_show_poll_button");
          t.addBlockOpt("poll_buttons");
        }
        t.addBlockOpt("poll_block");
      }

      Log.d("kek", "template check 3 " + (System.currentTimeMillis() - time2));
      page.setHtml(t.generateOutput());
      Log.d("kek", "template check 4 " + (System.currentTimeMillis() - time2));
      t.reset();
      Log.d("kek", "template check 5 " + (System.currentTimeMillis() - time2));
    }

    Log.d("kek", "theme parsing time " + (System.currentTimeMillis() - time));
    /*final String veryLongString = page.getHtml();

    int maxLogSize = 1000;
    for (int i = 0; i <= veryLongString.length() / maxLogSize; i++) {
        int start = i * maxLogSize;
        int end = (i + 1) * maxLogSize;
        end = end > veryLongString.length() ? veryLongString.length() : end;
        Log.v("SUKA", veryLongString.substring(start, end));
    }*/

    return page;
  }