예제 #1
0
  public void testURLEntities() {
    Entity entity = new Entity(0, 19, "http://t.co/0JG5Mcq", Entity.Type.URL);
    entity.setDisplayURL("blog.twitter.com/2011/05/twitte…");
    entity.setExpandedURL("http://blog.twitter.com/2011/05/twitter-for-mac-update.html");
    List<Entity> entities = new ArrayList<Entity>();
    entities.add(entity);
    String tweet = "http://t.co/0JG5Mcq";
    String expected =
        "<a href=\"http://t.co/0JG5Mcq\" rel=\"nofollow\"><span class='tco-ellipsis'><span style='position:absolute;left:-9999px;'>&nbsp;</span></span><span style='position:absolute;left:-9999px;'>http://</span><span class='js-display-url'>blog.twitter.com/2011/05/twitte</span><span style='position:absolute;left:-9999px;'>r-for-mac-update.html</span><span class='tco-ellipsis'><span style='position:absolute;left:-9999px;'>&nbsp;</span>…</span></a>";

    assertAutolink(expected, linker.autoLinkEntities(tweet, entities));
  }
예제 #2
0
  public void linkToHashtag(Entity entity, String text, StringBuilder builder) {
    // Get the original hash char from text as it could be a full-width
    // char.
    CharSequence hashChar = text.subSequence(entity.getStart(), entity.getStart() + 1);
    CharSequence hashtag = entity.getValue();

    Map<String, String> attrs = new LinkedHashMap<String, String>();
    attrs.put("href", hashtagUrlBase + "#" + hashtag);
    // attrs.put("title", "#" + hashtag);
    if (hashtagClass != null) {
      attrs.put("class", hashtagClass);
    }

    linkToTextWithSymbol(entity, hashChar, hashtag, attrs, builder);
  }
예제 #3
0
  public void linkToMentionAndList(Entity entity, String text, StringBuilder builder) {
    String mention = entity.getValue();
    // Get the original at char from text as it could be a full-width char.
    CharSequence atChar = text.subSequence(entity.getStart(), entity.getStart() + 1);

    Map<String, String> attrs = new LinkedHashMap<String, String>();
    if (entity.listSlug != null) {
      mention += entity.listSlug;
      if (listClass != null) {
        attrs.put("class", listClass);
      }
      attrs.put("href", listUrlBase + mention);
    } else {
      if (usernameClass != null) {
        attrs.put("class", usernameClass);
      }
      attrs.put("href", usernameUrlBase + mention);
    }

    linkToTextWithSymbol(entity, atChar, mention, attrs, builder);
  }
예제 #4
0
  public void linkToCashtag(Entity entity, String text, StringBuilder builder) {
    CharSequence cashtag = entity.getValue();

    Map<String, String> attrs = new LinkedHashMap<String, String>();
    attrs.put("href", cashtagUrlBase + cashtag);
    // attrs.put("title", "$" + cashtag);
    if (cashtagClass != null) {
      attrs.put("class", cashtagClass);
    }

    linkToTextWithSymbol(entity, "$", cashtag, attrs, builder);
  }
예제 #5
0
  public void linkToURL(Entity entity, String text, StringBuilder builder, URLEntity urlEntity) {
    CharSequence url = entity.getValue();
    CharSequence linkText = escapeHTML(url);

    if (urlEntity != null && urlEntity.getDisplayURL() != null) {
      linkText = urlEntity.getDisplayURL();
    } else if (entity.displayURL != null) {
      linkText = entity.displayURL;
    }

    Map<String, String> attrs = new LinkedHashMap<String, String>();
    attrs.put("href", url.toString());

    linkToText(entity, linkText, attrs, builder);
  }
예제 #6
0
  public String autoLinkEntities(
      String text,
      TwitterMediaEntity twitterMediaEntity,
      List<Entity> entities,
      MediaEntity[] mediaEntities,
      URLEntity[] urlEntities) {
    StringBuilder builder = new StringBuilder(text.length() * 2);
    int beginIndex = 0;

    int urlCount = 0;

    for (Entity entity : entities) {
      builder.append(text.subSequence(beginIndex, entity.start));

      switch (entity.type) {
        case URL:
          URLEntity urlEntity = null;

          if (urlEntities != null && urlCount < urlEntities.length) {
            urlEntity = urlEntities[urlCount];
            urlCount += 1;
            // if (twitterMediaEntity != null &&
            // twitterMediaEntity.getUrl().equals(urlEntity.getURL().toString()))
            // {
            // break;
            // }
          }

          if (urlEntity == null) {
            if (entity != null
                && entity.value != null
                && mediaEntities != null
                && mediaEntities.length > 0) {
              MediaEntity mediaEntity = mediaEntities[0];
              if (entity.value.equals(mediaEntity.getURL().toString())) {
                entity.displayURL = mediaEntity.getDisplayURL();
                entity.expandedURL = mediaEntity.getExpandedURL().toString();
              }
            }
          }

          /*
           * if (twitterMediaEntity != null && urlEntity != null) { if
           * (urlEntity.getExpandedURL
           * ().toString().equals(twitterMediaEntity.getExpandedUrl())) {
           * entity.expandedURL = DEFAULT_MEDIA_URL_BASE +
           * twitterMediaEntity.getExpandedUrl().toString(); entity.value
           * = DEFAULT_MEDIA_URL_BASE +
           * twitterMediaEntity.getExpandedUrl().toString(); } }
           */

          linkToURL(entity, text, builder, urlEntity);
          break;
        case HASHTAG:
          linkToHashtag(entity, text, builder);
          break;
        case MENTION:
          linkToMentionAndList(entity, text, builder);
          break;
        case CASHTAG:
          linkToCashtag(entity, text, builder);
          break;
      }
      beginIndex = entity.end;
    }
    builder.append(text.subSequence(beginIndex, text.length()));

    return builder.toString();
  }