private String replaceMacros()

in src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java [315:348]


  private String replaceMacros(
      Repository repo, Project.NameKey project, ObjectId revId, String abbrRevId, String raw)
      throws GitAPIException, IOException {
    Map<String, String> macros = Maps.newHashMap();

    String url = webUrl.get();
    if (Strings.isNullOrEmpty(url)) {
      url = "http://" + DEFAULT_HOST + "/";
    }
    macros.put("URL", url);

    macros.put("PROJECT", project.get());
    macros.put("PROJECT_URL", url + "#/admin/projects/" + project.get());
    macros.put("REVISION", abbrRevId);
    try (Git git = new Git(repo)) {
      macros.put(
          "GIT_DESCRIPTION",
          MoreObjects.firstNonNull(git.describe().setTarget(revId).call(), abbrRevId));
    }

    Matcher m = Pattern.compile("(\\\\)?@([A-Z_]+)@").matcher(raw);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
      String key = m.group(2);
      String val = macros.get(key);
      if (m.group(1) != null || val == null) {
        m.appendReplacement(sb, "@" + key + "@");
      } else {
        m.appendReplacement(sb, val);
      }
    }
    m.appendTail(sb);
    return sb.toString();
  }